Bug 1299411 - Remove extension param from NativeApp
The extension is always available as `context.extension`, so do not
unnecessarily pass the extension as a separate parameter.
MozReview-Commit-ID: 9OaestUAGVk
--- 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
@@ -55,22 +55,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();