OLD | NEW |
(Empty) | |
| 1 /* |
| 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 { |
| 20 /* Background page proxy */ |
| 21 var proxy = { |
| 22 objects: [], |
| 23 callbacks: [], |
| 24 |
| 25 send: function(message) |
| 26 { |
| 27 var evt = document.createEvent("Event"); |
| 28 evt.initEvent("beforeload"); |
| 29 return safari.self.tab.canLoad(evt, {type: "proxy", payload: message}); |
| 30 }, |
| 31 checkResult: function(result) |
| 32 { |
| 33 if (!result.succeed) |
| 34 throw result.error; |
| 35 }, |
| 36 deserializeResult: function(result) |
| 37 { |
| 38 this.checkResult(result); |
| 39 return this.deserialize(result.result); |
| 40 }, |
| 41 serialize: function(obj, memo) |
| 42 { |
| 43 var objectId = this.objects.indexOf(obj); |
| 44 if (objectId != -1) |
| 45 return {type: "hosted", objectId: objectId}; |
| 46 |
| 47 if (typeof obj == "function") |
| 48 { |
| 49 var callbackId = this.callbacks.indexOf(obj); |
| 50 |
| 51 if (callbackId == -1) |
| 52 { |
| 53 callbackId = this.callbacks.push(obj) - 1; |
| 54 |
| 55 safari.self.addEventListener("message", function(event) |
| 56 { |
| 57 if (event.name == "proxyCallback") |
| 58 if (event.message.callbackId == callbackId) |
| 59 obj.apply( |
| 60 this.getObject(event.message.contextId), |
| 61 this.deserializeSequence(event.message.args) |
| 62 ); |
| 63 }.bind(this)); |
| 64 } |
| 65 |
| 66 return {type: "callback", callbackId: callbackId}; |
| 67 } |
| 68 |
| 69 if (typeof obj == "object") |
| 70 if (obj != null) |
| 71 if (obj.constructor != Date) |
| 72 if (obj.constructor != RegExp) |
| 73 { |
| 74 if (!memo) |
| 75 memo = {specs: [], objects: []}; |
| 76 |
| 77 var idx = memo.objects.indexOf(obj); |
| 78 if (idx != -1) |
| 79 return memo.specs[idx]; |
| 80 |
| 81 var spec = {}; |
| 82 memo.specs.push(spec); |
| 83 memo.objects.push(obj); |
| 84 |
| 85 if (obj.constructor == Array) |
| 86 { |
| 87 spec.type = "array"; |
| 88 spec.items = []; |
| 89 |
| 90 for (var i = 0; i < obj.length; i++) |
| 91 spec.items.push(this.serialize(obj[i], memo)); |
| 92 } |
| 93 else |
| 94 { |
| 95 spec.type = "object"; |
| 96 spec.properties = {}; |
| 97 |
| 98 for (var k in obj) |
| 99 spec.properties[k] = this.serialize(obj[k], memo); |
| 100 } |
| 101 |
| 102 return spec; |
| 103 } |
| 104 |
| 105 return {type: "value", value: obj}; |
| 106 }, |
| 107 deserializeSequence: function(specs, array, memo) |
| 108 { |
| 109 if (!array) |
| 110 array = []; |
| 111 |
| 112 if (!memo) |
| 113 memo = {specs: [], arrays: []}; |
| 114 |
| 115 for (var i = 0; i < specs.length; i++) |
| 116 array.push(this.deserialize(specs[i], memo)); |
| 117 |
| 118 return array; |
| 119 }, |
| 120 deserialize: function(spec, memo) |
| 121 { |
| 122 switch (spec.type) |
| 123 { |
| 124 case "value": |
| 125 return spec.value; |
| 126 case "object": |
| 127 return this.getObject(spec.objectId); |
| 128 case "array": |
| 129 if (!memo) |
| 130 memo = {specs: [], arrays: []}; |
| 131 |
| 132 var idx = memo.specs.indexOf(spec); |
| 133 if (idx != -1) |
| 134 return memo.arrays[idx]; |
| 135 |
| 136 var array = []; |
| 137 memo.specs.push(spec); |
| 138 memo.arrays.push(array); |
| 139 |
| 140 return this.deserializeSequence(spec.items, array, memo); |
| 141 } |
| 142 }, |
| 143 getProperty: function(objectId, property) |
| 144 { |
| 145 return this.deserializeResult( |
| 146 this.send({ |
| 147 type: "getProperty", |
| 148 objectId: objectId, |
| 149 property: property |
| 150 }) |
| 151 ); |
| 152 }, |
| 153 createProperty: function(objectId, property, enumerable) |
| 154 { |
| 155 return { |
| 156 get: function() |
| 157 { |
| 158 return this.getProperty(objectId, property); |
| 159 }.bind(this), |
| 160 set: function(value) |
| 161 { |
| 162 this.checkResult( |
| 163 this.send({ |
| 164 type: "setProperty", |
| 165 objectId: objectId, |
| 166 property: property, |
| 167 value: this.serialize(value) |
| 168 }) |
| 169 ) |
| 170 }.bind(this), |
| 171 enumerable: enumerable, |
| 172 configurable: true |
| 173 }; |
| 174 }, |
| 175 createFunction: function(objectId) |
| 176 { |
| 177 var proxy = this; |
| 178 return function() |
| 179 { |
| 180 return proxy.deserializeResult( |
| 181 proxy.send({ |
| 182 type: "callFunction", |
| 183 functionId: objectId, |
| 184 contextId: proxy.objects.indexOf(this), |
| 185 args: Array.prototype.map.call( |
| 186 arguments, |
| 187 proxy.serialize.bind(proxy) |
| 188 ) |
| 189 }) |
| 190 ); |
| 191 }; |
| 192 }, |
| 193 getObject: function(objectId) { |
| 194 var objectInfo = this.send({ |
| 195 type: "inspectObject", |
| 196 objectId: objectId |
| 197 }); |
| 198 |
| 199 var obj = this.objects[objectId]; |
| 200 if (obj) |
| 201 Object.getOwnPropertyNames(obj).forEach(function(prop) { delete obj[prop
]; }); |
| 202 else |
| 203 { |
| 204 if (objectInfo.isFunction) |
| 205 obj = this.createFunction(objectId); |
| 206 else |
| 207 obj = {}; |
| 208 |
| 209 this.objects[objectId] = obj; |
| 210 } |
| 211 |
| 212 var ignored = []; |
| 213 if ("prototypeOf" in objectInfo) |
| 214 { |
| 215 var prototype = window[objectInfo.prototypeOf].prototype; |
| 216 |
| 217 ignored = Object.getOwnPropertyNames(prototype); |
| 218 ignored.splice(ignored.indexOf("constructor"), 1); |
| 219 |
| 220 obj.__proto__ = prototype; |
| 221 } |
| 222 else |
| 223 { |
| 224 if (objectInfo.isFunction) |
| 225 ignored = Object.getOwnPropertyNames(function() {}); |
| 226 else |
| 227 ignored = []; |
| 228 |
| 229 if ("prototypeId" in objectInfo) |
| 230 obj.__proto__ = this.getObject(objectInfo.prototypeId); |
| 231 else |
| 232 obj.__proto__ = null; |
| 233 } |
| 234 |
| 235 for (var property in objectInfo.properties) |
| 236 if (ignored.indexOf(property) == -1) |
| 237 Object.defineProperty(obj, property, this.createProperty( |
| 238 objectId, property, |
| 239 objectInfo.properties[property].enumerable |
| 240 )); |
| 241 |
| 242 if (objectInfo.isFunction) |
| 243 obj.prototype = this.getProperty(objectId, "prototype"); |
| 244 |
| 245 return obj; |
| 246 } |
| 247 }; |
| 248 |
| 249 |
| 250 /* Web request blocking */ |
| 251 |
| 252 document.addEventListener("beforeload", function(event) |
| 253 { |
| 254 var type; |
| 255 |
| 256 switch(event.target.nodeName) |
| 257 { |
| 258 case "FRAME": |
| 259 case "IFRAME": |
| 260 type = "frame"; |
| 261 break; |
| 262 case "IMG": |
| 263 type = "image"; |
| 264 break; |
| 265 case "OBJECT": |
| 266 case "EMBED": |
| 267 type = "object"; |
| 268 break; |
| 269 case "SCRIPT": |
| 270 type = "script"; |
| 271 break; |
| 272 case "LINK": |
| 273 if (/(^|\s)stylesheet($|\s)/i.test(event.target.rel)) |
| 274 { |
| 275 type = "stylesheet"; |
| 276 break; |
| 277 } |
| 278 default: |
| 279 type = "other"; |
| 280 } |
| 281 |
| 282 if (!safari.self.tab.canLoad(event, {type: "webRequest", payload: {url: even
t.url, type: type}})) |
| 283 event.preventDefault(); |
| 284 }, true); |
| 285 |
| 286 |
| 287 /* API */ |
| 288 |
| 289 ext.backgroundPage = { |
| 290 _eventTarget: safari.self, |
| 291 _messageDispatcher: safari.self.tab, |
| 292 |
| 293 sendMessage: sendMessage, |
| 294 getWindow: function() { return proxy.getObject(0); } |
| 295 }; |
| 296 |
| 297 ext.onMessage = new MessageEventTarget(safari.self); |
| 298 })(); |
OLD | NEW |