Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Delta Between Two Patch Sets: lib/appIntegration.js

Issue 8433028: added hook function to appIntegration to handle function-overwrites from other extensions (Closed)
Left Patch Set: applied changes from code review Created Sept. 26, 2012, 1:53 p.m.
Right Patch Set: added missing statement Created Sept. 28, 2012, 9:52 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « chrome/content/tests/tests/suffixTreeManipulation.js ('k') | lib/hooks.js » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* This Source Code Form is subject to the terms of the Mozilla Public 1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4 4
5 let {hook} = require("hooks");
5 let functionHooks = new WeakMap(); 6 let functionHooks = new WeakMap();
6 let {hook} = require("hooks"); 7
8 exports.removeFromWindow = function(window)
9 {
10 if (functionHooks.has(window))
11 {
12 let unhook = functionHooks.get(window);
13 unhook();
14 functionHooks.delete(window);
15 }
16 };
17
7 let {application} = require("info"); 18 let {application} = require("info");
8 switch (application) 19 switch (application)
9 { 20 {
10 case "firefox": 21 case "firefox":
11 { 22 {
12 // Firefox 23 // Firefox
13 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser"; 24 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
14 25
15 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; 26 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
16 27
17 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; 28 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
18 29
19 exports.applyToWindow = function(window, corrector) 30 exports.applyToWindow = function(window, corrector)
20 { 31 {
21 let urlbar = exports.getURLBar(window); 32 let urlbar = exports.getURLBar(window);
22 if (urlbar && urlbar.handleCommand && !functionHooks.has(window)) 33 if (urlbar && urlbar.handleCommand && !functionHooks.has(window))
23 { 34 {
24 // Handle new URLs being entered 35 // Handle new URLs being entered
25 let unhook = hook(urlbar, "handleCommand", function() { 36 let unhook = hook(urlbar, "handleCommand", function()
37 {
26 let correction = corrector(window, urlbar.value); 38 let correction = corrector(window, urlbar.value);
27 if (correction) 39 if (correction)
28 urlbar.value = correction; 40 urlbar.value = correction;
29 }); 41 });
30 functionHooks.set(window, unhook); 42 functionHooks.set(window, unhook);
31 }
32 };
33
34 exports.removeFromWindow = function(window)
35 {
36 if (functionHooks.has(window))
37 {
38 let unhook = functionHooks.get(window);
39 unhook();
40 functionHooks.delete(window);
41 } 43 }
42 }; 44 };
43 45
44 exports.openInfobar = function(window, id, message, buttons, persistence) 46 exports.openInfobar = function(window, id, message, buttons, persistence)
45 { 47 {
46 let browser = exports.getBrowser(window); 48 let browser = exports.getBrowser(window);
47 let infobar = browser.getNotificationBox(); 49 let infobar = browser.getNotificationBox();
48 let notif = infobar.getNotificationWithValue(id); 50 let notif = infobar.getNotificationWithValue(id);
49 51
50 if (notif) 52 if (notif)
51 { 53 {
52 infobar.removeNotification(notif); 54 infobar.removeNotification(notif);
53 } 55 }
54 56
55 notif = infobar.appendNotification( 57 notif = infobar.appendNotification(
56 message, 58 message,
57 id, 59 id,
58 require("info").addonRoot + "icon64.png", 60 require("info").addonRoot + "icon64.png",
59 infobar.PRIORITY_INFO_HIGH, 61 infobar.PRIORITY_INFO_HIGH,
60 buttons 62 buttons
61 ); 63 );
62 notif.persistence = persistence; 64 notif.persistence = persistence;
63 }; 65 };
64 66
65 exports.loadURI = function(uri) 67 exports.loadURI = function(window, uri)
66 { 68 {
67 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri); 69 exports.getBrowser(window).loadURI(uri);
68 }; 70 };
69 71
70 break; 72 break;
71 } 73 }
72 case "seamonkey": 74 case "seamonkey":
73 { 75 {
74 let eventListeners = new WeakMap(); 76 let eventListeners = new WeakMap();
75 77
76 // SeaMonkey 78 // SeaMonkey
77 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser"; 79 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
78 80
79 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; 81 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
80 82
81 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; 83 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
82 84
83 exports.applyToWindow = function(window, corrector) 85 exports.applyToWindow = function(window, corrector)
84 { 86 {
85 let urlbar = exports.getURLBar(window); 87 let urlbar = exports.getURLBar(window);
86 let goButton = window.document.getElementById("go-button-container"); 88 let goButton = window.document.getElementById("go-button-container");
87 89
88 if (urlbar && urlbar._fireEvent && !functionHooks.has(window)) 90 if (urlbar && urlbar._fireEvent && !functionHooks.has(window))
89 { 91 {
90 function correctURL() 92 function correctURL()
91 { 93 {
92 let correction = corrector(window, urlbar.value); 94 let correction = corrector(window, urlbar.value);
93 if (correction) 95 if (correction)
94 urlbar.value = correction; 96 urlbar.value = correction;
95 } 97 }
96 98
97 let unhook = hook(urlbar, "_fireEvent", function(eventType) { 99 let unhook = hook(urlbar, "_fireEvent", function(eventType)
Wladimir Palant 2012/09/27 14:36:01 This bracket should be on next line.
100 {
98 if (eventType == "textentered") 101 if (eventType == "textentered")
99 { 102 {
100 correctURL(); 103 correctURL();
101 } 104 }
102 }); 105 });
103 functionHooks.set(window, unhook); 106 functionHooks.set(window, unhook);
104 107
105 if (goButton) 108 if (goButton)
106 { 109 {
107 goButton.addEventListener("command", correctURL, true); 110 goButton.addEventListener("command", correctURL, true);
108 eventListeners.set(window, { 111 eventListeners.set(window, {
109 "listener": correctURL, 112 "listener": correctURL,
110 "element": goButton 113 "element": goButton
111 }); 114 });
112 } 115 }
113 } 116 }
114 }; 117 };
115 118
119 let basicRemove = exports.removeFromWindow;
116 exports.removeFromWindow = function(window) 120 exports.removeFromWindow = function(window)
117 { 121 {
118 if (functionHooks.has(window)) 122 basicRemove(window);
119 { 123
120 let unhook = functionHooks.get(window);
121 unhook();
122 functionHooks.delete(window);
123 }
124
125 if (eventListeners.has(window)) 124 if (eventListeners.has(window))
126 { 125 {
127 let eventListener = eventListeners.get(window); 126 let eventListener = eventListeners.get(window);
128 eventListener.element.removeEventListener("command", eventListener.liste ner, true); 127 eventListener.element.removeEventListener("command", eventListener.liste ner, true);
Wladimir Palant 2012/09/27 14:36:01 eventListeners.delete(window)?
128 eventListeners.delete(window);
129 } 129 }
130 }; 130 };
131 131
132 exports.openInfobar = function(window, id, message, buttons, persistence) 132 exports.openInfobar = function(window, id, message, buttons, persistence)
133 { 133 {
134 let browser = exports.getBrowser(window); 134 let browser = exports.getBrowser(window);
135 let infobar = browser.getNotificationBox(); 135 let infobar = browser.getNotificationBox();
136 let notif = infobar.getNotificationWithValue(id); 136 let notif = infobar.getNotificationWithValue(id);
137 137
138 if (notif) 138 if (notif)
139 { 139 {
140 infobar.removeNotification(notif); 140 infobar.removeNotification(notif);
141 } 141 }
142 142
143 notif = infobar.appendNotification( 143 notif = infobar.appendNotification(
144 message, 144 message,
145 id, 145 id,
146 require("info").addonRoot + "icon64.png", 146 require("info").addonRoot + "icon64.png",
147 infobar.PRIORITY_INFO_HIGH, 147 infobar.PRIORITY_INFO_HIGH,
148 buttons 148 buttons
149 ); 149 );
150 notif.persistence = persistence; 150 notif.persistence = persistence;
151 }; 151 };
152 152
153 exports.loadURI = function(uri) 153 exports.loadURI = function(window, uri)
154 { 154 {
155 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri); 155 exports.getBrowser(window).loadURI(uri);
156 }; 156 };
157 157
158 break; 158 break;
159 } 159 }
160 case "fennec": 160 case "fennec":
161 { 161 {
162 // XUL Fennec 162 // XUL Fennec
163 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser"; 163 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
164 164
165 exports.getURLBar = function(window) null; 165 exports.getURLBar = function(window) null;
166 166
167 exports.getBrowser = function(window) null; 167 exports.getBrowser = function(window) null;
168 168
169 exports.applyToWindow = function(window, corrector) 169 exports.applyToWindow = function(window, corrector)
170 { 170 {
171 if ("BrowserUI" in window && window.BrowserUI.goToURI && !("urlfixerOldHan dler" in window.BrowserUI.goToURI)) 171 if ("BrowserUI" in window && window.BrowserUI.goToURI && !functionHooks.ha s(window))
172 { 172 {
173 // Handle new URLs being entered 173 // Handle new URLs being entered
174 let oldHandler = window.BrowserUI.goToURI; 174 let unhook = hook(window.BrowserUI, "goToURI", function(url)
175 window.BrowserUI.goToURI = function(url)
176 { 175 {
177 url = url || this._edit.value; 176 url = url || this._edit.value;
178 try 177
179 { 178 let correction = corrector(window, url);
180 let correction = corrector(window, url); 179 if (correction)
181 if (correction) 180 url = correction;
182 url = correction; 181
183 } 182 return [url];
184 catch(e) 183 });
185 { 184 functionHooks.set(window, unhook);
186 Cu.reportError(e); 185 }
187 } 186 };
188 oldHandler.call(this, url); 187
189 } 188 exports.openInfobar = function(window, id, message, buttons, persistence)
190 window.BrowserUI.goToURI.urlfixerOldHandler = oldHandler; 189 {
191 window.BrowserUI.goToURI.toString = doNotRecompile; 190 if ("getNotificationBox" in window)
192 } 191 {
193 }; 192 let infobar = window.getNotificationBox();
194 193 let notification = infobar.getNotificationWithValue(id);
195 exports.removeFromWindow = function(window) 194
196 { 195 if (notification)
197 if ("BrowserUI" in window && window.BrowserUI.goToURI && "urlfixerOldHandl er" in window.BrowserUI.goToURI) 196 {
198 window.BrowserUI.goToURI = window.BrowserUI.goToURI.urlfixerOldHandler; 197 infobar.removeNotification(notification);
199 }; 198 }
200 199
201 exports.openInfobar = function() 200 notification = infobar.appendNotification(
202 { 201 message,
203 // TODO: Implement infobar 202 id,
204 }; 203 require("info").addonRoot + "icon64.png",
205 204 infobar.PRIORITY_INFO_HIGH,
206 exports.loadURI = function(window, uri) 205 buttons
207 { 206 );
208 // TODO: Implement infobar 207 notification.persistence = persistence;
208 }
209 };
210
211 exports.loadURI = function(window, uri)
212 {
213 if ("BrowserUI" in window && "goToURI" in window.BrowserUI)
214 {
215 window.BrowserUI.goToURI(uri);
216 }
209 }; 217 };
210 218
211 break; 219 break;
212 } 220 }
213 case "fennec2": 221 case "fennec2":
214 { 222 {
215 // Native Fennec 223 // Native Fennec
216 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser"; 224 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
217 225
218 exports.getURLBar = function(window) null; 226 exports.getURLBar = function(window) null;
219 227
220 exports.getBrowser = function(window) null; 228 exports.getBrowser = function(window) null;
221 229
222 exports.applyToWindow = function(window, corrector) 230 exports.applyToWindow = function(window, corrector)
223 { 231 {
224 if ("BrowserApp" in window && window.BrowserApp.observe && !("urlfixerOldH andler" in window.BrowserApp.observe)) 232 if ("BrowserApp" in window && window.BrowserApp.observe && !functionHooks. has(window))
225 { 233 {
226 let oldHandler = window.BrowserApp.observe; 234 let innerUnhook = null;
227 let oldFunc = null; 235 function cleanup()
228 let handler = function() 236 {
229 { 237 if (innerUnhook)
230 let params = Array.prototype.slice.apply(arguments); 238 innerUnhook();
231 try 239
232 { 240 innerUnhook = null;
233 let correction = corrector(window, params[0]); 241 }
234 if (correction) 242
235 params[0] = correction; 243 let unhook = hook(window.BrowserApp, "observe", function(subject, topic, data)
236 }
237 catch(e)
238 {
239 Cu.reportError(e);
240 }
241 return oldFunc.apply(this, params);
242 };
243
244 window.BrowserApp.observe = function(subject, topic, data)
245 { 244 {
246 // Huge hack: we replace addTab/loadURI when the observer is 245 // Huge hack: we replace addTab/loadURI when the observer is
247 // triggered. This seems to be the only way to know that the calls 246 // triggered. This seems to be the only way to know that the calls
248 // originate from user input. 247 // originate from user input.
249 let method = null; 248 let method = null;
250 if (topic == "Tab:Add") 249 if (topic == "Tab:Add")
251 method = "addTab"; 250 method = "addTab";
252 else if (topic == "Tab:Load") 251 else if (topic == "Tab:Load")
253 method = "loadURI"; 252 method = "loadURI";
254 253
255 if (method) 254 if (method)
256 { 255 {
257 oldFunc = this[method]; 256 innerUnhook = hook(this, method, function()
258 this[method] = handler; 257 {
258 let params = Array.prototype.slice.apply(arguments);
259 let correction = corrector(window, params[0]);
260 if (correction)
261 params[0] = correction;
262 return params;
263 });
259 } 264 }
260 265 }, cleanup);
261 try 266 functionHooks.set(window, unhook);
262 { 267 }
263 oldHandler.apply(this, arguments);
264 }
265 finally
266 {
267 if (method)
268 this[method] = oldFunc;
269 }
270 };
271 window.BrowserApp.observe.urlfixerOldHandler = oldHandler;
272 window.BrowserApp.observe.toString = doNotRecompile;
273 }
274 };
275
276 exports.removeFromWindow = function(window)
277 {
278 if ("BrowserApp" in window && window.BrowserApp.observe && "urlfixerOldHan dler" in window.BrowserApp.observe)
279 window.BrowserApp.observe = window.BrowserApp.observe.urlfixerOldHandler ;
280 }; 268 };
281 269
282 exports.openInfobar = function(window, id, message, buttons, persistence) 270 exports.openInfobar = function(window, id, message, buttons, persistence)
283 { 271 {
284 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp) 272 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp)
285 { 273 {
286 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser App.selectedTab.id, 274 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser App.selectedTab.id,
287 { 275 {
288 persistence: persistence 276 persistence: persistence
289 } 277 }
290 ); 278 );
291 } 279 }
292 }; 280 };
293 281
294 exports.loadURI = function(uri) 282 exports.loadURI = function(window, uri)
295 { 283 {
296 let window = Services.wm.getMostRecentWindow("navigator:browser");
297 if ("BrowserApp" in window && "loadURI" in window.BrowserApp) 284 if ("BrowserApp" in window && "loadURI" in window.BrowserApp)
298 window.BrowserApp.loadURI(uri); 285 window.BrowserApp.loadURI(uri);
299 }; 286 };
300 287
301 break; 288 break;
302 } 289 }
303 default: 290 default:
304 { 291 {
305 exports.isKnownWindow = function(window) false; 292 exports.isKnownWindow = function(window) false;
306 break; 293 break;
307 } 294 }
308 } 295 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld