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

Side by Side Diff: ext/content.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Patch Set: Using Services.vc Created Dec. 19, 2014, 5:45 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
« no previous file with comments | « ext/common.js ('k') | firstRun.js » ('j') | messageResponder.js » ('J')
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 <http://adblockplus.org/>, 2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH 3 * Copyright (C) 2006-2014 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(global) 18 (function(global)
19 { 19 {
20 if (!global.ext) 20 if (!global.ext)
21 global.ext = {}; 21 global.ext = {};
22 22
23 function updateFromURL(data) 23 var backgroundFrame = document.createElement("iframe");
24 backgroundFrame.setAttribute("src", "background.html" + window.location.search );
25 backgroundFrame.style.display = "none";
26 window.addEventListener("DOMContentLoaded", function()
24 { 27 {
25 if (window.location.search) 28 document.body.appendChild(backgroundFrame);
29 }, false);
30
31 var messageQueue = [];
32 var maxMessageId = -1;
33 var loadHandler = function(event)
34 {
35 if (event.data.type == "backgroundPageLoaded")
26 { 36 {
27 var params = window.location.search.substr(1).split("&"); 37 var queue = messageQueue;
28 for (var i = 0; i < params.length; i++) 38 messageQueue = null;
29 { 39 if (queue)
30 var parts = params[i].split("=", 2); 40 for (var i = 0; i < queue.length; i++)
31 if (parts.length == 2 && parts[0] in data) 41 backgroundFrame.contentWindow.postMessage(queue[i], "*");
32 data[parts[0]] = decodeURIComponent(parts[1]); 42 window.removeEventListener("message", loadHandler, false);
33 }
34 } 43 }
35 } 44 }
36 45 window.addEventListener("message", loadHandler, false);
37 var subscriptions =[
38 "https://easylist-downloads.adblockplus.org/easylistgermany+easylist.txt",
39 "https://easylist-downloads.adblockplus.org/exceptionrules.txt",
40 "https://easylist-downloads.adblockplus.org/fanboy-social.txt"
41 ];
42
43 var listenerFilter = null;
44 46
45 global.ext.backgroundPage = { 47 global.ext.backgroundPage = {
46 sendMessage: function(message, responseCallback) 48 sendMessage: function(message, responseCallback)
47 { 49 {
48 var respond = function(response) 50 var rawMessage = {
51 type: "message",
52 messageId: ++maxMessageId,
53 payload: message
54 };
55 if (messageQueue)
56 messageQueue.push(rawMessage);
57 else
58 backgroundFrame.contentWindow.postMessage(rawMessage, "*");
59
60 if (responseCallback)
49 { 61 {
50 setTimeout(responseCallback.bind(responseCallback, response), 0); 62 var callbackWrapper = function(event)
51 };
52
53 var dispatchListenerNotification = function(action)
54 {
55 var match = /^subscription\.(.*)/.exec(action);
56 if (match && listenerFilter && listenerFilter.indexOf(match[1]) >= 0)
57 { 63 {
58 global.ext.onMessage._dispatch({ 64 if (event.data.type == "response" && event.data.messageId == rawMessag e.messageId)
59 type: "subscriptions.listen",
60 action: match[1],
61 args: Array.prototype.slice.call(arguments, 1)
62 });
63 }
64 };
65
66 switch (message.type)
67 {
68 case "app.get":
69 if (message.what == "issues")
70 { 65 {
71 var response = {seenDataCorruption: false, filterlistsReinitialized: false}; 66 window.removeEventListener("message", callbackWrapper, false);
72 updateFromURL(response); 67 responseCallback(event.data.payload);
73
74 var info = {platform: "gecko", platformVersion: "34.0", application: "firefox", applicationVersion: "34.0"};
75 updateFromURL(info);
76 response.legacySafariVersion = (info.platform == "safari" && (
77 parseInt(info.platformVersion, 10) < 6 || // beforeload breaks we bsites in Safari 5
78 info.platformVersion == "6.1" || // extensions are broke n in 6.1 and 7.0
79 info.platformVersion == "7.0"));
80
81 respond(response);
82 } 68 }
83 else if (message.what == "doclink") 69 };
84 respond("https://adblockplus.org/redirect?link=" + encodeURIComponen t(message.link)); 70 window.addEventListener("message", callbackWrapper, false);
85 else
86 respond(null);
87 break;
88 case "app.open":
89 if (message.what == "options")
90 window.open("http://example.com/options.html", "_blank");
91 break;
92 case "subscriptions.get":
93 respond(subscriptions);
94 break;
95 case "filters.blocked":
96 var params = {blockedURLs: ""};
97 updateFromURL(params);
98 var blocked = params.blockedURLs.split(",");
99 respond(blocked.indexOf(message.url) >= 0);
100 break;
101 case "subscriptions.toggle":
102 var index = subscriptions.indexOf(message.url);
103 if (index >= 0)
104 {
105 subscriptions.splice(index, 1);
106 dispatchListenerNotification("subscription.removed", message.url);
107 }
108 else
109 {
110 subscriptions.push(message.url);
111 dispatchListenerNotification("subscription.added", message.url);
112 }
113 break;
114 case "subscriptions.listen":
115 listenerFilter = message.filter;
116 break;
117 } 71 }
118 } 72 }
119 }; 73 };
120
121 var EventTarget = function(cancelable)
122 {
123 this._listeners = [];
124 this._cancelable = cancelable;
125 };
126 EventTarget.prototype = {
127 addListener: function(listener)
128 {
129 if (this._listeners.indexOf(listener) == -1)
130 this._listeners.push(listener);
131 },
132 removeListener: function(listener)
133 {
134 var idx = this._listeners.indexOf(listener);
135 if (idx != -1)
136 this._listeners.splice(idx, 1);
137 },
138 _dispatch: function()
139 {
140 var result = null;
141
142 for (var i = 0; i < this._listeners.length; i++)
143 {
144 result = this._listeners[i].apply(null, arguments);
145
146 if (this._cancelable && result === false)
147 break;
148 }
149
150 return result;
151 }
152 };
153 global.ext.onMessage = new EventTarget();
154 })(this); 74 })(this);
OLDNEW
« no previous file with comments | « ext/common.js ('k') | firstRun.js » ('j') | messageResponder.js » ('J')

Powered by Google App Engine
This is Rietveld