1 // Copyright (c) 2012 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 "content/browser/profiler_controller_impl.h"
8 #include "base/process/process_handle.h"
9 #include "base/tracked_objects.h"
10 #include "content/common/child_process_messages.h"
11 #include "content/public/browser/browser_child_process_host_iterator.h"
12 #include "content/public/browser/browser_thread.h"
13 #include "content/public/browser/child_process_data.h"
14 #include "content/public/browser/profiler_subscriber.h"
15 #include "content/public/browser/render_process_host.h"
19 ProfilerController
* ProfilerController::GetInstance() {
20 return ProfilerControllerImpl::GetInstance();
23 ProfilerControllerImpl
* ProfilerControllerImpl::GetInstance() {
24 return Singleton
<ProfilerControllerImpl
>::get();
27 ProfilerControllerImpl::ProfilerControllerImpl() : subscriber_(NULL
) {
30 ProfilerControllerImpl::~ProfilerControllerImpl() {
33 void ProfilerControllerImpl::OnPendingProcesses(int sequence_number
,
34 int pending_processes
,
36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
38 subscriber_
->OnPendingProcesses(sequence_number
, pending_processes
, end
);
41 void ProfilerControllerImpl::OnProfilerDataCollected(
43 const tracked_objects::ProcessDataSnapshot
& profiler_data
,
45 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
46 BrowserThread::PostTask(
47 BrowserThread::UI
, FROM_HERE
,
48 base::Bind(&ProfilerControllerImpl::OnProfilerDataCollected
,
49 base::Unretained(this),
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
58 subscriber_
->OnProfilerDataCollected(sequence_number
, profiler_data
,
63 void ProfilerControllerImpl::Register(ProfilerSubscriber
* subscriber
) {
64 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
66 subscriber_
= subscriber
;
69 void ProfilerControllerImpl::Unregister(const ProfilerSubscriber
* subscriber
) {
70 DCHECK_EQ(subscriber_
, subscriber
);
74 void ProfilerControllerImpl::GetProfilerDataFromChildProcesses(
75 int sequence_number
) {
76 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
78 int pending_processes
= 0;
79 for (BrowserChildProcessHostIterator iter
; !iter
.Done(); ++iter
) {
80 // In some cases, there may be no child process of the given type (for
81 // example, the GPU process may not exist and there may instead just be a
82 // GPU thread in the browser process). If that's the case, then the process
83 // handle will be base::kNullProcessHandle and we shouldn't ask it for data.
84 if (iter
.GetData().handle
== base::kNullProcessHandle
)
88 if (!iter
.Send(new ChildProcessMsg_GetChildProfilerData(sequence_number
)))
92 BrowserThread::PostTask(
96 &ProfilerControllerImpl::OnPendingProcesses
,
97 base::Unretained(this),
103 void ProfilerControllerImpl::GetProfilerData(int sequence_number
) {
104 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
106 int pending_processes
= 0;
107 for (RenderProcessHost::iterator
it(RenderProcessHost::AllHostsIterator());
108 !it
.IsAtEnd(); it
.Advance()) {
110 if (!it
.GetCurrentValue()->Send(
111 new ChildProcessMsg_GetChildProfilerData(sequence_number
))) {
115 OnPendingProcesses(sequence_number
, pending_processes
, false);
117 BrowserThread::PostTask(
120 base::Bind(&ProfilerControllerImpl::GetProfilerDataFromChildProcesses
,
121 base::Unretained(this),
125 } // namespace content