Bug 712975 - Observe plugin pref changes. r=josh a=blocking-fennec
authorBrian Nicholson <bnicholson@mozilla.com>
Thu, 05 Jan 2012 16:02:51 -0800
changeset 92290 d4e74ea141278a73e272858820861e2286a3cb22
parent 92289 79af6cb5647cc894ec0217cf41ff0cd8adb47080
child 92291 c2a4edbb377ba8bb1ec48ec4411a244c9d6ab416
push id22529
push usereakhgari@mozilla.com
push dateWed, 25 Apr 2012 14:06:19 +0000
treeherdermozilla-central@83ef3c9a64b5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjosh, blocking-fennec
bugs712975
milestone14.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 712975 - Observe plugin pref changes. r=josh a=blocking-fennec
dom/plugins/base/nsPluginHost.cpp
dom/plugins/base/nsPluginHost.h
--- a/dom/plugins/base/nsPluginHost.cpp
+++ b/dom/plugins/base/nsPluginHost.cpp
@@ -348,16 +348,18 @@ nsPluginHost::nsPluginHost()
 {
   // check to see if pref is set at startup to let plugins take over in
   // full page mode for certain image mime types that we handle internally
   mOverrideInternalTypes =
     Preferences::GetBool("plugin.override_internal_types", false);
 
   mPluginsDisabled = Preferences::GetBool("plugin.disable", false);
 
+  Preferences::AddStrongObserver(this, "plugin.disable");
+
   nsCOMPtr<nsIObserverService> obsService =
     mozilla::services::GetObserverService();
   if (obsService) {
     obsService->AddObserver(this, NS_XPCOM_SHUTDOWN_OBSERVER_ID, false);
     obsService->AddObserver(this, NS_PRIVATE_BROWSING_SWITCH_TOPIC, false);
 #ifdef MOZ_WIDGET_ANDROID
     obsService->AddObserver(this, "application-foreground", false);
     obsService->AddObserver(this, "application-background", false);
@@ -382,17 +384,17 @@ nsPluginHost::nsPluginHost()
   mHiddenPluginTimer = do_CreateInstance(NS_TIMER_CONTRACTID);
 #endif
 }
 
 nsPluginHost::~nsPluginHost()
 {
   PLUGIN_LOG(PLUGIN_LOG_ALWAYS,("nsPluginHost::dtor\n"));
 
-  Destroy();
+  UnloadPlugins();
   sInst = nsnull;
 }
 
 NS_IMPL_ISUPPORTS4(nsPluginHost,
                    nsIPluginHost,
                    nsIObserver,
                    nsITimerCallback,
                    nsISupportsWeakReference)
@@ -806,25 +808,23 @@ nsresult nsPluginHost::FindProxyForURL(c
   return res;
 }
 
 nsresult nsPluginHost::Init()
 {
   return NS_OK;
 }
 
-nsresult nsPluginHost::Destroy()
+nsresult nsPluginHost::UnloadPlugins()
 {
-  PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsPluginHost::Destroy Called\n"));
-
-  if (mIsDestroyed)
+  PLUGIN_LOG(PLUGIN_LOG_NORMAL, ("nsPluginHost::UnloadPlugins Called\n"));
+
+  if (!mPluginsLoaded)
     return NS_OK;
 
-  mIsDestroyed = true;
-
   // we should call nsIPluginInstance::Stop and nsIPluginInstance::SetWindow
   // for those plugins who want it
   DestroyRunningInstances(nsnull, nsnull);
 
   nsPluginTag *pluginTag;
   for (pluginTag = mPlugins; pluginTag; pluginTag = pluginTag->mNext) {
     pluginTag->TryUnloadPlugin(true);
   }
@@ -844,16 +844,18 @@ nsresult nsPluginHost::Destroy()
     nsCOMPtr<nsIDirectoryService> dirService =
       do_GetService(kDirectoryServiceContractID);
     if (dirService)
       dirService->UnregisterProvider(mPrivateDirServiceProvider);
     mPrivateDirServiceProvider = nsnull;
   }
 #endif /* XP_WIN */
 
+  mPluginsLoaded = false;
+
   return NS_OK;
 }
 
 void nsPluginHost::OnPluginInstanceDestroyed(nsPluginTag* aPluginTag)
 {
   bool hasInstance = false;
   for (PRUint32 i = 0; i < mInstances.Length(); i++) {
     if (TagForPlugin(mInstances[i]->GetPlugin()) == aPluginTag) {
@@ -3321,25 +3323,34 @@ nsresult nsPluginHost::NewFullPagePlugin
 }
 
 NS_IMETHODIMP nsPluginHost::Observe(nsISupports *aSubject,
                                     const char *aTopic,
                                     const PRUnichar *someData)
 {
   if (!nsCRT::strcmp(NS_XPCOM_SHUTDOWN_OBSERVER_ID, aTopic)) {
     OnShutdown();
-    Destroy();
+    UnloadPlugins();
     sInst->Release();
   }
   if (!nsCRT::strcmp(NS_PRIVATE_BROWSING_SWITCH_TOPIC, aTopic)) {
     // inform all active plugins of changed private mode state
     for (PRUint32 i = 0; i < mInstances.Length(); i++) {
       mInstances[i]->PrivateModeStateChanged();
     }
   }
+  if (!nsCRT::strcmp(NS_PREFBRANCH_PREFCHANGE_TOPIC_ID, aTopic)) {
+    mPluginsDisabled = Preferences::GetBool("plugin.disable", false);
+    // Unload or load plugins as needed
+    if (mPluginsDisabled) {
+      UnloadPlugins();
+    } else {
+      LoadPlugins();
+    }
+  }
 #ifdef MOZ_WIDGET_ANDROID
   if (!nsCRT::strcmp("application-background", aTopic)) {
     for(PRUint32 i = 0; i < mInstances.Length(); i++) {
       mInstances[i]->NotifyForeground(false);
     }
   }
   if (!nsCRT::strcmp("application-foreground", aTopic)) {
     for(PRUint32 i = 0; i < mInstances.Length(); i++) {
--- a/dom/plugins/base/nsPluginHost.h
+++ b/dom/plugins/base/nsPluginHost.h
@@ -105,18 +105,18 @@ public:
   NS_DECL_AND_IMPL_ZEROING_OPERATOR_NEW
 
   NS_DECL_ISUPPORTS
   NS_DECL_NSIPLUGINHOST
   NS_DECL_NSIOBSERVER
   NS_DECL_NSITIMERCALLBACK
 
   nsresult Init();
-  nsresult Destroy();
   nsresult LoadPlugins();
+  nsresult UnloadPlugins();
   nsresult CreateListenerForChannel(nsIChannel* aChannel,
                                     nsObjectLoadingContent* aContent,
                                     nsIStreamListener** aListener);
   nsresult SetUpPluginInstance(const char *aMimeType,
                                nsIURI *aURL,
                                nsIPluginInstanceOwner *aOwner);
   nsresult IsPluginEnabledForType(const char* aMimeType);
   nsresult IsPluginEnabledForExtension(const char* aExtension, const char* &aMimeType);
@@ -303,17 +303,16 @@ private:
 
   void OnPluginInstanceDestroyed(nsPluginTag* aPluginTag);
 
   nsRefPtr<nsPluginTag> mPlugins;
   nsRefPtr<nsPluginTag> mCachedPlugins;
   nsRefPtr<nsInvalidPluginTag> mInvalidPlugins;
   bool mPluginsLoaded;
   bool mDontShowBadPluginMessage;
-  bool mIsDestroyed;
 
   // set by pref plugin.override_internal_types
   bool mOverrideInternalTypes;
 
   // set by pref plugin.disable
   bool mPluginsDisabled;
 
   // Any instances in this array will have valid plugin objects via GetPlugin().