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: Converted new calls to chrome.* to ext.* and fixed issue with background page proxy about added/rem… Created Nov. 9, 2013, 6:38 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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 return memo.specs[idx]; 212 return memo.specs[idx];
208 213
209 var spec = {type: "array"}; 214 var spec = {type: "array"};
210 memo.specs.push(spec); 215 memo.specs.push(spec);
211 memo.arrays.push(obj); 216 memo.arrays.push(obj);
212 217
213 spec.items = this.serializeSequence(obj, objects, memo); 218 spec.items = this.serializeSequence(obj, objects, memo);
214 return spec; 219 return spec;
215 } 220 }
216 221
217 if (obj.constructor != Date) 222 if (obj.constructor != Date && obj.constructor != RegExp)
218 if (obj.constructor != RegExp)
219 return {type: "object", objectId: this.registerObject(obj, objects)} ; 223 return {type: "object", objectId: this.registerObject(obj, objects)} ;
220 } 224 }
221 225
222 return {type: "value", value: obj}; 226 return {type: "value", value: obj};
223 }, 227 },
224 createCallback: function(callbackId, tab) 228 createCallback: function(callbackId, tab)
225 { 229 {
226 var proxy = this; 230 var proxy = this;
227 231
228 return function() 232 return function()
229 { 233 {
230 var idx = proxy.tabs.indexOf(tab); 234 var idx = proxy.tabs.indexOf(tab);
231 235
232 if (idx != -1) { 236 if (idx != -1) {
233 var objects = proxy.objects[idx]; 237 var objects = proxy.objects[idx];
234 238
235 tab.page.dispatchMessage("proxyCallback", { 239 tab.page.dispatchMessage("proxyCallback",
240 {
236 callbackId: callbackId, 241 callbackId: callbackId,
237 contextId: proxy.registerObject(this, objects), 242 contextId: proxy.registerObject(this, objects),
238 args: proxy.serializeSequence(arguments, objects) 243 args: proxy.serializeSequence(arguments, objects)
239 }); 244 });
240 } 245 }
241 }; 246 };
242 }, 247 },
243 deserialize: function(spec, objects, tab, memo) 248 deserialize: function(spec, objects, tab, memo)
244 { 249 {
245 switch (spec.type) 250 switch (spec.type)
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
318 }, 323 },
319 _handleMessage: function(message, tab) 324 _handleMessage: function(message, tab)
320 { 325 {
321 var objects = this.getObjectCache(tab); 326 var objects = this.getObjectCache(tab);
322 327
323 switch (message.type) 328 switch (message.type)
324 { 329 {
325 case "getProperty": 330 case "getProperty":
326 var obj = objects[message.objectId]; 331 var obj = objects[message.objectId];
327 332
328 try { 333 try
334 {
329 var value = obj[message.property]; 335 var value = obj[message.property];
330 } 336 }
331 catch (e) 337 catch (e)
332 { 338 {
333 return this.fail(e); 339 return this.fail(e);
334 } 340 }
335 341
336 return {succeed: true, result: this.serialize(value, objects)}; 342 return {succeed: true, result: this.serialize(value, objects)};
337 case "setProperty": 343 case "setProperty":
338 var obj = objects[message.objectId]; 344 var obj = objects[message.objectId];
339 var value = this.deserialize(message.value, objects, tab); 345 var value = this.deserialize(message.value, objects, tab);
340 346
341 try { 347 try
348 {
342 obj[message.property] = value; 349 obj[message.property] = value;
343 } 350 }
344 catch (e) 351 catch (e)
345 { 352 {
346 return this.fail(e); 353 return this.fail(e);
347 } 354 }
348 355
349 return {succeed: true}; 356 return {succeed: true};
350 case "callFunction": 357 case "callFunction":
351 var func = objects[message.functionId]; 358 var func = objects[message.functionId];
352 var context = objects[message.contextId]; 359 var context = objects[message.contextId];
353 360
354 var args = []; 361 var args = [];
355 for (var i = 0; i < message.args.length; i++) 362 for (var i = 0; i < message.args.length; i++)
356 args.push(this.deserialize(message.args[i], objects, tab)); 363 args.push(this.deserialize(message.args[i], objects, tab));
357 364
358 try { 365 try
366 {
359 var result = func.apply(context, args); 367 var result = func.apply(context, args);
360 } 368 }
361 catch (e) 369 catch (e)
362 { 370 {
363 return this.fail(e); 371 return this.fail(e);
364 } 372 }
365 373
366 return {succeed: true, result: this.serialize(result, objects)}; 374 return {succeed: true, result: this.serialize(result, objects)};
367 case "inspectObject": 375 case "inspectObject":
368 var obj = objects[message.objectId]; 376 var obj = objects[message.objectId];
(...skipping 28 matching lines...) Expand all
397 _urlPatterns: [], 405 _urlPatterns: [],
398 406
399 _handleMessage: function(message, tab) 407 _handleMessage: function(message, tab)
400 { 408 {
401 tab = new Tab(tab); 409 tab = new Tab(tab);
402 410
403 for (var i = 0; i < this._listeners.length; i++) 411 for (var i = 0; i < this._listeners.length; i++)
404 { 412 {
405 var regex = this._urlPatterns[i]; 413 var regex = this._urlPatterns[i];
406 414
407 if (!regex || regex.test(message)) 415 if ((!regex || regex.test(message.url)) && this._listeners[i](messag e.url, message.type, tab, 0, -1) === false)
408 if (this._listeners[i](message.url, message.type, tab, 0, -1) === fa lse)
409 return false; 416 return false;
410 } 417 }
411 418
412 return true; 419 return true;
413 }, 420 },
414 addListener: function(listener, urls) 421 addListener: function(listener, urls)
415 { 422 {
416 var regex; 423 var regex;
417 424
418 if (urls) 425 if (urls)
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
451 var handler; 458 var handler;
452 459
453 switch (event.message.type) 460 switch (event.message.type)
454 { 461 {
455 case "proxy": 462 case "proxy":
456 handler = proxy; 463 handler = proxy;
457 break; 464 break;
458 case "webRequest": 465 case "webRequest":
459 handler = ext.webRequest.onBeforeRequest; 466 handler = ext.webRequest.onBeforeRequest;
460 break; 467 break;
461 default:
462 console.log(event.message);
463 } 468 }
464 469
465 event.message = handler._handleMessage(event.message.payload, event.targ et); 470 event.message = handler._handleMessage(event.message.payload, event.targ et);
466 } 471 }
467 }, true); 472 }, true);
468 } 473 }
469 474
470 475
471 /* API */ 476 /* API */
472 477
473 ext.windows = { 478 ext.windows = {
474 getAll: function(callback) 479 getAll: function(callback)
475 { 480 {
476 callback(safari.application.browserWindows.map(function(win) 481 callback(safari.application.browserWindows.map(function(win)
477 { 482 {
478 return new Window(win); 483 return new Window(win);
479 })); 484 }));
480 }, 485 },
481 getLastFocused: function(callback) 486 getLastFocused: function(callback)
482 { 487 {
483 callback(new Window(safari.application.activeBrowserWindow)); 488 callback(new Window(safari.application.activeBrowserWindow));
484 } 489 }
485 }; 490 };
486 491
487 ext.tabs = { 492 ext.tabs = {
488 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t rue), 493 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t rue),
489 onCompleted: new TabEventTarget(safari.application, "navigate", t rue), 494 onCompleted: new TabEventTarget(safari.application, "navigate", true),
490 onActivated: new TabEventTarget(safari.application, "activate", t rue), 495 onActivated: new TabEventTarget(safari.application, "activate", true),
491 onRemoved: new TabEventTarget(safari.application, "close", t rue) 496 onRemoved: new TabEventTarget(safari.application, "close", true)
492 }; 497 };
493 498
494 ext.backgroundPage = { 499 ext.backgroundPage = {
495 getWindow: function() 500 getWindow: function()
496 { 501 {
497 return safari.extension.globalPage.contentWindow; 502 return safari.extension.globalPage.contentWindow;
498 } 503 }
499 }; 504 };
500 505
501 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);
502 })(); 519 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld