diff --git a/dom/base/Navigator.cpp b/dom/base/Navigator.cpp index 0f81421fe9d..1b109d6d2a9 100644 --- a/dom/base/Navigator.cpp +++ b/dom/base/Navigator.cpp @@ -38,7 +38,6 @@ #include "mozilla/ClearOnShutdown.h" #include "mozilla/StaticPtr.h" #include "Connection.h" -#include "nsIObserverService.h" #ifdef MOZ_B2G_RIL #include "MobileConnection.h" #include "mozilla/dom/CellBroadcast.h" @@ -104,10 +103,6 @@ Navigator::Navigator(nsPIDOMWindow* aWindow) { NS_ASSERTION(aWindow->IsInnerWindow(), "Navigator must get an inner window!"); - nsCOMPtr obsService = - mozilla::services::GetObserverService(); - if (obsService) - obsService->AddObserver(this, "plugin-info-updated", false); } Navigator::~Navigator() @@ -125,7 +120,6 @@ NS_INTERFACE_MAP_BEGIN(Navigator) NS_INTERFACE_MAP_ENTRY(nsIDOMNavigatorDesktopNotification) NS_INTERFACE_MAP_ENTRY(nsIDOMMozNavigatorSms) NS_INTERFACE_MAP_ENTRY(nsIDOMMozNavigatorMobileMessage) - NS_INTERFACE_MAP_ENTRY(nsIObserver) #ifdef MOZ_MEDIA_NAVIGATOR NS_INTERFACE_MAP_ENTRY(nsINavigatorUserMedia) NS_INTERFACE_MAP_ENTRY(nsIDOMNavigatorUserMedia) @@ -161,12 +155,6 @@ Navigator::Invalidate() { mWindow = nullptr; - nsCOMPtr obsService = - mozilla::services::GetObserverService(); - if (obsService) { - obsService->RemoveObserver(this, "plugin-info-updated"); - } - if (mPlugins) { mPlugins->Invalidate(); mPlugins = nullptr; @@ -268,20 +256,6 @@ Navigator::GetWindow() return win; } -//***************************************************************************** -// Navigator::nsIObserver -//***************************************************************************** - -NS_IMETHODIMP -Navigator::Observe(nsISupports *aSubject, const char *aTopic, - const PRUnichar *aData) { - if (!nsCRT::strcmp(aTopic, "plugin-info-updated") && mPlugins) { - mPlugins->Refresh(false); - } - - return NS_OK; -} - //***************************************************************************** // Navigator::nsIDOMNavigator //***************************************************************************** @@ -479,6 +453,7 @@ Navigator::GetPlugins(nsIDOMPluginArray** aPlugins) nsCOMPtr win(do_QueryReferent(mWindow)); mPlugins = new nsPluginArray(this, win ? win->GetDocShell() : nullptr); + mPlugins->Init(); } NS_ADDREF(*aPlugins = mPlugins); diff --git a/dom/base/Navigator.h b/dom/base/Navigator.h index 8068905ee40..4880c587993 100644 --- a/dom/base/Navigator.h +++ b/dom/base/Navigator.h @@ -16,7 +16,6 @@ #include "nsIDOMNavigatorSms.h" #include "nsIDOMNavigatorMobileMessage.h" #include "nsIDOMNavigatorNetwork.h" -#include "nsIObserver.h" #ifdef MOZ_AUDIO_CHANNEL_MANAGER #include "nsINavigatorAudioChannelManager.h" #endif @@ -98,7 +97,6 @@ class Navigator : public nsIDOMNavigator , public nsINavigatorBattery , public nsIDOMMozNavigatorSms , public nsIDOMMozNavigatorMobileMessage - , public nsIObserver #ifdef MOZ_MEDIA_NAVIGATOR , public nsINavigatorUserMedia , public nsIDOMNavigatorUserMedia @@ -137,7 +135,6 @@ public: NS_DECL_NSINAVIGATORBATTERY NS_DECL_NSIDOMMOZNAVIGATORSMS NS_DECL_NSIDOMMOZNAVIGATORMOBILEMESSAGE - NS_DECL_NSIOBSERVER #ifdef MOZ_MEDIA_NAVIGATOR NS_DECL_NSINAVIGATORUSERMEDIA NS_DECL_NSIDOMNAVIGATORUSERMEDIA diff --git a/dom/base/nsPluginArray.cpp b/dom/base/nsPluginArray.cpp index 7a3c6caf379..acda27a29b1 100644 --- a/dom/base/nsPluginArray.cpp +++ b/dom/base/nsPluginArray.cpp @@ -17,6 +17,9 @@ #include "nsPluginHost.h" #include "nsIContentViewer.h" #include "nsIDocument.h" +#include "nsIObserverService.h" +#include "nsIWeakReference.h" +#include "mozilla/Services.h" using namespace mozilla; using namespace mozilla::dom; @@ -31,6 +34,16 @@ nsPluginArray::nsPluginArray(Navigator* navigator, { } +void +nsPluginArray::Init() +{ + nsCOMPtr obsService = + mozilla::services::GetObserverService(); + if (obsService) { + obsService->AddObserver(this, "plugin-info-updated", true); + } +} + nsPluginArray::~nsPluginArray() { if (mPluginArray != nullptr) { @@ -47,6 +60,8 @@ DOMCI_DATA(PluginArray, nsPluginArray) NS_INTERFACE_MAP_BEGIN(nsPluginArray) NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMPluginArray) NS_INTERFACE_MAP_ENTRY(nsIDOMPluginArray) + NS_INTERFACE_MAP_ENTRY(nsIObserver) + NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference) NS_DOM_INTERFACE_MAP_ENTRY_CLASSINFO(PluginArray) NS_INTERFACE_MAP_END @@ -166,6 +181,12 @@ nsPluginArray::Invalidate() { mDocShell = nullptr; mNavigator = nullptr; + + nsCOMPtr obsService = + mozilla::services::GetObserverService(); + if (obsService) { + obsService->RemoveObserver(this, "plugin-info-updated"); + } } NS_IMETHODIMP @@ -256,7 +277,15 @@ nsPluginArray::GetPlugins() return rv; } -// +NS_IMETHODIMP +nsPluginArray::Observe(nsISupports *aSubject, const char *aTopic, + const PRUnichar *aData) { + if (!nsCRT::strcmp(aTopic, "plugin-info-updated")) { + Refresh(false); + } + + return NS_OK; +} nsPluginElement::nsPluginElement(nsIDOMPlugin* plugin) { diff --git a/dom/base/nsPluginArray.h b/dom/base/nsPluginArray.h index f0a98d66764..e9fd6865ed2 100644 --- a/dom/base/nsPluginArray.h +++ b/dom/base/nsPluginArray.h @@ -12,6 +12,7 @@ #include "nsIPluginHost.h" #include "nsIURL.h" #include "nsWeakReference.h" +#include "nsIObserver.h" namespace mozilla { namespace dom { @@ -24,6 +25,8 @@ class nsIDocShell; // NB: Due to weak references, Navigator has intimate knowledge of our // internals. class nsPluginArray : public nsIDOMPluginArray + , public nsIObserver + , public nsSupportsWeakReference { public: nsPluginArray(mozilla::dom::Navigator* navigator, nsIDocShell *aDocShell); @@ -33,6 +36,14 @@ public: // nsIDOMPluginArray NS_DECL_NSIDOMPLUGINARRAY + // nsIObserver + NS_DECL_NSIOBSERVER + + // nsPluginArray registers itself as an observer with a weak reference. + // This can't be done in the constructor, because at that point its + // refcount is 0 (and it gets destroyed upon registration). So, Init() + // must be called after construction. + void Init(); nsresult GetPluginHost(nsIPluginHost** aPluginHost); @@ -53,7 +64,7 @@ public: } #endif - return static_cast(aSupports); + return static_cast(static_cast(aSupports)); } private: