GMPTimerParent.cpp (2558B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* This Source Code Form is subject to the terms of the Mozilla Public 3 * License, v. 2.0. If a copy of the MPL was not distributed with this 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 5 6 #include "GMPTimerParent.h" 7 8 #include "GMPLog.h" 9 #include "nsComponentManagerUtils.h" 10 11 namespace mozilla { 12 13 extern LogModule* GetGMPLog(); 14 15 #ifdef __CLASS__ 16 # undef __CLASS__ 17 #endif 18 #define __CLASS__ "GMPTimerParent" 19 20 namespace gmp { 21 22 GMPTimerParent::GMPTimerParent(nsISerialEventTarget* aGMPEventTarget) 23 : mGMPEventTarget(aGMPEventTarget), mIsOpen(true) {} 24 25 mozilla::ipc::IPCResult GMPTimerParent::RecvSetTimer( 26 const uint32_t& aTimerId, const uint32_t& aTimeoutMs) { 27 GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this, 28 mIsOpen); 29 30 MOZ_ASSERT(mGMPEventTarget->IsOnCurrentThread()); 31 32 if (!mIsOpen) { 33 return IPC_OK(); 34 } 35 36 nsresult rv; 37 UniquePtr<Context> ctx(new Context()); 38 39 rv = NS_NewTimerWithFuncCallback( 40 getter_AddRefs(ctx->mTimer), &GMPTimerParent::GMPTimerExpired, ctx.get(), 41 aTimeoutMs, nsITimer::TYPE_ONE_SHOT, 42 "gmp::GMPTimerParent::RecvSetTimer"_ns, mGMPEventTarget); 43 NS_ENSURE_SUCCESS(rv, IPC_OK()); 44 45 ctx->mId = aTimerId; 46 ctx->mParent = this; 47 48 mTimers.Insert(ctx.release()); 49 50 return IPC_OK(); 51 } 52 53 void GMPTimerParent::Shutdown() { 54 GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this, 55 mIsOpen); 56 57 MOZ_ASSERT(mGMPEventTarget->IsOnCurrentThread()); 58 59 for (Context* context : mTimers) { 60 context->mTimer->Cancel(); 61 delete context; 62 } 63 64 mTimers.Clear(); 65 mIsOpen = false; 66 } 67 68 void GMPTimerParent::ActorDestroy(ActorDestroyReason aWhy) { 69 GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this, 70 mIsOpen); 71 72 Shutdown(); 73 } 74 75 /* static */ 76 void GMPTimerParent::GMPTimerExpired(nsITimer* aTimer, void* aClosure) { 77 MOZ_ASSERT(aClosure); 78 UniquePtr<Context> ctx(static_cast<Context*>(aClosure)); 79 MOZ_ASSERT(ctx->mParent); 80 if (ctx->mParent) { 81 ctx->mParent->TimerExpired(ctx.get()); 82 } 83 } 84 85 void GMPTimerParent::TimerExpired(Context* aContext) { 86 GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this, 87 mIsOpen); 88 MOZ_ASSERT(mGMPEventTarget->IsOnCurrentThread()); 89 90 if (!mIsOpen) { 91 return; 92 } 93 94 uint32_t id = aContext->mId; 95 mTimers.Remove(aContext); 96 if (id) { 97 (void)SendTimerExpired(id); 98 } 99 } 100 101 } // namespace gmp 102 } // namespace mozilla 103 104 #undef __CLASS__