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

Unified Diff: safari/ext/background.js

Issue 29339314: Issue 3870 - Rewrite legacy options page to use async messages (Closed)
Patch Set: Avoid another race condition Created April 7, 2016, 1:27 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 | « options.js ('k') | safari/ext/content.js » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: safari/ext/background.js
diff --git a/safari/ext/background.js b/safari/ext/background.js
index ebb96332a03c370ff5e7bdcd1bc6875135895f56..38870c0564b31fca093a5cb0c36419a0063014a7 100644
--- a/safari/ext/background.js
+++ b/safari/ext/background.js
@@ -382,230 +382,6 @@
}
};
-
- /* Background page proxy (for access from content scripts) */
-
- var backgroundPageProxy = {
- cache: new ext.PageMap(),
-
- registerObject: function(obj, objects)
- {
- var objectId = objects.indexOf(obj);
-
- if (objectId == -1)
- objectId = objects.push(obj) - 1;
-
- return objectId;
- },
- serializeSequence: function(sequence, objects, memo)
- {
- if (!memo)
- memo = {specs: [], arrays: []};
-
- var items = [];
- for (var i = 0; i < sequence.length; i++)
- items.push(this.serialize(sequence[i], objects, memo));
-
- return items;
- },
- serialize: function(obj, objects, memo)
- {
- if (typeof obj == "object" && obj != null || typeof obj == "function")
- {
- if (obj.constructor == Array)
- {
- if (!memo)
- memo = {specs: [], arrays: []};
-
- var idx = memo.arrays.indexOf(obj);
- if (idx != -1)
- return memo.specs[idx];
-
- var spec = {type: "array"};
- memo.specs.push(spec);
- memo.arrays.push(obj);
-
- spec.items = this.serializeSequence(obj, objects, memo);
- return spec;
- }
-
- if (obj.constructor != Date && obj.constructor != RegExp)
- return {type: "object", objectId: this.registerObject(obj, objects)};
- }
-
- return {type: "value", value: obj};
- },
- createCallback: function(callbackId, pageId, frameId)
- {
- var proxy = this;
-
- return function()
- {
- var page = pages[pageId];
- if (!page)
- return;
-
- var objects = proxy.cache.get(page);
- if (!objects)
- return;
-
- var targetDocument;
- for (var documentId in page._tab._documentLookup)
- {
- var result = page._tab._documentLookup[documentId];
- if (result.pageId == pageId && result.frameId == frameId)
- {
- targetDocument = documentId;
- break;
- }
- }
- if (!targetDocument)
- return;
-
- page._tab.page.dispatchMessage("proxyCallback",
- {
- targetDocuments: [targetDocument],
- callbackId: callbackId,
- contextId: proxy.registerObject(this, objects),
- args: proxy.serializeSequence(arguments, objects)
- });
- };
- },
- deserialize: function(spec, objects, pageId, frameId, memo)
- {
- switch (spec.type)
- {
- case "value":
- return spec.value;
- case "hosted":
- return objects[spec.objectId];
- case "callback":
- return this.createCallback(spec.callbackId, pageId, frameId);
- case "object":
- case "array":
- if (!memo)
- memo = {specs: [], objects: []};
-
- var idx = memo.specs.indexOf(spec);
- if (idx != -1)
- return memo.objects[idx];
-
- var obj;
- if (spec.type == "array")
- obj = [];
- else
- obj = {};
-
- memo.specs.push(spec);
- memo.objects.push(obj);
-
- if (spec.type == "array")
- for (var i = 0; i < spec.items.length; i++)
- obj.push(this.deserialize(spec.items[i], objects,
- pageId, frameId, memo));
- else
- for (var k in spec.properties)
- obj[k] = this.deserialize(spec.properties[k], objects,
- pageId, frameId, memo);
-
- return obj;
- }
- },
- getObjectCache: function(page)
- {
- var objects = this.cache.get(page);
- if (!objects)
- {
- objects = [window];
- this.cache.set(page, objects);
- }
- return objects;
- },
- fail: function(error)
- {
- if (error instanceof Error)
- error = error.message;
- return {succeed: false, error: error};
- },
- handleMessage: function(message)
- {
- var objects = this.getObjectCache(pages[message.pageId]);
-
- switch (message.type)
- {
- case "getProperty":
- var obj = objects[message.objectId];
-
- try
- {
- var value = obj[message.property];
- }
- catch (e)
- {
- return this.fail(e);
- }
-
- return {succeed: true, result: this.serialize(value, objects)};
- case "setProperty":
- var obj = objects[message.objectId];
- var value = this.deserialize(message.value, objects,
- message.pageId, message.frameId);
-
- try
- {
- obj[message.property] = value;
- }
- catch (e)
- {
- return this.fail(e);
- }
-
- return {succeed: true};
- case "callFunction":
- var func = objects[message.functionId];
- var context = objects[message.contextId];
-
- var args = [];
- for (var i = 0; i < message.args.length; i++)
- args.push(this.deserialize(message.args[i], objects,
- message.pageId, message.frameId));
-
- try
- {
- var result = func.apply(context, args);
- }
- catch (e)
- {
- return this.fail(e);
- }
-
- return {succeed: true, result: this.serialize(result, objects)};
- case "inspectObject":
- var obj = objects[message.objectId];
- var objectInfo = {properties: {}, isFunction: typeof obj == "function"};
-
- Object.getOwnPropertyNames(obj).forEach(function(prop)
- {
- objectInfo.properties[prop] = {
- enumerable: Object.prototype.propertyIsEnumerable.call(obj, prop)
- };
- });
-
- var proto = Object.getPrototypeOf(obj);
- if (proto)
- objectInfo.prototypeId = this.registerObject(proto, objects);
-
- if (obj == Object.prototype)
- objectInfo.prototypeOf = "Object";
- if (obj == Function.prototype)
- objectInfo.prototypeOf = "Function";
-
- return objectInfo;
- }
- }
- };
-
-
/* Message processing */
safari.application.addEventListener("message", function(event)
@@ -636,11 +412,6 @@
event.message = (results.indexOf(false) == -1);
break;
- case "proxy":
- message.pageId = sender.pageId;
- message.frameId = sender.frameId;
- event.message = backgroundPageProxy.handleMessage(message);
- break;
case "request":
var response = null;
var sendResponse = function(message) { response = message; };
« no previous file with comments | « options.js ('k') | safari/ext/content.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld