File: application.js

package info (click to toggle)
conkeror 0.9.2%2Bgit100804-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 1,680 kB
  • ctags: 1,182
  • sloc: sh: 547; ansic: 272; xml: 107; makefile: 79
file content (300 lines) | stat: -rw-r--r-- 10,924 bytes parent folder | download
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
/**
 * (C) Copyright 2007,2010 John J. Foerch
 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");

function application () {
    Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", this);

    this.wrappedJSObject = this;
    this.conkeror = this;

    this.load_url = this.subscript_loader.loadSubScript;
    this.loading_urls = [];
    this.loading_paths = [];
    this.loading_modules = [];
    this.loading_features = [];
    this.features = {};
    this.load_paths = [this.module_uri_prefix,
                       this.module_uri_prefix+'extensions',
                       this.module_uri_prefix+'page-modes'];
    this.after_load_functions = {};
    this.pending_loads = [];

    this.module_assert_conflict_error.prototype = Error.prototype;

    try {
        this.require("conkeror.js", null);
    } catch (e) {
        this.dumpln("Error initializing.");
        this.dump_error(e);
    }
}
application.prototype = {
    constructor: application,
    Cc: Cc,
    Ci: Ci,
    Cr: Cr,
    /* Note: resource://app currently doesn't result in xpcnativewrappers=yes */
    module_uri_prefix: "chrome://conkeror/content/",
    subscript_loader: Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader),
    preferences: Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService),
    get version () {
        var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"]
            .getService(Ci.nsIURLFormatter);
        return formatter.formatURL("%VERSION%");
    },
    dumpln: function (str) {
        dump(str);
        dump("\n");
    },
    dump_error: function (e) {
        if (e instanceof Error) {
            this.dumpln(e.name + ": " + e.message);
            this.dumpln(e.fileName + ":" + e.lineNumber);
            dump(e.stack);
        } else if (e instanceof Ci.nsIException) {
            this.dumpln(e.name + ": " + e.message);
            var stack_frame = e.location;
            while (stack_frame) {
                this.dumpln(stack_frame.name + "()@" + stack_frame.filename + ":" + stack_frame.lineNumber);
                stack_frame = stack_frame.caller;
            }
        } else {
            this.dumpln("Error: " + e);
        }
    },

    make_uri: function (uri, charset, base_uri) {
        const io_service = Cc["@mozilla.org/network/io-service;1"]
            .getService(Ci.nsIIOService2);
        if (uri instanceof Ci.nsIURI)
            return uri;
        if (uri instanceof Ci.nsIFile)
            return io_service.newFileURI(uri);
        return io_service.newURI(uri, charset, base_uri);
    },
    skip_module_load: {},
    load: function (module, as) {
        var conkeror = this;
        function module_scope () {
            this.__proto__ = conkeror;
            this.conkeror = conkeror;
        }
        function load1 (url, scope, path, as) {
            var success;
            try {
                this.loading_paths.unshift(path);
                this.loading_urls.unshift(url);
                this.loading_modules.unshift(as);
                this.loading_features.unshift({});
                if (this.loading_urls.indexOf(url, 1) != -1)
                    throw new Error("Circular module dependency detected: "+
                                    this.loading_urls.join(",\n"));
                this.load_url(url, scope);
                success = true;
                if (as)
                    this[as] = scope;
                // call-after-load callbacks
                for (let f in this.loading_features[0]) {
                    this.features[f] = true;
                    this.run_after_load_functions(f);
                }
            } finally {
                this.loading_paths.shift();
                this.loading_urls.shift();
                this.loading_modules.shift();
                this.loading_features.shift();
            }
            // do pending loads
            if (success && this.loading_urls[0] === undefined) {
                let pending = this.pending_loads;
                this.pending_loads = [];
                for (let i = 0, m; m = pending[i]; ++i) {
                    this.require(m[0], m[1]);
                }
            }
        }
        var scope = as;
        if (as == null)
            scope = this;
        else if (typeof as == 'string')
            scope = new module_scope();
        else
            as = null;
        var path, url;
        if (module instanceof Ci.nsIURI)
            path = module.path.substr(0, module.path.lastIndexOf('/')+1);
        else if (module instanceof Ci.nsIFile)
            path = module.parent.path;
        var restarted = false;
        if (path !== undefined) {
            url = this.make_uri(module).spec;
            do {
                try {
                    load1.call(this, url, scope, path, as);
                    return;
                } catch (e if e instanceof this.module_assert_error) {
                    if (restarted)
                        throw new this.module_assert_conflict_error(url);
                    as = e.module;
                    if (e.module)
                        scope = new module_scope();
                    else
                        scope = this;
                    restarted = true;
                } catch (e if e == this.skip_module_load) {
                    return;
                }
            } while (restarted);
        } else {
            // module name or relative path
            var autoext = module.substr(-3) != '.js';
            var suffix = false;
            var i = -1;
            var tried = {};
            path = this.loading_paths[0];
            if (path === undefined)
                path = this.load_paths[++i];
            while (path !== undefined) {
                let opath = path;
                try {
                    let sep = path[path.length-1] == '/' ? '' : '/';
                    url = path + sep + module + (suffix ? '.js' : '');
                    let si = module.lastIndexOf('/');
                    if (si > -1)
                        path += module.substr(0, si);
                    if (! tried[url] || tried[url] !== scope) {
                        tried[url] = scope;
                        load1.call(this, url, scope, path, as);
                        return;
                    }
                } catch (e if e instanceof this.module_assert_error) {
                    if (restarted)
                        throw new this.module_assert_conflict_error(url);
                    as = e.module;
                    if (e.module)
                        scope = new module_scope();
                    else
                        scope = this;
                    path = opath;
                    restarted = true;
                    continue;
                } catch (e if (typeof e == 'string' &&
                               {"ContentLength not available (not a local URL?)":true,
                                "Error creating channel (invalid URL scheme?)":true,
                                "Error opening input stream (invalid filename?)":true}
                               [e])) {
                    // null op. (suppress error, try next path)
                } catch (e if e == this.skip_module_load) {
                    return;
                }
                if (autoext)
                    suffix = !suffix;
                if (! suffix)
                    path = this.load_paths[++i];
            }
            throw new Error("Module not found");
        }
    },
    module_assert_error: function (module) {
        this.module = module;
    },
    module_assert_conflict_error: function (url) { //subclass of Error
        this.name = "module_assert_conflict_error";
        this.message = "Conflicting in_module calls";
        this.fileName = url;
        this.lineNumber = '';
    },
    in_module: function (module) {
        if (module != this.loading_modules[0])
            throw new this.module_assert_error(module);
    },
    provide: function (symbol) {
        if (! symbol)
            throw new Error("Cannot provide null feature");
        if (this.loading_urls[0] === undefined) {
            this.features[symbol] = true;
            this.run_after_load_functions(symbol);
        } else
            this.loading_features[0][symbol] = true;
    },
    featurep: function (symbol) {
        return this.features[symbol] || false;
    },
    call_after_load: function (feature, func) {
        if (this.featurep(feature))
            func();
        else {
            var funcs = this.after_load_functions[feature];
            if (! funcs)
                funcs = this.after_load_functions[feature] = [];
            funcs.push(func);
        }
    },
    run_after_load_functions: function (symbol) {
        var funcs = this.after_load_functions[symbol];
        if (funcs) {
            delete this.after_load_functions[symbol];
            for (var i = 0; funcs[i]; ++i) {
                try {
                    funcs[i]();
                } catch (e) {
                    dump_error(e);
                }
            }
        }
    },
    require: function (module, as) {
        var feature = as;
        if (! feature) {
            if (module instanceof Ci.nsIURI)
                feature = module.spec.substr(module.spec.lastIndexOf('/')+1);
            else if (module instanceof Ci.nsIFile)
                feature = module.leafName;
            else
                feature = module.substr(module.lastIndexOf('/')+1);
            let dot = feature.indexOf('.');
            if (dot == 0)
                return false;
            if (dot > 0)
                feature = feature.substr(0, dot);
            feature = feature.replace('_', '-', 'g');
        }
        if (this.featurep(feature))
            return true;
        if (as === undefined)
            as = feature.replace('-', '_', 'g');
        try {
            // ensure current path is not searched for 'require'
            this.loading_paths.unshift(undefined);
            this.load(module, as);
        } finally {
            this.loading_paths.shift();
        }
        return true;
    },
    require_later: function (module, as) {
        this.pending_loads.push([module, as]);
    },

    /* nsISupports */
    QueryInterface: XPCOMUtils.generateQI([]),

    /* XPCOM registration */
    classDescription: "Conkeror global object",
    classID: Components.ID("{72a7eea7-a894-47ec-93a9-a7bc172cf1ac}"),
    contractID: "@conkeror.mozdev.org/application;1"
};

function NSGetModule (compMgr, fileSpec) {
    return XPCOMUtils.generateModule([application]);
}