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

Delta Between Two Patch Sets: safari/content.js

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

Powered by Google App Engine
This is Rietveld