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

Unified Diff: ext/background.js

Issue 29715759: Issue 6440 - Use long-lived connections to listen to extension events (Closed)
Patch Set: Added message passing mock for ports Created March 7, 2018, 7:10 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ext/common.js » ('j') | ext/common.js » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ext/background.js
===================================================================
--- a/ext/background.js
+++ b/ext/background.js
@@ -29,55 +29,6 @@
}, "*");
}, false);
- function PageMap()
- {
- this._keys = [];
- this._values = [];
- }
- PageMap.prototype = {
- keys()
- {
- return this._keys.map((source) =>
- {
- return new window.ext.Page(source);
- });
- },
-
- get(page)
- {
- return this._values[this._keys.indexOf(page._source)];
- },
-
- set(page, value)
- {
- let index = this._keys.indexOf(page._source);
- if (index < 0)
- {
- index = this._keys.push(page._source) - 1;
-
- let callback = function()
- {
- page._source.removeEventListener("unload", callback, false);
- this.delete(page);
- }.bind(this);
- page._source.addEventListener("unload", callback, false);
- }
- this._values[index] = value;
- },
-
- delete(page)
- {
- let index = this._keys.indexOf(page._source);
- if (index >= 0)
- {
- this._keys.splice(index, 1);
- this._values.splice(index, 1);
- }
- }
- };
-
- window.ext.PageMap = PageMap;
-
window.ext.devtools = {
onCreated: {
addListener(listener)
@@ -90,4 +41,31 @@
}
}
};
+
+ /* Message passing */
+
+ if (!("runtime" in browser))
+ browser.runtime = {};
+
+ function postMessage(msg)
+ {
+ parent.postMessage({
+ type: "port",
+ name: this._name,
+ payload: msg
+ }, "*");
+ }
+ ext._Port.prototype.postMessage = postMessage;
+
+ function onConnect(listener)
+ {
+ window.addEventListener("message", (event) =>
+ {
+ if (event.data.type != "connect")
+ return;
+
+ listener(new ext._Port(event.data.name));
+ });
+ }
+ window.browser.runtime.onConnect = {addListener: onConnect};
}());
« no previous file with comments | « no previous file | ext/common.js » ('j') | ext/common.js » ('J')

Powered by Google App Engine
This is Rietveld