~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/xmlterm/ui/xmlterm-service.js

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is mozilla.org code.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is Netscape
 
15
 * Communications Corporation.  Portions created by Netscape are
 
16
 * Copyright (C) 1999 Netscape Communications Corporation.  All
 
17
 * Rights Reserved.
 
18
 * 
 
19
 * Contributor(s): 
 
20
 * Seth Spitzer <sspitzer@netscape.com>
 
21
 * Robert Ginda <rginda@netscape.com>
 
22
 * R. Saravanan <svn@xmlterm.org>
 
23
 */
 
24
 
 
25
/*
 
26
 * This file contains the following xmlterm related components:
 
27
 * 1. Command line handler service, for responding to the -terminal command line
 
28
 *    option. (XMLTermCLineHandler)
 
29
 * 2. Content handler for responding to content of type x-application-terminal
 
30
 *    (XMLTermContentHandler)
 
31
 * 3. Protocol handler for supplying a channel to the browser when an terminal://
 
32
 *    link is clicked. (XMLTermProtocolHandler)
 
33
 * 4. A (nearly empty) imeplementation of nsIChannel for telling the browser
 
34
 *    that terminal:// links have the content type x-application-terminal (BogusChannel)
 
35
 */
 
36
 
 
37
/* components defined in this file */
 
38
const XMLTERMCLINE_SERVICE_CONTRACTID =
 
39
    "@mozilla.org/commandlinehandler/general-startup;1?type=terminal";
 
40
const XMLTERMCLINE_SERVICE_CID =
 
41
    Components.ID("{0eb82bE0-43a2-11d3-8e76-006008948af5}");
 
42
const XMLTERMCNT_HANDLER_CONTRACTID =
 
43
    "@mozilla.org/uriloader/content-handler;1?type=x-application-terminal";
 
44
const XMLTERMCNT_HANDLER_CID =
 
45
    Components.ID("{0eb82bE1-43a2-11d3-8e76-006008948af5}");
 
46
const XMLTERMPROT_HANDLER_CONTRACTID =
 
47
    "@mozilla.org/network/protocol;1?name=terminal";
 
48
const XMLTERMPROT_HANDLER_CID =
 
49
    Components.ID("{0eb82bE2-43a2-11d3-8e76-006008948af5}");
 
50
 
 
51
/* components used in this file */
 
52
const MEDIATOR_CONTRACTID =
 
53
    "@mozilla.org/appshell/window-mediator;1"
 
54
const SIMPLEURI_CONTRACTID = 
 
55
    "@mozilla.org/network/simple-uri;1";
 
56
const ASS_CONTRACTID =
 
57
    "@mozilla.org/appshell/appShellService;1";
 
58
const SCRIPTSECURITYMANAGER_CONTRACTID =
 
59
    "@mozilla.org/scriptsecuritymanager;1";
 
60
const NS_IOSERVICE_CID_STR =
 
61
    "{9ac9e770-18bc-11d3-9337-00104ba0fd40}";
 
62
 
 
63
/* interfaces used in this file */
 
64
const nsIWindowMediator  = Components.interfaces.nsIWindowMediator;
 
65
const nsICmdLineHandler  = Components.interfaces.nsICmdLineHandler;
 
66
const nsICategoryManager = Components.interfaces.nsICategoryManager;
 
67
const nsIContentHandler  = Components.interfaces.nsIContentHandler;
 
68
const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler;
 
69
const nsIURI             = Components.interfaces.nsIURI;
 
70
const nsIChannel         = Components.interfaces.nsIChannel;
 
71
const nsIRequest         = Components.interfaces.nsIRequest;
 
72
const nsIAppShellService = Components.interfaces.nsIAppShellService;
 
73
const nsIIOService       = Components.interfaces.nsIIOService;
 
74
const nsIScriptSecurityManager = Components.interfaces.nsIScriptSecurityManager;
 
75
const nsISupports        = Components.interfaces.nsISupports;
 
76
 
 
77
/* Command Line handler service */
 
78
function XMLTermCLineService()
 
79
{}
 
80
 
 
81
XMLTermCLineService.prototype.commandLineArgument = "-terminal";
 
82
XMLTermCLineService.prototype.prefNameForStartup = "general.startup.terminal";
 
83
XMLTermCLineService.prototype.chromeUrlForTask="chrome://xmlterm/content";
 
84
XMLTermCLineService.prototype.helpText = "Start with a command line terminal";
 
85
XMLTermCLineService.prototype.handlesArgs=false;
 
86
XMLTermCLineService.prototype.defaultArgs ="";
 
87
XMLTermCLineService.prototype.openWindowWithArgs=false;
 
88
 
 
89
/* factory for command line handler service (XMLTermCLineService) */
 
90
XMLTermCLineFactory = new Object();
 
91
 
 
92
XMLTermCLineFactory.createInstance =
 
93
function (outer, iid) {
 
94
    if (outer != null)
 
95
        throw Components.results.NS_ERROR_NO_AGGREGATION;
 
96
 
 
97
    if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
 
98
        throw Components.results.NS_ERROR_INVALID_ARG;
 
99
 
 
100
    return new XMLTermCLineService();
 
101
}
 
102
 
 
103
/* x-application-terminal content handler */
 
104
function XMLTermContentHandler ()
 
105
{}
 
106
 
 
107
XMLTermContentHandler.prototype.QueryInterface =
 
108
function (iid) {
 
109
 
 
110
    if (!iid.equals(nsIContentHandler))
 
111
        throw Components.results.NS_ERROR_NO_INTERFACE;
 
112
 
 
113
    return this;
 
114
}
 
115
 
 
116
XMLTermContentHandler.prototype.handleContent =
 
117
function (aContentType, aCommand, aWindowContext, aRequest)
 
118
{
 
119
    var e;
 
120
 
 
121
    var aChannel = aRequest.QueryInterface(Components.interfaces.nsIChannel);
 
122
                
 
123
    debug("XMLTermContentHandler.handleContent (" + aContentType + ", " +
 
124
          aCommand + ", " + aWindowContext + ", " +
 
125
          aChannel.URI.spec + ")\n");
 
126
 
 
127
    var xmltermChromeURL = "chrome://xmlterm/content/xmlterm.xul?"+aChannel.URI.spec;
 
128
    //dump("XMLTermContentHandler:xmltermChromeURL = " + xmltermChromeURL + "\n");
 
129
 
 
130
    // Create new XMLterm window
 
131
    var appShellSvc = Components.classes[ASS_CONTRACTID].getService(nsIAppShellService);
 
132
    var domWin = appShellSvc.hiddenDOMWindow;
 
133
    domWin.open(xmltermChromeURL,"_blank", "chrome,menubar,toolbar,resizable");
 
134
}
 
135
 
 
136
/* content handler factory object (XMLTermContentHandler) */
 
137
XMLTermContentHandlerFactory = new Object();
 
138
 
 
139
XMLTermContentHandlerFactory.createInstance =
 
140
function (outer, iid) {
 
141
    if (outer != null)
 
142
        throw Components.results.NS_ERROR_NO_AGGREGATION;
 
143
 
 
144
    if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
 
145
        throw Components.results.NS_ERROR_INVALID_ARG;
 
146
 
 
147
    return new XMLTermContentHandler();
 
148
}
 
149
 
 
150
/* xmlterm protocol handler component */
 
151
function XMLTermProtocolHandler()
 
152
{
 
153
}
 
154
 
 
155
XMLTermProtocolHandler.prototype.scheme = "terminal";
 
156
XMLTermProtocolHandler.prototype.defaultPort = -1;
 
157
XMLTermProtocolHandler.prototype.URIType = 
 
158
                 Components.interfaces.nsIProtocolHandler.URI_NORELATIVE;
 
159
 
 
160
XMLTermProtocolHandler.prototype.newURI =
 
161
function (aSpec, aCharset, aBaseURI)
 
162
{
 
163
    var uri = Components.classes[SIMPLEURI_CONTRACTID].createInstance(nsIURI);
 
164
    uri.spec = aSpec;
 
165
    
 
166
    return uri;
 
167
}
 
168
 
 
169
// "Global" variable
 
170
var gSystemPrincipal = null;
 
171
 
 
172
XMLTermProtocolHandler.prototype.newChannel =
 
173
function (aURI)
 
174
{
 
175
    var uriSpec = aURI.spec
 
176
    //dump("XMLTermProtocolHandler.newChannel: uriSpec="+uriSpec+"\n");
 
177
 
 
178
    if (uriSpec != "terminal:xmlterm")
 
179
       return new BogusChannel(aURI);
 
180
 
 
181
    // Re-direct to chrome HTML document, but with system principal
 
182
 
 
183
    var ioServ = Components.classesByID[NS_IOSERVICE_CID_STR].getService();
 
184
    ioServ = ioServ.QueryInterface(nsIIOService);
 
185
 
 
186
    // Open temporary XUL channel
 
187
    var xulURI = ioServ.newURI("chrome://xmlterm/content/xmltermDummy.xul",
 
188
                               null, null);
 
189
    var temChannel = ioServ.newChannelFromURI(xulURI);
 
190
 
 
191
    // Get owner of XUL channel
 
192
    var xulOwner = temChannel.owner;
 
193
 
 
194
    if (!gSystemPrincipal) {
 
195
       if (!xulOwner) {
 
196
          debug("XMLTermProtocolHandler: Internal error; unable to obtain system principal\n");
 
197
          throw Components.results.NS_ERROR_FAILURE;
 
198
       }
 
199
       gSystemPrincipal = xulOwner;
 
200
    }
 
201
 
 
202
    //dump("gSystemPrincipal="+gSystemPrincipal+"\n");
 
203
 
 
204
    // Cancel XUL request and release channel
 
205
    
 
206
        // why are you canceling here?! you have not even opened anything yet - dougt.
 
207
        // temChannel.cancel(Components.results.NS_BINDING_ABORTED);
 
208
    
 
209
        temChannel = null;
 
210
 
 
211
    // Get current process directory
 
212
    var dscontractid = "@mozilla.org/file/directory_service;1";
 
213
    var ds = Components.classes[dscontractid].getService();
 
214
 
 
215
    var dsprops = ds.QueryInterface(Components.interfaces.nsIProperties);
 
216
    var file = dsprops.get("CurProcD", Components.interfaces.nsIFile);
 
217
 
 
218
    file.append("chrome");
 
219
    file.append("xmlterm.jar");
 
220
 
 
221
    //dump("file="+file.path+"\n");
 
222
 
 
223
    // Contruct JAR URI spec for xmlterm.html
 
224
    // Use file: rather than resource: or chrome: scheme to allow
 
225
    // xmlterm to load other file URLs without failing the security check
 
226
 
 
227
    var jarURI = "jar:file:"+file.path+"!/content/xmlterm/xmlterm.html";
 
228
 
 
229
    var newChannel = ioServ.newChannel(jarURI, null, null);
 
230
 
 
231
    // Make new channel owned by system principal
 
232
    newChannel.owner = gSystemPrincipal;
 
233
 
 
234
    return newChannel;
 
235
}
 
236
 
 
237
/* protocol handler factory object (XMLTermProtocolHandler) */
 
238
XMLTermProtocolHandlerFactory = new Object();
 
239
 
 
240
XMLTermProtocolHandlerFactory.createInstance =
 
241
function (outer, iid) {
 
242
    if (outer != null)
 
243
        throw Components.results.NS_ERROR_NO_AGGREGATION;
 
244
 
 
245
    if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
 
246
        throw Components.results.NS_ERROR_INVALID_ARG;
 
247
 
 
248
    return new XMLTermProtocolHandler();
 
249
}
 
250
 
 
251
/* bogus XMLTerm channel used by the XMLTermProtocolHandler */
 
252
function BogusChannel (aURI)
 
253
{
 
254
    this.URI = aURI;
 
255
    this.originalURI = aURI;
 
256
}
 
257
 
 
258
BogusChannel.prototype.QueryInterface =
 
259
function (iid) {
 
260
 
 
261
    if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
 
262
        !iid.equals(nsISupports))
 
263
        throw Components.results.NS_ERROR_NO_INTERFACE;
 
264
 
 
265
    return this;
 
266
}
 
267
 
 
268
/* nsIChannel */
 
269
BogusChannel.prototype.loadAttributes = null;
 
270
BogusChannel.prototype.contentType = "x-application-terminal";
 
271
BogusChannel.prototype.contentLength = 0;
 
272
BogusChannel.prototype.owner = null;
 
273
BogusChannel.prototype.loadGroup = null;
 
274
BogusChannel.prototype.notificationCallbacks = null;
 
275
BogusChannel.prototype.securityInfo = null;
 
276
BogusChannel.prototype.shouldCache = false;
 
277
 
 
278
BogusChannel.prototype.open =
 
279
BogusChannel.prototype.asyncOpen =
 
280
function ()
 
281
{
 
282
    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
 
283
}
 
284
 
 
285
BogusChannel.prototype.asyncOpen =
 
286
function (observer, ctxt)
 
287
{
 
288
    observer.onStartRequest (this, ctxt);
 
289
}
 
290
 
 
291
/* nsIRequest */
 
292
BogusChannel.prototype.isPending =
 
293
function ()
 
294
{
 
295
    return true;
 
296
}
 
297
 
 
298
BogusChannel.prototype.status = Components.results.NS_OK;
 
299
 
 
300
BogusChannel.prototype.cancel =
 
301
function (aStatus)
 
302
{
 
303
    this.status = aStatus;
 
304
}
 
305
 
 
306
BogusChannel.prototype.parent =
 
307
BogusChannel.prototype.suspend =
 
308
BogusChannel.prototype.resume =
 
309
function ()
 
310
{
 
311
    throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
 
312
}
 
313
 
 
314
var XMLtermModule = new Object();
 
315
 
 
316
XMLtermModule.registerSelf =
 
317
function (compMgr, fileSpec, location, type)
 
318
{
 
319
    debug("*** Registering -terminal handler.\n");
 
320
    
 
321
    compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
 
322
 
 
323
    compMgr.registerFactoryLocation(XMLTERMCLINE_SERVICE_CID,
 
324
                                    "XMLterm CommandLine Service",
 
325
                                    XMLTERMCLINE_SERVICE_CONTRACTID, 
 
326
                                    fileSpec,
 
327
                                    location, 
 
328
                                    type);
 
329
    
 
330
    catman = Components.classes["@mozilla.org/categorymanager;1"]
 
331
               .getService(nsICategoryManager);
 
332
                catman.addCategoryEntry("command-line-argument-handlers",
 
333
                             "terminal command line handler",
 
334
                 XMLTERMCLINE_SERVICE_CONTRACTID, true, true);
 
335
 
 
336
    debug("*** Registering x-application-terminal handler.\n");
 
337
    compMgr.registerFactoryLocation(XMLTERMCNT_HANDLER_CID,
 
338
                                    "XMLTerm Content Handler",
 
339
                                    XMLTERMCNT_HANDLER_CONTRACTID, 
 
340
                                    fileSpec,
 
341
                                    location, 
 
342
                                    type);
 
343
 
 
344
    debug("*** Registering terminal protocol handler.\n");
 
345
    compMgr.registerFactoryLocation(XMLTERMPROT_HANDLER_CID,
 
346
                                    "XMLTerm protocol handler",
 
347
                                    XMLTERMPROT_HANDLER_CONTRACTID, 
 
348
                                    fileSpec, 
 
349
                                    location,
 
350
                                    type);
 
351
 
 
352
}
 
353
 
 
354
XMLtermModule.unregisterSelf =
 
355
function(compMgr, fileSpec, location)
 
356
{
 
357
    compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
 
358
    compMgr.unregisterFactoryLocation(XMLTERMCLINE_SERVICE_CID, fileSpec);
 
359
        catman = Components.classes["@mozilla.org/categorymanager;1"]
 
360
        .getService(nsICategoryManager);
 
361
        catman.deleteCategoryEntry("command-line-argument-handlers",
 
362
                                   XMLTERMCLINE_SERVICE_CONTRACTID, true);
 
363
}
 
364
 
 
365
XMLtermModule.getClassObject =
 
366
function (compMgr, cid, iid) {
 
367
    if (cid.equals(XMLTERMCLINE_SERVICE_CID))
 
368
        return XMLTermCLineFactory;
 
369
 
 
370
    if (cid.equals(XMLTERMCNT_HANDLER_CID))
 
371
        return XMLTermContentHandlerFactory;
 
372
 
 
373
    if (cid.equals(XMLTERMPROT_HANDLER_CID))
 
374
        return XMLTermProtocolHandlerFactory;
 
375
    
 
376
    if (!iid.equals(Components.interfaces.nsIFactory))
 
377
        throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
 
378
 
 
379
    throw Components.results.NS_ERROR_NO_INTERFACE;
 
380
    
 
381
}
 
382
 
 
383
XMLtermModule.canUnload =
 
384
function(compMgr)
 
385
{
 
386
    return true;
 
387
}
 
388
 
 
389
/* entrypoint */
 
390
function NSGetModule(compMgr, fileSpec) {
 
391
    return XMLtermModule;
 
392
}