1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/* -*- 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
);
var { ExtensionPreferencesManager } = ChromeUtils.importESModule(
"resource://gre/modules/ExtensionPreferencesManager.sys.mjs"
);
var { getSettingsAPI } = ExtensionPreferencesManager;
const CAPTIVE_URL_PREF = "captivedetect.canonicalURL";
var { ExtensionError } = ExtensionUtils;
this.captivePortal = class extends ExtensionAPIPersistent {
checkCaptivePortalEnabled() {
if (!gCaptivePortalEnabled) {
throw new ExtensionError("Captive Portal detection is not enabled");
}
}
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";
}
}
PERSISTENT_EVENTS = {
onStateChanged({ fire }) {
this.checkCaptivePortalEnabled();
let observer = () => {
fire.async({ state: this.nameForCPSState(gCPS.state) });
};
Services.obs.addObserver(
observer,
"ipc:network:captive-portal-set-state"
);
return {
unregister: () => {
Services.obs.removeObserver(
observer,
"ipc:network:captive-portal-set-state"
);
},
convert(_fire) {
fire = _fire;
},
};
},
onConnectivityAvailable({ fire }) {
this.checkCaptivePortalEnabled();
let observer = (subject, topic, data) => {
fire.async({ status: data });
};
Services.obs.addObserver(observer, "network:captive-portal-connectivity");
return {
unregister: () => {
Services.obs.removeObserver(
observer,
"network:captive-portal-connectivity"
);
},
convert(_fire) {
fire = _fire;
},
};
},
"captiveURL.onChange": ({ fire }) => {
let listener = () => {
fire.async({
levelOfControl: "not_controllable",
value: Services.prefs.getStringPref(CAPTIVE_URL_PREF),
});
};
Services.prefs.addObserver(CAPTIVE_URL_PREF, listener);
return {
unregister: () => {
Services.prefs.removeObserver(CAPTIVE_URL_PREF, listener);
},
convert(_fire) {
fire = _fire;
},
};
},
};
getAPI(context) {
let self = this;
return {
captivePortal: {
getState() {
self.checkCaptivePortalEnabled();
return self.nameForCPSState(gCPS.state);
},
getLastChecked() {
self.checkCaptivePortalEnabled();
return gCPS.lastChecked;
},
onStateChanged: new EventManager({
context,
module: "captivePortal",
event: "onStateChanged",
extensionApi: self,
}).api(),
onConnectivityAvailable: new EventManager({
context,
module: "captivePortal",
event: "onConnectivityAvailable",
extensionApi: self,
}).api(),
canonicalURL: getSettingsAPI({
context,
name: "captiveURL",
callback() {
return Services.prefs.getStringPref(CAPTIVE_URL_PREF);
},
readOnly: true,
onChange: new ExtensionCommon.EventManager({
context,
module: "captivePortal",
event: "captiveURL.onChange",
extensionApi: self,
}).api(),
}),
},
};
}
};
|