Commit 2cbc937b authored by Shane Caraveo's avatar Shane Caraveo
Browse files

Bug 1545159 implement captivePortal api r=rpl,valentin

Differential Revision: https://phabricator.services.mozilla.com/D29602

--HG--
extra : moz-landing-system : lando
parent 97f51aaa
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
@@ -24,6 +24,14 @@
      ["browsingData"]
      ["browsingData"]
    ]
    ]
  },
  },
  "captivePortal": {
    "url": "chrome://extensions/content/parent/ext-captivePortal.js",
    "schema": "chrome://extensions/content/schemas/captive_portal.json",
    "scopes": ["addon_parent"],
    "paths": [
      ["captivePortal"]
    ]
  },
  "chrome_settings_overrides": {
  "chrome_settings_overrides": {
    "url": "chrome://browser/content/parent/ext-chrome-settings-overrides.js",
    "url": "chrome://browser/content/parent/ext-chrome-settings-overrides.js",
    "scopes": [],
    "scopes": [],
+3 −0
Original line number Original line Diff line number Diff line
@@ -10,6 +10,9 @@ toolkit.jar:
    content/extensions/parent/ext-alarms.js (parent/ext-alarms.js)
    content/extensions/parent/ext-alarms.js (parent/ext-alarms.js)
    content/extensions/parent/ext-backgroundPage.js (parent/ext-backgroundPage.js)
    content/extensions/parent/ext-backgroundPage.js (parent/ext-backgroundPage.js)
    content/extensions/parent/ext-browserSettings.js (parent/ext-browserSettings.js)
    content/extensions/parent/ext-browserSettings.js (parent/ext-browserSettings.js)
#ifndef ANDROID
    content/extensions/parent/ext-captivePortal.js (parent/ext-captivePortal.js)
#endif
    content/extensions/parent/ext-contentScripts.js (parent/ext-contentScripts.js)
    content/extensions/parent/ext-contentScripts.js (parent/ext-contentScripts.js)
    content/extensions/parent/ext-contextualIdentities.js (parent/ext-contextualIdentities.js)
    content/extensions/parent/ext-contextualIdentities.js (parent/ext-contextualIdentities.js)
    content/extensions/parent/ext-clipboard.js (parent/ext-clipboard.js)
    content/extensions/parent/ext-clipboard.js (parent/ext-clipboard.js)
+83 −0
Original line number Original line Diff line number Diff line
/* -*- Mode: indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set sts=2 sw=2 et tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

XPCOMUtils.defineLazyServiceGetter(this, "gCPS",
                                   "@mozilla.org/network/captive-portal-service;1",
                                   "nsICaptivePortalService");

XPCOMUtils.defineLazyPreferenceGetter(this, "gCaptivePortalEnabled",
                                      "network.captive-portal-service.enabled",
                                      false);

function nameForCPSState(state) {
  switch (state) {
    case gCPS.UNKNOWN: return "unknown";
    case gCPS.NOT_CAPTIVE: return "not_captive";
    case gCPS.UNLOCKED_PORTAL: return "unlocked_portal";
    case gCPS.LOCKED_PORTAL: return "locked_portal";
    default: return "unknown";
  }
}

var {
  ExtensionError,
} = ExtensionUtils;

this.captivePortal = class extends ExtensionAPI {
  getAPI(context) {
    function checkEnabled() {
      if (!gCaptivePortalEnabled) {
        throw new ExtensionError("Captive Portal detection is not enabled");
      }
    }

    return {
      captivePortal: {
        getState() {
          checkEnabled();
          return nameForCPSState(gCPS.state);
        },
        getLastChecked() {
          checkEnabled();
          return gCPS.lastChecked;
        },
        onStateChanged: new EventManager({
          context,
          name: "captivePortal.onStateChanged",
          register: fire => {
            checkEnabled();

            let observer = (subject, topic) => {
              fire.async({state: nameForCPSState(gCPS.state)});
            };

            Services.obs.addObserver(observer, "ipc:network:captive-portal-set-state");
            return () => {
              Services.obs.removeObserver(observer, "ipc:network:captive-portal-set-state");
            };
          },
        }).api(),
        onConnectivityAvailable: new EventManager({
          context,
          name: "captivePortal.onConnectivityAvailable",
          register: fire => {
            checkEnabled();

            let observer = (subject, topic, data) => {
              fire.async({status: data});
            };

            Services.obs.addObserver(observer, "network:captive-portal-connectivity");
            return () => {
              Services.obs.removeObserver(observer, "network:captive-portal-connectivity");
            };
          },
        }).api(),
      },
    };
  }
};
+69 −0
Original line number Original line Diff line number Diff line
[
  {
    "namespace": "manifest",
    "types": [
      {
        "$extend": "Permission",
        "choices": [{
          "type": "string",
          "enum": [
            "captivePortal"
          ]
        }]
      }
    ]
  },
  {
    "namespace": "captivePortal",
    "description": "This API provides the ability detect the captive portal state of the users connection.",
    "permissions": ["captivePortal"],
    "functions": [
      {
        "name": "getState",
        "type": "function",
        "description": "Returns the current portal state, one of `unknown`, `not_captive`, `unlocked_portal`, `locked_portal`.",
        "async": true,
        "parameters": []
      },
      {
        "name": "getLastChecked",
        "type": "function",
        "description": "Returns the time difference between NOW and the last time a request was completed in milliseconds.",
        "async": true,
        "parameters": []
      }
    ],
    "events": [
      {
        "name": "onStateChanged",
        "type": "function",
        "description": "Fired when the captive portal state changes.",
        "parameters": [
          {
            "type": "object",
            "name": "details",
            "properties": {
              "state": {
                "type": "string",
                "enum": ["unknown", "not_captive", "unlocked_portal", "locked_portal"],
                "description": "The current captive portal state."
              }
            }
          }
        ]
      },
      {
        "name": "onConnectivityAvailable",
        "type": "function",
        "description": "This notification will be emitted when the captive portal service has determined that we can connect to the internet. The service will pass either `captive` if there is an unlocked captive portal present, or `clear` if no captive portal was detected.",
        "parameters": [
          {
            "name": "status",
            "enum": ["captive", "clear"],
            "type": "string"
          }
        ]
      }
    ]
  }
]
 No newline at end of file
+3 −0
Original line number Original line Diff line number Diff line
@@ -6,6 +6,9 @@ toolkit.jar:
% content extensions %content/extensions/
% content extensions %content/extensions/
    content/extensions/schemas/alarms.json
    content/extensions/schemas/alarms.json
    content/extensions/schemas/browser_settings.json
    content/extensions/schemas/browser_settings.json
#ifndef ANDROID
    content/extensions/schemas/captive_portal.json
#endif
    content/extensions/schemas/clipboard.json
    content/extensions/schemas/clipboard.json
    content/extensions/schemas/content_scripts.json
    content/extensions/schemas/content_scripts.json
    content/extensions/schemas/contextual_identities.json
    content/extensions/schemas/contextual_identities.json
Loading