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

Side by Side Diff: chrome/background.js

Issue 16067002: Added Safari Support (Closed)
Patch Set: Created Oct. 21, 2013, 8:11 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
« block.js ('K') | « block.js ('k') | chrome/common.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 (function() {
Felix Dahlke 2013/10/24 16:30:34 Opening braces should go on their own line, likewi
2 /* Events */
3
4 var TabEventTarget = function() {
5 WrappedEventTarget.apply(this, arguments);
6
7 this._tabs = {};
8
9 this._sharedListener = this._sharedListener.bind(this);
10 this._removeTab = this._removeTab.bind(this);
11 };
12 TabEventTarget.prototype = {
13 __proto__: WrappedEventTarget.prototype,
14 _bindToTab: function(tab) {
15 return {
16 addListener: function(listener) {
17 var listeners = this._tabs[tab._id];
18
19 if (!listeners) {
20 this._tabs[tab._id] = listeners = [];
21
22 if (Object.keys(this._tabs).length == 1)
23 this.addListener(this._sharedListener);
24
25 tab.onRemoved.addListener(this._removeTab);
26 }
27
28 listeners.push(listener);
29 }.bind(this),
30 removeListener: function(listener) {
31 var listeners = this._tabs[tab._id];
32 if (!listeners)
33 return;
34
35 var idx = listeners.indexOf(listener);
36 if (idx == -1)
37 return;
38
39 listeners.splice(idx, 1);
40
41 if (listeners.length == 0)
42 tab.onRemoved.removeListener(this._removeTab);
43 else {
44 if (listeners.length > 1)
45 return;
46 if (listeners[0] != this._removeTab)
47 return;
48 }
49
50 this._removeTab(tab);
51 }.bind(this)
52 };
53 },
54 _sharedListener: function(tab) {
55 var listeners = this._tabs[tab._id];
56
57 if (!listeners)
58 return;
59
60 // copy listeners before calling them, because of they
Felix Dahlke 2013/10/24 16:30:34 s/because of/because/
61 // might add or remove other listeners, which must not be
62 // taken into account before the next occurrance of the event
Felix Dahlke 2013/10/24 16:30:34 s/occureace/occurence/
63 listeners = listeners.slice(0);
64
65 for (var i = 0; i < listeners.length; i++)
66 listeners[i](tab);
67 },
68 _removeTab: function(tab) {
69 delete this._tabs[tab._id];
70
71 if (Object.keys(this._tabs).length == 0)
72 this.removeListener(this._sharedListener);
73 }
74 };
75
76 var BeforeNavigateTabEventTarget = function() {
77 TabEventTarget.call(this, chrome.tabs.onUpdated);
78 };
79 BeforeNavigateTabEventTarget.prototype = {
80 __proto__: TabEventTarget.prototype,
81 _wrapListener: function(listener) {
82 return function(id, info, tab) {
83 if ("url" in info)
84 listener(new Tab(tab));
85 };
86 }
87 };
88
89 var CompletedTabEventTarget = function() {
90 TabEventTarget.call(this, chrome.tabs.onUpdated);
91 };
92 CompletedTabEventTarget.prototype = {
93 __proto__: TabEventTarget.prototype,
94 _wrapListener: function(listener) {
95 return function(id, info, tab) {
96 if (info.status == "complete")
97 listener(new Tab(tab));
98 };
99 }
100 };
101
102 var ActivatedTabEventTarget = function() {
103 TabEventTarget.call(this, chrome.tabs.onActivated);
104 };
105 ActivatedTabEventTarget.prototype = {
106 __proto__: TabEventTarget.prototype,
107 _wrapListener: function(listener) {
108 return function(info) {
109 chrome.tabs.get(info.tabId, function(tab) {
110 listener(new Tab(tab));
111 });
112 };
113 }
114 }
115
116 var RemovedTabEventTarget = function() {
117 TabEventTarget.call(this, chrome.tabs.onRemoved);
118 };
119 RemovedTabEventTarget.prototype = {
120 __proto__: TabEventTarget.prototype,
121 _wrapListener: function(listener) {
122 return function(id) {
123 listener(new Tab({id: id}));
124 };
125 }
126 };
127
128 var BeforeRequestEventTarget = function() {
129 WrappedEventTarget.call(this, chrome.webRequest.onBeforeRequest);
130 };
131 BeforeRequestEventTarget.prototype = {
132 __proto__: WrappedEventTarget.prototype,
133 _wrapListener: function(listener) {
134 return function(details) {
135 var tab = null;
136
137 if (details.tabId != -1)
138 tab = new Tab({id: details.tabId});
139
140 return {cancel: listener(
141 details.url,
142 details.type,
143 tab,
144 details.frameId,
145 details.parentFrameId
146 ) === false};
147 };
148 },
149 _prepareExtraArguments: function(urls) {
150 return [urls ? {urls: urls} : {}, ["blocking"]];
151 }
152 };
153
154
155 /* Tabs */
156
157 var PageAction = function(tabId) {
158 this._tabId = tabId;
159 };
160 PageAction.prototype = {
161 setIcon: function(path) {
162 chrome.pageAction.setIcon({tabId: this._tabId, path: path});
163 },
164 setTitle: function(title) {
165 chrome.pageAction.setTitle({tabId: this._tabId, title: title});
166 },
167 hide: function() {
168 chrome.pageAction.hide(this._tabId);
169 },
170 show: function() {
171 chrome.pageAction.show(this._tabId);
172 }
173 };
174
175 Tab = function(tab) {
176 this._id = tab.id;
177
178 this.url = tab.url;
179 this.pageAction = new PageAction(tab.id);
180
181 this.onBeforeNavigate = ext.tabs.onBeforeNavigate._bindToTab(this);
182 this.onCompleted = ext.tabs.onCompleted._bindToTab(this);
183 this.onActivated = ext.tabs.onActivated._bindToTab(this);
184 this.onRemoved = ext.tabs.onRemoved._bindToTab(this);
185 };
186 Tab.prototype = {
187 close: function() {
188 chrome.tabs.remove(this._id);
189 },
190 activate: function() {
191 chrome.tabs.update(this._id, {selected: true});
192 },
193 sendMessage: function(message, responseCallback) {
194 chrome.tabs.sendMessage(this._id, message, responseCallback);
195 }
196 };
197
198 TabMap = function() {
199 this._map = {};
200 this.delete = this.delete.bind(this);
201 };
202 TabMap.prototype = {
203 get: function(tab) {
204 return (this._map[tab._id] || {}).value;
205 },
206 set: function(tab, value) {
207 if (!(tab._id in this._map))
208 tab.onRemoved.addListener(this.delete);
209
210 this._map[tab._id] = {tab: tab, value: value};
211 },
212 has: function(tab) {
213 return tab._id in this._map;
214 },
215 clear: function() {
216 for (var id in this._map)
217 this.delete(this._map[id].tab);
218 }
219 };
220 TabMap.prototype["delete"] = function(tab) {
221 delete this._map[tab._id];
222 tab.onRemoved.removeListener(this.delete);
223 };
224
225
226 /* Windows */
227
228 Window = function(win) {
229 this._id = win.id;
230 this.visible = win.status != "minimized";
231 };
232 Window.prototype = {
233 getAllTabs: function(callback) {
234 chrome.tabs.query({windowId: this._id}, function(tabs) {
235 callback(tabs.map(function(tab) {
236 return new Tab(tab);
237 }));
238 });
239 },
240 getActiveTab: function(callback) {
241 chrome.tabs.query({windowId: this._id, active: true}, function(tabs) {
242 callback(new Tab(tabs[0]));
243 });
244 },
245 openTab: function(url, callback) {
246 var props = {windowId: this._id, url: url};
247
248 if (!callback)
249 chrome.tabs.create(props);
250 else
251 chrome.tabs.create(props, function(tab) {
252 callback(new Tab(tab));
253 });
254 }
255 };
256
257
258 /* API */
259
260 ext.windows = {
261 getAll: function(callback) {
262 chrome.windows.getAll(function(windows) {
263 callback(windows.map(function(win) {
264 return new Window(win);
265 }));
266 });
267 },
268 getLastFocused: function(callback) {
269 chrome.windows.getLastFocused(function(win) {
270 callback(new Window(win));
271 });
272 }
273 };
274
275 ext.tabs = {
276 onBeforeNavigate: new BeforeNavigateTabEventTarget(),
277 onCompleted: new CompletedTabEventTarget(),
278 onActivated: new ActivatedTabEventTarget(),
279 onRemoved: new RemovedTabEventTarget()
280 };
281
282 ext.webRequest = {
283 onBeforeRequest: new BeforeRequestEventTarget(),
284 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged
285 };
286 })();
OLDNEW
« block.js ('K') | « block.js ('k') | chrome/common.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld