Bug 1299411 - Remove extension param from NativeApp r=aswan
authorRob Wu <rob@robwu.nl>
Sat, 24 Sep 2016 13:18:48 +0200
changeset 346819 2829c46a636d7a2b77a88df0b9fbe36182fa836d
parent 346818 57f7a5c7044db9fba2cd039773ac058b7269aac5
child 346820 24d81c7b335e8cb1949efb2b27a978cf108de705
push id10298
push userraliiev@mozilla.com
push dateMon, 14 Nov 2016 12:33:03 +0000
treeherdermozilla-aurora@7e29173b1641 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1299411
milestone52.0a1
Bug 1299411 - Remove extension param from NativeApp r=aswan The extension is always available as `context.extension`, so do not unnecessarily pass the extension as a separate parameter. MozReview-Commit-ID: 9OaestUAGVk
toolkit/components/extensions/NativeMessaging.jsm
toolkit/components/extensions/ext-runtime.js
toolkit/components/extensions/test/xpcshell/test_native_messaging.js
--- a/toolkit/components/extensions/NativeMessaging.jsm
+++ b/toolkit/components/extensions/NativeMessaging.jsm
@@ -158,17 +158,17 @@ this.HostManifestManager = {
     if (!VALID_APPLICATION.test(application)) {
       throw new Error(`Invalid application "${application}"`);
     }
     return this.init().then(() => this._lookup(application, context));
   },
 };
 
 this.NativeApp = class extends EventEmitter {
-  constructor(extension, context, application) {
+  constructor(context, application) {
     super();
 
     this.context = context;
     this.name = application;
 
     // We want a close() notification when the window is destroyed.
     this.context.callOnClose(this);
 
@@ -179,17 +179,17 @@ this.NativeApp = class extends EventEmit
     this.sentDisconnect = false;
 
     this.startupPromise = HostManifestManager.lookupApplication(application, context)
       .then(hostInfo => {
         if (!hostInfo) {
           throw new Error(`No such native application ${application}`);
         }
 
-        if (!hostInfo.manifest.allowed_extensions.includes(extension.id)) {
+        if (!hostInfo.manifest.allowed_extensions.includes(context.extension.id)) {
           throw new Error(`This extension does not have permission to use native application ${application}`);
         }
 
         let command = hostInfo.manifest.path;
         if (AppConstants.platform == "win") {
           // OS.Path.join() ignores anything before the last absolute path
           // it sees, so if command is already absolute, it remains unchanged
           // here.  If it is relative, we get the proper absolute path here.
--- a/toolkit/components/extensions/ext-runtime.js
+++ b/toolkit/components/extensions/ext-runtime.js
@@ -71,22 +71,22 @@ extensions.registerSchemaAPI("runtime", 
           // Otherwise, reload the current extension.
           AddonManager.getAddonByID(extension.id, addon => {
             addon.reload();
           });
         }
       },
 
       connectNative(application) {
-        let app = new NativeApp(extension, context, application);
+        let app = new NativeApp(context, application);
         return app.portAPI();
       },
 
       sendNativeMessage(application, message) {
-        let app = new NativeApp(extension, context, application);
+        let app = new NativeApp(context, application);
         message = NativeApp.encodeMessage(context, message);
         return app.sendMessage(message);
       },
 
       get lastError() {
         // TODO(robwu): Figure out how to make sure that errors in the parent
         // process are propagated to the child process.
         // lastError should not be accessed from the parent.
--- a/toolkit/components/extensions/test/xpcshell/test_native_messaging.js
+++ b/toolkit/components/extensions/test/xpcshell/test_native_messaging.js
@@ -1,13 +1,14 @@
 "use strict";
 
 /* global OS, HostManifestManager, NativeApp */
 Cu.import("resource://gre/modules/AppConstants.jsm");
 Cu.import("resource://gre/modules/AsyncShutdown.jsm");
+Cu.import("resource://gre/modules/ExtensionUtils.jsm");
 Cu.import("resource://gre/modules/FileUtils.jsm");
 Cu.import("resource://gre/modules/Schemas.jsm");
 Cu.import("resource://gre/modules/Services.jsm");
 const {Subprocess, SubprocessImpl} = Cu.import("resource://gre/modules/Subprocess.jsm");
 Cu.import("resource://gre/modules/NativeMessaging.jsm");
 Cu.import("resource://gre/modules/osfile.jsm");
 
 let registry = null;
@@ -78,16 +79,32 @@ let context = {
   jsonStringify(...args) { return JSON.stringify(...args); },
   cloneScope: global,
   logError() {},
   preprocessors: {},
   callOnClose: () => {},
   forgetOnClose: () => {},
 };
 
+class MockContext extends ExtensionUtils.BaseContext {
+  constructor(extensionId) {
+    let fakeExtension = {id: extensionId};
+    super("testEnv", fakeExtension);
+    this.sandbox = Cu.Sandbox(global);
+  }
+
+  get cloneScope() {
+    return global;
+  }
+
+  get principal() {
+    return Cu.getObjectPrincipal(this.sandbox);
+  }
+}
+
 let templateManifest = {
   name: "test",
   description: "this is only a test",
   path: "/bin/cat",
   type: "stdio",
   allowed_extensions: ["extension@tests.mozilla.org"],
 };
 
@@ -250,18 +267,18 @@ while True:
                       `${REGPATH}\\wontdie`, "", manifestPath);
   } else {
     yield OS.File.writeAtomic(scriptPath, `#!${PYTHON} -u\n${SCRIPT}`);
     yield OS.File.setPermissions(scriptPath, {unixMode: 0o755});
     manifest.path = scriptPath;
     yield writeManifest(manifestPath, manifest);
   }
 
-  let extension = {id: ID};
-  let app = new NativeApp(extension, context, "wontdie");
+  let context = new MockContext(ID);
+  let app = new NativeApp(context, "wontdie");
 
   // send a message and wait for the reply to make sure the app is running
   let MSG = "test";
   let recvPromise = new Promise(resolve => {
     let listener = (what, msg) => {
       equal(msg, MSG, "Received test message");
       app.off("message", listener);
       resolve();