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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/* 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";
const { classes: Cc, interfaces: Ci, utils: Cu } = Components;
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
Cu.import("resource://gre/modules/Messaging.jsm");
Cu.import("resource://gre/modules/UITelemetry.jsm");
this.EXPORTED_SYMBOLS = ["NetErrorHelper"];
const KEY_CODE_ENTER = 13;
/* Handlers is a list of objects that will be notified when an error page is shown
* or when an event occurs on the page that they are registered to handle. Registration
* is done by just adding yourself to the dictionary.
*
* handlers.myKey = {
* onPageShown: function(browser) { },
* handleEvent: function(event) { },
* }
*
* The key that you register yourself with should match the ID of the element you want to
* watch for click events on.
*/
var handlers = {};
function NetErrorHelper(browser) {
browser.addEventListener("click", this.handleClick, true);
let listener = () => {
browser.removeEventListener("click", this.handleClick, true);
browser.removeEventListener("pagehide", listener, true);
};
browser.addEventListener("pagehide", listener, true);
// Handlers may want to customize the page
for (let id in handlers) {
if (handlers[id].onPageShown) {
handlers[id].onPageShown(browser);
}
}
}
NetErrorHelper.attachToBrowser = function(browser) {
return new NetErrorHelper(browser);
}
NetErrorHelper.prototype = {
handleClick: function(event) {
let node = event.target;
while(node) {
if (node.id in handlers && handlers[node.id].handleClick) {
handlers[node.id].handleClick(event);
return;
}
node = node.parentNode;
}
},
}
handlers.searchbutton = {
onPageShown: function(browser) {
let search = browser.contentDocument.querySelector("#searchbox");
if (!search) {
return;
}
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
let tab = browserWin.BrowserApp.getTabForBrowser(browser);
// If there is no stored userRequested, just hide the searchbox
if (!tab.userRequested) {
search.style.display = "none";
} else {
let text = browser.contentDocument.querySelector("#searchtext");
text.value = tab.userRequested;
text.addEventListener("keypress", (event) => {
if (event.keyCode === KEY_CODE_ENTER) {
this.doSearch(event.target.value);
}
});
}
},
handleClick: function(event) {
let value = event.target.previousElementSibling.value;
this.doSearch(value);
},
doSearch: function(value) {
UITelemetry.addEvent("neterror.1", "button", null, "search");
let engine = Services.search.defaultEngine;
let uri = engine.getSubmission(value).uri;
let browserWin = Services.wm.getMostRecentWindow("navigator:browser");
// Reset the user search to whatever the new search term was
browserWin.BrowserApp.loadURI(uri.spec, undefined, { isSearch: true, userRequested: value });
}
};
handlers.wifi = {
// This registers itself with the nsIObserverService as a weak ref,
// so we have to implement GetWeakReference as well.
QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver,
Ci.nsISupportsWeakReference]),
GetWeakReference: function() {
return Cu.getWeakReference(this);
},
onPageShown: function(browser) {
// If we have a connection, don't bother showing the wifi toggle.
let network = Cc["@mozilla.org/network/network-link-service;1"].getService(Ci.nsINetworkLinkService);
if (network.isLinkUp && network.linkStatusKnown) {
let nodes = browser.contentDocument.querySelectorAll("#wifi");
for (let i = 0; i < nodes.length; i++) {
nodes[i].style.display = "none";
}
}
},
handleClick: function(event) {
let node = event.target;
while(node && node.id !== "wifi") {
node = node.parentNode;
}
if (!node) {
return;
}
UITelemetry.addEvent("neterror.1", "button", null, "wifitoggle");
// Show indeterminate progress while we wait for the network.
node.disabled = true;
node.classList.add("inProgress");
this.node = Cu.getWeakReference(node);
Services.obs.addObserver(this, "network:link-status-changed", true);
Messaging.sendRequest({
type: "Wifi:Enable"
});
},
observe: function(subject, topic, data) {
let node = this.node.get();
if (!node) {
return;
}
// Remove the progress bar
node.disabled = false;
node.classList.remove("inProgress");
let network = Cc["@mozilla.org/network/network-link-service;1"].getService(Ci.nsINetworkLinkService);
if (network.isLinkUp && network.linkStatusKnown) {
// If everything worked, reload the page
UITelemetry.addEvent("neterror.1", "button", null, "wifitoggle.reload");
Services.obs.removeObserver(this, "network:link-status-changed");
// Even at this point, Android sometimes lies about the real state of the network and this reload request fails.
// Add a 500ms delay before refreshing the page.
node.ownerDocument.defaultView.setTimeout(function() {
node.ownerDocument.location.reload(false);
}, 500);
}
}
}
|