| Left: | ||
| Right: | 
| LEFT | RIGHT | 
|---|---|
| 1 (function() { | 1 /* | 
| 
 
Felix Dahlke
2013/10/24 16:30:34
Opening braces should go on their own line, likewi
 
 | |
| 2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2013 Eyeo GmbH | |
| 4 * | |
| 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 | |
| 7 * published by the Free Software Foundation. | |
| 8 * | |
| 9 * Adblock Plus is distributed in the hope that it will be useful, | |
| 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 12 * GNU General Public License for more details. | |
| 13 * | |
| 14 * You should have received a copy of the GNU General Public License | |
| 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. | |
| 16 */ | |
| 17 | |
| 18 (function() | |
| 19 { | |
| 2 /* Events */ | 20 /* Events */ | 
| 3 | 21 | 
| 4 var TabEventTarget = function() { | 22 var TabEventTarget = function() | 
| 23 { | |
| 5 WrappedEventTarget.apply(this, arguments); | 24 WrappedEventTarget.apply(this, arguments); | 
| 6 | 25 | 
| 7 this._tabs = {}; | 26 this._tabs = {}; | 
| 8 | 27 | 
| 9 this._sharedListener = this._sharedListener.bind(this); | 28 this._sharedListener = this._sharedListener.bind(this); | 
| 10 this._removeTab = this._removeTab.bind(this); | 29 this._removeTab = this._removeTab.bind(this); | 
| 11 }; | 30 }; | 
| 12 TabEventTarget.prototype = { | 31 TabEventTarget.prototype = { | 
| 13 __proto__: WrappedEventTarget.prototype, | 32 __proto__: WrappedEventTarget.prototype, | 
| 14 _bindToTab: function(tab) { | 33 _bindToTab: function(tab) | 
| 34 { | |
| 15 return { | 35 return { | 
| 16 addListener: function(listener) { | 36 addListener: function(listener) | 
| 37 { | |
| 17 var listeners = this._tabs[tab._id]; | 38 var listeners = this._tabs[tab._id]; | 
| 18 | 39 | 
| 19 if (!listeners) { | 40 if (!listeners) | 
| 41 { | |
| 20 this._tabs[tab._id] = listeners = []; | 42 this._tabs[tab._id] = listeners = []; | 
| 21 | 43 | 
| 22 if (Object.keys(this._tabs).length == 1) | 44 if (Object.keys(this._tabs).length == 1) | 
| 23 this.addListener(this._sharedListener); | 45 this.addListener(this._sharedListener); | 
| 24 | 46 | 
| 25 tab.onRemoved.addListener(this._removeTab); | 47 tab.onRemoved.addListener(this._removeTab); | 
| 26 } | 48 } | 
| 27 | 49 | 
| 28 listeners.push(listener); | 50 listeners.push(listener); | 
| 29 }.bind(this), | 51 }.bind(this), | 
| 30 removeListener: function(listener) { | 52 removeListener: function(listener) | 
| 53 { | |
| 31 var listeners = this._tabs[tab._id]; | 54 var listeners = this._tabs[tab._id]; | 
| 32 if (!listeners) | 55 if (!listeners) | 
| 33 return; | 56 return; | 
| 34 | 57 | 
| 35 var idx = listeners.indexOf(listener); | 58 var idx = listeners.indexOf(listener); | 
| 36 if (idx == -1) | 59 if (idx == -1) | 
| 37 return; | 60 return; | 
| 38 | 61 | 
| 39 listeners.splice(idx, 1); | 62 listeners.splice(idx, 1); | 
| 40 | 63 | 
| 41 if (listeners.length == 0) | 64 if (listeners.length == 0) | 
| 42 tab.onRemoved.removeListener(this._removeTab); | 65 tab.onRemoved.removeListener(this._removeTab); | 
| 43 else { | 66 else | 
| 67 { | |
| 44 if (listeners.length > 1) | 68 if (listeners.length > 1) | 
| 45 return; | 69 return; | 
| 46 if (listeners[0] != this._removeTab) | 70 if (listeners[0] != this._removeTab) | 
| 47 return; | 71 return; | 
| 48 } | 72 } | 
| 49 | 73 | 
| 50 this._removeTab(tab); | 74 this._removeTab(tab); | 
| 51 }.bind(this) | 75 }.bind(this) | 
| 52 }; | 76 }; | 
| 53 }, | 77 }, | 
| 54 _sharedListener: function(tab) { | 78 _sharedListener: function(tab) | 
| 79 { | |
| 55 var listeners = this._tabs[tab._id]; | 80 var listeners = this._tabs[tab._id]; | 
| 56 | 81 | 
| 57 if (!listeners) | 82 if (!listeners) | 
| 58 return; | 83 return; | 
| 59 | 84 | 
| 60 // copy listeners before calling them, because of they | 85 // copy listeners before calling them, because they might | 
| 
 
Felix Dahlke
2013/10/24 16:30:34
s/because of/because/
 
 | |
| 61 // might add or remove other listeners, which must not be | 86 // add or remove other listeners, which must not be taken | 
| 62 // taken into account before the next occurrance of the event | 87 // into account before the next occurrence of the event | 
| 
 
Felix Dahlke
2013/10/24 16:30:34
s/occureace/occurence/
 
 | |
| 63 listeners = listeners.slice(0); | 88 listeners = listeners.slice(0); | 
| 64 | 89 | 
| 65 for (var i = 0; i < listeners.length; i++) | 90 for (var i = 0; i < listeners.length; i++) | 
| 66 listeners[i](tab); | 91 listeners[i](tab); | 
| 67 }, | 92 }, | 
| 68 _removeTab: function(tab) { | 93 _removeTab: function(tab) | 
| 94 { | |
| 69 delete this._tabs[tab._id]; | 95 delete this._tabs[tab._id]; | 
| 70 | 96 | 
| 71 if (Object.keys(this._tabs).length == 0) | 97 if (Object.keys(this._tabs).length == 0) | 
| 72 this.removeListener(this._sharedListener); | 98 this.removeListener(this._sharedListener); | 
| 73 } | 99 } | 
| 74 }; | 100 }; | 
| 75 | 101 | 
| 76 var BeforeNavigateTabEventTarget = function() { | 102 var BeforeNavigateTabEventTarget = function() | 
| 103 { | |
| 77 TabEventTarget.call(this, chrome.tabs.onUpdated); | 104 TabEventTarget.call(this, chrome.tabs.onUpdated); | 
| 78 }; | 105 }; | 
| 79 BeforeNavigateTabEventTarget.prototype = { | 106 BeforeNavigateTabEventTarget.prototype = { | 
| 80 __proto__: TabEventTarget.prototype, | 107 __proto__: TabEventTarget.prototype, | 
| 81 _wrapListener: function(listener) { | 108 _wrapListener: function(listener) | 
| 82 return function(id, info, tab) { | 109 { | 
| 110 return function(id, info, tab) | |
| 111 { | |
| 83 if ("url" in info) | 112 if ("url" in info) | 
| 84 listener(new Tab(tab)); | 113 listener(new Tab(tab)); | 
| 85 }; | 114 }; | 
| 86 } | 115 } | 
| 87 }; | 116 }; | 
| 88 | 117 | 
| 89 var CompletedTabEventTarget = function() { | 118 var CompletedTabEventTarget = function() | 
| 119 { | |
| 90 TabEventTarget.call(this, chrome.tabs.onUpdated); | 120 TabEventTarget.call(this, chrome.tabs.onUpdated); | 
| 91 }; | 121 }; | 
| 92 CompletedTabEventTarget.prototype = { | 122 CompletedTabEventTarget.prototype = { | 
| 93 __proto__: TabEventTarget.prototype, | 123 __proto__: TabEventTarget.prototype, | 
| 94 _wrapListener: function(listener) { | 124 _wrapListener: function(listener) | 
| 95 return function(id, info, tab) { | 125 { | 
| 126 return function(id, info, tab) | |
| 127 { | |
| 96 if (info.status == "complete") | 128 if (info.status == "complete") | 
| 97 listener(new Tab(tab)); | 129 listener(new Tab(tab)); | 
| 98 }; | 130 }; | 
| 99 } | 131 } | 
| 100 }; | 132 }; | 
| 101 | 133 | 
| 102 var ActivatedTabEventTarget = function() { | 134 var ActivatedTabEventTarget = function() | 
| 135 { | |
| 103 TabEventTarget.call(this, chrome.tabs.onActivated); | 136 TabEventTarget.call(this, chrome.tabs.onActivated); | 
| 104 }; | 137 }; | 
| 105 ActivatedTabEventTarget.prototype = { | 138 ActivatedTabEventTarget.prototype = { | 
| 106 __proto__: TabEventTarget.prototype, | 139 __proto__: TabEventTarget.prototype, | 
| 107 _wrapListener: function(listener) { | 140 _wrapListener: function(listener) | 
| 108 return function(info) { | 141 { | 
| 109 chrome.tabs.get(info.tabId, function(tab) { | 142 return function(info) | 
| 143 { | |
| 144 chrome.tabs.get(info.tabId, function(tab) | |
| 145 { | |
| 110 listener(new Tab(tab)); | 146 listener(new Tab(tab)); | 
| 111 }); | 147 }); | 
| 112 }; | 148 }; | 
| 113 } | 149 } | 
| 114 } | 150 } | 
| 115 | 151 | 
| 116 var RemovedTabEventTarget = function() { | 152 var RemovedTabEventTarget = function() | 
| 153 { | |
| 117 TabEventTarget.call(this, chrome.tabs.onRemoved); | 154 TabEventTarget.call(this, chrome.tabs.onRemoved); | 
| 118 }; | 155 }; | 
| 119 RemovedTabEventTarget.prototype = { | 156 RemovedTabEventTarget.prototype = { | 
| 120 __proto__: TabEventTarget.prototype, | 157 __proto__: TabEventTarget.prototype, | 
| 121 _wrapListener: function(listener) { | 158 _wrapListener: function(listener) | 
| 122 return function(id) { | 159 { | 
| 123 listener(new Tab({id: id})); | 160 return function(id) { listener(new Tab({id: id})); }; | 
| 124 }; | 161 } | 
| 125 } | 162 }; | 
| 126 }; | 163 | 
| 127 | 164 var BeforeRequestEventTarget = function() | 
| 128 var BeforeRequestEventTarget = function() { | 165 { | 
| 129 WrappedEventTarget.call(this, chrome.webRequest.onBeforeRequest); | 166 WrappedEventTarget.call(this, chrome.webRequest.onBeforeRequest); | 
| 130 }; | 167 }; | 
| 131 BeforeRequestEventTarget.prototype = { | 168 BeforeRequestEventTarget.prototype = { | 
| 132 __proto__: WrappedEventTarget.prototype, | 169 __proto__: WrappedEventTarget.prototype, | 
| 133 _wrapListener: function(listener) { | 170 _wrapListener: function(listener) | 
| 134 return function(details) { | 171 { | 
| 172 return function(details) | |
| 173 { | |
| 135 var tab = null; | 174 var tab = null; | 
| 136 | 175 | 
| 137 if (details.tabId != -1) | 176 if (details.tabId != -1) | 
| 138 tab = new Tab({id: details.tabId}); | 177 tab = new Tab({id: details.tabId}); | 
| 139 | 178 | 
| 140 return {cancel: listener( | 179 return {cancel: listener( | 
| 141 details.url, | 180 details.url, | 
| 142 details.type, | 181 details.type, | 
| 143 tab, | 182 tab, | 
| 144 details.frameId, | 183 details.frameId, | 
| 145 details.parentFrameId | 184 details.parentFrameId | 
| 146 ) === false}; | 185 ) === false}; | 
| 147 }; | 186 }; | 
| 148 }, | 187 }, | 
| 149 _prepareExtraArguments: function(urls) { | 188 _prepareExtraArguments: function(urls) | 
| 189 { | |
| 150 return [urls ? {urls: urls} : {}, ["blocking"]]; | 190 return [urls ? {urls: urls} : {}, ["blocking"]]; | 
| 151 } | 191 } | 
| 152 }; | 192 }; | 
| 153 | 193 | 
| 154 | 194 | 
| 155 /* Tabs */ | 195 /* Tabs */ | 
| 156 | 196 | 
| 157 var PageAction = function(tabId) { | 197 var PageAction = function(tabId) | 
| 198 { | |
| 158 this._tabId = tabId; | 199 this._tabId = tabId; | 
| 159 }; | 200 }; | 
| 160 PageAction.prototype = { | 201 PageAction.prototype = { | 
| 161 setIcon: function(path) { | 202 setIcon: function(path) | 
| 203 { | |
| 162 chrome.pageAction.setIcon({tabId: this._tabId, path: path}); | 204 chrome.pageAction.setIcon({tabId: this._tabId, path: path}); | 
| 163 }, | 205 }, | 
| 164 setTitle: function(title) { | 206 setTitle: function(title) | 
| 207 { | |
| 165 chrome.pageAction.setTitle({tabId: this._tabId, title: title}); | 208 chrome.pageAction.setTitle({tabId: this._tabId, title: title}); | 
| 166 }, | 209 }, | 
| 167 hide: function() { | 210 hide: function() | 
| 211 { | |
| 168 chrome.pageAction.hide(this._tabId); | 212 chrome.pageAction.hide(this._tabId); | 
| 169 }, | 213 }, | 
| 170 show: function() { | 214 show: function() | 
| 215 { | |
| 171 chrome.pageAction.show(this._tabId); | 216 chrome.pageAction.show(this._tabId); | 
| 172 } | 217 } | 
| 173 }; | 218 }; | 
| 174 | 219 | 
| 175 Tab = function(tab) { | 220 Tab = function(tab) | 
| 221 { | |
| 176 this._id = tab.id; | 222 this._id = tab.id; | 
| 177 | 223 | 
| 178 this.url = tab.url; | 224 this.url = tab.url; | 
| 179 this.pageAction = new PageAction(tab.id); | 225 this.pageAction = new PageAction(tab.id); | 
| 180 | 226 | 
| 181 this.onBeforeNavigate = ext.tabs.onBeforeNavigate._bindToTab(this); | 227 this.onBeforeNavigate = ext.tabs.onBeforeNavigate._bindToTab(this); | 
| 182 this.onCompleted = ext.tabs.onCompleted._bindToTab(this); | 228 this.onCompleted = ext.tabs.onCompleted._bindToTab(this); | 
| 183 this.onActivated = ext.tabs.onActivated._bindToTab(this); | 229 this.onActivated = ext.tabs.onActivated._bindToTab(this); | 
| 184 this.onRemoved = ext.tabs.onRemoved._bindToTab(this); | 230 this.onRemoved = ext.tabs.onRemoved._bindToTab(this); | 
| 185 }; | 231 }; | 
| 186 Tab.prototype = { | 232 Tab.prototype = { | 
| 187 close: function() { | 233 close: function() | 
| 234 { | |
| 188 chrome.tabs.remove(this._id); | 235 chrome.tabs.remove(this._id); | 
| 189 }, | 236 }, | 
| 190 activate: function() { | 237 activate: function() | 
| 238 { | |
| 191 chrome.tabs.update(this._id, {selected: true}); | 239 chrome.tabs.update(this._id, {selected: true}); | 
| 192 }, | 240 }, | 
| 193 sendMessage: function(message, responseCallback) { | 241 sendMessage: function(message, responseCallback) | 
| 242 { | |
| 194 chrome.tabs.sendMessage(this._id, message, responseCallback); | 243 chrome.tabs.sendMessage(this._id, message, responseCallback); | 
| 195 } | 244 } | 
| 196 }; | 245 }; | 
| 197 | 246 | 
| 198 TabMap = function() { | 247 TabMap = function() | 
| 248 { | |
| 199 this._map = {}; | 249 this._map = {}; | 
| 200 this.delete = this.delete.bind(this); | 250 this.delete = this.delete.bind(this); | 
| 201 }; | 251 }; | 
| 202 TabMap.prototype = { | 252 TabMap.prototype = { | 
| 203 get: function(tab) { | 253 get: function(tab) | 
| 254 { | |
| 204 return (this._map[tab._id] || {}).value; | 255 return (this._map[tab._id] || {}).value; | 
| 205 }, | 256 }, | 
| 206 set: function(tab, value) { | 257 set: function(tab, value) | 
| 258 { | |
| 207 if (!(tab._id in this._map)) | 259 if (!(tab._id in this._map)) | 
| 208 tab.onRemoved.addListener(this.delete); | 260 tab.onRemoved.addListener(this.delete); | 
| 209 | 261 | 
| 210 this._map[tab._id] = {tab: tab, value: value}; | 262 this._map[tab._id] = {tab: tab, value: value}; | 
| 211 }, | 263 }, | 
| 212 has: function(tab) { | 264 has: function(tab) | 
| 265 { | |
| 213 return tab._id in this._map; | 266 return tab._id in this._map; | 
| 214 }, | 267 }, | 
| 215 clear: function() { | 268 clear: function() | 
| 269 { | |
| 216 for (var id in this._map) | 270 for (var id in this._map) | 
| 217 this.delete(this._map[id].tab); | 271 this.delete(this._map[id].tab); | 
| 218 } | 272 } | 
| 219 }; | 273 }; | 
| 220 TabMap.prototype["delete"] = function(tab) { | 274 TabMap.prototype["delete"] = function(tab) | 
| 275 { | |
| 221 delete this._map[tab._id]; | 276 delete this._map[tab._id]; | 
| 222 tab.onRemoved.removeListener(this.delete); | 277 tab.onRemoved.removeListener(this.delete); | 
| 223 }; | 278 }; | 
| 224 | 279 | 
| 225 | 280 | 
| 226 /* Windows */ | 281 /* Windows */ | 
| 227 | 282 | 
| 228 Window = function(win) { | 283 Window = function(win) | 
| 284 { | |
| 229 this._id = win.id; | 285 this._id = win.id; | 
| 230 this.visible = win.status != "minimized"; | 286 this.visible = win.status != "minimized"; | 
| 231 }; | 287 }; | 
| 232 Window.prototype = { | 288 Window.prototype = { | 
| 233 getAllTabs: function(callback) { | 289 getAllTabs: function(callback) | 
| 234 chrome.tabs.query({windowId: this._id}, function(tabs) { | 290 { | 
| 235 callback(tabs.map(function(tab) { | 291 chrome.tabs.query({windowId: this._id}, function(tabs) | 
| 236 return new Tab(tab); | 292 { | 
| 237 })); | 293 callback(tabs.map(function(tab) { return new Tab(tab); })); | 
| 238 }); | 294 }); | 
| 239 }, | 295 }, | 
| 240 getActiveTab: function(callback) { | 296 getActiveTab: function(callback) | 
| 241 chrome.tabs.query({windowId: this._id, active: true}, function(tabs) { | 297 { | 
| 298 chrome.tabs.query({windowId: this._id, active: true}, function(tabs) | |
| 299 { | |
| 242 callback(new Tab(tabs[0])); | 300 callback(new Tab(tabs[0])); | 
| 243 }); | 301 }); | 
| 244 }, | 302 }, | 
| 245 openTab: function(url, callback) { | 303 openTab: function(url, callback) | 
| 304 { | |
| 246 var props = {windowId: this._id, url: url}; | 305 var props = {windowId: this._id, url: url}; | 
| 247 | 306 | 
| 248 if (!callback) | 307 if (!callback) | 
| 249 chrome.tabs.create(props); | 308 chrome.tabs.create(props); | 
| 250 else | 309 else | 
| 251 chrome.tabs.create(props, function(tab) { | 310 chrome.tabs.create(props, function(tab) | 
| 311 { | |
| 252 callback(new Tab(tab)); | 312 callback(new Tab(tab)); | 
| 253 }); | 313 }); | 
| 254 } | 314 } | 
| 255 }; | 315 }; | 
| 256 | 316 | 
| 257 | 317 | 
| 258 /* API */ | 318 /* API */ | 
| 259 | 319 | 
| 260 ext.windows = { | 320 ext.windows = { | 
| 261 getAll: function(callback) { | 321 getAll: function(callback) | 
| 262 chrome.windows.getAll(function(windows) { | 322 { | 
| 263 callback(windows.map(function(win) { | 323 chrome.windows.getAll(function(windows) | 
| 324 { | |
| 325 callback(windows.map(function(win) | |
| 326 { | |
| 264 return new Window(win); | 327 return new Window(win); | 
| 265 })); | 328 })); | 
| 266 }); | 329 }); | 
| 267 }, | 330 }, | 
| 268 getLastFocused: function(callback) { | 331 getLastFocused: function(callback) | 
| 269 chrome.windows.getLastFocused(function(win) { | 332 { | 
| 333 chrome.windows.getLastFocused(function(win) | |
| 334 { | |
| 270 callback(new Window(win)); | 335 callback(new Window(win)); | 
| 271 }); | 336 }); | 
| 272 } | 337 } | 
| 273 }; | 338 }; | 
| 274 | 339 | 
| 275 ext.tabs = { | 340 ext.tabs = { | 
| 276 onBeforeNavigate: new BeforeNavigateTabEventTarget(), | 341 onBeforeNavigate: new BeforeNavigateTabEventTarget(), | 
| 277 onCompleted: new CompletedTabEventTarget(), | 342 onCompleted: new CompletedTabEventTarget(), | 
| 278 onActivated: new ActivatedTabEventTarget(), | 343 onActivated: new ActivatedTabEventTarget(), | 
| 279 onRemoved: new RemovedTabEventTarget() | 344 onRemoved: new RemovedTabEventTarget() | 
| 280 }; | 345 }; | 
| 281 | 346 | 
| 282 ext.webRequest = { | 347 ext.webRequest = { | 
| 283 onBeforeRequest: new BeforeRequestEventTarget(), | 348 onBeforeRequest: new BeforeRequestEventTarget(), | 
| 284 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged | 349 handlerBehaviorChanged: chrome.webRequest.handlerBehaviorChanged | 
| 285 }; | 350 }; | 
| 286 })(); | 351 })(); | 
| LEFT | RIGHT |