home *** CD-ROM | disk | FTP | other *** search
/ Chip 2003 September / Chip_2003-09_cd1.bin / zkuste / macos / Files / wamcom.sit / wamcom-131-macos9-20030721 / Components / venkman-service.js < prev    next >
Text File  |  2003-06-30  |  13KB  |  466 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public License
  4.  * Version 1.1 (the "License"); you may not use this file except in
  5.  * compliance with the License. You may obtain a copy of the License at
  6.  * http://www.mozilla.org/MPL/ 
  7.  * 
  8.  * Software distributed under the License is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  10.  * for the specific language governing rights and limitations under the
  11.  * License. 
  12.  *
  13.  * The Original Code is The JavaScript Debugger
  14.  * 
  15.  * The Initial Developer of the Original Code is
  16.  * Netscape Communications Corporation
  17.  * Portions created by Netscape are
  18.  * Copyright (C) 1998 Netscape Communications Corporation.
  19.  *
  20.  * Alternatively, the contents of this file may be used under the
  21.  * terms of the GNU Public License (the "GPL"), in which case the
  22.  * provisions of the GPL are applicable instead of those above.
  23.  * If you wish to allow use of your version of this file only
  24.  * under the terms of the GPL and not to allow others to use your
  25.  * version of this file under the MPL, indicate your decision by
  26.  * deleting the provisions above and replace them with the notice
  27.  * and other provisions required by the GPL.  If you do not delete
  28.  * the provisions above, a recipient may use your version of this
  29.  * file under either the MPL or the GPL.
  30.  *
  31.  * Contributor(s):
  32.  *  Robert Ginda, <rginda@netscape.com>, original author
  33.  *
  34.  */
  35.  
  36. /* components defined in this file */
  37. const CLINE_SERVICE_CTRID =
  38.     "@mozilla.org/commandlinehandler/general-startup;1?type=venkman";
  39. const CLINE_SERVICE_CID =
  40.     Components.ID("{18269616-1dd2-11b2-afa8-b612439bda27}");
  41. const JSDPROT_HANDLER_CTRID =
  42.     "@mozilla.org/network/protocol;1?name=x-jsd";
  43. const JSDPROT_HANDLER_CID =
  44.     Components.ID("{12ec790d-304e-4525-89a9-3e723d489d14}");
  45.  
  46. /* components used by this file */
  47. const CATMAN_CTRID = "@mozilla.org/categorymanager;1";
  48. const STRING_STREAM_CTRID = "@mozilla.org/io/string-input-stream;1";
  49. const MEDIATOR_CTRID =
  50.     "@mozilla.org/appshell/window-mediator;1";
  51. const SIMPLEURI_CTRID = "@mozilla.org/network/simple-uri;1";
  52.  
  53. const nsIWindowMediator    = Components.interfaces.nsIWindowMediator;
  54. const nsICmdLineHandler    = Components.interfaces.nsICmdLineHandler;
  55. const nsICategoryManager   = Components.interfaces.nsICategoryManager;
  56. const nsIProtocolHandler   = Components.interfaces.nsIProtocolHandler;
  57. const nsIURI               = Components.interfaces.nsIURI;
  58. const nsIURL               = Components.interfaces.nsIURL;
  59. const nsIStringInputStream = Components.interfaces.nsIStringInputStream;
  60. const nsIChannel           = Components.interfaces.nsIChannel;
  61. const nsIRequest           = Components.interfaces.nsIRequest;
  62. const nsIProgressEventSink = Components.interfaces.nsIProgressEventSink;
  63. const nsISupports          = Components.interfaces.nsISupports;
  64.  
  65. function findDebuggerWindow ()
  66. {
  67.     var windowManager =
  68.         Components.classes[MEDIATOR_CTRID].getService(nsIWindowMediator);
  69.  
  70.     var window = windowManager.getMostRecentWindow("mozapp:venkman");
  71.  
  72.     return window;
  73. }
  74.  
  75. function safeHTML(str)
  76. {
  77.     function replaceChars(ch)
  78.     {
  79.         switch (ch)
  80.         {
  81.             case "<":
  82.                 return "<";
  83.             
  84.             case ">":
  85.                 return ">";
  86.                     
  87.             case "&":
  88.                 return "&";
  89.         }
  90.  
  91.         return "?";
  92.     };
  93.         
  94.     return String(str).replace(/[<>&]/g, replaceChars);
  95. }
  96.  
  97. /* Command Line handler service */
  98. function CLineService()
  99. {}
  100.  
  101. CLineService.prototype.commandLineArgument = "-venkman";
  102. CLineService.prototype.prefNameForStartup = "general.startup.venkman";
  103. CLineService.prototype.chromeUrlForTask = "chrome://venkman/content";
  104. CLineService.prototype.helpText = "Start with JavaScript Debugger.";
  105. CLineService.prototype.handlesArgs = false;
  106. CLineService.prototype.defaultArgs = "";
  107. CLineService.prototype.openWindowWithArgs = false;
  108.  
  109. /* factory for command line handler service (CLineService) */
  110. var CLineFactory = new Object();
  111.  
  112. CLineFactory.createInstance =
  113. function clf_create (outer, iid) {
  114.     if (outer != null)
  115.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  116.  
  117.     if (!iid.equals(nsICmdLineHandler) && !iid.equals(nsISupports))
  118.         throw Components.results.NS_ERROR_INVALID_ARG;
  119.  
  120.     return new CLineService();
  121. }
  122.  
  123. /* x-jsd: protocol handler */
  124.  
  125. const JSD_DEFAULT_PORT = 2206; /* Dana's apartment number. */
  126.  
  127. /* protocol handler factory object */
  128. var JSDProtocolHandlerFactory = new Object();
  129.  
  130. JSDProtocolHandlerFactory.createInstance =
  131. function jsdhf_create (outer, iid) {
  132.     if (outer != null)
  133.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  134.  
  135.     if (!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports))
  136.         throw Components.results.NS_ERROR_INVALID_ARG;
  137.  
  138.     return new JSDProtocolHandler();
  139. }
  140.  
  141. function JSDURI (spec, charset)
  142. {
  143.     this.spec = this.prePath = spec;
  144.     this.charset = this.originCharset = charset;
  145. }
  146.  
  147. JSDURI.prototype.QueryInterface =
  148. function jsdch_qi (iid)
  149. {
  150.     if (!iid.equals(nsIURI) && !iid.equals(nsIURL) &&
  151.         !iid.equals(nsISupports))
  152.         throw Components.results.NS_ERROR_NO_INTERFACE;
  153.  
  154.     return this;
  155. }
  156.  
  157. JSDURI.prototype.scheme = "x-jsd";
  158.  
  159. JSDURI.prototype.fileBaseName =
  160. JSDURI.prototype.fileExtension =
  161. JSDURI.prototype.filePath  =
  162. JSDURI.prototype.param     =
  163. JSDURI.prototype.query     =
  164. JSDURI.prototype.ref       =
  165. JSDURI.prototype.directory =
  166. JSDURI.prototype.fileName  =
  167. JSDURI.prototype.username  =
  168. JSDURI.prototype.password  =
  169. JSDURI.prototype.hostPort  =
  170. JSDURI.prototype.path      =
  171. JSDURI.prototype.asciiHost =
  172. JSDURI.prototype.userPass  = "";
  173.  
  174. JSDURI.prototype.port = JSD_DEFAULT_PORT;
  175.  
  176. JSDURI.prototype.schemeIs =
  177. function jsduri_schemeis (scheme)
  178. {
  179.     return scheme.toLowerCase() == "x-jsd";
  180. }
  181.  
  182. JSDURI.prototype.getCommonBaseSpec =
  183. function jsduri_commonbase (uri)
  184. {
  185.     return "x-jsd:";
  186. }
  187.  
  188. JSDURI.prototype.getRelativeSpec =
  189. function jsduri_commonbase (uri)
  190. {
  191.     return uri;
  192. }
  193.  
  194. JSDURI.prototype.equals =
  195. function jsduri_equals (uri)
  196. {
  197.     return uri.spec == this.spec;
  198. }
  199.  
  200. JSDURI.prototype.clone =
  201. function jsduri_clone ()
  202. {
  203.     return new JSDURI (this.spec);
  204. }
  205.  
  206. JSDURI.prototype.resolve =
  207. function jsduri_resolve(path)
  208. {
  209.     //dump ("resolve " + path + " from " + this.spec + "\n");
  210.     if (path[0] == "#")
  211.         return this.spec + path;
  212.     
  213.     return path;
  214. }
  215.  
  216. function JSDProtocolHandler()
  217. {
  218.     /* nothing here */
  219. }
  220.  
  221. JSDProtocolHandler.prototype.scheme = "x-jsd";
  222. JSDProtocolHandler.prototype.defaultPort = JSD_DEFAULT_PORT;
  223. JSDProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE ||
  224.                                              nsIProtocolHandler.URI_NOAUTH;
  225.  
  226. JSDProtocolHandler.prototype.allowPort =
  227. function jsdph_allowport (aPort, aScheme)
  228. {
  229.     return false;
  230. }
  231.  
  232. JSDProtocolHandler.prototype.newURI =
  233. function jsdph_newuri (spec, charset, baseURI)
  234. {
  235.     var clazz = Components.classes[SIMPLEURI_CTRID];
  236.     var uri = clazz.createInstance(nsIURI);
  237.     uri.spec = spec;
  238.     return uri;
  239. }
  240.  
  241. JSDProtocolHandler.prototype.newChannel =
  242. function jsdph_newchannel (uri)
  243. {
  244.     return new JSDChannel (uri);
  245. }
  246.  
  247. function JSDChannel (uri)
  248. {
  249.     this.URI = uri;
  250.     this.originalURI = uri;
  251.     this._isPending = true;
  252.     var clazz = Components.classes[STRING_STREAM_CTRID];
  253.     this.stringStream = clazz.createInstance(nsIStringInputStream);
  254. }
  255.  
  256. JSDChannel.prototype.QueryInterface =
  257. function jsdch_qi (iid)
  258. {
  259.  
  260.     if (!iid.equals(nsIChannel) && !iid.equals(nsIRequest) &&
  261.         !iid.equals(nsISupports))
  262.         throw Components.results.NS_ERROR_NO_INTERFACE;
  263.  
  264.     return this;
  265. }
  266.  
  267. /* nsIChannel */
  268. JSDChannel.prototype.loadAttributes = null;
  269. JSDChannel.prototype.contentType = "text/html";
  270. JSDChannel.prototype.contentLength = -1;
  271. JSDChannel.prototype.owner = null;
  272. JSDChannel.prototype.loadGroup = null;
  273. JSDChannel.prototype.notificationCallbacks = null;
  274. JSDChannel.prototype.securityInfo = null;
  275.  
  276. JSDChannel.prototype.open =
  277. function jsdch_open()
  278. {
  279.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  280. }
  281.  
  282. JSDChannel.prototype.asyncOpen =
  283. function jsdch_aopen (streamListener, context)
  284. {
  285.     this.streamListener = streamListener;
  286.     this.context = context;
  287.     if (this.loadGroup)
  288.         this.loadGroup.addRequest (this, null);
  289.  
  290.     var window = findDebuggerWindow();
  291.     var ary = this.URI.spec.match (/x-jsd:([^:]+)/);
  292.     var exception;
  293.     
  294.     if (window && "console" in window && ary)
  295.     {
  296.         try
  297.         {
  298.             window.asyncOpenJSDURL (this, streamListener, context);
  299.             return;
  300.         }
  301.         catch (ex)
  302.         {
  303.             exception = ex;
  304.         }
  305.     }
  306.     
  307.     var str =
  308.         "<html><head><title>Error</title></head><body>Could not load <<b>" +
  309.         safeHTML(this.URI.spec) + "</b>><br>";
  310.     
  311.     if (!ary)
  312.     {
  313.         str += "<b>Error parsing uri.</b>";
  314.     }
  315.     else if (exception)
  316.     {
  317.         str += "<b>Internal error: " + safeHTML(exception) + "</b><br><pre>" + 
  318.             safeHTML(exception.stack);
  319.     }
  320.     else
  321.     {
  322.         str += "<b>Debugger is not running.</b>";
  323.     }
  324.     
  325.     str += "</body></html>";
  326.     
  327.     this.respond (str);
  328. }
  329.  
  330. JSDChannel.prototype.respond =
  331. function jsdch_respond (str)
  332. {
  333.     this.streamListener.onStartRequest (this, this.context);
  334.  
  335.     var len = str.length;
  336.     this.stringStream.setData (str, len);
  337.     this.streamListener.onDataAvailable (this, this.context,
  338.                                          this.stringStream, 0, len);
  339.     this.streamListener.onStopRequest (this, this.context,
  340.                                        Components.results.NS_OK);
  341.     if (this.loadGroup)
  342.         this.loadGroup.removeRequest (this, null, Components.results.NS_OK);
  343.     this._isPending = false;    
  344. }
  345.  
  346. /* nsIRequest */
  347. JSDChannel.prototype.isPending =
  348. function jsdch_ispending ()
  349. {
  350.     return this._isPending;
  351. }
  352.  
  353. JSDChannel.prototype.status = Components.results.NS_OK;
  354.  
  355. JSDChannel.prototype.cancel =
  356. function jsdch_cancel (status)
  357. {
  358.     if (this._isPending)
  359.     {
  360.         this._isPending = false;
  361.         this.streamListener.onStopRequest (this, this.context, status);
  362.         if (this.loadGroup)
  363.         {
  364.             try
  365.             {
  366.                 this.loadGroup.removeRequest (this, null, status);
  367.             }
  368.             catch (ex)
  369.             {
  370.                 debug ("we're not in the load group?\n");
  371.             }
  372.         }
  373.     }
  374.     
  375.     this.status = status;
  376. }
  377.  
  378. JSDChannel.prototype.suspend =
  379. JSDChannel.prototype.resume =
  380. function jsdch_notimpl ()
  381. {
  382.     throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  383. }
  384.  
  385. /*****************************************************************************/
  386.  
  387. var Module = new Object();
  388.  
  389. Module.registerSelf =
  390. function (compMgr, fileSpec, location, type)
  391. {
  392.     debug("*** Registering -venkman handler.\n");
  393.     
  394.     compMgr =
  395.         compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  396.  
  397.     compMgr.registerFactoryLocation(CLINE_SERVICE_CID,
  398.                                     "Venkman CommandLine Service",
  399.                                     CLINE_SERVICE_CTRID, 
  400.                                     fileSpec,
  401.                                     location, 
  402.                                     type);
  403.  
  404.     catman = Components.classes[CATMAN_CTRID].getService(nsICategoryManager);
  405.     catman.addCategoryEntry("command-line-argument-handlers",
  406.                             "venkman command line handler",
  407.                             CLINE_SERVICE_CTRID, true, true);
  408.  
  409.     debug("*** Registering x-jsd protocol handler.\n");
  410.     compMgr.registerFactoryLocation(JSDPROT_HANDLER_CID,
  411.                                     "x-jsd protocol handler",
  412.                                     JSDPROT_HANDLER_CTRID, 
  413.                                     fileSpec, 
  414.                                     location,
  415.                                     type);
  416.     try
  417.     {
  418.         const JSD_CTRID = "@mozilla.org/js/jsd/debugger-service;1";
  419.         const jsdIDebuggerService = Components.interfaces.jsdIDebuggerService;
  420.         var jsds = Components.classes[JSD_CTRID].getService(jsdIDebuggerService);
  421.         jsds.initAtStartup = true;
  422.     }
  423.     catch (ex)
  424.     {
  425.         debug ("*** ERROR initializing debugger service");
  426.         debug (ex);
  427.     }
  428. }
  429.  
  430. Module.unregisterSelf =
  431. function(compMgr, fileSpec, location)
  432. {
  433.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  434.  
  435.     compMgr.unregisterFactoryLocation(CLINE_SERVICE_CID, fileSpec);
  436.     catman = Components.classes[CATMAN_CTRID].getService(nsICategoryManager);
  437.     catman.deleteCategoryEntry("command-line-argument-handlers",
  438.                                CLINE_SERVICE_CTRID, true);
  439. }
  440.  
  441. Module.getClassObject =
  442. function (compMgr, cid, iid) {
  443.     if (cid.equals(CLINE_SERVICE_CID))
  444.         return CLineFactory;
  445.  
  446.     if (cid.equals(JSDPROT_HANDLER_CID))
  447.         return JSDProtocolHandlerFactory;
  448.     
  449.     if (!iid.equals(Components.interfaces.nsIFactory))
  450.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  451.  
  452.     throw Components.results.NS_ERROR_NO_INTERFACE;
  453.     
  454. }
  455.  
  456. Module.canUnload =
  457. function(compMgr)
  458. {
  459.     return true;
  460. }
  461.  
  462. /* entrypoint */
  463. function NSGetModule(compMgr, fileSpec) {
  464.     return Module;
  465. }
  466.