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

Delta Between Two Patch Sets: ext/background.js

Issue 29715759: Issue 6440 - Use long-lived connections to listen to extension events (Closed)
Left Patch Set: Created March 6, 2018, 6:35 p.m.
Right Patch Set: Added message passing mock for ports Created March 7, 2018, 7:10 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | ext/common.js » ('j') | ext/common.js » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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-present eyeo GmbH 3 * Copyright (C) 2006-present 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 11 matching lines...) Expand all
22 if (typeof ext == "undefined") 22 if (typeof ext == "undefined")
23 window.ext = {}; 23 window.ext = {};
24 24
25 window.addEventListener("load", () => 25 window.addEventListener("load", () =>
26 { 26 {
27 parent.postMessage({ 27 parent.postMessage({
28 type: "backgroundPageLoaded" 28 type: "backgroundPageLoaded"
29 }, "*"); 29 }, "*");
30 }, false); 30 }, false);
31 31
32 function PageMap()
33 {
34 this._keys = [];
35 this._values = [];
36 }
37 PageMap.prototype = {
38 keys()
39 {
40 return this._keys.map((source) =>
41 {
42 return new window.ext.Page(source);
43 });
44 },
45
46 get(page)
47 {
48 return this._values[this._keys.indexOf(page._source)];
49 },
50
51 set(page, value)
52 {
53 let index = this._keys.indexOf(page._source);
54 if (index < 0)
55 {
56 index = this._keys.push(page._source) - 1;
57
58 let callback = function()
59 {
60 page._source.removeEventListener("unload", callback, false);
61 this.delete(page);
62 }.bind(this);
63 page._source.addEventListener("unload", callback, false);
64 }
65 this._values[index] = value;
66 },
67
68 delete(page)
69 {
70 let index = this._keys.indexOf(page._source);
71 if (index >= 0)
72 {
73 this._keys.splice(index, 1);
74 this._values.splice(index, 1);
75 }
76 }
77 };
78
79 window.ext.PageMap = PageMap;
80
81 window.ext.devtools = { 32 window.ext.devtools = {
82 onCreated: { 33 onCreated: {
83 addListener(listener) 34 addListener(listener)
84 { 35 {
85 window.addEventListener("message", (event) => 36 window.addEventListener("message", (event) =>
86 { 37 {
87 if (event.data.type == "devtools") 38 if (event.data.type == "devtools")
88 listener(new ext.Page(event.source)); 39 listener(new ext.Page(event.source));
89 }); 40 });
90 } 41 }
91 } 42 }
92 }; 43 };
44
45 /* Message passing */
46
47 if (!("runtime" in browser))
48 browser.runtime = {};
49
50 function postMessage(msg)
51 {
52 parent.postMessage({
53 type: "port",
54 name: this._name,
55 payload: msg
56 }, "*");
57 }
58 ext._Port.prototype.postMessage = postMessage;
59
60 function onConnect(listener)
61 {
62 window.addEventListener("message", (event) =>
63 {
64 if (event.data.type != "connect")
65 return;
66
67 listener(new ext._Port(event.data.name));
68 });
69 }
70 window.browser.runtime.onConnect = {addListener: onConnect};
93 }()); 71 }());
LEFTRIGHT
« no previous file | ext/common.js » ('j') | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld