home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / components / nsSample.js < prev    next >
Text File  |  2001-02-14  |  5KB  |  132 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2.  
  3. /*
  4.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  5.  * If you must perform some initialization, and it could possibly fail (even
  6.  * due to an out-of-memory condition), you should use an Init method, which
  7.  * can convey failure appropriately (thrown exception in JS,
  8.  * NS_FAILED(nsresult) return in C++).
  9.  *
  10.  * In JS, you can actually cheat, because a thrown exception will cause the
  11.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  12.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  13.  * for portability reasons -- and even when you're building completely
  14.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  15.  */
  16. function mySample() { /* big comment for no code, eh? */ }
  17.  
  18. /* decorate prototype to provide ``class'' methods and property accessors */
  19. mySample.prototype = {
  20.     /*
  21.      * get and set are new Magic in JS1.5, borrowing the intent -- if not
  22.      * the exact syntax -- from the JS2 design.  They define accessors for
  23.      * properties on the JS object, follow the expected rules for prototype
  24.      * delegation, and make a mean cup of coffee.
  25.      */
  26.     get value()       { return this.val; },
  27.     set value(newval) { return this.val = newval; },
  28.  
  29.     writeValue: function (aPrefix) {
  30.         dump("mySample::writeValue => " + aPrefix + this.val + "\n");
  31.     },
  32.     poke: function (aValue) { this.val = aValue; },
  33.  
  34.     /*
  35.      * Note that until bug 14460 is resolved, you need to name the method
  36.      * QueryInterface, not queryInterface as you might expect given the
  37.      * interCaps naming convention used in most XPIDL.
  38.      */
  39.     QueryInterface: function (iid) {
  40.         if (!iid.equals(Components.interfaces.nsISample) &&
  41.             !iid.equals(Components.interfaces.nsISupports)) {
  42.             throw Components.results.NS_ERROR_NO_INTERFACE;
  43.         }
  44.         return this;
  45.     },
  46.  
  47.     val: "<default value>"
  48. }
  49.  
  50. var myModule = {
  51.     firstTime: true,
  52.  
  53.     /*
  54.      * RegisterSelf is called at registration time (component installation
  55.      * or the only-until-release startup autoregistration) and is responsible
  56.      * for notifying the component manager of all components implemented in
  57.      * this module.  The fileSpec, location and type parameters are mostly
  58.      * opaque, and should be passed on to the registerComponent call
  59.      * unmolested.
  60.      */
  61.     registerSelf: function (compMgr, fileSpec, location, type) {
  62.         if (this.firstTime) {
  63.             dump("*** Deferring registration of sample JS components\n");
  64.             this.firstTime = false;
  65.             throw Components.results.NS_ERROR_FACTORY_REGISTER_AGAIN;
  66.         }
  67.         dump("*** Registering sample JS components\n");
  68.         compMgr.registerComponentWithType(this.myCID,
  69.                                           "Sample JS Component",
  70.                                           this.myProgID, fileSpec,
  71.                                           location, true, true,
  72.                                           type);
  73.     },
  74.  
  75.     /*
  76.      * The GetClassObject method is responsible for producing Factory and
  77.      * SingletonFactory objects (the latter are specialized for services).
  78.      */
  79.     getClassObject: function (compMgr, cid, iid) {
  80.         if (!cid.equals(this.myCID))
  81.             throw Components.results.NS_ERROR_NO_INTERFACE;
  82.  
  83.         if (!iid.equals(Components.interfaces.nsIFactory))
  84.             throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  85.  
  86.         return this.myFactory;
  87.     },
  88.  
  89.     /* CID for this class */
  90.     myCID: Components.ID("{dea98e50-1dd1-11b2-9344-8902b4805a2e}"),
  91.  
  92.     /* ProgID for this class */
  93.     myProgID: "@mozilla.org/jssample;1",
  94.  
  95.     /* factory object */
  96.     myFactory: {
  97.         /*
  98.          * Construct an instance of the interface specified by iid, possibly
  99.          * aggregating it with the provided outer.  (If you don't know what
  100.          * aggregation is all about, you don't need to.  It reduces even the
  101.          * mightiest of XPCOM warriors to snivelling cowards.)
  102.          */
  103.         createInstance: function (outer, iid) {
  104.             dump("CI: " + iid + "\n");
  105.             if (outer != null)
  106.                 throw Components.results.NS_ERROR_NO_AGGREGATION;
  107.  
  108.             return (new mySample()).QueryInterface(iid);
  109.         }
  110.     },
  111.  
  112.     /*
  113.      * The canUnload method signals that the component is about to be unloaded.
  114.      * C++ components can return false to indicate that they don't wish to be
  115.      * unloaded, but the return value from JS components' canUnload is ignored:
  116.      * mark-and-sweep will keep everything around until it's no longer in use,
  117.      * making unconditional ``unload'' safe.
  118.      *
  119.      * You still need to provide a (likely useless) canUnload method, though:
  120.      * it's part of the nsIModule interface contract, and the JS loader _will_
  121.      * call it.
  122.      */
  123.     canUnload: function(compMgr) {
  124.         dump("*** Unloading sample JS components\n");
  125.         return true;
  126.     }
  127. };
  128.  
  129. function NSGetModule(compMgr, fileSpec) {
  130.     return myModule;
  131. }
  132.