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/tracked_objects.h"
9 #include "content/common/child_process_messages.h"
10 #include "content/public/browser/browser_child_process_host_iterator.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/child_process_data.h"
13 #include "content/public/browser/profiler_subscriber.h"
14 #include "content/public/browser/render_process_host.h"
18 ProfilerController
* ProfilerController::GetInstance() {
19 return ProfilerControllerImpl::GetInstance();
22 ProfilerControllerImpl
* ProfilerControllerImpl::GetInstance() {
23 return Singleton
<ProfilerControllerImpl
>::get();
26 ProfilerControllerImpl::ProfilerControllerImpl() : subscriber_(NULL
) {
29 ProfilerControllerImpl::~ProfilerControllerImpl() {
32 void ProfilerControllerImpl::OnPendingProcesses(int sequence_number
,
33 int pending_processes
,
35 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
37 subscriber_
->OnPendingProcesses(sequence_number
, pending_processes
, end
);
40 void ProfilerControllerImpl::OnProfilerDataCollected(
42 const tracked_objects::ProcessDataSnapshot
& profiler_data
,
43 ProcessType process_type
) {
44 if (!BrowserThread::CurrentlyOn(BrowserThread::UI
)) {
45 BrowserThread::PostTask(
46 BrowserThread::UI
, FROM_HERE
,
47 base::Bind(&ProfilerControllerImpl::OnProfilerDataCollected
,
48 base::Unretained(this),
55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
57 subscriber_
->OnProfilerDataCollected(sequence_number
, profiler_data
,
62 void ProfilerControllerImpl::Register(ProfilerSubscriber
* subscriber
) {
63 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
65 subscriber_
= subscriber
;
68 void ProfilerControllerImpl::Unregister(const ProfilerSubscriber
* subscriber
) {
69 DCHECK_EQ(subscriber_
, subscriber
);
73 void ProfilerControllerImpl::GetProfilerDataFromChildProcesses(
74 int sequence_number
) {
75 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
77 int pending_processes
= 0;
78 for (BrowserChildProcessHostIterator iter
; !iter
.Done(); ++iter
) {
80 if (!iter
.Send(new ChildProcessMsg_GetChildProfilerData(sequence_number
)))
84 BrowserThread::PostTask(
88 &ProfilerControllerImpl::OnPendingProcesses
,
89 base::Unretained(this),
95 void ProfilerControllerImpl::GetProfilerData(int sequence_number
) {
96 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
98 int pending_processes
= 0;
99 for (RenderProcessHost::iterator
it(RenderProcessHost::AllHostsIterator());
100 !it
.IsAtEnd(); it
.Advance()) {
102 if (!it
.GetCurrentValue()->Send(
103 new ChildProcessMsg_GetChildProfilerData(sequence_number
))) {
107 OnPendingProcesses(sequence_number
, pending_processes
, false);
109 BrowserThread::PostTask(
112 base::Bind(&ProfilerControllerImpl::GetProfilerDataFromChildProcesses
,
113 base::Unretained(this),
117 } // namespace content