Bug 1316396: Part 2 - Rename contexts to make it clearer how and where they're used. r=aswan
authorKris Maglione <maglione.k@gmail.com>
Wed, 09 Nov 2016 12:08:42 -0800
changeset 322120 d921a38dd17b69a5dea2bad3366b79576f3751c3
parent 322119 4e31a2cb26bdc0147dcf5b6cf9e104ab0a48c80d
child 322121 1731b48caa40c1753400d6867a6b2fb99dafd60d
push id21
push usermaklebus@msu.edu
push dateThu, 01 Dec 2016 06:22:08 +0000
reviewersaswan
bugs1316396
milestone52.0a1
Bug 1316396: Part 2 - Rename contexts to make it clearer how and where they're used. r=aswan MozReview-Commit-ID: 25oSbulhCmX
toolkit/components/extensions/ExtensionChild.jsm
toolkit/components/extensions/ExtensionContent.jsm
toolkit/components/extensions/ExtensionParent.jsm
--- a/toolkit/components/extensions/ExtensionChild.jsm
+++ b/toolkit/components/extensions/ExtensionChild.jsm
@@ -745,37 +745,40 @@ class PseudoChildAPIManager extends Chil
       // will be available, but no actual implementation is given.
       // You should either provide an implementation or rewrite the JSON schema.
     }
 
     return super.getFallbackImplementation(namespace, name);
   }
 }
 
-class ExtensionContext extends BaseContext {
+class ExtensionPageContextChild extends BaseContext {
   /**
-   * This ExtensionContext represents a privileged addon execution environment
-   * that has full access to the WebExtensions APIs (provided that the correct
-   * permissions have been requested).
+   * This ExtensionPageContextChild represents a privileged addon
+   * execution environment that has full access to the WebExtensions
+   * APIs (provided that the correct permissions have been requested).
+   *
+   * This is the child side of the ExtensionPageContextParent class
+   * defined in ExtensionParent.jsm.
    *
    * @param {BrowserExtensionContent} extension This context's owner.
    * @param {object} params
    * @param {nsIDOMWindow} params.contentWindow The window where the addon runs.
    * @param {string} params.viewType One of "background", "popup" or "tab".
    *     "background" and "tab" are used by `browser.extension.getViews`.
    *     "popup" is only used internally to identify page action and browser
    *     action popups and options_ui pages.
    * @param {number} [params.tabId] This tab's ID, used if viewType is "tab".
    */
   constructor(extension, params) {
     super("addon_child", extension);
     if (Services.appinfo.processType != Services.appinfo.PROCESS_TYPE_DEFAULT) {
       // This check is temporary. It should be removed once the proxy creation
       // is asynchronous.
-      throw new Error("ExtensionContext cannot be created in child processes");
+      throw new Error("ExtensionPageContextChild cannot be created in child processes");
     }
 
     let {viewType, uri, contentWindow, tabId} = params;
     this.viewType = viewType;
     this.uri = uri || extension.baseURI;
 
     this.setContentWindow(contentWindow);
 
@@ -843,28 +846,28 @@ class ExtensionContext extends BaseConte
       this.contentWindow.close();
     }
 
     super.unload();
     this.extension.views.delete(this);
   }
 }
 
-defineLazyGetter(ExtensionContext.prototype, "messenger", function() {
+defineLazyGetter(ExtensionPageContextChild.prototype, "messenger", function() {
   let filter = {extensionId: this.extension.id};
   let optionalFilter = {};
   // Addon-generated messages (not necessarily from the same process as the
   // addon itself) are sent to the main process, which forwards them via the
   // parent process message manager. Specific replies can be sent to the frame
   // message manager.
   return new Messenger(this, [Services.cpmm, this.messageManager], this.sender,
                        filter, optionalFilter);
 });
 
-defineLazyGetter(ExtensionContext.prototype, "childManager", function() {
+defineLazyGetter(ExtensionPageContextChild.prototype, "childManager", function() {
   let localApis = {};
   apiManager.generateAPIs(this, localApis);
 
   if (this.viewType == "background") {
     apiManager.global.initializeBackgroundPage(this.contentWindow);
   }
 
   let childManager = new PseudoChildAPIManager(this, this.messageManager, localApis, {
@@ -976,17 +979,17 @@ class ContentGlobal {
     }
   }
 }
 
 ExtensionChild = {
   // Map<nsIContentFrameMessageManager, ContentGlobal>
   contentGlobals: new Map(),
 
-  // Map<innerWindowId, ExtensionContext>
+  // Map<innerWindowId, ExtensionPageContextChild>
   extensionContexts: new Map(),
 
   initOnce() {
     // This initializes the default message handler for messages targeted at
     // an addon process, in case the addon process receives a message before
     // its Messenger has been instantiated. For example, if a content script
     // sends a message while there is no background page.
     MessageChannel.setupMessageManagers([Services.cpmm]);
@@ -1026,22 +1029,22 @@ ExtensionChild = {
       .QueryInterface(Ci.nsIInterfaceRequestor)
       .getInterface(Ci.nsIDocShell)
       .QueryInterface(Ci.nsIInterfaceRequestor)
       .getInterface(Ci.nsIContentFrameMessageManager);
     let {viewType, tabId} = this.contentGlobals.get(mm).ensureInitialized();
 
     let uri = contentWindow.document.documentURIObject;
 
-    context = new ExtensionContext(extension, {viewType, contentWindow, uri, tabId});
+    context = new ExtensionPageContextChild(extension, {viewType, contentWindow, uri, tabId});
     this.extensionContexts.set(windowId, context);
   },
 
   /**
-   * Close the ExtensionContext belonging to the given window, if any.
+   * Close the ExtensionPageContextChild belonging to the given window, if any.
    *
    * @param {number} windowId The inner window ID of the destroyed context.
    */
   destroyExtensionContext(windowId) {
     let context = this.extensionContexts.get(windowId);
     if (context) {
       context.unload();
       this.extensionContexts.delete(windowId);
--- a/toolkit/components/extensions/ExtensionContent.jsm
+++ b/toolkit/components/extensions/ExtensionContent.jsm
@@ -281,20 +281,23 @@ function getWindowMessageManager(content
     // Some windows don't support this interface (hidden window).
     return null;
   }
 }
 
 var DocumentManager;
 var ExtensionManager;
 
-// Scope in which extension content script code can run. It uses
-// Cu.Sandbox to run the code. There is a separate scope for each
-// frame.
-class ExtensionContext extends BaseContext {
+/**
+ * An execution context for semi-privileged extension content scripts.
+ *
+ * This is the child side of the ContentScriptContextParent class
+ * defined in ExtensionParent.jsm.
+ */
+class ContentScriptContextChild extends BaseContext {
   constructor(extension, contentWindow, contextOptions = {}) {
     super("content_child", extension);
 
     let {isExtensionPage} = contextOptions;
 
     this.isExtensionPage = isExtensionPage;
 
     this.setContentWindow(contentWindow);
@@ -436,26 +439,26 @@ class ExtensionContext extends BaseConte
         Cu.createObjectIn(this.contentWindow, {defineAs: "chrome"});
       }
     }
     Cu.nukeSandbox(this.sandbox);
     this.sandbox = null;
   }
 }
 
-defineLazyGetter(ExtensionContext.prototype, "messenger", function() {
+defineLazyGetter(ContentScriptContextChild.prototype, "messenger", function() {
   // The |sender| parameter is passed directly to the extension.
   let sender = {id: this.extension.uuid, frameId: this.frameId, url: this.url};
   let filter = {extensionId: this.extension.id};
   let optionalFilter = {frameId: this.frameId};
 
   return new Messenger(this, [this.messageManager], sender, filter, optionalFilter);
 });
 
-defineLazyGetter(ExtensionContext.prototype, "childManager", function() {
+defineLazyGetter(ContentScriptContextChild.prototype, "childManager", function() {
   let localApis = {};
   apiManager.generateAPIs(this, localApis);
 
   let childManager = new ChildAPIManager(this, this.messageManager, localApis, {
     envType: "content_parent",
     url: this.url,
   });
 
@@ -464,20 +467,20 @@ defineLazyGetter(ExtensionContext.protot
   return childManager;
 });
 
 // Responsible for creating ExtensionContexts and injecting content
 // scripts into them when new documents are created.
 DocumentManager = {
   extensionCount: 0,
 
-  // Map[windowId -> Map[extensionId -> ExtensionContext]]
+  // Map[windowId -> Map[extensionId -> ContentScriptContextChild]]
   contentScriptWindows: new Map(),
 
-  // Map[windowId -> ExtensionContext]
+  // Map[windowId -> ContentScriptContextChild]
   extensionPageWindows: new Map(),
 
   init() {
     Services.obs.addObserver(this, "content-document-global-created", false);
     Services.obs.addObserver(this, "document-element-inserted", false);
     Services.obs.addObserver(this, "inner-window-destroyed", false);
   },
 
@@ -666,29 +669,29 @@ DocumentManager = {
   getContentScriptContext(extension, window) {
     let winId = getInnerWindowID(window);
     if (!this.contentScriptWindows.has(winId)) {
       this.contentScriptWindows.set(winId, new Map());
     }
 
     let extensions = this.contentScriptWindows.get(winId);
     if (!extensions.has(extension.id)) {
-      let context = new ExtensionContext(extension, window);
+      let context = new ContentScriptContextChild(extension, window);
       extensions.set(extension.id, context);
     }
 
     return extensions.get(extension.id);
   },
 
   getExtensionPageContext(extension, window) {
     let winId = getInnerWindowID(window);
 
     let context = this.extensionPageWindows.get(winId);
     if (!context) {
-      let context = new ExtensionContext(extension, window, {isExtensionPage: true});
+      let context = new ContentScriptContextChild(extension, window, {isExtensionPage: true});
       this.extensionPageWindows.set(winId, context);
     }
 
     return context;
   },
 
   startupExtension(extensionId) {
     if (this.extensionCount == 0) {
--- a/toolkit/components/extensions/ExtensionParent.jsm
+++ b/toolkit/components/extensions/ExtensionParent.jsm
@@ -262,17 +262,21 @@ class BrowserDocshellFollower {
   handleEvent({detail: otherBrowser}) {
     this.xulBrowser.removeEventListener("SwapDocShells", this);
     this.xulBrowser = otherBrowser;
     this.xulBrowser.addEventListener("SwapDocShells", this);
     this.onBrowserChange(otherBrowser);
   }
 }
 
-class ProxyContext extends BaseContext {
+/**
+ * The proxied parent side of a context in ExtensionChild.jsm, for the
+ * parent side of a proxied API.
+ */
+class ProxyContextParent extends BaseContext {
   constructor(envType, extension, params, xulBrowser, principal) {
     super(envType, extension);
 
     this.uri = NetUtil.newURI(params.url);
 
     this.incognito = params.incognito;
 
     // This message manager is used by ParentAPIManager to send messages and to
@@ -316,28 +320,39 @@ class ProxyContext extends BaseContext {
       return;
     }
     this._docShellTracker.destroy();
     super.unload();
     apiManager.emit("proxy-context-unload", this);
   }
 }
 
-defineLazyGetter(ProxyContext.prototype, "apiObj", function() {
+defineLazyGetter(ProxyContextParent.prototype, "apiObj", function() {
   let obj = {};
   GlobalManager.injectInObject(this, false, obj);
   return obj;
 });
 
-defineLazyGetter(ProxyContext.prototype, "sandbox", function() {
+defineLazyGetter(ProxyContextParent.prototype, "sandbox", function() {
   return Cu.Sandbox(this.principal);
 });
 
-// The parent ProxyContext of an ExtensionContext in ExtensionChild.jsm.
-class ExtensionChildProxyContext extends ProxyContext {
+/**
+ * The parent side of proxied API context for extension content script
+ * running in ExtensionContent.jsm.
+ */
+class ContentScriptContextParent extends ProxyContextParent {
+}
+
+/**
+ * The parent side of proxied API context for extension page, such as a
+ * background script, a tab page, or a popup, running in
+ * ExtensionChild.jsm.
+ */
+class ExtensionPageContextParent extends ProxyContextParent {
   constructor(envType, extension, params, xulBrowser) {
     super(envType, extension, params, xulBrowser, extension.principal);
 
     this.viewType = params.viewType;
     // WARNING: The xulBrowser may change when docShells are swapped, e.g. when
     // the tab moves to a different window.
     this.xulBrowser = xulBrowser;
   }
@@ -446,19 +461,19 @@ ParentAPIManager = {
     let context;
     if (envType == "addon_parent") {
       // Privileged addon contexts can only be loaded in documents whose main
       // frame is also the same addon.
       if (principal.URI.prePath !== extension.baseURI.prePath ||
           !target.contentPrincipal.subsumes(principal)) {
         throw new Error(`Refused to create privileged WebExtension context for ${principal.URI.spec}`);
       }
-      context = new ExtensionChildProxyContext(envType, extension, data, target);
+      context = new ExtensionPageContextParent(envType, extension, data, target);
     } else if (envType == "content_parent") {
-      context = new ProxyContext(envType, extension, data, target, principal);
+      context = new ContentScriptContextParent(envType, extension, data, target, principal);
     } else {
       throw new Error(`Invalid WebExtension context envType: ${envType}`);
     }
     this.proxyContexts.set(childId, context);
   },
 
   closeProxyContext(childId) {
     let context = this.proxyContexts.get(childId);