Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/plugins/ipc/FunctionBrokerChild.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2
 * vim: sw=4 ts=4 et :
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 "FunctionBrokerChild.h"
8
#include "FunctionBrokerThread.h"
9
10
namespace mozilla {
11
namespace plugins {
12
13
FunctionBrokerChild* FunctionBrokerChild::sInstance = nullptr;
14
15
bool
16
FunctionBrokerChild::IsDispatchThread()
17
0
{
18
0
  return mThread->IsOnThread();
19
0
}
20
21
void
22
FunctionBrokerChild::PostToDispatchThread(already_AddRefed<nsIRunnable>&& runnable)
23
0
{
24
0
  mThread->Dispatch(std::move(runnable));
25
0
}
26
27
/* static */ bool
28
FunctionBrokerChild::Initialize(Endpoint<PFunctionBrokerChild>&& aBrokerEndpoint)
29
0
{
30
0
  MOZ_RELEASE_ASSERT(XRE_IsPluginProcess(),
31
0
                     "FunctionBrokerChild can only be used in plugin processes");
32
0
33
0
  MOZ_ASSERT(!sInstance);
34
0
  FunctionBrokerThread* thread = FunctionBrokerThread::Create();
35
0
  if (!thread) {
36
0
    return false;
37
0
  }
38
0
  sInstance = new FunctionBrokerChild(thread, std::move(aBrokerEndpoint));
39
0
  return true;
40
0
}
41
42
/* static */ FunctionBrokerChild*
43
FunctionBrokerChild::GetInstance()
44
0
{
45
0
  MOZ_RELEASE_ASSERT(XRE_IsPluginProcess(),
46
0
                     "FunctionBrokerChild can only be used in plugin processes");
47
0
48
0
  MOZ_ASSERT(sInstance, "Must initialize FunctionBrokerChild before using it");
49
0
  return sInstance;
50
0
}
51
52
FunctionBrokerChild::FunctionBrokerChild(FunctionBrokerThread* aThread,
53
                                         Endpoint<PFunctionBrokerChild>&& aEndpoint) :
54
    mThread(aThread)
55
  , mShutdownDone(false)
56
  , mMonitor("FunctionBrokerChild Lock")
57
0
{
58
0
  MOZ_ASSERT(aThread);
59
0
  PostToDispatchThread(NewNonOwningRunnableMethod<Endpoint<PFunctionBrokerChild>&&>(
60
0
                       "FunctionBrokerChild::Bind", this, &FunctionBrokerChild::Bind,
61
0
                       std::move(aEndpoint)));
62
0
}
63
64
void
65
FunctionBrokerChild::Bind(Endpoint<PFunctionBrokerChild>&& aEndpoint)
66
0
{
67
0
  MOZ_RELEASE_ASSERT(mThread->IsOnThread());
68
0
  DebugOnly<bool> ok = aEndpoint.Bind(this);
69
0
  MOZ_ASSERT(ok);
70
0
}
71
72
void
73
FunctionBrokerChild::ShutdownOnDispatchThread()
74
0
{
75
0
  MOZ_ASSERT(mThread->IsOnThread());
76
0
77
0
  // Set mShutdownDone and notify waiting thread (if any) that we are done.
78
0
  MonitorAutoLock lock(mMonitor);
79
0
  mShutdownDone = true;
80
0
  mMonitor.Notify();
81
0
}
82
83
void
84
FunctionBrokerChild::ActorDestroy(ActorDestroyReason aWhy)
85
0
{
86
0
  MOZ_ASSERT(mThread->IsOnThread());
87
0
88
0
  // Queue up a task on the PD thread.  When that task is executed then
89
0
  // we know that anything queued before ActorDestroy has completed.
90
0
  // At that point, we can set mShutdownDone and alert any waiting
91
0
  // threads that it is safe to destroy us.
92
0
  sInstance->PostToDispatchThread(NewNonOwningRunnableMethod(
93
0
                                  "FunctionBrokerChild::ShutdownOnDispatchThread", sInstance,
94
0
                                  &FunctionBrokerChild::ShutdownOnDispatchThread));
95
0
}
96
97
void
98
FunctionBrokerChild::Destroy()
99
0
{
100
0
  MOZ_ASSERT(NS_IsMainThread());
101
0
102
0
  if (!sInstance) {
103
0
    return;
104
0
  }
105
0
106
0
  // mShutdownDone will tell us when ActorDestroy has been run and any tasks
107
0
  // on the FunctionBrokerThread have completed.  At that point, we can
108
0
  // safely delete the actor.
109
0
  {
110
0
    MonitorAutoLock lock(sInstance->mMonitor);
111
0
    while (!sInstance->mShutdownDone) {
112
0
      // Release lock and wait.  Regain lock when we are notified that
113
0
      // we have ShutdownOnDispatchThread.
114
0
      sInstance->mMonitor.Wait();
115
0
    }
116
0
  }
117
0
118
0
  delete sInstance;
119
0
  sInstance = nullptr;
120
0
}
121
122
} // namespace plugins
123
} // namespace mozilla