summaryrefslogtreecommitdiffstats
path: root/toolkit/components/utils/simpleServices.js
blob: 0b8dfe877cbf22bed99b0283ecc733040f2c354b (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
/* 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/. */

/*
 * Dumping ground for simple services for which the isolation of a full global
 * is overkill. Be careful about namespace pollution, and be mindful about
 * importing lots of JSMs in global scope, since this file will almost certainly
 * be loaded from enough callsites that any such imports will always end up getting
 * eagerly loaded at startup.
 */

"use strict";

const Cc = Components.classes;
const Cu = Components.utils;
const Ci = Components.interfaces;
const Cr = Components.results;

Cu.import("resource://gre/modules/XPCOMUtils.jsm");

XPCOMUtils.defineLazyModuleGetter(this, "NetUtil",
                                  "resource://gre/modules/NetUtil.jsm");
XPCOMUtils.defineLazyModuleGetter(this, "Services",
                                  "resource://gre/modules/Services.jsm");

function AddonPolicyService()
{
  this.wrappedJSObject = this;
  this.cspStrings = new Map();
  this.backgroundPageUrlCallbacks = new Map();
  this.checkHasPermissionCallbacks = new Map();
  this.mayLoadURICallbacks = new Map();
  this.localizeCallbacks = new Map();

  XPCOMUtils.defineLazyPreferenceGetter(
    this, "baseCSP", "extensions.webextensions.base-content-security-policy",
    "script-src 'self' https://* moz-extension: blob: filesystem: 'unsafe-eval' 'unsafe-inline'; " +
    "object-src 'self' https://* moz-extension: blob: filesystem:;");

  XPCOMUtils.defineLazyPreferenceGetter(
    this, "defaultCSP", "extensions.webextensions.default-content-security-policy",
    "script-src 'self'; object-src 'self';");
}

AddonPolicyService.prototype = {
  classID: Components.ID("{89560ed3-72e3-498d-a0e8-ffe50334d7c5}"),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIAddonPolicyService]),

  /**
   * Returns the content security policy which applies to documents belonging
   * to the extension with the given ID. This may be either a custom policy,
   * if one was supplied, or the default policy if one was not.
   */
  getAddonCSP(aAddonId) {
    let csp = this.cspStrings.get(aAddonId);
    return csp || this.defaultCSP;
  },

  /**
   * Returns the generated background page as a data-URI, if any. If the addon
   * does not have an auto-generated background page, an empty string is
   * returned.
   */
  getGeneratedBackgroundPageUrl(aAddonId) {
    let cb = this.backgroundPageUrlCallbacks.get(aAddonId);
    return cb && cb(aAddonId) || '';
  },

  /*
   * Invokes a callback (if any) associated with the addon to determine whether
   * the addon is granted the |aPerm| API permission.
   *
   * @see nsIAddonPolicyService.addonHasPermission
   */
  addonHasPermission(aAddonId, aPerm) {
    let cb = this.checkHasPermissionCallbacks.get(aAddonId);
    return cb ? cb(aPerm) : false;
  },

  /*
   * Invokes a callback (if any) associated with the addon to determine whether
   * unprivileged code running within the addon is allowed to perform loads from
   * the given URI.
   *
   * @see nsIAddonPolicyService.addonMayLoadURI
   */
  addonMayLoadURI(aAddonId, aURI) {
    let cb = this.mayLoadURICallbacks.get(aAddonId);
    return cb ? cb(aURI) : false;
  },

  /*
   * Invokes a callback (if any) associated with the addon to loclaize a
   * resource belonging to that add-on.
   */
  localizeAddonString(aAddonId, aString) {
    let cb = this.localizeCallbacks.get(aAddonId);
    return cb ? cb(aString) : aString;
  },

  /*
   * Invokes a callback (if any) to determine if an extension URI should be
   * web-accessible.
   *
   * @see nsIAddonPolicyService.extensionURILoadableByAnyone
   */
  extensionURILoadableByAnyone(aURI) {
    if (aURI.scheme != "moz-extension") {
      throw new TypeError("non-extension URI passed");
    }

    let cb = this.extensionURILoadCallback;
    return cb ? cb(aURI) : false;
  },

  /*
   * Maps an extension URI to an addon ID.
   *
   * @see nsIAddonPolicyService.extensionURIToAddonId
   */
  extensionURIToAddonId(aURI) {
    if (aURI.scheme != "moz-extension") {
      throw new TypeError("non-extension URI passed");
    }

    let cb = this.extensionURIToAddonIdCallback;
    if (!cb) {
      throw new Error("no callback set to map extension URIs to addon Ids");
    }
    return cb(aURI);
  },

  /*
   * Sets the callbacks used in addonHasPermission above. Not accessible over
   * XPCOM - callers should use .wrappedJSObject on the service to call it
   * directly.
   */
  setAddonHasPermissionCallback(aAddonId, aCallback) {
    if (aCallback) {
      this.checkHasPermissionCallbacks.set(aAddonId, aCallback);
    } else {
      this.checkHasPermissionCallbacks.delete(aAddonId);
    }
  },

  /*
   * Sets the callbacks used in addonMayLoadURI above. Not accessible over
   * XPCOM - callers should use .wrappedJSObject on the service to call it
   * directly.
   */
  setAddonLoadURICallback(aAddonId, aCallback) {
    if (aCallback) {
      this.mayLoadURICallbacks.set(aAddonId, aCallback);
    } else {
      this.mayLoadURICallbacks.delete(aAddonId);
    }
  },

  /*
   * Sets the custom CSP string to be used for the add-on. Not accessible over
   * XPCOM - callers should use .wrappedJSObject on the service to call it
   * directly.
   */
  setAddonCSP(aAddonId, aCSPString) {
    if (aCSPString) {
      this.cspStrings.set(aAddonId, aCSPString);
    } else {
      this.cspStrings.delete(aAddonId);
    }
  },

  /**
   * Set the callback that generates a data-URL for the background page.
   */
  setBackgroundPageUrlCallback(aAddonId, aCallback) {
    if (aCallback) {
      this.backgroundPageUrlCallbacks.set(aAddonId, aCallback);
    } else {
      this.backgroundPageUrlCallbacks.delete(aAddonId);
    }
  },

  /*
   * Sets the callbacks used by the stream converter service to localize
   * add-on resources.
   */
  setAddonLocalizeCallback(aAddonId, aCallback) {
    if (aCallback) {
      this.localizeCallbacks.set(aAddonId, aCallback);
    } else {
      this.localizeCallbacks.delete(aAddonId);
    }
  },

  /*
   * Sets the callback used in extensionURILoadableByAnyone above. Not
   * accessible over XPCOM - callers should use .wrappedJSObject on the
   * service to call it directly.
   */
  setExtensionURILoadCallback(aCallback) {
    var old = this.extensionURILoadCallback;
    this.extensionURILoadCallback = aCallback;
    return old;
  },

  /*
   * Sets the callback used in extensionURIToAddonId above. Not accessible over
   * XPCOM - callers should use .wrappedJSObject on the service to call it
   * directly.
   */
  setExtensionURIToAddonIdCallback(aCallback) {
    var old = this.extensionURIToAddonIdCallback;
    this.extensionURIToAddonIdCallback = aCallback;
    return old;
  }
};

/*
 * This class provides a stream filter for locale messages in CSS files served
 * by the moz-extension: protocol handler.
 *
 * See SubstituteChannel in netwerk/protocol/res/ExtensionProtocolHandler.cpp
 * for usage.
 */
function AddonLocalizationConverter()
{
  this.aps = Cc["@mozilla.org/addons/policy-service;1"].getService(Ci.nsIAddonPolicyService)
    .wrappedJSObject;
}

AddonLocalizationConverter.prototype = {
  classID: Components.ID("{ded150e3-c92e-4077-a396-0dba9953e39f}"),
  QueryInterface: XPCOMUtils.generateQI([Ci.nsIStreamConverter]),

  FROM_TYPE: "application/vnd.mozilla.webext.unlocalized",
  TO_TYPE: "text/css",

  checkTypes(aFromType, aToType) {
    if (aFromType != this.FROM_TYPE) {
      throw Components.Exception("Invalid aFromType value", Cr.NS_ERROR_INVALID_ARG,
                                 Components.stack.caller.caller);
    }
    if (aToType != this.TO_TYPE) {
      throw Components.Exception("Invalid aToType value", Cr.NS_ERROR_INVALID_ARG,
                                 Components.stack.caller.caller);
    }
  },

  // aContext must be a nsIURI object for a valid moz-extension: URL.
  getAddonId(aContext) {
    // In this case, we want the add-on ID even if the URL is web accessible,
    // so check the root rather than the exact path.
    let uri = Services.io.newURI("/", null, aContext);

    let id = this.aps.extensionURIToAddonId(uri);
    if (id == undefined) {
      throw new Components.Exception("Invalid context", Cr.NS_ERROR_INVALID_ARG);
    }
    return id;
  },

  convertToStream(aAddonId, aString) {
    let stream = Cc["@mozilla.org/io/string-input-stream;1"]
      .createInstance(Ci.nsIStringInputStream);

    stream.data = this.aps.localizeAddonString(aAddonId, aString);
    return stream;
  },

  convert(aStream, aFromType, aToType, aContext) {
    this.checkTypes(aFromType, aToType);
    let addonId = this.getAddonId(aContext);

    let string = (
      aStream.available() ?
      NetUtil.readInputStreamToString(aStream, aStream.available()): ""
    );
    return this.convertToStream(addonId, string);
  },

  asyncConvertData(aFromType, aToType, aListener, aContext) {
    this.checkTypes(aFromType, aToType);
    this.addonId = this.getAddonId(aContext);
    this.listener = aListener;
  },

  onStartRequest(aRequest, aContext) {
    this.parts = [];
  },

  onDataAvailable(aRequest, aContext, aInputStream, aOffset, aCount) {
    this.parts.push(NetUtil.readInputStreamToString(aInputStream, aCount));
  },

  onStopRequest(aRequest, aContext, aStatusCode) {
    try {
      this.listener.onStartRequest(aRequest, null);
      if (Components.isSuccessCode(aStatusCode)) {
        let string = this.parts.join("");
        let stream = this.convertToStream(this.addonId, string);

        this.listener.onDataAvailable(aRequest, null, stream, 0, stream.data.length);
      }
    } catch (e) {
      aStatusCode = e.result || Cr.NS_ERROR_FAILURE;
    }
    this.listener.onStopRequest(aRequest, null, aStatusCode);
  },
};

this.NSGetFactory = XPCOMUtils.generateNSGetFactory([AddonPolicyService,
                                                     AddonLocalizationConverter]);