Debian package: Update debian/{changelog,NEWS}
[conkeror/arlinius.git] / components / application.js
blob962ca7328308b23b4c7a6bdaaabb209727345b5a
1 /**
2  * (C) Copyright 2007,2010,2012 John J. Foerch
3  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 const Cc = Components.classes;
10 const Ci = Components.interfaces;
11 const Cr = Components.results;
12 Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
14 function application () {
15     Components.utils.import("resource://gre/modules/XPCOMUtils.jsm", this);
17     this.wrappedJSObject = this;
18     this.conkeror = this;
20     this.load_url = this.subscript_loader.loadSubScript;
21     this.loading_urls = [];
22     this.loading_paths = [];
23     this.loading_features = [];
24     this.features = {};
25     this.load_paths = [this.module_uri_prefix,
26                        this.module_uri_prefix+'extensions',
27                        this.module_uri_prefix+'page-modes'];
28     this.after_load_functions = {};
29     this.pending_loads = [];
31     // clear the startup-cache so that modules and the user's rc are
32     // loaded from disk, not from a cache.  this problem is a
33     // characteristic of using mozIJSSubScriptLoader.loadSubScript as our
34     // primary means of loading, since XULRunner 8.0.
35     var obs = Cc["@mozilla.org/observer-service;1"]
36         .getService(Ci.nsIObserverService);
37     obs.notifyObservers(null, "startupcache-invalidate", null);
39     try {
40         this.require("conkeror.js");
41     } catch (e) {
42         this.dumpln("Error initializing.");
43         this.dump_error(e);
44     }
46 application.prototype = {
47     constructor: application,
48     Cc: Cc,
49     Ci: Ci,
50     Cr: Cr,
51     /* Note: resource://app currently doesn't result in xpcnativewrappers=yes */
52     module_uri_prefix: "chrome://conkeror/content/",
53     subscript_loader: Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader),
54     preferences: Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService),
55     get version () {
56         var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"]
57             .getService(Ci.nsIURLFormatter);
58         return formatter.formatURL("%VERSION%");
59     },
60     dumpln: function (str) {
61         dump(str);
62         dump("\n");
63     },
64     dump_error: function (e) {
65         if (e instanceof Error) {
66             this.dumpln(e.name + ": " + e.message);
67             this.dumpln(e.fileName + ":" + e.lineNumber);
68             dump(e.stack);
69         } else if (e instanceof Ci.nsIException) {
70             this.dumpln(e.name + ": " + e.message);
71             var stack_frame = e.location;
72             while (stack_frame) {
73                 this.dumpln(stack_frame.name + "()@" + stack_frame.filename + ":" + stack_frame.lineNumber);
74                 stack_frame = stack_frame.caller;
75             }
76         } else {
77             this.dumpln("Error: " + e);
78         }
79     },
81     make_uri: function (uri, charset, base_uri) {
82         const io_service = Cc["@mozilla.org/network/io-service;1"]
83             .getService(Ci.nsIIOService2);
84         if (uri instanceof Ci.nsIURI)
85             return uri;
86         if (uri instanceof Ci.nsIFile)
87             return io_service.newFileURI(uri);
88         return io_service.newURI(uri, charset, base_uri);
89     },
90     load: function (module) {
91         function load1 (url, path) {
92             try {
93                 this.loading_paths.unshift(path);
94                 this.loading_urls.unshift(url);
95                 this.loading_features.unshift({});
96                 if (this.loading_urls.indexOf(url, 1) != -1)
97                     throw new Error("Circular module dependency detected: "+
98                                     this.loading_urls.join(",\n"));
99                 if (url.substr(-4) == ".jsx") {
100                     var scopename = url.substr(url.lastIndexOf('/')+1)
101                         .replace('-', '_', 'g');
102                     var dot = scopename.indexOf(".");
103                     if (dot > -1)
104                         scopename = scopename.substr(0, dot);
105                     var scope = { __proto__: this };
106                 } else
107                     scope = this;
108                 this.load_url(url, scope);
109                 if (scopename)
110                     this[scopename] = scope;
111                 var success = true;
112                 // call-after-load callbacks
113                 for (let f in this.loading_features[0]) {
114                     this.features[f] = true;
115                     this.run_after_load_functions(f);
116                 }
117             } finally {
118                 this.loading_paths.shift();
119                 this.loading_urls.shift();
120                 this.loading_features.shift();
121             }
122             // do pending loads
123             if (success && this.loading_urls[0] === undefined) {
124                 let pending = this.pending_loads;
125                 this.pending_loads = [];
126                 for (let i = 0, m; m = pending[i]; ++i) {
127                     this.require(m);
128                 }
129             }
130         }
131         if (module instanceof Ci.nsIURI)
132             var path = module.spec.substr(0, module.spec.lastIndexOf('/')+1);
133         else if (module instanceof Ci.nsIFile)
134             path = module.parent.path;
135         if (path !== undefined) {
136             var url = this.make_uri(module).spec;
137             load1.call(this.conkeror, url, path);
138         } else {
139             // module name or relative path
140             var si = module.lastIndexOf('/');
141             var module_leaf = module.substr(si+1);
142             var autoext = module_leaf.lastIndexOf(".") <= 0;
143             var exts = { 0:"", 1:".js", 2:".jsx", len:3 };
144             var exti = 0;
145             var i = -1;
146             var tried = {};
147             path = this.loading_paths[0];
148             if (path === undefined)
149                 path = this.load_paths[++i];
150             while (path !== undefined) {
151                 var truepath = path;
152                 var sep = path.substr(-1) == '/' ? '' : '/';
153                 var ext = exts[exti];
154                 try {
155                     url = path + sep + module + ext;
156                     if (si > -1)
157                         truepath += sep + module.substr(0, si);
158                     if (! tried[url]) {
159                         tried[url] = true;
160                         load1.call(this.conkeror, url, truepath);
161                         return;
162                     }
163                 } catch (e if (typeof e == 'string' &&
164                                {"ContentLength not available (not a local URL?)":true,
165                                 "Error creating channel (invalid URL scheme?)":true,
166                                 "Error opening input stream (invalid filename?)":true}
167                                [e])) {
168                     // null op. (suppress error, try next path)
169                 }
170                 if (autoext)
171                     exti = (exti + 1) % exts.len;
172                 if (exti == 0)
173                     path = this.load_paths[++i];
174             }
175             throw new Error("Module not found ("+module+")");
176         }
177     },
178     provide: function (symbol) {
179         if (! symbol)
180             throw new Error("Cannot provide null feature");
181         if (this.loading_urls[0] === undefined) {
182             this.features[symbol] = true;
183             this.run_after_load_functions(symbol);
184         } else
185             this.loading_features[0][symbol] = true;
186     },
187     featurep: function (symbol) {
188         return this.features[symbol] || false;
189     },
190     call_after_load: function (feature, func) {
191         if (this.featurep(feature))
192             func();
193         else {
194             var funcs = this.after_load_functions[feature];
195             if (! funcs)
196                 funcs = this.after_load_functions[feature] = [];
197             funcs.push(func);
198         }
199     },
200     run_after_load_functions: function (symbol) {
201         var funcs = this.after_load_functions[symbol];
202         if (funcs) {
203             delete this.after_load_functions[symbol];
204             for (var i = 0; funcs[i]; ++i) {
205                 try {
206                     funcs[i]();
207                 } catch (e) {
208                     this.dump_error(e);
209                 }
210             }
211         }
212     },
213     require: function (module) {
214         if (module instanceof Ci.nsIURI)
215             var feature = module.spec.substr(module.spec.lastIndexOf('/')+1);
216         else if (module instanceof Ci.nsIFile)
217             feature = module.leafName;
218         else
219             feature = module.substr(module.lastIndexOf('/')+1);
220         var dot = feature.indexOf('.');
221         if (dot == 0)
222             return false;
223         if (dot > 0)
224             feature = feature.substr(0, dot);
225         feature = feature.replace('_', '-', 'g');
226         if (this.featurep(feature))
227             return true;
228         try {
229             // ensure current path is not searched for 'require'
230             this.loading_paths.unshift(undefined);
231             this.load(module);
232         } finally {
233             this.loading_paths.shift();
234         }
235         return true;
236     },
237     require_later: function (module) {
238         this.pending_loads.push(module);
239     },
241     /* nsISupports */
242     QueryInterface: XPCOMUtils.generateQI([]),
244     /* XPCOM registration */
245     classDescription: "Conkeror global object",
246     classID: Components.ID("{72a7eea7-a894-47ec-93a9-a7bc172cf1ac}"),
247     contractID: "@conkeror.mozdev.org/application;1"
250 if (XPCOMUtils.generateNSGetFactory)
251     var NSGetFactory = XPCOMUtils.generateNSGetFactory([application]); //XULRunner 2.0
252 else
253     var NSGetModule = XPCOMUtils.generateNSGetModule([application]);