aboutsummaryrefslogtreecommitdiff
path: root/tools/addon-sdk-1.7/python-lib/cuddlefish/app-extension/bootstrap.js
blob: 661f2bbda56ae3c59df48eb303207bdc10b515e2 (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
/* vim:set ts=2 sw=2 sts=2 expandtab */
/* 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/. */

// @see http://mxr.mozilla.org/mozilla-central/source/js/src/xpconnect/loader/mozJSComponentLoader.cpp

"use strict";

const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu,
        results: Cr, manager: Cm } = Components;
const ioService = Cc['@mozilla.org/network/io-service;1'].
                  getService(Ci.nsIIOService);
const resourceHandler = ioService.getProtocolHandler('resource')
                        .QueryInterface(Ci.nsIResProtocolHandler);
const XMLHttpRequest = CC('@mozilla.org/xmlextras/xmlhttprequest;1',
                          'nsIXMLHttpRequest');
const prefs = Cc["@mozilla.org/preferences-service;1"].
              getService(Ci.nsIPrefService).
              QueryInterface(Ci.nsIPrefBranch2);
const mozIJSSubScriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"].
                            getService(Ci.mozIJSSubScriptLoader);

const REASON = [ 'unknown', 'startup', 'shutdown', 'enable', 'disable',
                 'install', 'uninstall', 'upgrade', 'downgrade' ];

let loader = null;
let loaderUri = null;

const URI = __SCRIPT_URI_SPEC__.replace(/bootstrap\.js$/, "");

// Initializes default preferences
function setDefaultPrefs() {
  let branch = prefs.getDefaultBranch("");
  let prefLoaderScope = {
    pref: function(key, val) {
      switch (typeof val) {
        case "boolean":
          branch.setBoolPref(key, val);
          break;
        case "number":
          if (val % 1 == 0) // number must be a integer, otherwise ignore it
            branch.setIntPref(key, val);
          break;
        case "string":
          branch.setCharPref(key, val);
          break;
      }
    }
  };

  let uri = ioService.newURI(
      "defaults/preferences/prefs.js",
      null,
      ioService.newURI(URI, null, null));

  // if there is a prefs.js file, then import the default prefs
  try {
    // setup default prefs
    mozIJSSubScriptLoader.loadSubScript(uri.spec, prefLoaderScope);
  }
  // errors here should not kill addon
  catch (e) {
    Cu.reportError(e);
  }
}

// Gets the topic that fit best as application startup event, in according with
// the current application (e.g. Firefox, Fennec, Thunderbird...)
function getAppStartupTopic() {
  // The following mapping of application names to GUIDs was taken
  // from `xul-app` module. They should keep in sync, so if you change one,
  // change the other too!
  let ids = {
    Firefox: '{ec8030f7-c20a-464f-9b0e-13a3a9e97384}',
    Mozilla: '{86c18b42-e466-45a9-ae7a-9b95ba6f5640}',
    Sunbird: '{718e30fb-e89b-41dd-9da7-e25a45638b28}',
    SeaMonkey: '{92650c4d-4b8e-4d2a-b7eb-24ecf4f6b63a}',
    Fennec: '{aa3c5121-dab2-40e2-81ca-7ea25febc110}',
    Thunderbird: '{3550f703-e582-4d05-9a08-453d09bdfdc6}'
  };

  let id = Cc['@mozilla.org/xre/app-info;1'].getService(Ci.nsIXULAppInfo).ID;

  switch (id) {
    case ids.Firefox:
    case ids.SeaMonkey:
      return 'sessionstore-windows-restored';
    case ids.Thunderbird:
      return 'mail-startup-done';
    // Temporary, until Fennec Birch will support sessionstore event
    case ids.Fennec:
    default:
      return 'final-ui-startup';
  }
}

// Utility function that synchronously reads local resource from the given
// `uri` and returns content string.
function readURI(uri) {
  let ioservice = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
  let channel = ioservice.newChannel(uri, "UTF-8", null);
  let stream = channel.open();

  let cstream = Cc["@mozilla.org/intl/converter-input-stream;1"].createInstance(Ci.nsIConverterInputStream);  
  cstream.init(stream, "UTF-8", 0, 0);

  let str = {};
  let data = "";
  let read = 0;
  do {
    read = cstream.readString(0xffffffff, str);
    data += str.value;
  } while (read != 0);

  cstream.close();

  return data;
}

// Function takes `topic` to be observer via `nsIObserverService` and returns
// promise that will be delivered once notification is published.
function on(topic) {
  return function promise(deliver) {
    const observerService = Cc['@mozilla.org/observer-service;1'].
                            getService(Ci.nsIObserverService);

    observerService.addObserver({
      observe: function observer(subject, topic, data) {
        observerService.removeObserver(this, topic);
        deliver(subject, topic, data);
      }
    }, topic, false);
  }
}

// We don't do anything on install & uninstall yet, but in a future
// we should allow add-ons to cleanup after uninstall.
function install(data, reason) {}
function uninstall(data, reason) {}

function startup(data, reason) {
  // TODO: When bug 564675 is implemented this will no longer be needed
  // Always set the default prefs, because they disappear on restart
  setDefaultPrefs();

  // TODO: Maybe we should perform read harness-options.json asynchronously,
  // since we can't do anything until 'sessionstore-windows-restored' anyway.
  let options = JSON.parse(readURI(URI + './harness-options.json'));
  options.loadReason = REASON[reason];

  // URI for the root of the XPI file.
  // 'jar:' URI if the addon is packed, 'file:' URI otherwise.
  // (Used by l10n module in order to fetch `locale` folder)
  options.rootURI = data.resourceURI.spec;

  // Register a new resource "domain" for this addon which is mapping to
  // XPI's `resources` folder.
  // Generate the domain name by using jetpack ID, which is the extension ID
  // by stripping common characters that doesn't work as a domain name:
  let uuidRe =
    /^\{([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})\}$/;
  let domain = options.jetpackID.toLowerCase()
                            .replace(/@/g, "-at-")
                            .replace(/\./g, "-dot-")
                            .replace(uuidRe, "$1");

  let resourcesUri = ioService.newURI(URI + '/resources/', null, null);
  resourceHandler.setSubstitution(domain, resourcesUri);
  options.uriPrefix = "resource://" + domain + "/";

  // Import loader module using `Cu.imports` and bootstrap module loader.
  loaderUri = options.uriPrefix + options.loader;
  loader = Cu.import(loaderUri).Loader.new(options);

  // Creating a promise, that will be delivered once application is ready.
  // If application is at startup then promise is delivered on
  // the application startup topic, otherwise it's delivered immediately.
  let promise = reason === APP_STARTUP ? on(getAppStartupTopic()) :
                                         function promise(deliver) deliver()

  // Once application is ready we spawn a new process with main module of
  // on add-on.
  promise(function() {
    try {
      loader.spawn(options.main, options.mainPath);
    } catch (error) {
      // If at this stage we have an error only thing we can do is report about
      // it via error console. Keep in mind that error won't automatically show
      // up there when called via observerService.
      Cu.reportError(error);
      throw error;
    }
  });

};

function shutdown(data, reason) {
  // If loader is already present unload it, since add-on is disabled.
  if (loader) {
    reason = REASON[reason];
    let system = loader.require('api-utils/system');
    loader.unload(reason);

    // Bug 724433: We need to unload JSM otherwise it will stay alive
    // and keep a reference to this compartment.
    Cu.unload(loaderUri);
    loader = null;

    // If add-on is lunched via `cfx run` we need to use `system.exit` to let
    // cfx know we're done (`cfx test` will take care of exit so we don't do
    // anything here).
    if (system.env.CFX_COMMAND === 'run' && reason === 'shutdown')
      system.exit(0);
  }
};