Roll src/third_party/WebKit 9f7fb92:f103b33 (svn 202621:202622)
[chromium-blink-merge.git] / components / tracing / child_memory_dump_manager_delegate_impl.cc
blobfe5c952f6a9a91ecf83ce42e81e49b543eb42974
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/tracing/child_memory_dump_manager_delegate_impl.h"
7 #include "base/single_thread_task_runner.h"
8 #include "components/tracing/child_trace_message_filter.h"
10 namespace tracing {
12 namespace {
13 void AbortDumpRequest(const base::trace_event::MemoryDumpRequestArgs& args,
14 const base::trace_event::MemoryDumpCallback& callback) {
15 if (!callback.is_null())
16 callback.Run(args.dump_guid, false /* success */);
18 } // namespace
20 // static
21 ChildMemoryDumpManagerDelegateImpl*
22 ChildMemoryDumpManagerDelegateImpl::GetInstance() {
23 return base::Singleton<
24 ChildMemoryDumpManagerDelegateImpl,
25 base::LeakySingletonTraits<ChildMemoryDumpManagerDelegateImpl>>::get();
28 ChildMemoryDumpManagerDelegateImpl::ChildMemoryDumpManagerDelegateImpl()
29 : ctmf_(nullptr),
30 tracing_process_id_(
31 base::trace_event::MemoryDumpManager::kInvalidTracingProcessId) {
34 ChildMemoryDumpManagerDelegateImpl::~ChildMemoryDumpManagerDelegateImpl() {}
36 void ChildMemoryDumpManagerDelegateImpl::SetChildTraceMessageFilter(
37 ChildTraceMessageFilter* ctmf) {
38 const auto& task_runner = ctmf ? (ctmf->ipc_task_runner()) : nullptr;
39 // Check that we are either registering the CTMF or tearing it down, but not
40 // replacing a valid instance with another one (should never happen).
41 DCHECK(ctmf_ == nullptr || (ctmf == nullptr && ctmf_task_runner_ != nullptr));
42 ctmf_ = ctmf;
45 base::AutoLock lock(lock_);
46 ctmf_task_runner_ = task_runner;
49 if (ctmf) {
50 base::trace_event::MemoryDumpManager::GetInstance()->Initialize(
51 this /* delegate */, false /* is_coordinator */);
55 // Invoked in child processes by the MemoryDumpManager.
56 void ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump(
57 const base::trace_event::MemoryDumpRequestArgs& args,
58 const base::trace_event::MemoryDumpCallback& callback) {
59 // RequestGlobalMemoryDump can be called on any thread, cannot access
60 // ctmf_task_runner_ as it could be racy.
61 scoped_refptr<base::SingleThreadTaskRunner> ctmf_task_runner;
63 base::AutoLock lock(lock_);
64 ctmf_task_runner = ctmf_task_runner_;
67 // Bail out if we receive a dump request from the manager before the
68 // ChildTraceMessageFilter has been initialized.
69 if (!ctmf_task_runner)
70 return AbortDumpRequest(args, callback);
72 // Make sure we access |ctmf_| only on the thread where it lives to avoid
73 // races on shutdown.
74 if (!ctmf_task_runner->BelongsToCurrentThread()) {
75 const bool did_post_task = ctmf_task_runner->PostTask(
76 FROM_HERE,
77 base::Bind(&ChildMemoryDumpManagerDelegateImpl::RequestGlobalMemoryDump,
78 base::Unretained(this), args, callback));
79 if (!did_post_task)
80 return AbortDumpRequest(args, callback);
81 return;
84 // The ChildTraceMessageFilter could have been destroyed while hopping on the
85 // right thread. If this is the case, bail out.
86 if (!ctmf_)
87 return AbortDumpRequest(args, callback);
89 // Send the request up to the browser process' MessageDumpmanager.
90 ctmf_->SendGlobalMemoryDumpRequest(args, callback);
93 uint64 ChildMemoryDumpManagerDelegateImpl::GetTracingProcessId() const {
94 return tracing_process_id_;
97 } // namespace tracing