Back to index

lightning-sunbird  0.9+nobinonly
mozJSComponentLoader.h
Go to the documentation of this file.
00001 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is Mozilla Communicator client code, released
00017  * March 31, 1998.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 1999
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributors:
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "plhash.h"
00041 #include "jsapi.h"
00042 #include "nsIComponentLoader.h"
00043 #include "nsIComponentLoaderManager.h"
00044 #include "nsIJSRuntimeService.h"
00045 #include "nsIJSContextStack.h"
00046 #include "nsISupports.h"
00047 #include "nsIXPConnect.h"
00048 #include "nsIModule.h"
00049 #include "nsSupportsArray.h"
00050 #include "nsIFile.h"
00051 #include "nsAutoPtr.h"
00052 #include "nsIFastLoadService.h"
00053 #include "nsIObjectInputStream.h"
00054 #include "nsIObjectOutputStream.h"
00055 #include "nsITimer.h"
00056 #include "nsIObserver.h"
00057 #ifndef XPCONNECT_STANDALONE
00058 #include "nsIPrincipal.h"
00059 #endif
00060 
00061 class nsIFastLoadService;
00062 
00063 /* 6bd13476-1dd2-11b2-bbef-f0ccb5fa64b6 (thanks, mozbot) */
00064 
00065 #define MOZJSCOMPONENTLOADER_CID \
00066   {0x6bd13476, 0x1dd2, 0x11b2, \
00067     { 0xbb, 0xef, 0xf0, 0xcc, 0xb5, 0xfa, 0x64, 0xb6 }}
00068 #define MOZJSCOMPONENTLOADER_CONTRACTID "@mozilla.org/moz/jsloader;1"
00069 #define MOZJSCOMPONENTLOADER_TYPE_NAME "text/javascript"
00070 
00071 // nsIFastLoadFileIO implementation for component fastload
00072 class nsXPCFastLoadIO : public nsIFastLoadFileIO
00073 {
00074  public:
00075     NS_DECL_ISUPPORTS
00076     NS_DECL_NSIFASTLOADFILEIO
00077 
00078     nsXPCFastLoadIO(nsIFile *file) : mFile(file) {}
00079 
00080     void SetInputStream(nsIInputStream *stream) { mInputStream = stream; }
00081     void SetOutputStream(nsIOutputStream *stream) { mOutputStream = stream; }
00082 
00083  private:
00084     ~nsXPCFastLoadIO() {}
00085 
00086     nsCOMPtr<nsIFile> mFile;
00087     nsCOMPtr<nsIInputStream> mInputStream;
00088     nsCOMPtr<nsIOutputStream> mOutputStream;
00089 };
00090 
00091 
00092 class mozJSComponentLoader : public nsIComponentLoader,
00093                              public nsIObserver
00094 {
00095  public:
00096     NS_DECL_ISUPPORTS
00097     NS_DECL_NSICOMPONENTLOADER
00098     NS_DECL_NSIOBSERVER
00099 
00100     mozJSComponentLoader();
00101     virtual ~mozJSComponentLoader();
00102 
00103  protected:
00104     nsresult ReallyInit();
00105     nsresult AttemptRegistration(nsIFile *component, PRBool deferred);
00106     nsresult UnregisterComponent(nsIFile *component);
00107     nsresult RegisterComponentsInDir(PRInt32 when, nsIFile *dir);
00108     nsresult GlobalForLocation(const char *aLocation, nsIFile *aComponent,
00109                                JSObject **aGlobal);
00110     nsIModule* ModuleForLocation(const char *aLocation, nsIFile *component,
00111                                  nsresult *status);
00112     PRBool HasChanged(const char *registryLocation, nsIFile *component);
00113     nsresult SetRegistryInfo(const char *registryLocation, nsIFile *component);
00114     nsresult RemoveRegistryInfo(nsIFile *component, const char *registryLocation);
00115 
00116     nsresult StartFastLoad(nsIFastLoadService *flSvc);
00117     nsresult ReadScript(nsIFastLoadService *flSvc, const char *nativePath,
00118                         nsIURI *uri, JSContext *cx, JSScript **script);
00119     nsresult WriteScript(nsIFastLoadService *flSvc, JSScript *script,
00120                          nsIFile *component, const char *nativePath,
00121                          nsIURI *uri, JSContext *cx);
00122     static void CloseFastLoad(nsITimer *timer, void *closure);
00123     void CloseFastLoad();
00124 
00125     nsCOMPtr<nsIComponentManager> mCompMgr;
00126     nsCOMPtr<nsIComponentLoaderManager> mLoaderManager;
00127     nsCOMPtr<nsIJSRuntimeService> mRuntimeService;
00128     nsCOMPtr<nsIFile> mFastLoadFile;
00129     nsRefPtr<nsXPCFastLoadIO> mFastLoadIO;
00130     nsCOMPtr<nsIObjectInputStream> mFastLoadInput;
00131     nsCOMPtr<nsIObjectOutputStream> mFastLoadOutput;
00132     nsCOMPtr<nsITimer> mFastLoadTimer;
00133 #ifndef XPCONNECT_STANDALONE
00134     nsCOMPtr<nsIPrincipal> mSystemPrincipal;
00135 #endif
00136     JSRuntime *mRuntime;
00137     JSContext *mContext;
00138     PLHashTable *mModules;
00139     PLHashTable *mGlobals;
00140 
00141     PRBool mInitialized;
00142     nsSupportsArray mDeferredComponents;
00143 };