Line data Source code
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 "ChildProfilerController.h"
8 : #include "nsThreadUtils.h"
9 : #include "ProfilerChild.h"
10 :
11 : using namespace mozilla::ipc;
12 :
13 : namespace mozilla {
14 :
15 : /* static */ already_AddRefed<ChildProfilerController>
16 2 : ChildProfilerController::Create(mozilla::ipc::Endpoint<PProfilerChild>&& aEndpoint)
17 : {
18 2 : MOZ_RELEASE_ASSERT(NS_IsMainThread());
19 4 : RefPtr<ChildProfilerController> cpc = new ChildProfilerController();
20 2 : cpc->Init(Move(aEndpoint));
21 4 : return cpc.forget();
22 : }
23 :
24 2 : ChildProfilerController::ChildProfilerController()
25 : {
26 2 : MOZ_COUNT_CTOR(ChildProfilerController);
27 2 : }
28 :
29 : void
30 2 : ChildProfilerController::Init(Endpoint<PProfilerChild>&& aEndpoint)
31 : {
32 2 : if (NS_SUCCEEDED(NS_NewNamedThread("ProfilerChild", getter_AddRefs(mThread)))) {
33 : // Now that mThread has been set, run SetupProfilerChild on the thread.
34 6 : mThread->Dispatch(NewRunnableMethod<Endpoint<PProfilerChild>&&>(
35 : "ChildProfilerController::SetupProfilerChild",
36 : this,
37 : &ChildProfilerController::SetupProfilerChild,
38 2 : Move(aEndpoint)),
39 4 : NS_DISPATCH_NORMAL);
40 : }
41 2 : }
42 :
43 : nsCString
44 0 : ChildProfilerController::GrabShutdownProfileAndShutdown()
45 : {
46 0 : nsCString shutdownProfile;
47 0 : ShutdownAndMaybeGrabShutdownProfileFirst(&shutdownProfile);
48 0 : return shutdownProfile;
49 : }
50 :
51 : void
52 0 : ChildProfilerController::Shutdown()
53 : {
54 0 : ShutdownAndMaybeGrabShutdownProfileFirst(nullptr);
55 0 : }
56 :
57 : void
58 0 : ChildProfilerController::ShutdownAndMaybeGrabShutdownProfileFirst(nsCString* aOutShutdownProfile)
59 : {
60 0 : if (mThread) {
61 0 : mThread->Dispatch(NewRunnableMethod<nsCString*>(
62 : "ChildProfilerController::ShutdownProfilerChild",
63 : this,
64 : &ChildProfilerController::ShutdownProfilerChild,
65 : aOutShutdownProfile),
66 0 : NS_DISPATCH_NORMAL);
67 : // Shut down the thread. This call will spin until all runnables (including
68 : // the ShutdownProfilerChild runnable) have been processed.
69 0 : mThread->Shutdown();
70 0 : mThread = nullptr;
71 : }
72 0 : }
73 :
74 0 : ChildProfilerController::~ChildProfilerController()
75 : {
76 0 : MOZ_COUNT_DTOR(ChildProfilerController);
77 :
78 0 : MOZ_ASSERT(!mThread, "Please call Shutdown before destroying ChildProfilerController");
79 0 : MOZ_ASSERT(!mProfilerChild);
80 0 : }
81 :
82 : void
83 2 : ChildProfilerController::SetupProfilerChild(Endpoint<PProfilerChild>&& aEndpoint)
84 : {
85 2 : MOZ_RELEASE_ASSERT(mThread == NS_GetCurrentThread());
86 2 : MOZ_ASSERT(aEndpoint.IsValid());
87 :
88 2 : mProfilerChild = new ProfilerChild();
89 4 : Endpoint<PProfilerChild> endpoint = Move(aEndpoint);
90 :
91 2 : if (!endpoint.Bind(mProfilerChild)) {
92 0 : MOZ_CRASH("Failed to bind ProfilerChild!");
93 : }
94 2 : }
95 :
96 : void
97 0 : ChildProfilerController::ShutdownProfilerChild(nsCString* aOutShutdownProfile)
98 : {
99 0 : MOZ_RELEASE_ASSERT(mThread == NS_GetCurrentThread());
100 :
101 0 : if (aOutShutdownProfile) {
102 0 : *aOutShutdownProfile = mProfilerChild->GrabShutdownProfile();
103 : }
104 0 : mProfilerChild->Destroy();
105 0 : mProfilerChild = nullptr;
106 0 : }
107 :
108 : } // namespace mozilla
|