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

Delta Between Two Patch Sets: safari/ext/background.js

Issue 6346177440120832: Added abstraction for frames, to fix domain-based rules, whitelisting and ad counter on Safari (Closed)
Left Patch Set: Created Dec. 21, 2013, 7:48 p.m.
Right Patch Set: Addressed another comment Created Jan. 20, 2014, 8:50 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/ext/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 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
74 } 74 }
75 }; 75 };
76 76
77 77
78 /* Tabs */ 78 /* Tabs */
79 79
80 Tab = function(tab) 80 Tab = function(tab)
81 { 81 {
82 this._tab = tab; 82 this._tab = tab;
83 83
84 this.browserAction = new BrowserAction(this);
85
84 this.onLoading = new LoadingTabEventTarget(tab); 86 this.onLoading = new LoadingTabEventTarget(tab);
85 this.onBeforeNavigate = new TabEventTarget(tab, "beforeNavigate", false); 87 this.onBeforeNavigate = new TabEventTarget(tab, "beforeNavigate", false);
86 this.onCompleted = new TabEventTarget(tab, "navigate", false); 88 this.onCompleted = new TabEventTarget(tab, "navigate", false);
87 this.onActivated = new TabEventTarget(tab, "activate", false); 89 this.onActivated = new TabEventTarget(tab, "activate", false);
88 this.onRemoved = new TabEventTarget(tab, "close", false); 90 this.onRemoved = new TabEventTarget(tab, "close", false);
89 }; 91 };
90 Tab.prototype = { 92 Tab.prototype = {
91 get url() 93 get url()
92 { 94 {
93 return this._tab.url; 95 return this._tab.url;
94 }, 96 },
95 close: function() 97 close: function()
96 { 98 {
97 this._tab.close(); 99 this._tab.close();
98 }, 100 },
99 activate: function() 101 activate: function()
100 { 102 {
101 this._tab.activate(); 103 this._tab.activate();
102 }, 104 },
103 sendMessage: function(message, responseCallback) 105 sendMessage: function(message, responseCallback)
104 { 106 {
105 _sendMessage( 107 _sendMessage(
106 message, responseCallback, 108 message, responseCallback,
107 this._tab.page, this._tab 109 this._tab.page, this._tab
108 ); 110 );
109 },
110 browserAction: {
111 setIcon: function(path)
112 {
113 safari.extension.toolbarItems[0].image = safari.extension.baseURI + path ;
114 },
115 setTitle: function(title)
116 {
117 safari.extension.toolbarItems[0].toolTip = title;
118 },
119 setBadge: function(badge)
120 {
121 if (!badge)
122 safari.extension.toolbarItems[0].badge = 0;
123 else if ("number" in badge)
124 safari.extension.toolbarItems[0].badge = badge.number;
125 }
126 } 111 }
127 }; 112 };
128 113
129 TabMap = function(deleteTabOnBeforeNavigate) 114 TabMap = function(deleteTabOnBeforeNavigate)
130 { 115 {
131 this._tabs = []; 116 this._tabs = [];
132 this._values = []; 117 this._values = [];
133 118
134 this._deleteOnEvent = this._deleteOnEvent.bind(this); 119 this._deleteOnEvent = this._deleteOnEvent.bind(this);
135 this._deleteTabOnBeforeNavigate = deleteTabOnBeforeNavigate; 120 this._deleteTabOnBeforeNavigate = deleteTabOnBeforeNavigate;
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 { 162 {
178 this._tabs.splice(idx, 1); 163 this._tabs.splice(idx, 1);
179 this._values.splice(idx, 1); 164 this._values.splice(idx, 1);
180 165
181 tab.removeEventListener("close", this._deleteOnEvent, false); 166 tab.removeEventListener("close", this._deleteOnEvent, false);
182 tab.removeEventListener("beforeNavigate", this._deleteOnEvent, false); 167 tab.removeEventListener("beforeNavigate", this._deleteOnEvent, false);
183 } 168 }
184 }, 169 },
185 _deleteOnEvent: function(event) 170 _deleteOnEvent: function(event)
186 { 171 {
187 // delay so that other event handlers can still lookup this tab 172 // delay so that other event handlers can still look this tab up
188 setTimeout(this._delete.bind(this, event.target), 0); 173 setTimeout(this._delete.bind(this, event.target), 0);
Felix Dahlke 2014/01/18 13:39:19 s/lookup this tab/look this tab up/
Sebastian Noack 2014/01/19 10:19:40 Done.
189 } 174 }
190 }; 175 };
191 TabMap.prototype["delete"] = function(tab) 176 TabMap.prototype["delete"] = function(tab)
192 { 177 {
193 this._delete(tab._tab); 178 this._delete(tab._tab);
194 }; 179 };
180
181 ext.tabs = {
182 onLoading: new LoadingTabEventTarget(safari.application),
183 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t rue),
184 onCompleted: new TabEventTarget(safari.application, "navigate", true),
185 onActivated: new TabEventTarget(safari.application, "activate", true),
186 onRemoved: new TabEventTarget(safari.application, "close", true)
187 };
188
189
190 /* Browser actions */
191
192 var toolbarItemProperties = {};
193
194 var getToolbarItemProperty = function(name)
195 {
196 var property = toolbarItemProperties[name];
197 if (!property)
198 {
199 property = {tabs: new TabMap()};
200 toolbarItemProperties[name] = property;
201 }
202 return property;
203 };
204
205 var getToolbarItemForWindow = function(win)
206 {
207 for (var i = 0; i < safari.extension.toolbarItems.length; i++)
208 {
209 var toolbarItem = safari.extension.toolbarItems[i];
210
211 if (toolbarItem.browserWindow == win)
212 return toolbarItem;
213 }
214
215 return null;
216 };
217
218 var BrowserAction = function(tab)
219 {
220 this._tab = tab;
221 };
222 BrowserAction.prototype = {
223 _set: function(name, value)
224 {
225 var currentWindow = this._tab._tab.browserWindow;
226 var toolbarItem = getToolbarItemForWindow(currentWindow);
227
228 if (toolbarItem)
229 {
230 var property = getToolbarItemProperty(name);
231 property.tabs.set(this._tab, value);
232
233 if (!("global" in property))
234 property.global = toolbarItem[name];
235
236 if (this._tab._tab == currentWindow.activeTab)
237 toolbarItem[name] = value;
238 }
239 },
240 setIcon: function(path)
241 {
242 this._set("image", safari.extension.baseURI + path);
243 },
244 setTitle: function(title)
245 {
246 this._set("label", title);
247 this._set("toolTip", title);
248 },
249 setBadge: function(badge)
250 {
251 if (!badge)
252 this._set("badge", 0);
253 else if ("number" in badge)
254 this._set("badge", badge.number);
255 }
256 };
257
258 ext.tabs.onActivated.addListener(function(tab)
259 {
260 var toolbarItem = getToolbarItemForWindow(tab._tab.browserWindow);
261
262 if (!toolbarItem)
263 return;
264
265 for (var name in toolbarItemProperties)
266 {
267 var property = toolbarItemProperties[name];
268
269 if (property.tabs.has(tab))
270 toolbarItem[name] = property.tabs.get(tab);
271 else
272 toolbarItem[name] = property.global;
273 }
274 });
195 275
196 276
197 /* Windows */ 277 /* Windows */
198 278
199 Window = function(win) 279 Window = function(win)
200 { 280 {
201 this._win = win; 281 this._win = win;
202 } 282 }
203 Window.prototype = { 283 Window.prototype = {
204 get visible() 284 get visible()
(...skipping 20 matching lines...) Expand all
225 305
226 306
227 /* Frames */ 307 /* Frames */
228 308
229 Frame = function(url, isTopLevel, tab) 309 Frame = function(url, isTopLevel, tab)
230 { 310 {
231 this.url = url; 311 this.url = url;
232 312
233 // there is no way to discover frames with Safari's API. 313 // there is no way to discover frames with Safari's API.
234 // so if this isn't the top level frame, assume that the parent is. 314 // so if this isn't the top level frame, assume that the parent is.
235 // this is the best we can do for Safari. :( 315 // this is the best we can do for Safari. :(
Felix Dahlke 2014/01/18 13:39:19 We could cache a chain of frames like we do in And
236 if (!isTopLevel) 316 if (!isTopLevel)
237 this.parent = new Frame(tab.url, true); 317 this.parent = new Frame(tab.url, true);
238 else 318 else
239 this.parent = null; 319 this.parent = null;
240 }; 320 };
241 321
242 322
243 /* Background page proxy */ 323 /* Background page proxy */
244 324
245 var proxy = { 325 var proxy = {
(...skipping 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
531 { 611 {
532 return new Window(win); 612 return new Window(win);
533 })); 613 }));
534 }, 614 },
535 getLastFocused: function(callback) 615 getLastFocused: function(callback)
536 { 616 {
537 callback(new Window(safari.application.activeBrowserWindow)); 617 callback(new Window(safari.application.activeBrowserWindow));
538 } 618 }
539 }; 619 };
540 620
541 ext.tabs = {
542 onLoading: new LoadingTabEventTarget(safari.application),
543 onBeforeNavigate: new TabEventTarget(safari.application, "beforeNavigate", t rue),
544 onCompleted: new TabEventTarget(safari.application, "navigate", true),
545 onActivated: new TabEventTarget(safari.application, "activate", true),
546 onRemoved: new TabEventTarget(safari.application, "close", true)
547 };
548
549 ext.backgroundPage = { 621 ext.backgroundPage = {
550 getWindow: function() 622 getWindow: function()
551 { 623 {
552 return safari.extension.globalPage.contentWindow; 624 return safari.extension.globalPage.contentWindow;
553 } 625 }
554 }; 626 };
555 627
556 ext.onMessage = new BackgroundMessageEventTarget(); 628 ext.onMessage = new BackgroundMessageEventTarget();
557 629
558 // TODO: Implement context menu 630 var contextMenuItems = [];
631 var isContextMenuHidden = true;
559 ext.contextMenus = { 632 ext.contextMenus = {
560 create: function(title, contexts, onclick) {}, 633 addMenuItem: function(title, contexts, onclick)
561 removeAll: function(callback) {} 634 {
562 }; 635 contextMenuItems.push({
636 id: String(contextMenuItems.length),
637 title: title,
638 item: null,
639 contexts: contexts,
640 onclick: onclick
641 });
642 this.showMenuItems();
643 },
644 removeMenuItems: function()
645 {
646 contextMenuItems = [];
647 this.hideMenuItems();
648 },
649 showMenuItems: function()
650 {
651 isContextMenuHidden = false;
652 },
653 hideMenuItems: function()
654 {
655 isContextMenuHidden = true;
656 }
657 };
658
659 // Create context menu items
660 safari.application.addEventListener("contextmenu", function(event)
661 {
662 if (isContextMenuHidden)
663 return;
664
665 var context = event.userInfo.tagName;
666 if (context == "img")
667 context = "image";
668 if (!event.userInfo.srcUrl)
669 context = null;
670
671 for (var i = 0; i < contextMenuItems.length; i++)
672 {
673 // Supported contexts are: all, audio, image, video
674 var menuItem = contextMenuItems[i];
675 if (menuItem.contexts.indexOf("all") == -1 && menuItem.contexts.indexOf(co ntext) == -1)
676 continue;
677
678 event.contextMenu.appendContextMenuItem(menuItem.id, menuItem.title);
679 }
680 }, false);
681
682 // Handle context menu item clicks
683 safari.application.addEventListener("command", function(event)
684 {
685 for (var i = 0; i < contextMenuItems.length; i++)
686 {
687 if (contextMenuItems[i].id == event.command)
688 {
689 contextMenuItems[i].onclick(event.userInfo.srcUrl, new Tab(safari.applic ation.activeBrowserWindow.activeTab));
690 break;
691 }
692 }
693 }, false);
563 })(); 694 })();
LEFTRIGHT

Powered by Google App Engine
This is Rietveld