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

Side by Side Diff: lib/appIntegration.js

Issue 8433028: added hook function to appIntegration to handle function-overwrites from other extensions (Closed)
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:
View unified diff | Download patch
« no previous file with comments | « chrome/content/tests/tests/suffixTreeManipulation.js ('k') | lib/hooks.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // Extensions like Tab Mix Plus will try to recompile our handlers and fail 5 let {hook} = require("hooks");
6 // badly - they manage to replace our handler but the recompiled version 6 let functionHooks = new WeakMap();
7 // won't work because the closure is missing its variables. We replace 7
8 // toString() function to prevent this kind of recompiling on earlier stages 8 exports.removeFromWindow = function(window)
9 // (produce a syntax error but still leave the source code viewable).
10 function doNotRecompile()
11 { 9 {
12 let result = Function.prototype.toString.apply(this); 10 if (functionHooks.has(window))
13 return result + "\n$%&!/DO_NOT_RECOMPILE" 11 {
14 } 12 let unhook = functionHooks.get(window);
13 unhook();
14 functionHooks.delete(window);
15 }
16 };
15 17
16 let {application} = require("info"); 18 let {application} = require("info");
17 switch (application) 19 switch (application)
18 { 20 {
19 case "firefox": 21 case "firefox":
20 { 22 {
21 // Firefox 23 // Firefox
22 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";
23 25
24 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; 26 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
25 27
26 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; 28 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
27 29
28 exports.applyToWindow = function(window, corrector) 30 exports.applyToWindow = function(window, corrector)
29 { 31 {
30 let urlbar = exports.getURLBar(window); 32 let urlbar = exports.getURLBar(window);
31 if (urlbar && urlbar.handleCommand && !("urlfixerOldHandler" in urlbar.han dleCommand)) 33 if (urlbar && urlbar.handleCommand && !functionHooks.has(window))
32 { 34 {
33 // Handle new URLs being entered 35 // Handle new URLs being entered
34 let oldHandler = urlbar.handleCommand; 36 let unhook = hook(urlbar, "handleCommand", function()
35 urlbar.handleCommand = function()
36 { 37 {
37 try 38 let correction = corrector(window, urlbar.value);
38 { 39 if (correction)
39 let correction = corrector(window, urlbar.value); 40 urlbar.value = correction;
40 if (correction) 41 });
41 urlbar.value = correction; 42 functionHooks.set(window, unhook);
42 }
43 catch(e)
44 {
45 if (e == Cr.NS_BINDING_ABORTED)
46 return;
47 else
48 Cu.reportError(e);
49 }
50 oldHandler.apply(this, arguments);
51 }
52 urlbar.handleCommand.urlfixerOldHandler = oldHandler;
53 urlbar.handleCommand.toString = doNotRecompile;
54 } 43 }
55 }; 44 };
56 45
57 exports.removeFromWindow = function(window)
58 {
59 let urlbar = exports.getURLBar(window);
60 if (urlbar && urlbar.handleCommand && "urlfixerOldHandler" in urlbar.handl eCommand)
61 urlbar.handleCommand = urlbar.handleCommand.urlfixerOldHandler;
62 };
63
64 exports.openInfobar = function(window, id, message, buttons, persistence) 46 exports.openInfobar = function(window, id, message, buttons, persistence)
65 { 47 {
66 let browser = exports.getBrowser(window); 48 let browser = exports.getBrowser(window);
67 let infobar = browser.getNotificationBox(); 49 let infobar = browser.getNotificationBox();
68 let notif = infobar.getNotificationWithValue(id); 50 let notif = infobar.getNotificationWithValue(id);
69 51
70 if (notif) 52 if (notif)
71 { 53 {
72 infobar.removeNotification(notif); 54 infobar.removeNotification(notif);
73 } 55 }
74 56
75 notif = infobar.appendNotification( 57 notif = infobar.appendNotification(
76 message, 58 message,
77 id, 59 id,
78 require("info").addonRoot + "icon64.png", 60 require("info").addonRoot + "icon64.png",
79 infobar.PRIORITY_INFO_HIGH, 61 infobar.PRIORITY_INFO_HIGH,
80 buttons 62 buttons
81 ); 63 );
82 notif.persistence = persistence; 64 notif.persistence = persistence;
83 }; 65 };
84 66
85 exports.loadURI = function(uri) 67 exports.loadURI = function(window, uri)
86 { 68 {
87 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri); 69 exports.getBrowser(window).loadURI(uri);
88 }; 70 };
89 71
90 break; 72 break;
91 } 73 }
92 case "seamonkey": 74 case "seamonkey":
93 { 75 {
76 let eventListeners = new WeakMap();
77
94 // SeaMonkey 78 // SeaMonkey
95 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";
96 80
97 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; 81 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
98 82
99 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; 83 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
100 84
101 exports.applyToWindow = function(window, corrector) 85 exports.applyToWindow = function(window, corrector)
102 { 86 {
103 let urlbar = exports.getURLBar(window); 87 let urlbar = exports.getURLBar(window);
104 if (urlbar && window.handleURLBarCommand && !("urlfixerOldHandler" in wind ow.handleURLBarCommand)) 88 let goButton = window.document.getElementById("go-button-container");
89
90 if (urlbar && urlbar._fireEvent && !functionHooks.has(window))
105 { 91 {
106 // Handle new URLs being entered 92 function correctURL()
107 let oldHandler = window.handleURLBarCommand;
108 window.handleURLBarCommand = function()
109 { 93 {
110 try 94 let correction = corrector(window, urlbar.value);
95 if (correction)
96 urlbar.value = correction;
97 }
98
99 let unhook = hook(urlbar, "_fireEvent", function(eventType)
100 {
101 if (eventType == "textentered")
111 { 102 {
112 let correction = corrector(window, urlbar.value); 103 correctURL();
113 if (correction)
114 urlbar.value = correction;
115 } 104 }
116 catch(e) 105 });
117 { 106 functionHooks.set(window, unhook);
118 if (e == Cr.NS_BINDING_ABORTED) 107
119 return; 108 if (goButton)
120 else 109 {
121 Cu.reportError(e); 110 goButton.addEventListener("command", correctURL, true);
122 } 111 eventListeners.set(window, {
123 oldHandler.apply(this, arguments); 112 "listener": correctURL,
113 "element": goButton
114 });
124 } 115 }
125 window.handleURLBarCommand.urlfixerOldHandler = oldHandler;
126 window.handleURLBarCommand.toString = doNotRecompile;
127 } 116 }
128 }; 117 };
129 118
119 let basicRemove = exports.removeFromWindow;
130 exports.removeFromWindow = function(window) 120 exports.removeFromWindow = function(window)
131 { 121 {
132 if (window.handleURLBarCommand && "urlfixerOldHandler" in window.handleURL BarCommand) 122 basicRemove(window);
133 window.handleURLBarCommand = window.handleURLBarCommand.urlfixerOldHandl er; 123
124 if (eventListeners.has(window))
125 {
126 let eventListener = eventListeners.get(window);
127 eventListener.element.removeEventListener("command", eventListener.liste ner, true);
128 eventListeners.delete(window);
129 }
134 }; 130 };
135 131
136 exports.openInfobar = function(window, id, message, buttons, persistence) 132 exports.openInfobar = function(window, id, message, buttons, persistence)
137 { 133 {
138 let browser = exports.getBrowser(window); 134 let browser = exports.getBrowser(window);
139 let infobar = browser.getNotificationBox(); 135 let infobar = browser.getNotificationBox();
140 let notif = infobar.getNotificationWithValue(id); 136 let notif = infobar.getNotificationWithValue(id);
141 137
142 if (notif) 138 if (notif)
143 { 139 {
144 infobar.removeNotification(notif); 140 infobar.removeNotification(notif);
145 } 141 }
146 142
147 notif = infobar.appendNotification( 143 notif = infobar.appendNotification(
148 message, 144 message,
149 id, 145 id,
150 require("info").addonRoot + "icon64.png", 146 require("info").addonRoot + "icon64.png",
151 infobar.PRIORITY_INFO_HIGH, 147 infobar.PRIORITY_INFO_HIGH,
152 buttons 148 buttons
153 ); 149 );
154 notif.persistence = persistence; 150 notif.persistence = persistence;
155 }; 151 };
156 152
157 exports.loadURI = function(uri) 153 exports.loadURI = function(window, uri)
158 { 154 {
159 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri); 155 exports.getBrowser(window).loadURI(uri);
160 }; 156 };
161 157
162 break; 158 break;
163 } 159 }
164 case "fennec": 160 case "fennec":
165 { 161 {
166 // XUL Fennec 162 // XUL Fennec
167 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";
168 164
169 exports.getURLBar = function(window) null; 165 exports.getURLBar = function(window) null;
170 166
171 exports.getBrowser = function(window) null; 167 exports.getBrowser = function(window) null;
172 168
173 exports.applyToWindow = function(window, corrector) 169 exports.applyToWindow = function(window, corrector)
174 { 170 {
175 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))
176 { 172 {
177 // Handle new URLs being entered 173 // Handle new URLs being entered
178 let oldHandler = window.BrowserUI.goToURI; 174 let unhook = hook(window.BrowserUI, "goToURI", function(url)
179 window.BrowserUI.goToURI = function(url)
180 { 175 {
181 url = url || this._edit.value; 176 url = url || this._edit.value;
182 try 177
183 { 178 let correction = corrector(window, url);
184 let correction = corrector(window, url); 179 if (correction)
185 if (correction) 180 url = correction;
186 url = correction; 181
187 } 182 return [url];
188 catch(e) 183 });
189 { 184 functionHooks.set(window, unhook);
190 if (e == Cr.NS_BINDING_ABORTED)
191 return;
192 else
193 Cu.reportError(e);
194 }
195 oldHandler.call(this, url);
196 }
197 window.BrowserUI.goToURI.urlfixerOldHandler = oldHandler;
198 window.BrowserUI.goToURI.toString = doNotRecompile;
199 } 185 }
200 }; 186 };
201 187
202 exports.removeFromWindow = function(window) 188 exports.openInfobar = function(window, id, message, buttons, persistence)
203 { 189 {
204 if ("BrowserUI" in window && window.BrowserUI.goToURI && "urlfixerOldHandl er" in window.BrowserUI.goToURI) 190 if ("getNotificationBox" in window)
205 window.BrowserUI.goToURI = window.BrowserUI.goToURI.urlfixerOldHandler; 191 {
192 let infobar = window.getNotificationBox();
193 let notification = infobar.getNotificationWithValue(id);
194
195 if (notification)
196 {
197 infobar.removeNotification(notification);
198 }
199
200 notification = infobar.appendNotification(
201 message,
202 id,
203 require("info").addonRoot + "icon64.png",
204 infobar.PRIORITY_INFO_HIGH,
205 buttons
206 );
207 notification.persistence = persistence;
208 }
206 }; 209 };
207 210
208 exports.openInfobar = function()
209 {
210 // TODO: Implement infobar
211 };
212
213 exports.loadURI = function(window, uri) 211 exports.loadURI = function(window, uri)
214 { 212 {
215 // TODO: Implement infobar 213 if ("BrowserUI" in window && "goToURI" in window.BrowserUI)
214 {
215 window.BrowserUI.goToURI(uri);
216 }
216 }; 217 };
217 218
218 break; 219 break;
219 } 220 }
220 case "fennec2": 221 case "fennec2":
221 { 222 {
222 // Native Fennec 223 // Native Fennec
223 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";
224 225
225 exports.getURLBar = function(window) null; 226 exports.getURLBar = function(window) null;
226 227
227 exports.getBrowser = function(window) null; 228 exports.getBrowser = function(window) null;
228 229
229 exports.applyToWindow = function(window, corrector) 230 exports.applyToWindow = function(window, corrector)
230 { 231 {
231 if ("BrowserApp" in window && window.BrowserApp.observe && !("urlfixerOldH andler" in window.BrowserApp.observe)) 232 if ("BrowserApp" in window && window.BrowserApp.observe && !functionHooks. has(window))
232 { 233 {
233 let oldHandler = window.BrowserApp.observe; 234 let innerUnhook = null;
234 let oldFunc = null; 235 function cleanup()
235 let handler = function()
236 { 236 {
237 let params = Array.prototype.slice.apply(arguments); 237 if (innerUnhook)
238 try 238 innerUnhook();
239 {
240 let correction = corrector(window, params[0]);
241 if (correction)
242 params[0] = correction;
243 }
244 catch(e)
245 {
246 if (e == Cr.NS_BINDING_ABORTED)
247 return null;
248 else
249 Cu.reportError(e);
250 }
251 return oldFunc.apply(this, params);
252 };
253 239
254 window.BrowserApp.observe = function(subject, topic, data) 240 innerUnhook = null;
241 }
242
243 let unhook = hook(window.BrowserApp, "observe", function(subject, topic, data)
255 { 244 {
256 // Huge hack: we replace addTab/loadURI when the observer is 245 // Huge hack: we replace addTab/loadURI when the observer is
257 // 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
258 // originate from user input. 247 // originate from user input.
259 let method = null; 248 let method = null;
260 if (topic == "Tab:Add") 249 if (topic == "Tab:Add")
261 method = "addTab"; 250 method = "addTab";
262 else if (topic == "Tab:Load") 251 else if (topic == "Tab:Load")
263 method = "loadURI"; 252 method = "loadURI";
264 253
265 if (method) 254 if (method)
266 { 255 {
267 oldFunc = this[method]; 256 innerUnhook = hook(this, method, function()
268 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 });
269 } 264 }
270 265 }, cleanup);
271 try 266 functionHooks.set(window, unhook);
272 {
273 oldHandler.apply(this, arguments);
274 }
275 finally
276 {
277 if (method)
278 this[method] = oldFunc;
279 }
280 };
281 window.BrowserApp.observe.urlfixerOldHandler = oldHandler;
282 window.BrowserApp.observe.toString = doNotRecompile;
283 } 267 }
284 }; 268 };
285 269
286 exports.removeFromWindow = function(window)
287 {
288 if ("BrowserApp" in window && window.BrowserApp.observe && "urlfixerOldHan dler" in window.BrowserApp.observe)
289 window.BrowserApp.observe = window.BrowserApp.observe.urlfixerOldHandler ;
290 };
291
292 exports.openInfobar = function(window, id, message, buttons, persistence) 270 exports.openInfobar = function(window, id, message, buttons, persistence)
293 { 271 {
294 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp) 272 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp)
295 { 273 {
296 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,
297 { 275 {
298 persistence: persistence 276 persistence: persistence
299 } 277 }
300 ); 278 );
301 } 279 }
302 }; 280 };
303 281
304 exports.loadURI = function(uri) 282 exports.loadURI = function(window, uri)
305 { 283 {
306 let window = Services.wm.getMostRecentWindow("navigator:browser");
307 if ("BrowserApp" in window && "loadURI" in window.BrowserApp) 284 if ("BrowserApp" in window && "loadURI" in window.BrowserApp)
308 window.BrowserApp.loadURI(uri); 285 window.BrowserApp.loadURI(uri);
309 }; 286 };
310 287
311 break; 288 break;
312 } 289 }
313 default: 290 default:
314 { 291 {
315 exports.isKnownWindow = function(window) false; 292 exports.isKnownWindow = function(window) false;
316 break; 293 break;
317 } 294 }
318 } 295 }
OLDNEW
« no previous file with comments | « chrome/content/tests/tests/suffixTreeManipulation.js ('k') | lib/hooks.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld