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

Delta Between Two Patch Sets: ext/common.js

Issue 4731979438227456: Issue 1663 - Emulate background page and implement proper message responder (Closed)
Left Patch Set: Created Dec. 17, 2014, 6:32 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « ext/background.js ('k') | ext/content.js » ('j') | messageResponder.js » ('J')
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-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 Page(source)
24 {
25 this._source = source;
26 }
27 Page.prototype =
28 {
29 sendMessage: function(message)
30 {
31 this._source.postMessage({
32 type: "message",
33 messageId: -1,
34 payload: message
35 }, "*");
36 }
37 };
38
39 global.ext.Page = Page;
40
23 /* Message passing */ 41 /* Message passing */
24 42
25 global.ext.onMessage = 43 global.ext.onMessage =
26 { 44 {
27 addListener: function(listener) 45 addListener: function(listener)
28 { 46 {
29 listener.__wrapper = function(event) 47 listener._extWrapper = function(event)
30 { 48 {
31 if (event.data.type != "message") 49 if (event.data.type != "message")
32 return; 50 return;
33 51
34 var message = event.data.payload; 52 var message = event.data.payload;
35 var messageId = event.data.messageId; 53 var messageId = event.data.messageId;
36 var sender = { 54 var sender = {
37 sendMessage: function(message) 55 page: new Page(event.source)
38 {
39 event.source.postMessage({
40 type: "message",
41 messageId: -1,
42 payload: message
43 }, "*");
44 }
45 }; 56 };
46 var callback = function(message) 57 var callback = function(message)
47 { 58 {
48 event.source.postMessage({ 59 event.source.postMessage({
49 type: "response", 60 type: "response",
50 messageId: messageId, 61 messageId: messageId,
51 payload: message 62 payload: message
52 }, "*"); 63 }, "*");
53 }; 64 };
54 listener(message, sender, callback); 65 listener(message, sender, callback);
55 }; 66 };
56 window.addEventListener("message", listener.__wrapper, false); 67 window.addEventListener("message", listener._extWrapper, false);
57 }, 68 },
58 69
59 removeListener: function(listener) 70 removeListener: function(listener)
60 { 71 {
61 if ("__wrapper" in listener) 72 if ("_extWrapper" in listener)
62 window.removeEventListener("message", listener.__wrapper, false); 73 window.removeEventListener("message", listener._extWrapper, false);
63 } 74 }
64 }; 75 };
65 76
66 /* I18n */ 77 /* I18n */
67 78
68 var getLocaleCandidates = function(selectedLocale) 79 var getLocaleCandidates = function(selectedLocale)
69 { 80 {
70 var candidates = []; 81 var candidates = [];
71 var defaultLocale = "en-US"; 82 var defaultLocale = "en-US";
72 83
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
177 } 188 }
178 189
179 if (locales.length == 0) 190 if (locales.length == 0)
180 return ""; 191 return "";
181 192
182 readCatalog(locales.shift()); 193 readCatalog(locales.shift());
183 } 194 }
184 } 195 }
185 }; 196 };
186 })(this); 197 })(this);
LEFTRIGHT

Powered by Google App Engine
This is Rietveld