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: applied changes from code review Created Sept. 26, 2012, 1:53 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
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 functionHooks = new WeakMap();
6 // badly - they manage to replace our handler but the recompiled version 6 let {hook} = require("hooks");
7 // won't work because the closure is missing its variables. We replace
8 // toString() function to prevent this kind of recompiling on earlier stages
9 // (produce a syntax error but still leave the source code viewable).
10 function doNotRecompile()
11 {
12 let result = Function.prototype.toString.apply(this);
13 return result + "\n$%&!/DO_NOT_RECOMPILE"
14 }
15
16 let {application} = require("info"); 7 let {application} = require("info");
17 switch (application) 8 switch (application)
18 { 9 {
19 case "firefox": 10 case "firefox":
20 { 11 {
21 // Firefox 12 // Firefox
22 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser"; 13 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
23 14
24 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; 15 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
25 16
26 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; 17 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
27 18
28 exports.applyToWindow = function(window, corrector) 19 exports.applyToWindow = function(window, corrector)
29 { 20 {
30 let urlbar = exports.getURLBar(window); 21 let urlbar = exports.getURLBar(window);
31 if (urlbar && urlbar.handleCommand && !("urlfixerOldHandler" in urlbar.han dleCommand)) 22 if (urlbar && urlbar.handleCommand && !functionHooks.has(window))
32 { 23 {
33 // Handle new URLs being entered 24 // Handle new URLs being entered
34 let oldHandler = urlbar.handleCommand; 25 let unhook = hook(urlbar, "handleCommand", function() {
35 urlbar.handleCommand = function() 26 let correction = corrector(window, urlbar.value);
36 { 27 if (correction)
37 try 28 urlbar.value = correction;
38 { 29 });
39 let correction = corrector(window, urlbar.value); 30 functionHooks.set(window, unhook);
40 if (correction)
41 urlbar.value = correction;
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 } 31 }
55 }; 32 };
56 33
57 exports.removeFromWindow = function(window) 34 exports.removeFromWindow = function(window)
58 { 35 {
59 let urlbar = exports.getURLBar(window); 36 if (functionHooks.has(window))
60 if (urlbar && urlbar.handleCommand && "urlfixerOldHandler" in urlbar.handl eCommand) 37 {
61 urlbar.handleCommand = urlbar.handleCommand.urlfixerOldHandler; 38 let unhook = functionHooks.get(window);
39 unhook();
40 functionHooks.delete(window);
41 }
62 }; 42 };
63 43
64 exports.openInfobar = function(window, id, message, buttons, persistence) 44 exports.openInfobar = function(window, id, message, buttons, persistence)
65 { 45 {
66 let browser = exports.getBrowser(window); 46 let browser = exports.getBrowser(window);
67 let infobar = browser.getNotificationBox(); 47 let infobar = browser.getNotificationBox();
68 let notif = infobar.getNotificationWithValue(id); 48 let notif = infobar.getNotificationWithValue(id);
69 49
70 if (notif) 50 if (notif)
71 { 51 {
72 infobar.removeNotification(notif); 52 infobar.removeNotification(notif);
73 } 53 }
74 54
75 notif = infobar.appendNotification( 55 notif = infobar.appendNotification(
76 message, 56 message,
77 id, 57 id,
78 require("info").addonRoot + "icon64.png", 58 require("info").addonRoot + "icon64.png",
79 infobar.PRIORITY_INFO_HIGH, 59 infobar.PRIORITY_INFO_HIGH,
80 buttons 60 buttons
81 ); 61 );
82 notif.persistence = persistence; 62 notif.persistence = persistence;
83 }; 63 };
84 64
85 exports.loadURI = function(uri) 65 exports.loadURI = function(uri)
86 { 66 {
87 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri); 67 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri);
88 }; 68 };
89 69
90 break; 70 break;
91 } 71 }
92 case "seamonkey": 72 case "seamonkey":
93 { 73 {
74 let eventListeners = new WeakMap();
75
94 // SeaMonkey 76 // SeaMonkey
95 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser"; 77 exports.isKnownWindow = function(window) window.document.documentElement.get Attribute("windowtype") == "navigator:browser";
96 78
97 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null; 79 exports.getURLBar = function(window) "gURLBar" in window ? window.gURLBar : null;
98 80
99 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null; 81 exports.getBrowser = function(window) "gBrowser" in window ? window.gBrowser : null;
100 82
101 exports.applyToWindow = function(window, corrector) 83 exports.applyToWindow = function(window, corrector)
102 { 84 {
103 let urlbar = exports.getURLBar(window); 85 let urlbar = exports.getURLBar(window);
104 if (urlbar && window.handleURLBarCommand && !("urlfixerOldHandler" in wind ow.handleURLBarCommand)) 86 let goButton = window.document.getElementById("go-button-container");
87
88 if (urlbar && urlbar._fireEvent && !functionHooks.has(window))
105 { 89 {
106 // Handle new URLs being entered 90 function correctURL()
107 let oldHandler = window.handleURLBarCommand;
108 window.handleURLBarCommand = function()
109 { 91 {
110 try 92 let correction = corrector(window, urlbar.value);
93 if (correction)
94 urlbar.value = correction;
95 }
96
97 let unhook = hook(urlbar, "_fireEvent", function(eventType) {
Wladimir Palant 2012/09/27 14:36:01 This bracket should be on next line.
98 if (eventType == "textentered")
111 { 99 {
112 let correction = corrector(window, urlbar.value); 100 correctURL();
113 if (correction)
114 urlbar.value = correction;
115 } 101 }
116 catch(e) 102 });
117 { 103 functionHooks.set(window, unhook);
118 if (e == Cr.NS_BINDING_ABORTED) 104
119 return; 105 if (goButton)
120 else 106 {
121 Cu.reportError(e); 107 goButton.addEventListener("command", correctURL, true);
122 } 108 eventListeners.set(window, {
123 oldHandler.apply(this, arguments); 109 "listener": correctURL,
110 "element": goButton
111 });
124 } 112 }
125 window.handleURLBarCommand.urlfixerOldHandler = oldHandler;
126 window.handleURLBarCommand.toString = doNotRecompile;
127 } 113 }
128 }; 114 };
129 115
130 exports.removeFromWindow = function(window) 116 exports.removeFromWindow = function(window)
131 { 117 {
132 if (window.handleURLBarCommand && "urlfixerOldHandler" in window.handleURL BarCommand) 118 if (functionHooks.has(window))
133 window.handleURLBarCommand = window.handleURLBarCommand.urlfixerOldHandl er; 119 {
120 let unhook = functionHooks.get(window);
121 unhook();
122 functionHooks.delete(window);
123 }
124
125 if (eventListeners.has(window))
126 {
127 let eventListener = eventListeners.get(window);
128 eventListener.element.removeEventListener("command", eventListener.liste ner, true);
Wladimir Palant 2012/09/27 14:36:01 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(uri)
158 { 154 {
159 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(uri); 155 exports.getBrowser(Services.wm.getMostRecentWindow("navigator:browser")).l oadURI(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
(...skipping 13 matching lines...) Expand all
180 { 176 {
181 url = url || this._edit.value; 177 url = url || this._edit.value;
182 try 178 try
183 { 179 {
184 let correction = corrector(window, url); 180 let correction = corrector(window, url);
185 if (correction) 181 if (correction)
186 url = correction; 182 url = correction;
187 } 183 }
188 catch(e) 184 catch(e)
189 { 185 {
190 if (e == Cr.NS_BINDING_ABORTED) 186 Cu.reportError(e);
191 return;
192 else
193 Cu.reportError(e);
194 } 187 }
195 oldHandler.call(this, url); 188 oldHandler.call(this, url);
196 } 189 }
197 window.BrowserUI.goToURI.urlfixerOldHandler = oldHandler; 190 window.BrowserUI.goToURI.urlfixerOldHandler = oldHandler;
198 window.BrowserUI.goToURI.toString = doNotRecompile; 191 window.BrowserUI.goToURI.toString = doNotRecompile;
199 } 192 }
200 }; 193 };
201 194
202 exports.removeFromWindow = function(window) 195 exports.removeFromWindow = function(window)
203 { 196 {
204 if ("BrowserUI" in window && window.BrowserUI.goToURI && "urlfixerOldHandl er" in window.BrowserUI.goToURI) 197 if ("BrowserUI" in window && window.BrowserUI.goToURI && "urlfixerOldHandl er" in window.BrowserUI.goToURI)
205 window.BrowserUI.goToURI = window.BrowserUI.goToURI.urlfixerOldHandler; 198 window.BrowserUI.goToURI = window.BrowserUI.goToURI.urlfixerOldHandler;
206 }; 199 };
207 200
208 exports.openInfobar = function() 201 exports.openInfobar = function()
209 { 202 {
210 // TODO: Implement infobar 203 // TODO: Implement infobar
211 }; 204 };
212 205
213 exports.loadURI = function(window, uri) 206 exports.loadURI = function(window, uri)
214 { 207 {
215 // TODO: Implement infobar 208 // TODO: Implement infobar
216 }; 209 };
217 210
218 break; 211 break;
219 } 212 }
220 case "fennec2": 213 case "fennec2":
221 { 214 {
222 // Native Fennec 215 // Native Fennec
(...skipping 13 matching lines...) Expand all
236 { 229 {
237 let params = Array.prototype.slice.apply(arguments); 230 let params = Array.prototype.slice.apply(arguments);
238 try 231 try
239 { 232 {
240 let correction = corrector(window, params[0]); 233 let correction = corrector(window, params[0]);
241 if (correction) 234 if (correction)
242 params[0] = correction; 235 params[0] = correction;
243 } 236 }
244 catch(e) 237 catch(e)
245 { 238 {
246 if (e == Cr.NS_BINDING_ABORTED) 239 Cu.reportError(e);
247 return null;
248 else
249 Cu.reportError(e);
250 } 240 }
251 return oldFunc.apply(this, params); 241 return oldFunc.apply(this, params);
252 }; 242 };
253 243
254 window.BrowserApp.observe = function(subject, topic, data) 244 window.BrowserApp.observe = function(subject, topic, data)
255 { 245 {
256 // Huge hack: we replace addTab/loadURI when the observer is 246 // Huge hack: we replace addTab/loadURI when the observer is
257 // triggered. This seems to be the only way to know that the calls 247 // triggered. This seems to be the only way to know that the calls
258 // originate from user input. 248 // originate from user input.
259 let method = null; 249 let method = null;
(...skipping 21 matching lines...) Expand all
281 window.BrowserApp.observe.urlfixerOldHandler = oldHandler; 271 window.BrowserApp.observe.urlfixerOldHandler = oldHandler;
282 window.BrowserApp.observe.toString = doNotRecompile; 272 window.BrowserApp.observe.toString = doNotRecompile;
283 } 273 }
284 }; 274 };
285 275
286 exports.removeFromWindow = function(window) 276 exports.removeFromWindow = function(window)
287 { 277 {
288 if ("BrowserApp" in window && window.BrowserApp.observe && "urlfixerOldHan dler" in window.BrowserApp.observe) 278 if ("BrowserApp" in window && window.BrowserApp.observe && "urlfixerOldHan dler" in window.BrowserApp.observe)
289 window.BrowserApp.observe = window.BrowserApp.observe.urlfixerOldHandler ; 279 window.BrowserApp.observe = window.BrowserApp.observe.urlfixerOldHandler ;
290 }; 280 };
291 281
292 exports.openInfobar = function(window, id, message, buttons, persistence) 282 exports.openInfobar = function(window, id, message, buttons, persistence)
293 { 283 {
294 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp) 284 if ("BrowserApp" in window && "selectedTab" in window.BrowserApp)
295 { 285 {
296 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser App.selectedTab.id, 286 window.NativeWindow.doorhanger.show(message, id, buttons, window.Browser App.selectedTab.id,
297 { 287 {
298 persistence: persistence 288 persistence: persistence
299 } 289 }
300 ); 290 );
301 } 291 }
302 }; 292 };
303 293
304 exports.loadURI = function(uri) 294 exports.loadURI = function(uri)
305 { 295 {
306 let window = Services.wm.getMostRecentWindow("navigator:browser"); 296 let window = Services.wm.getMostRecentWindow("navigator:browser");
307 if ("BrowserApp" in window && "loadURI" in window.BrowserApp) 297 if ("BrowserApp" in window && "loadURI" in window.BrowserApp)
308 window.BrowserApp.loadURI(uri); 298 window.BrowserApp.loadURI(uri);
309 }; 299 };
310 300
311 break; 301 break;
312 } 302 }
313 default: 303 default:
314 { 304 {
315 exports.isKnownWindow = function(window) false; 305 exports.isKnownWindow = function(window) false;
316 break; 306 break;
317 } 307 }
318 } 308 }
OLDNEW
« no previous file with comments | « chrome/content/tests/tests/suffixTreeManipulation.js ('k') | lib/hooks.js » ('j') | lib/hooks.js » ('J')

Powered by Google App Engine
This is Rietveld