Bug 1482109 - Use the generic memory pressure observer in PuppetWidget. r=sotaro
authorNicolas Silva <nsilva@mozilla.com>
Fri, 10 Aug 2018 17:15:18 +0200
changeset 431014 4bf45d1a86655070ca2e41ad97d35fab45e18908
parent 431013 594a80cf1466e7ee67b6258f8821d11a803468cc
child 431015 cfc33798c721117c0d16ab8d569b59f13fe2156c
push id34419
push userbtara@mozilla.com
push dateSat, 11 Aug 2018 03:43:33 +0000
treeherdermozilla-central@7ed5ed3d4814 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssotaro
bugs1482109
milestone63.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 1482109 - Use the generic memory pressure observer in PuppetWidget. r=sotaro
widget/PuppetWidget.cpp
widget/PuppetWidget.h
--- a/widget/PuppetWidget.cpp
+++ b/widget/PuppetWidget.cpp
@@ -134,21 +134,17 @@ PuppetWidget::InfallibleCreate(nsIWidget
   PuppetWidget* parent = static_cast<PuppetWidget*>(aParent);
   if (parent) {
     parent->SetChild(this);
     mLayerManager = parent->GetLayerManager();
   }
   else {
     Resize(mBounds.X(), mBounds.Y(), mBounds.Width(), mBounds.Height(), false);
   }
-  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
-  if (obs) {
-    mMemoryPressureObserver = new MemoryPressureObserver(this);
-    obs->AddObserver(mMemoryPressureObserver, "memory-pressure", false);
-  }
+  mMemoryPressureObserver = MemoryPressureObserver::Create(this);
 }
 
 nsresult
 PuppetWidget::Create(nsIWidget* aParent,
                      nsNativeWidget aNativeParent,
                      const LayoutDeviceIntRect& aRect,
                      nsWidgetInitData* aInitData)
 {
@@ -188,19 +184,19 @@ PuppetWidget::Destroy()
     return;
   }
   mOnDestroyCalled = true;
 
   Base::OnDestroy();
   Base::Destroy();
   mPaintTask.Revoke();
   if (mMemoryPressureObserver) {
-    mMemoryPressureObserver->Remove();
+    mMemoryPressureObserver->Unregister();
+    mMemoryPressureObserver = nullptr;
   }
-  mMemoryPressureObserver = nullptr;
   mChild = nullptr;
   if (mLayerManager) {
     mLayerManager->Destroy();
   }
   mLayerManager = nullptr;
   mTabChild = nullptr;
 }
 
@@ -1132,46 +1128,25 @@ PuppetWidget::PaintTask::Run()
 void
 PuppetWidget::PaintNowIfNeeded()
 {
   if (IsVisible() && mPaintTask.IsPending()) {
     Paint();
   }
 }
 
-NS_IMPL_ISUPPORTS(PuppetWidget::MemoryPressureObserver, nsIObserver)
-
-NS_IMETHODIMP
-PuppetWidget::MemoryPressureObserver::Observe(nsISupports* aSubject,
-                                              const char* aTopic,
-                                              const char16_t* aData)
+void
+PuppetWidget::OnMemoryPressure(layers::MemoryPressureReason aWhy)
 {
-  if (!mWidget) {
-    return NS_OK;
+  if (aWhy != MemoryPressureReason::LOW_MEMORY_ONGOING &&
+      mVisible &&
+      mLayerManager &&
+      XRE_IsContentProcess()) {
+    mLayerManager->ClearCachedResources();
   }
-
-  if (strcmp("memory-pressure", aTopic) == 0 &&
-      !StringBeginsWith(nsDependentString(aData),
-                        NS_LITERAL_STRING("low-memory-ongoing"))) {
-    if (!mWidget->mVisible && mWidget->mLayerManager &&
-        XRE_IsContentProcess()) {
-      mWidget->mLayerManager->ClearCachedResources();
-    }
-  }
-  return NS_OK;
-}
-
-void
-PuppetWidget::MemoryPressureObserver::Remove()
-{
-  nsCOMPtr<nsIObserverService> obs = mozilla::services::GetObserverService();
-  if (obs) {
-    obs->RemoveObserver(this, "memory-pressure");
-  }
-  mWidget = nullptr;
 }
 
 bool
 PuppetWidget::NeedsPaint()
 {
   // e10s popups are handled by the parent process, so never should be painted here
   if (XRE_IsContentProcess() &&
       gRemoteDesktopBehaviorEnabled &&
--- a/widget/PuppetWidget.h
+++ b/widget/PuppetWidget.h
@@ -22,29 +22,31 @@
 #include "nsCOMArray.h"
 #include "nsIKeyEventInPluginCallback.h"
 #include "nsIScreenManager.h"
 #include "nsThreadUtils.h"
 #include "mozilla/Attributes.h"
 #include "mozilla/ContentCache.h"
 #include "mozilla/EventForwards.h"
 #include "mozilla/TextEventDispatcherListener.h"
+#include "mozilla/layers/MemoryPressureObserver.h"
 
 namespace mozilla {
 
 namespace dom {
 class TabChild;
 } // namespace dom
 
 namespace widget {
 
 struct AutoCacheNativeKeyCommands;
 
 class PuppetWidget : public nsBaseWidget
                    , public TextEventDispatcherListener
+                   , public layers::MemoryPressureListener
 {
   typedef mozilla::CSSRect CSSRect;
   typedef mozilla::dom::TabChild TabChild;
   typedef mozilla::gfx::DrawTarget DrawTarget;
 
   // Avoiding to make compiler confused between mozilla::widget and nsIWidget.
   typedef mozilla::widget::TextEventDispatcher TextEventDispatcher;
   typedef mozilla::widget::TextEventDispatcherListener
@@ -317,16 +319,17 @@ public:
   NS_IMETHOD_(void) OnRemovedFrom(
                       TextEventDispatcher* aTextEventDispatcher) override;
   NS_IMETHOD_(void) WillDispatchKeyboardEvent(
                       TextEventDispatcher* aTextEventDispatcher,
                       WidgetKeyboardEvent& aKeyboardEvent,
                       uint32_t aIndexOfKeypress,
                       void* aData) override;
 
+  virtual void OnMemoryPressure(layers::MemoryPressureReason aWhy) override;
 private:
   nsresult Paint();
 
   void SetChild(PuppetWidget* aChild);
 
   nsresult RequestIMEToCommitComposition(bool aCancel);
   nsresult NotifyIMEOfFocusChange(const IMENotification& aIMENotification);
   nsresult NotifyIMEOfSelectionChange(const IMENotification& aIMENotification);
@@ -353,41 +356,29 @@ private:
     NS_DECL_NSIRUNNABLE
     explicit PaintTask(PuppetWidget* widget)
      : Runnable("PuppetWidget::PaintTask"), mWidget(widget) {}
     void Revoke() { mWidget = nullptr; }
   private:
     PuppetWidget* mWidget;
   };
 
-  class MemoryPressureObserver : public nsIObserver {
-  public:
-    NS_DECL_ISUPPORTS
-    NS_DECL_NSIOBSERVER
-    explicit MemoryPressureObserver(PuppetWidget* aWidget) : mWidget(aWidget) {}
-    void Remove();
-  private:
-    virtual ~MemoryPressureObserver() {}
-    PuppetWidget* mWidget;
-  };
-  friend class MemoryPressureObserver;
-
   // TabChild normally holds a strong reference to this PuppetWidget
   // or its root ancestor, but each PuppetWidget also needs a
   // reference back to TabChild (e.g. to delegate nsIWidget IME calls
   // to chrome) So we hold a weak reference to TabChild here.  Since
   // it's possible for TabChild to outlive the PuppetWidget, we clear
   // this weak reference in Destroy()
   TabChild* mTabChild;
   // The "widget" to which we delegate events if we don't have an
   // event handler.
   RefPtr<PuppetWidget> mChild;
   LayoutDeviceIntRegion mDirtyRegion;
   nsRevocableEventPtr<PaintTask> mPaintTask;
-  RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
+  RefPtr<layers::MemoryPressureObserver> mMemoryPressureObserver;
   // XXX/cjones: keeping this around until we teach LayerManager to do
   // retained-content-only transactions
   RefPtr<DrawTarget> mDrawTarget;
   // IME
   IMENotificationRequests mIMENotificationRequestsOfParent;
   InputContext mInputContext;
   // mNativeIMEContext is initialized when this dispatches every composition
   // event both from parent process's widget and TextEventDispatcher in same