michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsPluginHost_h_ michael@0: #define nsPluginHost_h_ michael@0: michael@0: #include "nsIPluginHost.h" michael@0: #include "nsIObserver.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "prlink.h" michael@0: #include "prclist.h" michael@0: #include "npapi.h" michael@0: #include "nsIPluginTag.h" michael@0: #include "nsPluginsDir.h" michael@0: #include "nsPluginDirServiceProvider.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsWeakPtr.h" michael@0: #include "nsIPrompt.h" michael@0: #include "nsWeakReference.h" michael@0: #include "MainThreadUtils.h" michael@0: #include "nsTArray.h" michael@0: #include "nsTObserverArray.h" michael@0: #include "nsITimer.h" michael@0: #include "nsPluginTags.h" michael@0: #include "nsPluginPlayPreviewInfo.h" michael@0: #include "nsIEffectiveTLDService.h" michael@0: #include "nsIIDNService.h" michael@0: #include "nsCRT.h" michael@0: michael@0: class nsNPAPIPlugin; michael@0: class nsIComponentManager; michael@0: class nsIFile; michael@0: class nsIChannel; michael@0: class nsPluginNativeWindow; michael@0: class nsObjectLoadingContent; michael@0: class nsPluginInstanceOwner; michael@0: class nsNPAPIPluginInstance; michael@0: class nsNPAPIPluginStreamListener; michael@0: class nsIPluginInstanceOwner; michael@0: class nsIInputStream; michael@0: class nsIStreamListener; michael@0: michael@0: class nsInvalidPluginTag : public nsISupports michael@0: { michael@0: public: michael@0: nsInvalidPluginTag(const char* aFullPath, int64_t aLastModifiedTime = 0); michael@0: virtual ~nsInvalidPluginTag(); michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: michael@0: nsCString mFullPath; michael@0: int64_t mLastModifiedTime; michael@0: bool mSeen; michael@0: michael@0: nsRefPtr mPrev; michael@0: nsRefPtr mNext; michael@0: }; michael@0: michael@0: class nsPluginHost : public nsIPluginHost, michael@0: public nsIObserver, michael@0: public nsITimerCallback, michael@0: public nsSupportsWeakReference michael@0: { michael@0: public: michael@0: nsPluginHost(); michael@0: virtual ~nsPluginHost(); michael@0: michael@0: static already_AddRefed GetInst(); michael@0: michael@0: NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW michael@0: michael@0: NS_DECL_ISUPPORTS michael@0: NS_DECL_NSIPLUGINHOST michael@0: NS_DECL_NSIOBSERVER michael@0: NS_DECL_NSITIMERCALLBACK michael@0: michael@0: nsresult Init(); michael@0: nsresult LoadPlugins(); michael@0: nsresult UnloadPlugins(); michael@0: michael@0: nsresult SetUpPluginInstance(const char *aMimeType, michael@0: nsIURI *aURL, michael@0: nsPluginInstanceOwner *aOwner); michael@0: bool PluginExistsForType(const char* aMimeType); michael@0: michael@0: nsresult IsPluginEnabledForExtension(const char* aExtension, const char* &aMimeType); michael@0: michael@0: void GetPlugins(nsTArray >& aPluginArray); michael@0: michael@0: nsresult GetURL(nsISupports* pluginInst, michael@0: const char* url, michael@0: const char* target, michael@0: nsNPAPIPluginStreamListener* streamListener, michael@0: const char* altHost, michael@0: const char* referrer, michael@0: bool forceJSEnabled); michael@0: nsresult PostURL(nsISupports* pluginInst, michael@0: const char* url, michael@0: uint32_t postDataLen, michael@0: const char* postData, michael@0: bool isFile, michael@0: const char* target, michael@0: nsNPAPIPluginStreamListener* streamListener, michael@0: const char* altHost, michael@0: const char* referrer, michael@0: bool forceJSEnabled, michael@0: uint32_t postHeadersLength, michael@0: const char* postHeaders); michael@0: michael@0: nsresult FindProxyForURL(const char* url, char* *result); michael@0: nsresult UserAgent(const char **retstring); michael@0: nsresult ParsePostBufferToFixHeaders(const char *inPostData, uint32_t inPostDataLen, michael@0: char **outPostData, uint32_t *outPostDataLen); michael@0: nsresult CreateTempFileToPost(const char *aPostDataURL, nsIFile **aTmpFile); michael@0: nsresult NewPluginNativeWindow(nsPluginNativeWindow ** aPluginNativeWindow); michael@0: michael@0: void AddIdleTimeTarget(nsIPluginInstanceOwner* objectFrame, bool isVisible); michael@0: void RemoveIdleTimeTarget(nsIPluginInstanceOwner* objectFrame); michael@0: michael@0: nsresult GetPluginName(nsNPAPIPluginInstance *aPluginInstance, const char** aPluginName); michael@0: nsresult StopPluginInstance(nsNPAPIPluginInstance* aInstance); michael@0: nsresult GetPluginTagForInstance(nsNPAPIPluginInstance *aPluginInstance, nsIPluginTag **aPluginTag); michael@0: michael@0: nsresult michael@0: NewPluginURLStream(const nsString& aURL, michael@0: nsNPAPIPluginInstance *aInstance, michael@0: nsNPAPIPluginStreamListener *aListener, michael@0: nsIInputStream *aPostStream = nullptr, michael@0: const char *aHeadersData = nullptr, michael@0: uint32_t aHeadersDataLen = 0); michael@0: michael@0: nsresult michael@0: GetURLWithHeaders(nsNPAPIPluginInstance *pluginInst, michael@0: const char* url, michael@0: const char* target = nullptr, michael@0: nsNPAPIPluginStreamListener* streamListener = nullptr, michael@0: const char* altHost = nullptr, michael@0: const char* referrer = nullptr, michael@0: bool forceJSEnabled = false, michael@0: uint32_t getHeadersLength = 0, michael@0: const char* getHeaders = nullptr); michael@0: michael@0: nsresult michael@0: DoURLLoadSecurityCheck(nsNPAPIPluginInstance *aInstance, michael@0: const char* aURL); michael@0: michael@0: nsresult michael@0: AddHeadersToChannel(const char *aHeadersData, uint32_t aHeadersDataLen, michael@0: nsIChannel *aGenericChannel); michael@0: michael@0: static nsresult GetPluginTempDir(nsIFile **aDir); michael@0: michael@0: // Writes updated plugins settings to disk and unloads the plugin michael@0: // if it is now disabled michael@0: nsresult UpdatePluginInfo(nsPluginTag* aPluginTag); michael@0: michael@0: // Helper that checks if a type is whitelisted in plugin.allowed_types. michael@0: // Always returns true if plugin.allowed_types is not set michael@0: static bool IsTypeWhitelisted(const char *aType); michael@0: michael@0: // checks whether aTag is a "java" plugin tag (a tag for a plugin michael@0: // that does Java) michael@0: static bool IsJavaMIMEType(const char *aType); michael@0: michael@0: static nsresult PostPluginUnloadEvent(PRLibrary* aLibrary); michael@0: michael@0: void PluginCrashed(nsNPAPIPlugin* plugin, michael@0: const nsAString& pluginDumpID, michael@0: const nsAString& browserDumpID); michael@0: michael@0: nsNPAPIPluginInstance *FindInstance(const char *mimetype); michael@0: nsNPAPIPluginInstance *FindOldestStoppedInstance(); michael@0: uint32_t StoppedInstanceCount(); michael@0: michael@0: nsTArray< nsRefPtr > *InstanceArray(); michael@0: michael@0: void DestroyRunningInstances(nsPluginTag* aPluginTag); michael@0: michael@0: // Return the tag for |aLibrary| if found, nullptr if not. michael@0: nsPluginTag* FindTagForLibrary(PRLibrary* aLibrary); michael@0: michael@0: // The last argument should be false if we already have an in-flight stream michael@0: // and don't need to set up a new stream. michael@0: nsresult InstantiatePluginInstance(const char *aMimeType, nsIURI* aURL, michael@0: nsObjectLoadingContent *aContent, michael@0: nsPluginInstanceOwner** aOwner); michael@0: michael@0: // Does not accept nullptr and should never fail. michael@0: nsPluginTag* TagForPlugin(nsNPAPIPlugin* aPlugin); michael@0: michael@0: nsresult GetPlugin(const char *aMimeType, nsNPAPIPlugin** aPlugin); michael@0: michael@0: nsresult NewPluginStreamListener(nsIURI* aURL, michael@0: nsNPAPIPluginInstance* aInstance, michael@0: nsIStreamListener **aStreamListener); michael@0: michael@0: private: michael@0: nsresult michael@0: TrySetUpPluginInstance(const char *aMimeType, nsIURI *aURL, nsPluginInstanceOwner *aOwner); michael@0: michael@0: nsPluginTag* michael@0: FindPreferredPlugin(const InfallibleTArray& matches); michael@0: michael@0: // Return an nsPluginTag for this type, if any. If aCheckEnabled is michael@0: // true, only enabled plugins will be returned. michael@0: nsPluginTag* michael@0: FindPluginForType(const char* aMimeType, bool aCheckEnabled); michael@0: michael@0: nsPluginTag* michael@0: FindPluginEnabledForExtension(const char* aExtension, const char* &aMimeType); michael@0: michael@0: nsresult michael@0: FindStoppedPluginForURL(nsIURI* aURL, nsIPluginInstanceOwner *aOwner); michael@0: michael@0: nsresult michael@0: FindPlugins(bool aCreatePluginList, bool * aPluginsChanged); michael@0: michael@0: // Registers or unregisters the given mime type with the category manager michael@0: // (performs no checks - see UpdateCategoryManager) michael@0: enum nsRegisterType { ePluginRegister, ePluginUnregister }; michael@0: void RegisterWithCategoryManager(nsCString &aMimeType, nsRegisterType aType); michael@0: michael@0: nsresult michael@0: ScanPluginsDirectory(nsIFile *pluginsDir, michael@0: bool aCreatePluginList, michael@0: bool *aPluginsChanged); michael@0: michael@0: nsresult michael@0: ScanPluginsDirectoryList(nsISimpleEnumerator *dirEnum, michael@0: bool aCreatePluginList, michael@0: bool *aPluginsChanged); michael@0: michael@0: nsresult EnsurePluginLoaded(nsPluginTag* aPluginTag); michael@0: michael@0: bool IsRunningPlugin(nsPluginTag * aPluginTag); michael@0: michael@0: // Stores all plugins info into the registry michael@0: nsresult WritePluginInfo(); michael@0: michael@0: // Loads all cached plugins info into mCachedPlugins michael@0: nsresult ReadPluginInfo(); michael@0: michael@0: PRBool GhettoBlacklist(nsIFile *pluginFile); michael@0: michael@0: // Given a file path, returns the plugins info from our cache michael@0: // and removes it from the cache. michael@0: void RemoveCachedPluginsInfo(const char *filePath, michael@0: nsPluginTag **result); michael@0: michael@0: // Checks to see if a tag object is in our list of live tags. michael@0: bool IsLiveTag(nsIPluginTag* tag); michael@0: michael@0: // Checks our list of live tags for an equivalent tag. michael@0: nsPluginTag* HaveSamePlugin(const nsPluginTag * aPluginTag); michael@0: michael@0: // Returns the first plugin at |path| michael@0: nsPluginTag* FirstPluginWithPath(const nsCString& path); michael@0: michael@0: nsresult EnsurePrivateDirServiceProvider(); michael@0: michael@0: void OnPluginInstanceDestroyed(nsPluginTag* aPluginTag); michael@0: michael@0: nsRefPtr mPlugins; michael@0: nsRefPtr mCachedPlugins; michael@0: nsRefPtr mInvalidPlugins; michael@0: nsTArray< nsRefPtr > mPlayPreviewMimeTypes; michael@0: bool mPluginsLoaded; michael@0: michael@0: // set by pref plugin.override_internal_types michael@0: bool mOverrideInternalTypes; michael@0: michael@0: // set by pref plugin.disable michael@0: bool mPluginsDisabled; michael@0: // set by pref plugins.click_to_play michael@0: bool mPluginsClickToPlay; michael@0: michael@0: // Any instances in this array will have valid plugin objects via GetPlugin(). michael@0: // When removing an instance it might not die - be sure to null out it's plugin. michael@0: nsTArray< nsRefPtr > mInstances; michael@0: michael@0: nsCOMPtr mPluginRegFile; michael@0: #ifdef XP_WIN michael@0: nsRefPtr mPrivateDirServiceProvider; michael@0: #endif michael@0: michael@0: nsCOMPtr mTLDService; michael@0: nsCOMPtr mIDNService; michael@0: michael@0: // Helpers for ClearSiteData and SiteHasData. michael@0: nsresult NormalizeHostname(nsCString& host); michael@0: nsresult EnumerateSiteData(const nsACString& domain, michael@0: const InfallibleTArray& sites, michael@0: InfallibleTArray& result, michael@0: bool firstMatchOnly); michael@0: michael@0: nsWeakPtr mCurrentDocument; // weak reference, we use it to id document only michael@0: michael@0: static nsIFile *sPluginTempDir; michael@0: michael@0: // We need to hold a global ptr to ourselves because we register for michael@0: // two different CIDs for some reason... michael@0: static nsPluginHost* sInst; michael@0: }; michael@0: michael@0: class MOZ_STACK_CLASS PluginDestructionGuard : protected PRCList michael@0: { michael@0: public: michael@0: PluginDestructionGuard(nsNPAPIPluginInstance *aInstance); michael@0: michael@0: PluginDestructionGuard(NPP npp); michael@0: michael@0: ~PluginDestructionGuard(); michael@0: michael@0: static bool DelayDestroy(nsNPAPIPluginInstance *aInstance); michael@0: michael@0: protected: michael@0: void Init() michael@0: { michael@0: NS_ASSERTION(NS_IsMainThread(), "Should be on the main thread"); michael@0: michael@0: mDelayedDestroy = false; michael@0: michael@0: PR_INIT_CLIST(this); michael@0: PR_INSERT_BEFORE(this, &sListHead); michael@0: } michael@0: michael@0: nsRefPtr mInstance; michael@0: bool mDelayedDestroy; michael@0: michael@0: static PRCList sListHead; michael@0: }; michael@0: michael@0: #endif // nsPluginHost_h_