summaryrefslogtreecommitdiffstats
path: root/dom/media/gmp/GMPTimerParent.cpp
blob: 31b17ef2146b473e6ba81feb6f4b6e29be8c1dd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "GMPTimerParent.h"

#include "GMPLog.h"
#include "mozilla/Unused.h"
#include "nsComponentManagerUtils.h"

namespace mozilla {

extern LogModule* GetGMPLog();

#ifdef __CLASS__
#  undef __CLASS__
#endif
#define __CLASS__ "GMPTimerParent"

namespace gmp {

GMPTimerParent::GMPTimerParent(nsISerialEventTarget* aGMPEventTarget)
    : mGMPEventTarget(aGMPEventTarget), mIsOpen(true) {}

mozilla::ipc::IPCResult GMPTimerParent::RecvSetTimer(
    const uint32_t& aTimerId, const uint32_t& aTimeoutMs) {
  GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this,
                mIsOpen);

  MOZ_ASSERT(mGMPEventTarget->IsOnCurrentThread());

  if (!mIsOpen) {
    return IPC_OK();
  }

  nsresult rv;
  UniquePtr<Context> ctx(new Context());

  rv = NS_NewTimerWithFuncCallback(
      getter_AddRefs(ctx->mTimer), &GMPTimerParent::GMPTimerExpired, ctx.get(),
      aTimeoutMs, nsITimer::TYPE_ONE_SHOT, "gmp::GMPTimerParent::RecvSetTimer",
      mGMPEventTarget);
  NS_ENSURE_SUCCESS(rv, IPC_OK());

  ctx->mId = aTimerId;
  ctx->mParent = this;

  mTimers.Insert(ctx.release());

  return IPC_OK();
}

void GMPTimerParent::Shutdown() {
  GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this,
                mIsOpen);

  MOZ_ASSERT(mGMPEventTarget->IsOnCurrentThread());

  for (Context* context : mTimers) {
    context->mTimer->Cancel();
    delete context;
  }

  mTimers.Clear();
  mIsOpen = false;
}

void GMPTimerParent::ActorDestroy(ActorDestroyReason aWhy) {
  GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this,
                mIsOpen);

  Shutdown();
}

/* static */
void GMPTimerParent::GMPTimerExpired(nsITimer* aTimer, void* aClosure) {
  MOZ_ASSERT(aClosure);
  UniquePtr<Context> ctx(static_cast<Context*>(aClosure));
  MOZ_ASSERT(ctx->mParent);
  if (ctx->mParent) {
    ctx->mParent->TimerExpired(ctx.get());
  }
}

void GMPTimerParent::TimerExpired(Context* aContext) {
  GMP_LOG_DEBUG("%s::%s: %p mIsOpen=%d", __CLASS__, __FUNCTION__, this,
                mIsOpen);
  MOZ_ASSERT(mGMPEventTarget->IsOnCurrentThread());

  if (!mIsOpen) {
    return;
  }

  uint32_t id = aContext->mId;
  mTimers.Remove(aContext);
  if (id) {
    Unused << SendTimerExpired(id);
  }
}

}  // namespace gmp
}  // namespace mozilla

#undef __CLASS__