Back to index

lightning-sunbird  0.9+nobinonly
nsPluginHostImpl.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsPluginHostImpl_h__
00039 #define nsPluginHostImpl_h__
00040 
00041 #include "nsIPluginManager.h"
00042 #include "nsIPluginManager2.h"
00043 #include "nsIPluginHost.h"
00044 #include "nsIObserver.h"
00045 #include "nsPIPluginHost.h"
00046 #include "nsCRT.h"
00047 #include "nsCOMPtr.h"
00048 #include "prlink.h"
00049 
00050 #include "nsIPlugin.h"
00051 #include "nsIPluginTagInfo2.h"
00052 #include "nsIPluginInstancePeer2.h"
00053 
00054 #include "nsIFileUtilities.h"
00055 #include "nsICookieStorage.h"
00056 #include "nsPluginsDir.h"
00057 #include "nsVoidArray.h"  // array for holding "active" streams
00058 #include "nsPluginDirServiceProvider.h"
00059 #include "nsAutoPtr.h"
00060 #include "nsWeakPtr.h"
00061 #include "nsIPrompt.h"
00062 #include "nsISupportsArray.h"
00063 #include "nsPluginNativeWindow.h"
00064 #include "nsIPrefBranch.h"
00065 
00066 class ns4xPlugin;
00067 class nsIComponentManager;
00068 class nsIFile;
00069 class nsIChannel;
00070 class nsIRegistry;
00071 class nsPluginHostImpl;
00072 
00078 class nsPluginTag
00079 {
00080 public:
00081   nsPluginTag();
00082   nsPluginTag(nsPluginTag* aPluginTag);
00083   nsPluginTag(nsPluginInfo* aPluginInfo);
00084 
00085   nsPluginTag(const char* aName,
00086               const char* aDescription,
00087               const char* aFileName,
00088               const char* aFullPath,
00089               const char* const* aMimeTypes,
00090               const char* const* aMimeDescriptions,
00091               const char* const* aExtensions,
00092               PRInt32 aVariants,
00093               PRInt64 aLastModifiedTime = 0,
00094               PRBool aCanUnload = PR_TRUE);
00095 
00096   ~nsPluginTag();
00097 
00098   void SetHost(nsPluginHostImpl * aHost);
00099   void TryUnloadPlugin(PRBool aForceShutdown = PR_FALSE);
00100   void Mark(PRUint32 mask) { mFlags |= mask; }
00101   PRBool Equals(nsPluginTag* aPluginTag);
00102 
00103   enum nsRegisterType {
00104     ePluginRegister,
00105     ePluginUnregister
00106   };
00107   void RegisterWithCategoryManager(PRBool aOverrideInternalTypes,
00108                                    nsRegisterType aType = ePluginRegister);
00109 
00110   nsPluginTag   *mNext;
00111   nsPluginHostImpl *mPluginHost;
00112   char          *mName;
00113   char          *mDescription;
00114   PRInt32       mVariants;
00115   char          **mMimeTypeArray;
00116   char          **mMimeDescriptionArray;
00117   char          **mExtensionsArray;
00118   PRLibrary     *mLibrary;
00119   nsIPlugin     *mEntryPoint;
00120   PRUint32      mFlags;
00121   PRPackedBool  mCanUnloadLibrary;
00122   PRPackedBool  mXPConnected;
00123   char          *mFileName;
00124   char          *mFullPath;
00125   PRInt64       mLastModifiedTime;
00126 };
00127 
00128 struct nsActivePlugin
00129 {
00130   nsActivePlugin*        mNext;
00131   char*                  mURL;
00132   nsIPluginInstancePeer* mPeer;
00133   nsPluginTag*           mPluginTag;
00134   nsIPluginInstance*     mInstance;
00135   PRTime                 mllStopTime;
00136   PRPackedBool           mStopped;
00137   PRPackedBool           mDefaultPlugin;
00138   PRPackedBool           mXPConnected;
00139   //Array holding all opened stream listeners for this entry
00140   nsCOMPtr <nsISupportsArray>  mStreams; 
00141 
00142   nsActivePlugin(nsPluginTag* aPluginTag,
00143                  nsIPluginInstance* aInstance, 
00144                  const char * url,
00145                  PRBool aDefaultPlugin,
00146                  nsIPluginInstancePeer *peer);
00147   ~nsActivePlugin();
00148 
00149   void setStopped(PRBool stopped);
00150 };
00151 
00152 class nsActivePluginList
00153 {
00154 public:
00155   nsActivePlugin * mFirst;
00156   nsActivePlugin * mLast;
00157   PRInt32 mCount;
00158 
00159   nsActivePluginList();
00160   ~nsActivePluginList();
00161 
00162   void shut();
00163   PRBool add(nsActivePlugin * plugin);
00164   PRBool remove(nsActivePlugin * plugin);
00165   nsActivePlugin * find(nsIPluginInstance* instance);
00166   nsActivePlugin * find(const char * mimetype);
00167   nsActivePlugin * findStopped(const char * url);
00168   PRUint32 getStoppedCount();
00169   nsActivePlugin * findOldestStopped();
00170   void removeAllStopped();
00171   void stopRunning(nsISupportsArray* aReloadDocs);
00172   PRBool IsLastInstance(nsActivePlugin * plugin);
00173 };
00174 
00175 #define NS_PLUGIN_FLAG_ENABLED    0x0001    //is this plugin enabled?
00176 #define NS_PLUGIN_FLAG_OLDSCHOOL  0x0002    //is this a pre-xpcom plugin?
00177 #define NS_PLUGIN_FLAG_FROMCACHE  0x0004    // this plugintag info was loaded from cache
00178 #define NS_PLUGIN_FLAG_UNWANTED   0x0008    // this is an unwanted plugin
00179 
00180 class nsPluginHostImpl : public nsIPluginManager2,
00181                          public nsIPluginHost,
00182                          public nsIFileUtilities,
00183                          public nsICookieStorage,
00184                          public nsIObserver,
00185                          public nsPIPluginHost
00186 {
00187 public:
00188   nsPluginHostImpl();
00189   virtual ~nsPluginHostImpl();
00190 
00191   static NS_METHOD
00192   Create(nsISupports* aOuter, REFNSIID aIID, void** aResult);
00193 
00194   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
00195 
00196   NS_DECL_ISUPPORTS
00197 
00198   static const char *GetPluginName(nsIPluginInstance *aPluginInstance);
00199 
00200   //nsIPluginManager interface - the main interface nsIPlugin communicates to
00201 
00202   NS_IMETHOD
00203   GetValue(nsPluginManagerVariable variable, void *value);
00204 
00205   NS_IMETHOD
00206   ReloadPlugins(PRBool reloadPages);
00207 
00208   NS_IMETHOD
00209   UserAgent(const char* *resultingAgentString);
00210 
00211   NS_IMETHOD
00212   GetURL(nsISupports* pluginInst, 
00213            const char* url, 
00214            const char* target = NULL,
00215            nsIPluginStreamListener* streamListener = NULL,
00216            const char* altHost = NULL,
00217            const char* referrer = NULL,
00218            PRBool forceJSEnabled = PR_FALSE);
00219 
00220   NS_IMETHOD
00221   GetURLWithHeaders(nsISupports* pluginInst, 
00222                     const char* url, 
00223                     const char* target = NULL,
00224                     nsIPluginStreamListener* streamListener = NULL,
00225                     const char* altHost = NULL,
00226                     const char* referrer = NULL,
00227                     PRBool forceJSEnabled = PR_FALSE,
00228                     PRUint32 getHeadersLength = 0, 
00229                     const char* getHeaders = NULL);
00230   
00231   NS_IMETHOD
00232   PostURL(nsISupports* pluginInst,
00233             const char* url,
00234             PRUint32 postDataLen, 
00235             const char* postData,
00236             PRBool isFile = PR_FALSE,
00237             const char* target = NULL,
00238             nsIPluginStreamListener* streamListener = NULL,
00239             const char* altHost = NULL, 
00240             const char* referrer = NULL,
00241             PRBool forceJSEnabled = PR_FALSE,
00242             PRUint32 postHeadersLength = 0, 
00243             const char* postHeaders = NULL);
00244 
00245   NS_IMETHOD
00246   RegisterPlugin(REFNSIID aCID,
00247                  const char* aPluginName,
00248                  const char* aDescription,
00249                  const char** aMimeTypes,
00250                  const char** aMimeDescriptions,
00251                  const char** aFileExtensions,
00252                  PRInt32 aCount);
00253 
00254   NS_IMETHOD
00255   UnregisterPlugin(REFNSIID aCID);
00256 
00257   //nsIPluginHost interface - used to communicate to the nsPluginInstanceOwner
00258 
00259   NS_IMETHOD
00260   Init(void);
00261 
00262   NS_IMETHOD
00263   Destroy(void);
00264 
00265   NS_IMETHOD
00266   LoadPlugins(void);
00267 
00268   NS_IMETHOD
00269   GetPluginFactory(const char *aMimeType, nsIPlugin** aPlugin);
00270 
00271   NS_IMETHOD
00272   InstantiateEmbeddedPlugin(const char *aMimeType, nsIURI* aURL, nsIPluginInstanceOwner *aOwner);
00273 
00274   NS_IMETHOD
00275   InstantiateFullPagePlugin(const char *aMimeType, nsIURI* aURI, nsIStreamListener *&aStreamListener, nsIPluginInstanceOwner *aOwner);
00276 
00277   NS_IMETHOD
00278   SetUpPluginInstance(const char *aMimeType, nsIURI *aURL, nsIPluginInstanceOwner *aOwner);
00279 
00280   NS_IMETHOD
00281   IsPluginEnabledForType(const char* aMimeType);
00282 
00283   NS_IMETHOD
00284   IsPluginEnabledForExtension(const char* aExtension, const char* &aMimeType);
00285 
00286   NS_IMETHOD
00287   GetPluginCount(PRUint32* aPluginCount);
00288   
00289   NS_IMETHOD
00290   GetPlugins(PRUint32 aPluginCount, nsIDOMPlugin** aPluginArray);
00291 
00292   NS_IMETHOD
00293   HandleBadPlugin(PRLibrary* aLibrary, nsIPluginInstance *instance);
00294 
00295   //nsIPluginManager2 interface - secondary methods that nsIPlugin communicates to
00296 
00297   NS_IMETHOD
00298   BeginWaitCursor(void);
00299 
00300   NS_IMETHOD
00301   EndWaitCursor(void);
00302 
00303   NS_IMETHOD
00304   SupportsURLProtocol(const char* protocol, PRBool *result);
00305 
00306   NS_IMETHOD
00307   NotifyStatusChange(nsIPlugin* plugin, nsresult errorStatus);
00308   
00309   NS_IMETHOD
00310   FindProxyForURL(const char* url, char* *result);
00311 
00312   NS_IMETHOD
00313   RegisterWindow(nsIEventHandler* handler, nsPluginPlatformWindowRef window);
00314   
00315   NS_IMETHOD
00316   UnregisterWindow(nsIEventHandler* handler, nsPluginPlatformWindowRef window);
00317 
00318   NS_IMETHOD
00319   AllocateMenuID(nsIEventHandler* handler, PRBool isSubmenu, PRInt16 *result);
00320 
00321   NS_IMETHOD
00322   DeallocateMenuID(nsIEventHandler* handler, PRInt16 menuID);
00323 
00324   NS_IMETHOD
00325   HasAllocatedMenuID(nsIEventHandler* handler, PRInt16 menuID, PRBool *result);
00326 
00327   NS_IMETHOD
00328   ProcessNextEvent(PRBool *bEventHandled);
00329 
00330   // nsIFactory interface, from nsIPlugin.
00331   // XXX not currently used?
00332   NS_IMETHOD CreateInstance(nsISupports *aOuter,
00333                             REFNSIID aIID,
00334                             void **aResult);
00335 
00336   NS_IMETHOD LockFactory(PRBool aLock);
00337 
00338   // nsIFileUtilities interface
00339 
00340   NS_IMETHOD GetProgramPath(const char* *result);
00341 
00342   NS_IMETHOD GetTempDirPath(const char* *result);
00343 
00344   NS_IMETHOD NewTempFileName(const char* prefix, PRUint32 bufLen, char* resultBuf);
00345 
00346   // nsICookieStorage interface
00347 
00354   NS_IMETHOD
00355   GetCookie(const char* inCookieURL, void* inOutCookieBuffer, PRUint32& inOutCookieSize);
00356   
00363   NS_IMETHOD
00364   SetCookie(const char* inCookieURL, const void* inCookieBuffer, PRUint32 inCookieSize);
00365   
00366   // Methods from nsIObserver
00367   NS_IMETHOD
00368   Observe(nsISupports *aSubject, const char *aTopic, const PRUnichar *someData);
00369 
00370   // Methods from nsPIPluginHost
00371   NS_IMETHOD
00372   SetIsScriptableInstance(nsIPluginInstance *aPluginInstance, PRBool aScriptable);
00373 
00374   NS_IMETHOD
00375   ParsePostBufferToFixHeaders(const char *inPostData, PRUint32 inPostDataLen, 
00376               char **outPostData, PRUint32 *outPostDataLen);
00377   
00378   NS_IMETHOD
00379   CreateTmpFileToPost(const char *postDataURL, char **pTmpFileName);
00380 
00381   NS_IMETHOD
00382   NewPluginNativeWindow(nsPluginNativeWindow ** aPluginNativeWindow);
00383 
00384   NS_IMETHOD
00385   DeletePluginNativeWindow(nsPluginNativeWindow * aPluginNativeWindow);
00386 
00387   /* Called by GetURL and PostURL */
00388 
00389   NS_IMETHOD
00390   NewPluginURLStream(const nsString& aURL, 
00391                      nsIPluginInstance *aInstance, 
00392                      nsIPluginStreamListener *aListener,
00393                      const char *aPostData = nsnull, 
00394                      PRBool isFile = PR_FALSE,
00395                      PRUint32 aPostDataLen = 0, 
00396                      const char *aHeadersData = nsnull, 
00397                      PRUint32 aHeadersDataLen = 0);
00398 
00399   nsresult
00400   DoURLLoadSecurityCheck(nsIPluginInstance *aInstance,
00401                          const char* aURL);
00402 
00403   NS_IMETHOD
00404   AddHeadersToChannel(const char *aHeadersData, PRUint32 aHeadersDataLen, 
00405                       nsIChannel *aGenericChannel);
00406 
00407   NS_IMETHOD
00408   StopPluginInstance(nsIPluginInstance* aInstance);
00409 
00410   NS_IMETHOD
00411   AddUnusedLibrary(PRLibrary * aLibrary);
00412 
00413   static nsresult GetPluginTempDir(nsIFile **aDir);
00414 
00415 private:
00416   NS_IMETHOD
00417   TrySetUpPluginInstance(const char *aMimeType, nsIURI *aURL, nsIPluginInstanceOwner *aOwner);
00418 
00419   nsresult
00420   LoadXPCOMPlugins(nsIComponentManager* aComponentManager);
00421 
00422   nsresult
00423   NewEmbeddedPluginStream(nsIURI* aURL, nsIPluginInstanceOwner *aOwner, nsIPluginInstance* aInstance);
00424 
00425   nsresult
00426   NewFullPagePluginStream(nsIStreamListener *&aStreamListener, nsIPluginInstance *aInstance);
00427 
00428   nsresult
00429   FindPluginEnabledForType(const char* aMimeType, nsPluginTag* &aPlugin);
00430 
00431   nsresult
00432   FindStoppedPluginForURL(nsIURI* aURL, nsIPluginInstanceOwner *aOwner);
00433 
00434   nsresult
00435   SetUpDefaultPluginInstance(const char *aMimeType, nsIURI *aURL, nsIPluginInstanceOwner *aOwner);
00436 
00437   nsresult
00438   AddInstanceToActiveList(nsCOMPtr<nsIPlugin> aPlugin,
00439                           nsIPluginInstance* aInstance,
00440                           nsIURI* aURL, PRBool aDefaultPlugin,
00441                           nsIPluginInstancePeer *peer);
00442 
00443   nsresult
00444   FindPlugins(PRBool aCreatePluginList, PRBool * aPluginsChanged);
00445 
00446   nsresult
00447   ScanPluginsDirectory(nsIFile * pluginsDir, 
00448                        nsIComponentManager * compManager, 
00449                        PRBool aCreatePluginList,
00450                        PRBool * aPluginsChanged,
00451                        PRBool checkForUnwantedPlugins = PR_FALSE);
00452                        
00453   nsresult
00454   ScanPluginsDirectoryList(nsISimpleEnumerator * dirEnum,
00455                            nsIComponentManager * compManager, 
00456                            PRBool aCreatePluginList,
00457                            PRBool * aPluginsChanged,
00458                            PRBool checkForUnwantedPlugins = PR_FALSE);
00459 
00460   PRBool IsRunningPlugin(nsPluginTag * plugin);
00461 
00462   // Stores all plugins info into the registry
00463   nsresult WritePluginInfo();
00464 
00465   // Loads all cached plugins info into mCachedPlugins
00466   nsresult ReadPluginInfo();
00467 
00468   // Given a filename, returns the plugins info from our cache
00469   // and removes it from the cache.
00470   nsPluginTag* RemoveCachedPluginsInfo(const char *filename);
00471 
00472   //checks if the list already have the same plugin as given
00473   nsPluginTag* HaveSamePlugin(nsPluginTag * aPluginTag);
00474 
00475   // checks if given plugin is a duplicate of what we already have
00476   // in the plugin list but found in some different place
00477   PRBool IsDuplicatePlugin(nsPluginTag * aPluginTag);
00478   
00479   // destroys plugin info list
00480   void ClearCachedPluginInfoList();
00481   
00482   nsresult EnsurePrivateDirServiceProvider();
00483 
00484   nsresult GetPrompt(nsIPluginInstanceOwner *aOwner, nsIPrompt **aPrompt);
00485 
00486   // one-off hack to include nppl3260.dll from the components folder
00487   nsresult ScanForRealInComponentsFolder(nsIComponentManager * aCompManager);
00488 
00489   // calls PostPluginUnloadEvent for each library in mUnusedLibraries
00490   void UnloadUnusedLibraries();
00491 
00492   char        *mPluginPath;
00493   nsPluginTag *mPlugins;
00494   nsPluginTag *mCachedPlugins;
00495   PRPackedBool mPluginsLoaded;
00496   PRPackedBool mDontShowBadPluginMessage;
00497   PRPackedBool mIsDestroyed;
00498 
00499   // set by pref plugin.override_internal_types
00500   PRPackedBool mOverrideInternalTypes;
00501 
00502   // set by pref plugin.allow_alien_star_handler
00503   PRPackedBool mAllowAlienStarHandler;
00504 
00505   // set by pref plugin.default_plugin_disabled
00506   PRPackedBool mDefaultPluginDisabled;
00507 
00508   nsActivePluginList mActivePluginList;
00509   nsVoidArray mUnusedLibraries;
00510 
00511   nsCOMPtr<nsIFile>                    mPluginRegFile;
00512   nsCOMPtr<nsIPrefBranch>              mPrefService;
00513   nsRefPtr<nsPluginDirServiceProvider> mPrivateDirServiceProvider;
00514 
00515   nsWeakPtr mCurrentDocument; // weak reference, we use it to id document only
00516 
00517   static nsIFile *sPluginTempDir;
00518 };
00519 
00520 #endif