application.js: reorganizing
[conkeror.git] / components / application.js
blobff3577c05efe7ec24abfbf4e318403ac6ecbe737
1 /**
2  * (C) Copyright 2007 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.loaded_modules = [];
21     this.loading_modules = [];
22     this.module_after_load_functions = new Object();
23     this.pending_loads = [];
24     this.load_paths = [this.module_uri_prefix];
26     try {
27         this.require("conkeror.js");
28     } catch (e) {
29         this.dumpln("Error initializing.");
30         this.dump_error(e);
31     }
33 application.prototype = {
34     Cc: Cc,
35     Ci: Ci,
36     Cr: Cr,
37     /* Note: resource://app currently doesn't result in xpcnativewrappers=yes */
38     module_uri_prefix: "chrome://conkeror/content/",
39     subscript_loader: Cc["@mozilla.org/moz/jssubscript-loader;1"].getService(Ci.mozIJSSubScriptLoader),
40     preferences: Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService),
41     get version () {
42         var formatter = Cc["@mozilla.org/toolkit/URLFormatterService;1"]
43             .getService(Ci.nsIURLFormatter);
44         return formatter.formatURL("%VERSION%");
45     },
46     dumpln: function (str) {
47         dump(str);
48         dump("\n");
49     },
50     dump_error: function (e) {
51         if (e instanceof Error) {
52             this.dumpln(e.name + ": " + e.message);
53             this.dumpln(e.fileName + ":" + e.lineNumber);
54             dump(e.stack);
55         } else if (e instanceof Ci.nsIException) {
56             this.dumpln(e.name + ": " + e.message);
57             var stack_frame = e.location;
58             while (stack_frame) {
59                 this.dumpln(stack_frame.name + "()@" + stack_frame.filename + ":" + stack_frame.lineNumber);
60                 stack_frame = stack_frame.caller;
61             }
62         } else {
63             this.dumpln("Error: " + e);
64         }
65     },
66     loaded: function (module) {
67         return (this.loaded_modules.indexOf(module) != -1);
68     },
69     provide: function(module) {
70         if (this.loaded_modules.indexOf(module) == -1)
71             this.loaded_modules.push(module);
72     },
73     skip_module_load : {},
74     load_module: function(module_name) {
75         if (this.loading_modules.indexOf(module_name) != -1)
76             throw new Error("Circular module dependency detected: "
77                             + this.loading_modules.join(" -> ") + " -> " + module_name);
78         this.loading_modules.push(module_name);
79         try {
80             let j = 0;
81             while (true) {
82                 if (j >= this.load_paths.length)
83                     throw new Error("Module not found: " + module_name);
84                 try {
85                     this.subscript_loader.loadSubScript(this.load_paths[j] + module_name,
86                                                         this);
87                     this.provide(module_name);
88                     let funcs;
89                     if ((funcs = this.module_after_load_functions[module_name]) != null)
90                     {
91                         for (let i = 0; i < funcs.length; ++i)
92                             funcs[i]();
93                         delete this.module_after_load_functions[module_name];
94                     }
95                 }
96                 catch (e if e == this.skip_module_load) {}
97                 catch (e) {
98                     if (!(e instanceof Error) && !(e instanceof Ci.nsIException) &&
99                         (String(e) == "ContentLength not available (not a local URL?)" ||
100                          String(e) == "Error creating channel (invalid URL scheme?)" ||
101                          String(e) == "Error opening input stream (invalid filename?)")) {
102                         ++j;
103                         continue;
104                     }
105                     throw e;
106                 }
107                 break;
108             }
109         }
110         finally {
111             this.loading_modules.pop();
112         }
114         if (this.loading_modules.length == 0)
115         {
116             while (this.pending_loads.length > 0)
117             {
118                 this.require(this.pending_loads.pop());
119             }
120         }
121     },
122     require: function (module) {
123         if (!this.loaded(module))
124             this.load_module(module);
125     },
126     require_later: function (module) {
127         if (!this.loaded(module)
128             && this.pending_loads.indexOf(module) == -1)
129             this.pending_loads.push(module);
130     },
131     call_after_load: function (module, func) {
132         if (this.loaded(module))
133             func();
134         else
135         {
136             var funcs;
137             if (!(funcs = this.module_after_load_functions[module]))
138                 funcs = this.module_after_load_functions[module] = [];
139             funcs.push(func);
140         }
141     },
143     /* nsISupports */
144     QueryInterface: XPCOMUtils.generateQI([]),
146     /* XPCOM registration */
147     classDescription: "Conkeror global object",
148     classID: Components.ID("{72a7eea7-a894-47ec-93a9-a7bc172cf1ac}"),
149     contractID: "@conkeror.mozdev.org/application;1"
152 function NSGetModule (compMgr, fileSpec) {
153     return XPCOMUtils.generateModule([application]);