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: Properly convert Subscription objects as notification arguments Created Dec. 17, 2014, 10:15 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
« ext/common.js ('K') | « ext/common.js ('k') | firstRun.js » ('j') | 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 <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 getURLParameters(data) 23 var backgroundFrame = document.createElement("iframe");
24 backgroundFrame.setAttribute("src", "background.html" + window.location.search );
25 backgroundFrame.style.visibility = "hidden";
Thomas Greiner 2014/12/18 17:32:06 Rather use `backgroundFrame.style.display = "none"
Wladimir Palant 2014/12/18 22:04:44 Right, I forgot that frames will still load despit
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.doclink":
69 respond("https://adblockplus.org/redirect?link=" + encodeURIComponent( message.args[0]));
70 break;
71 case "app.info":
72 var response = {platform: "gecko", platformVersion: "34.0", applicatio n: "firefox", applicationVersion: "34.0"};
73 getURLParameters(response);
74 respond(response);
75 break;
76 case "app.issues":
77 var response = {seenDataCorruption: false, filterlistsReinitialized: f alse};
78 getURLParameters(response);
79 respond(response);
80 break;
81 case "app.options":
82 window.open("http://example.com/options.html", "_blank");
83 break;
84 case "subscriptions.get":
85 respond(subscriptions);
86 break;
87 case "filters.blocked":
88 var params = {blockedURLs: ""};
89 getURLParameters(params);
90 var blocked = params.blockedURLs.split(",");
91 respond(blocked.indexOf(message.url) >= 0);
92 break;
93 case "subscriptions.toggle":
94 var index = subscriptions.indexOf(message.url);
95 if (index >= 0)
96 { 65 {
97 subscriptions.splice(index, 1); 66 window.removeEventListener("message", callbackWrapper, false);
98 dispatchListenerNotification("subscription.removed", message.url); 67 responseCallback(event.data.payload);
99 } 68 }
100 else 69 };
101 { 70 window.addEventListener("message", callbackWrapper, false);
102 subscriptions.push(message.url);
103 dispatchListenerNotification("subscription.added", message.url);
104 }
105 break;
106 case "subscriptions.listen":
107 listenerFilter = message.filter;
108 break;
109 } 71 }
110 } 72 }
111 }; 73 };
112 74
113 var EventTarget = function(cancelable) 75 window.addEventListener("unload", function()
Thomas Greiner 2014/12/18 17:32:06 I don't see why an "unload" handler make sense in
Wladimir Palant 2014/12/18 22:04:44 Happens to be good enough for testing, the message
114 { 76 {
115 this._listeners = []; 77 global.ext.backgroundPage.sendMessage({type: "removePage"});
116 this._cancelable = cancelable; 78 }, false);
117 };
118 EventTarget.prototype = {
119 addListener: function(listener)
120 {
121 if (this._listeners.indexOf(listener) == -1)
122 this._listeners.push(listener);
123 },
124 removeListener: function(listener)
125 {
126 var idx = this._listeners.indexOf(listener);
127 if (idx != -1)
128 this._listeners.splice(idx, 1);
129 },
130 _dispatch: function()
131 {
132 var result = null;
133
134 for (var i = 0; i < this._listeners.length; i++)
135 {
136 result = this._listeners[i].apply(null, arguments);
137
138 if (this._cancelable && result === false)
139 break;
140 }
141
142 return result;
143 }
144 };
145 global.ext.onMessage = new EventTarget();
146 })(this); 79 })(this);
OLDNEW
« ext/common.js ('K') | « ext/common.js ('k') | firstRun.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld