Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Change implementation of self module.

  • Loading branch information...
commit c29fcfe8d7f5ec034ed5adea3672cae8409d8533 1 parent 3db919f
Irakli Gozalishvili authored August 19, 2011

Showing 1 changed file with 99 additions and 0 deletions. Show diff stats Hide diff stats

  1. 99  packages/api-utils/lib/self!.js
99  packages/api-utils/lib/self!.js
... ...
@@ -0,0 +1,99 @@
  1
+/* vim:st=2:sts=2:sw=2:
  2
+ * ***** BEGIN LICENSE BLOCK *****
  3
+ * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4
+ *
  5
+ * The contents of this file are subject to the Mozilla Public License Version
  6
+ * 1.1 (the "License"); you may not use this file except in compliance with
  7
+ * the License. You may obtain a copy of the License at
  8
+ * http://www.mozilla.org/MPL/
  9
+ *
  10
+ * Software distributed under the License is distributed on an "AS IS" basis,
  11
+ * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12
+ * for the specific language governing rights and limitations under the
  13
+ * License.
  14
+ *
  15
+ * The Original Code is Jetpack.
  16
+ *
  17
+ * The Initial Developer of the Original Code is Mozilla.
  18
+ * Portions created by the Initial Developer are Copyright (C) 2011
  19
+ * the Initial Developer. All Rights Reserved.
  20
+ *
  21
+ * Contributor(s):
  22
+ *   Brian Warner <warner@mozilla.com>
  23
+ *   Erik Vold <erikvvold@gmail.com>
  24
+ *   Irakli Gozalishvili <gozala@mozilla.com>
  25
+ *
  26
+ * Alternatively, the contents of this file may be used under the terms of
  27
+ * either the GNU General Public License Version 2 or later (the "GPL"), or
  28
+ * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29
+ * in which case the provisions of the GPL or the LGPL are applicable instead
  30
+ * of those above. If you wish to allow use of your version of this file only
  31
+ * under the terms of either the GPL or the LGPL, and not to allow others to
  32
+ * use your version of this file under the terms of the MPL, indicate your
  33
+ * decision by deleting the provisions above and replace them with the notice
  34
+ * and other provisions required by the GPL or the LGPL. If you do not delete
  35
+ * the provisions above, a recipient may use your version of this file under
  36
+ * the terms of any one of the MPL, the GPL or the LGPL.
  37
+ *
  38
+ * ***** END LICENSE BLOCK ***** */
  39
+
  40
+"use strict";
  41
+
  42
+const { CC } = require('chrome');
  43
+const { jetpackID, name, manifest } = require('@packaging');
  44
+
  45
+const XMLHttpRequest = CC('@mozilla.org/xmlextras/xmlhttprequest;1',
  46
+                          'nsIXMLHttpRequest');
  47
+
  48
+// Utility function that synchronously reads local resource from the given
  49
+// `uri` and returns content string.
  50
+function readURI(uri) {
  51
+  let request = XMLHttpRequest();
  52
+  request.open('GET', uri, false);
  53
+  request.overrideMimeType('text/plain');
  54
+  request.send();
  55
+  return request.responseText;
  56
+}
  57
+
  58
+
  59
+// Some XPCOM APIs require valid URIs as an argument for certain operations (see
  60
+// `nsILoginManager` for example). This property represents add-on associated
  61
+// unique URI string that can be used for that.
  62
+const uri = 'addon:' + jetpackID
  63
+
  64
+function url(root, path) root + (path || "")
  65
+function read(root, path) readURI(url(root, path))
  66
+
  67
+exports.create = function create(base) {
  68
+  let moduleData = manifest[base] && manifest[base].requirements['self'];
  69
+
  70
+  if (!moduleData) {
  71
+    // we don't know where you live, so we must search for your data
  72
+    // resource://api-utils-api-utils-tests/test-self.js
  73
+    // make a prefix of resource://api-utils-api-utils-data/
  74
+    let doubleslash = base.indexOf('//');
  75
+    let prefix = base.slice(0, doubleslash + 2);
  76
+    let rest = base.slice(doubleslash + 2);
  77
+    let slash = rest.indexOf('/');
  78
+    prefix = prefix + rest.slice(0, slash);
  79
+    prefix = prefix.slice(0, prefix.lastIndexOf('-')) + '-data/';
  80
+    moduleData = { dataURIPrefix: prefix };
  81
+    // moduleData also wants mapName and mapSHA256, but they're
  82
+    // currently unused
  83
+  }
  84
+
  85
+  // a module loaded from URI has called require(MODULE)
  86
+  // URI is like resource://jid0-$JID/$PACKAGE-$SECTION/$SUBDIR/$FILENAME
  87
+  // resource://jid0-abc123/reading-data-lib/main.js
  88
+  // and we want resource://jid0-abc123/reading-data-data/
  89
+
  90
+  return Object.freeze({
  91
+    id: 'self',
  92
+    exports: Object.freeze({
  93
+      id: jetpackID,
  94
+      uri: uri,
  95
+      url: url.bind(null, moduleData.dataURIPrefix),
  96
+      load: read.bind(null, moduleData.dataURIPrefix)
  97
+    })
  98
+  });
  99
+};

0 notes on commit c29fcfe

Please sign in to comment.
Something went wrong with that request. Please try again.