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/
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.
12
* The Original Code is mozilla.org code.
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
20
* Seth Spitzer <sspitzer@netscape.com>
21
* Robert Ginda <rginda@netscape.com>
22
* R. Saravanan <svn@xmlterm.org>
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)
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}");
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}";
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;
77
/* Command Line handler service */
78
function XMLTermCLineService()
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;
89
/* factory for command line handler service (XMLTermCLineService) */
90
XMLTermCLineFactory = new Object();
92
XMLTermCLineFactory.createInstance =
93
function (outer, iid) {
95
throw Components.results.NS_ERROR_NO_AGGREGATION;
97
if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
98
throw Components.results.NS_ERROR_INVALID_ARG;
100
return new XMLTermCLineService();
103
/* x-application-terminal content handler */
104
function XMLTermContentHandler ()
107
XMLTermContentHandler.prototype.QueryInterface =
110
if (!iid.equals(nsIContentHandler))
111
throw Components.results.NS_ERROR_NO_INTERFACE;
116
XMLTermContentHandler.prototype.handleContent =
117
function (aContentType, aCommand, aWindowContext, aRequest)
121
var aChannel = aRequest.QueryInterface(Components.interfaces.nsIChannel);
123
debug("XMLTermContentHandler.handleContent (" + aContentType + ", " +
124
aCommand + ", " + aWindowContext + ", " +
125
aChannel.URI.spec + ")\n");
127
var xmltermChromeURL = "chrome://xmlterm/content/xmlterm.xul?"+aChannel.URI.spec;
128
//dump("XMLTermContentHandler:xmltermChromeURL = " + xmltermChromeURL + "\n");
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");
136
/* content handler factory object (XMLTermContentHandler) */
137
XMLTermContentHandlerFactory = new Object();
139
XMLTermContentHandlerFactory.createInstance =
140
function (outer, iid) {
142
throw Components.results.NS_ERROR_NO_AGGREGATION;
144
if (!iid.equals(nsIContentHandler) && !iid.equals(nsISupports))
145
throw Components.results.NS_ERROR_INVALID_ARG;
147
return new XMLTermContentHandler();
150
/* xmlterm protocol handler component */
151
function XMLTermProtocolHandler()
155
XMLTermProtocolHandler.prototype.scheme = "terminal";
156
XMLTermProtocolHandler.prototype.defaultPort = -1;
157
XMLTermProtocolHandler.prototype.URIType =
158
Components.interfaces.nsIProtocolHandler.URI_NORELATIVE;
160
XMLTermProtocolHandler.prototype.newURI =
161
function (aSpec, aCharset, aBaseURI)
163
var uri = Components.classes[SIMPLEURI_CONTRACTID].createInstance(nsIURI);
170
var gSystemPrincipal = null;
172
XMLTermProtocolHandler.prototype.newChannel =
175
var uriSpec = aURI.spec
176
//dump("XMLTermProtocolHandler.newChannel: uriSpec="+uriSpec+"\n");
178
if (uriSpec != "terminal:xmlterm")
179
return new BogusChannel(aURI);
181
// Re-direct to chrome HTML document, but with system principal
183
var ioServ = Components.classesByID[NS_IOSERVICE_CID_STR].getService();
184
ioServ = ioServ.QueryInterface(nsIIOService);
186
// Open temporary XUL channel
187
var xulURI = ioServ.newURI("chrome://xmlterm/content/xmltermDummy.xul",
189
var temChannel = ioServ.newChannelFromURI(xulURI);
191
// Get owner of XUL channel
192
var xulOwner = temChannel.owner;
194
if (!gSystemPrincipal) {
196
debug("XMLTermProtocolHandler: Internal error; unable to obtain system principal\n");
197
throw Components.results.NS_ERROR_FAILURE;
199
gSystemPrincipal = xulOwner;
202
//dump("gSystemPrincipal="+gSystemPrincipal+"\n");
204
// Cancel XUL request and release channel
206
// why are you canceling here?! you have not even opened anything yet - dougt.
207
// temChannel.cancel(Components.results.NS_BINDING_ABORTED);
211
// Get current process directory
212
var dscontractid = "@mozilla.org/file/directory_service;1";
213
var ds = Components.classes[dscontractid].getService();
215
var dsprops = ds.QueryInterface(Components.interfaces.nsIProperties);
216
var file = dsprops.get("CurProcD", Components.interfaces.nsIFile);
218
file.append("chrome");
219
file.append("xmlterm.jar");
221
//dump("file="+file.path+"\n");
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
227
var jarURI = "jar:file:"+file.path+"!/content/xmlterm/xmlterm.html";
229
var newChannel = ioServ.newChannel(jarURI, null, null);
231
// Make new channel owned by system principal
232
newChannel.owner = gSystemPrincipal;
237
/* protocol handler factory object (XMLTermProtocolHandler) */
238
XMLTermProtocolHandlerFactory = new Object();
240
XMLTermProtocolHandlerFactory.createInstance =
241
function (outer, iid) {
243
throw Components.results.NS_ERROR_NO_AGGREGATION;
245
if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
246
throw Components.results.NS_ERROR_INVALID_ARG;
248
return new XMLTermProtocolHandler();
251
/* bogus XMLTerm channel used by the XMLTermProtocolHandler */
252
function BogusChannel (aURI)
255
this.originalURI = aURI;
258
BogusChannel.prototype.QueryInterface =
261
if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
262
!iid.equals(nsISupports))
263
throw Components.results.NS_ERROR_NO_INTERFACE;
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;
278
BogusChannel.prototype.open =
279
BogusChannel.prototype.asyncOpen =
282
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
285
BogusChannel.prototype.asyncOpen =
286
function (observer, ctxt)
288
observer.onStartRequest (this, ctxt);
292
BogusChannel.prototype.isPending =
298
BogusChannel.prototype.status = Components.results.NS_OK;
300
BogusChannel.prototype.cancel =
303
this.status = aStatus;
306
BogusChannel.prototype.parent =
307
BogusChannel.prototype.suspend =
308
BogusChannel.prototype.resume =
311
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
314
var XMLtermModule = new Object();
316
XMLtermModule.registerSelf =
317
function (compMgr, fileSpec, location, type)
319
debug("*** Registering -terminal handler.\n");
321
compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
323
compMgr.registerFactoryLocation(XMLTERMCLINE_SERVICE_CID,
324
"XMLterm CommandLine Service",
325
XMLTERMCLINE_SERVICE_CONTRACTID,
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);
336
debug("*** Registering x-application-terminal handler.\n");
337
compMgr.registerFactoryLocation(XMLTERMCNT_HANDLER_CID,
338
"XMLTerm Content Handler",
339
XMLTERMCNT_HANDLER_CONTRACTID,
344
debug("*** Registering terminal protocol handler.\n");
345
compMgr.registerFactoryLocation(XMLTERMPROT_HANDLER_CID,
346
"XMLTerm protocol handler",
347
XMLTERMPROT_HANDLER_CONTRACTID,
354
XMLtermModule.unregisterSelf =
355
function(compMgr, fileSpec, location)
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);
365
XMLtermModule.getClassObject =
366
function (compMgr, cid, iid) {
367
if (cid.equals(XMLTERMCLINE_SERVICE_CID))
368
return XMLTermCLineFactory;
370
if (cid.equals(XMLTERMCNT_HANDLER_CID))
371
return XMLTermContentHandlerFactory;
373
if (cid.equals(XMLTERMPROT_HANDLER_CID))
374
return XMLTermProtocolHandlerFactory;
376
if (!iid.equals(Components.interfaces.nsIFactory))
377
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
379
throw Components.results.NS_ERROR_NO_INTERFACE;
383
XMLtermModule.canUnload =
390
function NSGetModule(compMgr, fileSpec) {
391
return XMLtermModule;