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

Delta Between Two Patch Sets: safari/background.js

Issue 16067002: Added Safari Support (Closed)
Left Patch Set: Made description for Safari long again. The 100 char limit given by the extension builder doesn't a… Created Oct. 31, 2013, 2:12 p.m.
Right Patch Set: Bugfixes Created Nov. 15, 2013, 8:58 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 | « popupBlocker.js ('k') | safari/common.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 /* 1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2013 Eyeo GmbH 3 * Copyright (C) 2006-2013 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
(...skipping 11 matching lines...) Expand all
22 var TabEventTarget = function() 22 var TabEventTarget = function()
23 { 23 {
24 WrappedEventTarget.apply(this, arguments); 24 WrappedEventTarget.apply(this, arguments);
25 }; 25 };
26 TabEventTarget.prototype = { 26 TabEventTarget.prototype = {
27 __proto__: WrappedEventTarget.prototype, 27 __proto__: WrappedEventTarget.prototype,
28 _wrapListener: function(listener) 28 _wrapListener: function(listener)
29 { 29 {
30 return function(event) 30 return function(event)
31 { 31 {
32 listener(new Tab(event.target)); 32 if (event.target instanceof SafariBrowserTab)
33 listener(new Tab(event.target));
33 }; 34 };
34 } 35 }
35 }; 36 };
36 37
37 Tab = function(tab) 38 Tab = function(tab)
38 { 39 {
39 this._tab = tab; 40 this._tab = tab;
40 41
41 this._eventTarget = tab; 42 this._eventTarget = tab;
42 this._messageDispatcher = tab.page; 43 this._messageDispatcher = tab.page;
43 44
44 this.url = tab.url;
45
46 this.onBeforeNavigate = new TabEventTarget(tab, "beforeNavigate", false); 45 this.onBeforeNavigate = new TabEventTarget(tab, "beforeNavigate", false);
47 this.onCompleted = new TabEventTarget(tab, "navigate", false); 46 this.onCompleted = new TabEventTarget(tab, "navigate", false);
48 this.onActivated = new TabEventTarget(tab, "activate", false); 47 this.onActivated = new TabEventTarget(tab, "activate", false);
49 this.onRemoved = new TabEventTarget(tab, "close", false); 48 this.onRemoved = new TabEventTarget(tab, "close", false);
50 }; 49 };
51 Tab.prototype = { 50 Tab.prototype = {
51 get url()
52 {
53 return this._tab.url;
54 },
52 close: function() 55 close: function()
53 { 56 {
54 this._tab.close(); 57 this._tab.close();
55 }, 58 },
56 activate: function() 59 activate: function()
57 { 60 {
58 this._tab.activate(); 61 this._tab.activate();
59 }, 62 },
60 sendMessage: sendMessage, 63 sendMessage: sendMessage,
61 pageAction: { 64 pageAction: {
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 this._values.splice(idx, 1); 128 this._values.splice(idx, 1);
126 129
127 tab.removeEventListener("close", this._onClosed, false); 130 tab.removeEventListener("close", this._onClosed, false);
128 } 131 }
129 }, 132 },
130 _onClosed: function(event) 133 _onClosed: function(event)
131 { 134 {
132 this._delete(event.target); 135 this._delete(event.target);
133 } 136 }
134 }; 137 };
135 TabMap.prototype["delete"] = function(tab) { 138 TabMap.prototype["delete"] = function(tab)
139 {
136 this._delete(tab._tab); 140 this._delete(tab._tab);
137 }; 141 };
138 142
139 143
140 /* Windows */ 144 /* Windows */
141 145
142 Window = function(win) 146 Window = function(win)
143 { 147 {
144 this._win = win; 148 this._win = win;
145 this.visible = win.visible;
146 } 149 }
147 Window.prototype = { 150 Window.prototype = {
151 get visible()
152 {
153 return this._win.visible;
154 },
148 getAllTabs: function(callback) 155 getAllTabs: function(callback)
149 { 156 {
150 callback(this._win.tabs.map(function(tab) { 157 callback(this._win.tabs.map(function(tab) { return new Tab(tab); }));
151 return new Tab(tab);
152 }));
153 }, 158 },
154 getActiveTab: function(callback) 159 getActiveTab: function(callback)
155 { 160 {
156 callback(new Tab(this._win.activeTab)); 161 callback(new Tab(this._win.activeTab));
157 }, 162 },
158 openTab: function(url, callback) 163 openTab: function(url, callback)
159 { 164 {
160 var tab = this._win.openTab(); 165 var tab = this._win.openTab();
161 tab.url = url; 166 tab.url = url;
162 167
(...skipping 16 matching lines...) Expand all
179 184
180 if (objectId == -1) 185 if (objectId == -1)
181 objectId = objects.push(obj) - 1; 186 objectId = objects.push(obj) - 1;
182 187
183 return objectId; 188 return objectId;
184 }, 189 },
185 serializeSequence: function(sequence, objects, memo) 190 serializeSequence: function(sequence, objects, memo)
186 { 191 {
187 if (!memo) 192 if (!memo)
188 memo = {specs: [], arrays: []}; 193 memo = {specs: [], arrays: []};
189 194
190 var items = []; 195 var items = [];
191 for (var i = 0; i < sequence.length; i++) 196 for (var i = 0; i < sequence.length; i++)
192 items.push(this.serialize(sequence[i], objects, memo)); 197 items.push(this.serialize(sequence[i], objects, memo));
193 198
194 return items; 199 return items;
195 }, 200 },
196 serialize: function(obj, objects, memo) 201 serialize: function(obj, objects, memo)
197 { 202 {
198 if (typeof obj == "function") 203 if (typeof obj == "object" && obj != null || typeof obj == "function")
199 return {type: "function", objectId: this.registerObject(obj, objects)} ;
200
201 if (typeof obj == "object" && obj != null)
202 { 204 {
203 if (obj.constructor == Array) 205 if (obj.constructor == Array)
204 { 206 {
205 if (!memo) 207 if (!memo)
206 memo = {specs: [], arrays: []}; 208 memo = {specs: [], arrays: []};
207 209
208 var idx = memo.arrays.indexOf(obj); 210 var idx = memo.arrays.indexOf(obj);
209 if (idx != -1) 211 if (idx != -1)
210 return memo.specs[idx]; 212 return memo.specs[idx];
211 213
212 var spec = {type: "array"}; 214 var spec = {type: "array"};
213 memo.specs.push(spec); 215 memo.specs.push(spec);
214 memo.arrays.push(obj); 216 memo.arrays.push(obj);
215 217
216 spec.items = this.serializeSequence(obj, objects, memo); 218 spec.items = this.serializeSequence(obj, objects, memo);
217 return spec; 219 return spec;
218 } 220 }
219 221
220 if (obj.constructor != Date) 222 if (obj.constructor != Date && obj.constructor != RegExp)
221 if (obj.constructor != RegExp)
222 return {type: "object", objectId: this.registerObject(obj, objects)} ; 223 return {type: "object", objectId: this.registerObject(obj, objects)} ;
223 } 224 }
224 225
225 return {type: "value", value: obj}; 226 return {type: "value", value: obj};
226 }, 227 },
227 createCallback: function(callbackId, tab) 228 createCallback: function(callbackId, tab)
228 { 229 {
229 var proxy = this; 230 var proxy = this;
230 231
231 return function() 232 return function()
232 { 233 {
233 var idx = proxy.tabs.indexOf(tab); 234 var idx = proxy.tabs.indexOf(tab);
234 235
235 if (idx != -1) { 236 if (idx != -1) {
236 var objects = proxy.objects[idx]; 237 var objects = proxy.objects[idx];
237 238
238 tab.page.dispatchMessage("proxyCallback", { 239 tab.page.dispatchMessage("proxyCallback",
240 {
239 callbackId: callbackId, 241 callbackId: callbackId,
240 contextId: proxy.registerObject(this, objects), 242 contextId: proxy.registerObject(this, objects),
241 args: proxy.serializeSequence(arguments, objects) 243 args: proxy.serializeSequence(arguments, objects)
242 }); 244 });
243 } 245 }
244 }; 246 };
245 }, 247 },
246 deserialize: function(spec, objects, tab, memo) 248 deserialize: function(spec, objects, tab, memo)
247 { 249 {
248 switch (spec.type) 250 switch (spec.type)
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
321 }, 323 },
322 _handleMessage: function(message, tab) 324 _handleMessage: function(message, tab)
323 { 325 {
324 var objects = this.getObjectCache(tab); 326 var objects = this.getObjectCache(tab);
325 327
326 switch (message.type) 328 switch (message.type)
327 { 329 {
328 case "getProperty": 330 case "getProperty":
329 var obj = objects[message.objectId]; 331 var obj = objects[message.objectId];
330 332
331 try { 333 try
334 {
332 var value = obj[message.property]; 335 var value = obj[message.property];
333 } 336 }
334 catch (e) 337 catch (e)
335 { 338 {
336 return this.fail(e); 339 return this.fail(e);
337 } 340 }
338 341
339 return {succeed: true, result: this.serialize(value, objects)}; 342 return {succeed: true, result: this.serialize(value, objects)};
340 case "setProperty": 343 case "setProperty":
341 var obj = objects[message.objectId]; 344 var obj = objects[message.objectId];
342 var value = this.deserialize(message.value, objects, tab); 345 var value = this.deserialize(message.value, objects, tab);
343 346
344 try { 347 try
348 {
345 obj[message.property] = value; 349 obj[message.property] = value;
346 } 350 }
347 catch (e) 351 catch (e)
348 { 352 {
349 return this.fail(e); 353 return this.fail(e);
350 } 354 }
351 355
352 return {succeed: true}; 356 return {succeed: true};
353 case "callFunction": 357 case "callFunction":
354 var func = objects[message.functionId]; 358 var func = objects[message.functionId];
355 var context = objects[message.contextId]; 359 var context = objects[message.contextId];
356 360
357 var args = []; 361 var args = [];
358 for (var i = 0; i < message.args.length; i++) 362 for (var i = 0; i < message.args.length; i++)
359 args.push(this.deserialize(message.args[i], objects, tab)); 363 args.push(this.deserialize(message.args[i], objects, tab));
360 364
361 try { 365 try
366 {
362 var result = func.apply(context, args); 367 var result = func.apply(context, args);
363 } 368 }
364 catch (e) 369 catch (e)
365 { 370 {
366 return this.fail(e); 371 return this.fail(e);
367 } 372 }
368 373
369 return {succeed: true, result: this.serialize(result, objects)}; 374 return {succeed: true, result: this.serialize(result, objects)};
370 case "inspectObject": 375 case "inspectObject":
371 var obj = objects[message.objectId]; 376 var obj = objects[message.objectId];
372 377 var objectInfo = {properties: {}, isFunction: typeof obj == "functio n"};
373 var prototype = Object.getPrototypeOf(obj); 378
374 var prototypeId;
375 if (prototype != null)
376 prototypeId = this.registerObject(prototype, objects);
377 else
378 prototypeId = null;
379
380 var properties = {};
381 Object.getOwnPropertyNames(obj).forEach(function(prop) 379 Object.getOwnPropertyNames(obj).forEach(function(prop)
382 { 380 {
383 if (obj != Object.prototype || prop == "constructor") 381 objectInfo.properties[prop] = {
384 properties[prop] = { 382 enumerable: Object.prototype.propertyIsEnumerable.call(obj, prop )
385 enumerable: Object.getOwnPropertyDescriptor(obj, prop).enumera ble 383 };
386 };
387 }); 384 });
388 385
389 return {prototypeId: prototypeId, properties: properties}; 386 if (obj.__proto__)
387 objectInfo.prototypeId = this.registerObject(obj.__proto__, object s);
388
389 if (obj == Object.prototype)
390 objectInfo.prototypeOf = "Object";
391 if (obj == Function.prototype)
392 objectInfo.prototypeOf = "Function";
393
394 return objectInfo;
390 } 395 }
391 } 396 }
392 }; 397 };
393 398
394 399
395 /* Web request blocking */ 400 /* Web request blocking */
396 401
397 ext.webRequest = { 402 ext.webRequest = {
398 onBeforeRequest: { 403 onBeforeRequest: {
399 _listeners: [], 404 _listeners: [],
400 _urlPatterns: [], 405 _urlPatterns: [],
401 406
402 _handleMessage: function(message, tab) 407 _handleMessage: function(message, tab)
403 { 408 {
404 tab = new Tab(tab); 409 tab = new Tab(tab);
405 410
406 for (var i = 0; i < this._listeners.length; i++) 411 for (var i = 0; i < this._listeners.length; i++)
407 { 412 {
408 var regex = this._urlPatterns[i]; 413 var regex = this._urlPatterns[i];
409 414
410 if (!regex || regex.test(message)) 415 if ((!regex || regex.test(message.url)) && this._listeners[i](messag e.url, message.type, tab, 0, -1) === false)
411 if (this._listeners[i](message.url, message.type, tab, 0, -1) === fa lse)
412 return false; 416 return false;
413 } 417 }
414 418
415 return true; 419 return true;
416 }, 420 },
417 addListener: function(listener, urls) 421 addListener: function(listener, urls)
418 { 422 {
419 var regex; 423 var regex;
420 424
421 if (urls) 425 if (urls)
422 regex = new RegExp("^(?:" + urls.map(function(url) 426 regex = new RegExp("^(?:" + urls.map(function(url)
423 { 427 {
424 return url.split("*").map(function(s) 428 return url.split("*").map(function(s)
425 { 429 {
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 })); 484 }));
481 }, 485 },
482 getLastFocused: function(callback) 486 getLastFocused: function(callback)
483 { 487 {
484 callback(new Window(safari.application.activeBrowserWindow)); 488 callback(new Window(safari.application.activeBrowserWindow));
485 } 489 }
486 }; 490 };
487 491
488 ext.tabs = { 492 ext.tabs = {
489 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t rue), 493 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t rue),
490 onCompleted: new TabEventTarget(safari.application, "navigate", t rue), 494 onCompleted: new TabEventTarget(safari.application, "navigate", true),
491 onActivated: new TabEventTarget(safari.application, "activate", t rue), 495 onActivated: new TabEventTarget(safari.application, "activate", true),
492 onRemoved: new TabEventTarget(safari.application, "close", t rue) 496 onRemoved: new TabEventTarget(safari.application, "close", true)
493 }; 497 };
494 498
495 ext.backgroundPage = { 499 ext.backgroundPage = {
496 getWindow: function() 500 getWindow: function()
497 { 501 {
498 return safari.extension.globalPage.contentWindow; 502 return safari.extension.globalPage.contentWindow;
499 } 503 }
500 }; 504 };
501 505
502 ext.onMessage = new MessageEventTarget(safari.application); 506 ext.onMessage = new MessageEventTarget(safari.application);
507
508
509 // Safari will load the bubble once, and then show it everytime the icon is
510 // clicked. While Chrome loads it everytime you click the icon. So in order to
511 // force the same behavior in Safari, we are going to reload the page of the
512 // bubble everytime it is shown.
513 if (safari.extension.globalPage.contentWindow != window)
514 safari.application.addEventListener("popover", function()
515 {
516 document.documentElement.style.display = "none";
517 document.location.reload();
518 }, true);
503 })(); 519 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld