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

Side by Side Diff: safari/ext/content.js

Issue 29338621: Issue 3788 - Keep track of Safari tabs without canLoad (Closed)
Patch Set: Created March 18, 2016, 2:21 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
« safari/ext/background.js ('K') | « safari/ext/common.js ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 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 13 matching lines...) Expand all
24 24
25 25
26 /* Intialization */ 26 /* Intialization */
27 27
28 var beforeLoadEvent = document.createEvent("Event"); 28 var beforeLoadEvent = document.createEvent("Event");
29 beforeLoadEvent.initEvent("beforeload"); 29 beforeLoadEvent.initEvent("beforeload");
30 30
31 var isTopLevel = window == window.top; 31 var isTopLevel = window == window.top;
32 var isPrerendered = document.visibilityState == "prerender"; 32 var isPrerendered = document.visibilityState == "prerender";
33 33
34 var documentInfo = safari.self.tab.canLoad( 34 // Notify the background page that this frame is loading, generating ourselves
35 beforeLoadEvent, 35 // a random documentId while we're at it. That way the background page can
36 { 36 // communicate with us reliably, despite limitations in Safari's extension
37 category: "loading", 37 // API.
38 url: window.location.href, 38 var documentId = Math.random().toString();
Sebastian Noack 2016/03/18 19:32:51 Nit: It's not important for the logic, but more an
kzar 2016/03/18 19:53:38 Done.
39 referrer: document.referrer, 39 safari.self.tab.dispatchMessage("loading", {
40 isTopLevel: isTopLevel, 40 url: window.location.href,
41 isPrerendered: isPrerendered 41 referrer: document.referrer,
42 } 42 isTopLevel: isTopLevel,
43 ); 43 isPrerendered: isPrerendered,
44 documentId: documentId
45 });
44 46
45 if (isTopLevel && isPrerendered) 47 if (isTopLevel && isPrerendered)
46 { 48 {
47 var onVisibilitychange = function() 49 var onVisibilitychange = function()
48 { 50 {
49 safari.self.tab.dispatchMessage("replaced", {pageId: documentInfo.pageId}) ; 51 safari.self.tab.dispatchMessage("replaced", {documentId: documentId});
50 document.removeEventListener("visibilitychange", onVisibilitychange); 52 document.removeEventListener("visibilitychange", onVisibilitychange);
51 }; 53 };
52 document.addEventListener("visibilitychange", onVisibilitychange); 54 document.addEventListener("visibilitychange", onVisibilitychange);
53 } 55 }
54 56
55 57
56 /* Web requests */ 58 /* Web requests */
57 59
58 document.addEventListener("beforeload", function(event) 60 document.addEventListener("beforeload", function(event)
59 { 61 {
(...skipping 29 matching lines...) Expand all
89 break; 91 break;
90 case "script": 92 case "script":
91 type = "SCRIPT"; 93 type = "SCRIPT";
92 break; 94 break;
93 case "link": 95 case "link":
94 if (/\bstylesheet\b/i.test(event.target.rel)) 96 if (/\bstylesheet\b/i.test(event.target.rel))
95 type = "STYLESHEET"; 97 type = "STYLESHEET";
96 break; 98 break;
97 } 99 }
98 100
99 if (!safari.self.tab.canLoad( 101 if (!safari.self.tab.canLoad(event, {
100 event, {
101 category: "webRequest", 102 category: "webRequest",
102 url: event.url, 103 url: event.url,
103 type: type, 104 type: type,
104 pageId: documentInfo.pageId, 105 documentId: documentId}))
105 frameId: documentInfo.frameId
106 }
107 ))
108 { 106 {
109 event.preventDefault(); 107 event.preventDefault();
110 108
111 // Safari doesn't dispatch the expected events for elements that have been 109 // Safari doesn't dispatch the expected events for elements that have been
112 // prevented from loading by having their "beforeload" event cancelled. 110 // prevented from loading by having their "beforeload" event cancelled.
113 // That is a "load" event for blocked frames, and an "error" event for 111 // That is a "load" event for blocked frames, and an "error" event for
114 // other blocked elements. We need to dispatch those events manually here 112 // other blocked elements. We need to dispatch those events manually here
115 // to avoid breaking element collapsing and pages that rely on those event s. 113 // to avoid breaking element collapsing and pages that rely on those event s.
116 setTimeout(function() 114 setTimeout(function()
117 { 115 {
118 var evt = document.createEvent("Event"); 116 var evt = document.createEvent("Event");
119 evt.initEvent(eventName); 117 evt.initEvent(eventName);
120 event.target.dispatchEvent(evt); 118 event.target.dispatchEvent(evt);
121 }, 0); 119 }, 0);
122 } 120 }
123 }, true); 121 }, true);
124 122
125 123
126 /* Context menus */ 124 /* Context menus */
127 125
128 document.addEventListener("contextmenu", function(event) 126 document.addEventListener("contextmenu", function(event)
129 { 127 {
130 var element = event.srcElement; 128 var element = event.srcElement;
131 safari.self.tab.setContextMenuEventUserInfo(event, { 129 safari.self.tab.setContextMenuEventUserInfo(event, {
132 pageId: documentInfo.pageId, 130 documentId: documentId,
133 tagName: element.localName 131 tagName: element.localName
134 }); 132 });
135 }); 133 });
136 134
137 135
138 /* Background page */ 136 /* Background page */
139 137
140 var backgroundPageProxy = { 138 var backgroundPageProxy = {
141 objects: [], 139 objects: [],
142 callbacks: [], 140 callbacks: [],
143 141
144 send: function(message) 142 send: function(message)
145 { 143 {
146 message.category = "proxy"; 144 message.category = "proxy";
147 message.pageId = documentInfo.pageId; 145 message.documentId = documentId;
148 146
149 return safari.self.tab.canLoad(beforeLoadEvent, message); 147 return safari.self.tab.canLoad(beforeLoadEvent, message);
150 }, 148 },
151 checkResult: function(result) 149 checkResult: function(result)
152 { 150 {
153 if (!result.succeed) 151 if (!result.succeed)
154 throw result.error; 152 throw result.error;
155 }, 153 },
156 deserializeResult: function(result) 154 deserializeResult: function(result)
157 { 155 {
(...skipping 11 matching lines...) Expand all
169 { 167 {
170 var callbackId; 168 var callbackId;
171 if ("__proxyCallbackId" in obj) 169 if ("__proxyCallbackId" in obj)
172 callbackId = obj.__proxyCallbackId; 170 callbackId = obj.__proxyCallbackId;
173 else 171 else
174 { 172 {
175 callbackId = this.callbacks.push(obj) - 1; 173 callbackId = this.callbacks.push(obj) - 1;
176 Object.defineProperty(obj, "__proxyCallbackId", {value: callbackId}) ; 174 Object.defineProperty(obj, "__proxyCallbackId", {value: callbackId}) ;
177 } 175 }
178 176
179 return {type: "callback", callbackId: callbackId, frameId: documentInf o.frameId}; 177 return {type: "callback", callbackId: callbackId,
178 documentId: documentId};
180 } 179 }
181 180
182 if (obj.constructor != Date && obj.constructor != RegExp) 181 if (obj.constructor != Date && obj.constructor != RegExp)
183 { 182 {
184 if (!memo) 183 if (!memo)
185 memo = {specs: [], objects: []}; 184 memo = {specs: [], objects: []};
186 185
187 var idx = memo.objects.indexOf(obj); 186 var idx = memo.objects.indexOf(obj);
188 if (idx != -1) 187 if (idx != -1)
189 return memo.specs[idx]; 188 return memo.specs[idx];
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
375 } 374 }
376 } 375 }
377 376
378 return obj; 377 return obj;
379 } 378 }
380 }; 379 };
381 380
382 ext.backgroundPage = { 381 ext.backgroundPage = {
383 sendMessage: function(message, responseCallback) 382 sendMessage: function(message, responseCallback)
384 { 383 {
385 messageProxy.sendMessage(message, responseCallback, documentInfo); 384 messageProxy.sendMessage(message, responseCallback,
385 {documentId: documentId});
386 }, 386 },
387 sendMessageSync: function(message) 387 sendMessageSync: function(message)
388 { 388 {
389 return safari.self.tab.canLoad( 389 return safari.self.tab.canLoad(
390 beforeLoadEvent, 390 beforeLoadEvent,
391 { 391 {
392 category: "request", 392 category: "request",
393 pageId: documentInfo.pageId, 393 documentId: documentId,
394 frameId: documentInfo.frameId,
395 payload: message 394 payload: message
396 } 395 }
397 ); 396 );
398 }, 397 },
399 getWindow: function() 398 getWindow: function()
400 { 399 {
401 return backgroundPageProxy.getObject(0); 400 return backgroundPageProxy.getObject(0);
402 } 401 }
403 }; 402 };
404 403
405 404
406 /* Message processing */ 405 /* Message processing */
407 406
408 var messageProxy = new ext._MessageProxy(safari.self.tab); 407 var messageProxy = new ext._MessageProxy(safari.self.tab);
409 408
410 safari.self.addEventListener("message", function(event) 409 safari.self.addEventListener("message", function(event)
411 { 410 {
412 if (event.message.pageId == documentInfo.pageId) 411 if (event.name == "requestDocumentId" && isTopLevel)
413 { 412 {
414 if (event.name == "request") 413 safari.self.tab.dispatchMessage("documentId", {
414 pageId: event.message.pageId,
415 documentId: documentId
416 });
417 }
418 else if (event.message.targetDocuments.indexOf(documentId) != -1)
419 {
420 switch (event.name)
415 { 421 {
416 messageProxy.handleRequest(event.message, {}); 422 case "request":
417 return; 423 messageProxy.handleRequest(event.message, {});
418 } 424 break;
419 425 case "response":
420 if (event.message.frameId == documentInfo.frameId) 426 messageProxy.handleResponse(event.message);
421 { 427 break;
422 switch (event.name) 428 case "proxyCallback":
423 { 429 backgroundPageProxy.handleCallback(event.message);
424 case "response": 430 break;
425 messageProxy.handleResponse(event.message);
426 break;
427 case "proxyCallback":
428 backgroundPageProxy.handleCallback(event.message);
429 break;
430 }
431 } 431 }
432 } 432 }
433 }); 433 });
434 434
435 435
436 /* Detecting extension reload/disable/uninstall (not supported on Safari) */ 436 /* Detecting extension reload/disable/uninstall (not supported on Safari) */
437 437
438 ext.onExtensionUnloaded = { 438 ext.onExtensionUnloaded = {
439 addListener: function() {}, 439 addListener: function() {},
440 removeListener: function() {} 440 removeListener: function() {}
441 }; 441 };
442 })(); 442 })();
OLDNEW
« safari/ext/background.js ('K') | « safari/ext/common.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld