Bug 1315718 - Replace mGamepadManager raw pointer with RefPtr in VRManagerChild. r=smaug, kip, dmu
authorDaosheng Mu <daoshengmu@gmail.com>
Thu, 01 Dec 2016 13:12:10 +0800
changeset 324969 0dff674b449764433e5870121de42178c571825f
parent 324968 033d5532cac71ffdc9459fbf21f628448fac3b04
child 324970 467e719c9e9a03972e738fde1b75ffed1cc10514
push id24
push usermaklebus@msu.edu
push dateTue, 20 Dec 2016 03:11:33 +0000
reviewerssmaug, kip, dmu
bugs1315718
milestone53.0a1
Bug 1315718 - Replace mGamepadManager raw pointer with RefPtr in VRManagerChild. r=smaug, kip, dmu MozReview-Commit-ID: HOqQm2F6Dpn --- dom/gamepad/GamepadManager.cpp | 1 - gfx/vr/ipc/VRManagerChild.cpp | 18 ++++++------------ gfx/vr/ipc/VRManagerChild.h | 4 ---- gfx/vr/ipc/VRManagerParent.cpp | 21 +++++++++++++++++---- gfx/vr/ipc/VRManagerParent.h | 4 +++- 5 files changed, 26 insertions(+), 22 deletions(-)
dom/gamepad/GamepadManager.cpp
gfx/vr/ipc/VRManagerChild.cpp
gfx/vr/ipc/VRManagerChild.h
gfx/vr/ipc/VRManagerParent.cpp
gfx/vr/ipc/VRManagerParent.h
--- a/dom/gamepad/GamepadManager.cpp
+++ b/dom/gamepad/GamepadManager.cpp
@@ -679,17 +679,16 @@ GamepadManager::ActorCreated(PBackground
   MOZ_ASSERT(initedChild == child);
   child->SendGamepadListenerAdded();
   mChannelChildren.AppendElement(child);
 
 #if defined(XP_WIN) || defined(XP_MACOSX) || defined(XP_LINUX)
   // Construct VRManagerChannel and ask adding the connected
   // VR controllers to GamepadManager
   mVRChannelChild = gfx::VRManagerChild::Get();
-  mVRChannelChild->SetGamepadManager(this);
   mVRChannelChild->SendControllerListenerAdded();
 #endif
 }
 
 //Override nsIIPCBackgroundChildCreateCallback
 void
 GamepadManager::ActorFailed()
 {
--- a/gfx/vr/ipc/VRManagerChild.cpp
+++ b/gfx/vr/ipc/VRManagerChild.cpp
@@ -37,17 +37,16 @@ static StaticRefPtr<VRManagerParent> sVR
 
 void ReleaseVRManagerParentSingleton() {
   sVRManagerParentSingleton = nullptr;
 }
 
 VRManagerChild::VRManagerChild()
   : TextureForwarder()
   , mDisplaysInitialized(false)
-  , mGamepadManager(nullptr)
   , mInputFrameID(-1)
   , mMessageLoop(MessageLoop::current())
   , mFrameRequestCallbackCounter(0)
   , mBackend(layers::LayersBackend::LAYERS_NONE)
 {
   MOZ_COUNT_CTOR(VRManagerChild);
   MOZ_ASSERT(NS_IsMainThread());
 
@@ -479,20 +478,23 @@ VRManagerChild::RecvNotifyVRVSync(const 
   return IPC_OK();
 }
 
 mozilla::ipc::IPCResult
 VRManagerChild::RecvGamepadUpdate(const GamepadChangeEvent& aGamepadEvent)
 {
 #ifdef MOZ_GAMEPAD
   // VRManagerChild could be at other processes, but GamepadManager
-  // only exists at the content process or the parent process
+  // only exists at the content process or the same process
   // in non-e10s mode.
-  if (mGamepadManager) {
-      mGamepadManager->Update(aGamepadEvent);
+  MOZ_ASSERT(XRE_IsContentProcess() || IsSameProcess());
+
+  RefPtr<GamepadManager> gamepadManager(GamepadManager::GetService());
+  if (gamepadManager) {
+    gamepadManager->Update(aGamepadEvent);
   }
 #endif
 
   return IPC_OK();
 }
 
 void
 VRManagerChild::RunFrameRequestCallbacks()
@@ -582,18 +584,10 @@ VRManagerChild::RemoveListener(dom::VREv
 }
 
 void
 VRManagerChild::HandleFatalError(const char* aName, const char* aMsg) const
 {
   dom::ContentChild::FatalErrorIfNotUsingGPUProcess(aName, aMsg, OtherPid());
 }
 
-void
-VRManagerChild::SetGamepadManager(dom::GamepadManager* aGamepadManager)
-{
-  MOZ_ASSERT(aGamepadManager);
-
-  mGamepadManager = aGamepadManager;
-}
-
 } // namespace gfx
 } // namespace mozilla
--- a/gfx/vr/ipc/VRManagerChild.h
+++ b/gfx/vr/ipc/VRManagerChild.h
@@ -73,19 +73,16 @@ public:
 
   virtual MessageLoop* GetMessageLoop() const override { return mMessageLoop; }
   virtual base::ProcessId GetParentPid() const override { return OtherPid(); }
 
   nsresult ScheduleFrameRequestCallback(mozilla::dom::FrameRequestCallback& aCallback,
     int32_t *aHandle);
   void CancelFrameRequestCallback(int32_t aHandle);
   void RunFrameRequestCallbacks();
-  // GamepadManager has to be set by the content side to make sure we are using
-  // the same singleton GamepadManager from the same process.
-  void SetGamepadManager(dom::GamepadManager* aGamepadManager);
 
   void UpdateDisplayInfo(nsTArray<VRDisplayInfo>& aDisplayUpdates);
   void FireDOMVRDisplayConnectEvent();
   void FireDOMVRDisplayDisconnectEvent();
   void FireDOMVRDisplayPresentChangeEvent();
 
   virtual void HandleFatalError(const char* aName, const char* aMsg) const override;
 
@@ -148,17 +145,16 @@ private:
   * Notify id of Texture When host side end its use. Transaction id is used to
   * make sure if there is no newer usage.
   */
   void NotifyNotUsed(uint64_t aTextureId, uint64_t aFwdTransactionId);
 
   nsTArray<RefPtr<VRDisplayClient> > mDisplays;
   bool mDisplaysInitialized;
   nsTArray<uint64_t> mNavigatorCallbacks;
-  dom::GamepadManager* mGamepadManager;
 
   int32_t mInputFrameID;
 
   MessageLoop* mMessageLoop;
 
   struct FrameRequest;
 
   nsTArray<FrameRequest> mFrameRequestCallbacks;
--- a/gfx/vr/ipc/VRManagerParent.cpp
+++ b/gfx/vr/ipc/VRManagerParent.cpp
@@ -14,19 +14,20 @@
 #include "mozilla/layers/CompositorThread.h"
 #include "mozilla/Unused.h"
 #include "VRManager.h"
 
 namespace mozilla {
 using namespace layers;
 namespace gfx {
 
-VRManagerParent::VRManagerParent(ProcessId aChildProcessId)
+VRManagerParent::VRManagerParent(ProcessId aChildProcessId, bool aIsContentChild)
   : HostIPCAllocator()
   , mHaveEventListener(false)
+  , mIsContentChild(aIsContentChild)
 {
   MOZ_COUNT_CTOR(VRManagerParent);
   MOZ_ASSERT(NS_IsMainThread());
 
   SetOtherProcessId(aChildProcessId);
 }
 
 VRManagerParent::~VRManagerParent()
@@ -151,17 +152,17 @@ VRManagerParent::UnregisterFromManager()
   mVRManagerHolder = nullptr;
 }
 
 /* static */ bool
 VRManagerParent::CreateForContent(Endpoint<PVRManagerParent>&& aEndpoint)
 {
   MessageLoop* loop = layers::CompositorThreadHolder::Loop();
 
-  RefPtr<VRManagerParent> vmp = new VRManagerParent(aEndpoint.OtherPid());
+  RefPtr<VRManagerParent> vmp = new VRManagerParent(aEndpoint.OtherPid(), true);
   loop->PostTask(NewRunnableMethod<Endpoint<PVRManagerParent>&&>(
     vmp, &VRManagerParent::Bind, Move(aEndpoint)));
 
   return true;
 }
 
 void
 VRManagerParent::Bind(Endpoint<PVRManagerParent>&& aEndpoint)
@@ -179,29 +180,29 @@ VRManagerParent::RegisterVRManagerInComp
 {
   aVRManager->RegisterWithManager();
 }
 
 /*static*/ VRManagerParent*
 VRManagerParent::CreateSameProcess()
 {
   MessageLoop* loop = mozilla::layers::CompositorThreadHolder::Loop();
-  RefPtr<VRManagerParent> vmp = new VRManagerParent(base::GetCurrentProcId());
+  RefPtr<VRManagerParent> vmp = new VRManagerParent(base::GetCurrentProcId(), false);
   vmp->mCompositorThreadHolder = layers::CompositorThreadHolder::GetSingleton();
   vmp->mSelfRef = vmp;
   loop->PostTask(NewRunnableFunction(RegisterVRManagerInCompositorThread, vmp.get()));
   return vmp.get();
 }
 
 bool
 VRManagerParent::CreateForGPUProcess(Endpoint<PVRManagerParent>&& aEndpoint)
 {
   MessageLoop* loop = mozilla::layers::CompositorThreadHolder::Loop();
 
-  RefPtr<VRManagerParent> vmp = new VRManagerParent(aEndpoint.OtherPid());
+  RefPtr<VRManagerParent> vmp = new VRManagerParent(aEndpoint.OtherPid(), false);
   vmp->mCompositorThreadHolder = layers::CompositorThreadHolder::GetSingleton();
   loop->PostTask(NewRunnableMethod<Endpoint<PVRManagerParent>&&>(
     vmp, &VRManagerParent::Bind, Move(aEndpoint)));
   return true;
 }
 
 void
 VRManagerParent::DeferredDestroy()
@@ -310,10 +311,22 @@ VRManagerParent::RecvControllerListenerR
 mozilla::ipc::IPCResult
 VRManagerParent::RecvGetControllers(nsTArray<VRControllerInfo> *aControllers)
 {
   VRManager* vm = VRManager::Get();
   vm->GetVRControllerInfo(*aControllers);
   return IPC_OK();
 }
 
+bool
+VRManagerParent::SendGamepadUpdate(const GamepadChangeEvent& aGamepadEvent)
+{
+  // GamepadManager only exists at the content process
+  // or the same process in non-e10s mode.
+  if (mIsContentChild || IsSameProcess()) {
+    return PVRManagerParent::SendGamepadUpdate(aGamepadEvent);
+  } else {
+    return true;
+  }
+}
+
 } // namespace gfx
 } // namespace mozilla
--- a/gfx/vr/ipc/VRManagerParent.h
+++ b/gfx/vr/ipc/VRManagerParent.h
@@ -22,17 +22,17 @@ namespace gfx {
 
 class VRManager;
 
 class VRManagerParent final : public PVRManagerParent
                             , public HostIPCAllocator
                             , public ShmemAllocator
 {
 public:
-  explicit VRManagerParent(ProcessId aChildProcessId);
+  explicit VRManagerParent(ProcessId aChildProcessId, bool aIsContentChild);
 
   static VRManagerParent* CreateSameProcess();
   static bool CreateForGPUProcess(Endpoint<PVRManagerParent>&& aEndpoint);
   static bool CreateForContent(Endpoint<PVRManagerParent>&& aEndpoint);
 
   virtual base::ProcessId GetChildProcessId() override;
 
   // ShmemAllocator
@@ -49,16 +49,17 @@ public:
 
   virtual void DeallocShmem(ipc::Shmem& aShmem) override;
 
   virtual bool IsSameProcess() const override;
   bool HaveEventListener();
 
   virtual void NotifyNotUsed(PTextureParent* aTexture, uint64_t aTransactionId) override;
   virtual void SendAsyncMessage(const InfallibleTArray<AsyncParentMessageData>& aMessage) override;
+  bool SendGamepadUpdate(const GamepadChangeEvent& aGamepadEvent);
 
 protected:
   ~VRManagerParent();
 
   virtual PTextureParent* AllocPTextureParent(const SurfaceDescriptor& aSharedData,
                                               const LayersBackend& aLayersBackend,
                                               const TextureFlags& aFlags,
                                               const uint64_t& aSerial) override;
@@ -103,14 +104,15 @@ private:
   RefPtr<VRManagerParent> mSelfRef;
 
   // Keep the compositor thread alive, until we have destroyed ourselves.
   RefPtr<layers::CompositorThreadHolder> mCompositorThreadHolder;
 
   // Keep the VRManager alive, until we have destroyed ourselves.
   RefPtr<VRManager> mVRManagerHolder;
   bool mHaveEventListener;
+  bool mIsContentChild;
 };
 
 } // namespace mozilla
 } // namespace gfx
 
 #endif // MOZILLA_GFX_VR_VRMANAGERPARENT_H