VsyncWorkerChild.cpp (2085B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "VsyncWorkerChild.h" 8 9 #include "mozilla/dom/WorkerPrivate.h" 10 #include "mozilla/dom/WorkerRef.h" 11 #include "mozilla/dom/WorkerScope.h" 12 13 namespace mozilla::dom { 14 15 VsyncWorkerChild::VsyncWorkerChild() = default; 16 17 VsyncWorkerChild::~VsyncWorkerChild() = default; 18 19 bool VsyncWorkerChild::Initialize(WorkerPrivate* aWorkerPrivate) { 20 MOZ_ASSERT(aWorkerPrivate); 21 MOZ_ASSERT(!mWorkerRef); 22 23 mWorkerRef = 24 IPCWorkerRef::Create(aWorkerPrivate, "VsyncWorkerChild", 25 [self = RefPtr{this}]() { self->Destroy(); }); 26 return !!mWorkerRef; 27 } 28 29 void VsyncWorkerChild::Destroy() { 30 MOZ_ASSERT_IF(!CanSend(), !mWorkerRef); 31 MOZ_ASSERT_IF(!CanSend(), !mObserving); 32 Send__delete__(this); 33 MOZ_ASSERT(!mWorkerRef); 34 MOZ_ASSERT(!mObserving); 35 } 36 37 void VsyncWorkerChild::TryObserve() { 38 if (CanSend() && !mObserving) { 39 mObserving = SendObserve(); 40 } 41 } 42 43 void VsyncWorkerChild::TryUnobserve() { 44 if (CanSend() && mObserving) { 45 mObserving = !SendUnobserve(); 46 } 47 } 48 49 mozilla::ipc::IPCResult VsyncWorkerChild::RecvNotify(const VsyncEvent& aVsync, 50 const float& aVsyncRate) { 51 // For MOZ_CAN_RUN_SCRIPT_BOUNDARY purposes: We know IPDL is explicitly 52 // keeping our actor alive until it is done processing the messages. We know 53 // that the WorkerGlobalScope callee is responsible keeping itself alive 54 // during the OnVsync callback. 55 WorkerPrivate* workerPrivate = mWorkerRef->Private(); 56 WorkerGlobalScope* scope = workerPrivate->GlobalScope(); 57 if (scope) { 58 scope->OnVsync(aVsync); 59 } 60 return IPC_OK(); 61 } 62 63 void VsyncWorkerChild::ActorDestroy(ActorDestroyReason aActorDestroyReason) { 64 mWorkerRef = nullptr; 65 mObserving = false; 66 } 67 68 } // namespace mozilla::dom