-
Notifications
You must be signed in to change notification settings - Fork 54
/
ext-runtime.js
144 lines (119 loc) · 4.37 KB
/
ext-runtime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";
this.runtime = class extends ExtensionAPI {
getAPI(context) {
let { extension } = context;
return {
runtime: {
onConnect: context.messenger.onConnect("runtime.onConnect"),
onMessage: context.messenger.onMessage("runtime.onMessage"),
onConnectExternal: context.messenger.onConnectExternal(
"runtime.onConnectExternal"
),
onMessageExternal: context.messenger.onMessageExternal(
"runtime.onMessageExternal"
),
connect: function(extensionId, connectInfo) {
let name = (connectInfo !== null && connectInfo.name) || "";
extensionId = extensionId || extension.id;
let recipient = { extensionId };
return context.messenger.connect(
context.messageManager,
name,
recipient
);
},
sendMessage(...args) {
let extensionId, message, options, responseCallback;
if (typeof args[args.length - 1] === "function") {
responseCallback = args.pop();
}
function checkOptions(options) {
if (typeof options !== "object") {
return [
false,
"runtime.sendMessage's options argument is invalid",
];
}
for (let key of Object.keys(options)) {
return [false, `Unexpected property ${key}`];
}
return [true, {}];
}
if (!args.length) {
return Promise.reject({
message: "runtime.sendMessage's message argument is missing",
});
} else if (args.length === 1) {
message = args[0];
} else if (args.length === 2) {
// With two optional arguments, this is the ambiguous case,
// particularly sendMessage("string", {} or null)
// Given that sending a message within the extension is generally
// more common than sending the empty object to another extension,
// we prefer that conclusion, as long as the second argument looks
// like valid options object, or is null/undefined.
let [validOpts] = checkOptions(args[1]);
if (validOpts || args[1] == null) {
[message, options] = args;
} else {
[extensionId, message] = args;
}
} else if (
args.length === 3 ||
(args.length === 4 && args[3] == null)
) {
[extensionId, message, options] = args;
} else if (args.length === 4 && !responseCallback) {
return Promise.reject({
message: "runtime.sendMessage's last argument is not a function",
});
} else {
return Promise.reject({
message: "runtime.sendMessage received too many arguments",
});
}
if (extensionId != null && typeof extensionId !== "string") {
return Promise.reject({
message: "runtime.sendMessage's extensionId argument is invalid",
});
}
extensionId = extensionId || extension.id;
let recipient = { extensionId };
if (options != null) {
let [valid, arg] = checkOptions(options);
if (!valid) {
return Promise.reject({ message: arg });
}
Object.assign(recipient, arg);
}
return context.messenger.sendMessage(
context.messageManager,
message,
recipient,
responseCallback
);
},
connectNative(nativeApp) {
return context.messenger.nm.connectNative(nativeApp);
},
sendNativeMessage(nativeApp, message) {
return context.messenger.nm.sendNativeMessage(nativeApp, message);
},
get lastError() {
return context.lastError;
},
getManifest() {
return Cu.cloneInto(extension.manifest, context.cloneScope);
},
id: extension.id,
isCliqz: true,
getURL(url) {
return extension.baseURI.resolve(url);
},
},
};
}
};