OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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. |
| 4 |
| 5 #include "content/browser/profiler_controller_impl.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/values.h" |
| 9 #include "content/browser/browser_child_process_host.h" |
| 10 #include "content/common/child_process_info.h" |
| 11 #include "content/common/child_process_messages.h" |
| 12 #include "content/public/browser/browser_thread.h" |
| 13 #include "content/public/browser/profiler_subscriber.h" |
| 14 #include "content/public/browser/render_process_host.h" |
| 15 #include "content/public/common/process_type.h" |
| 16 |
| 17 using content::BrowserThread; |
| 18 |
| 19 namespace content { |
| 20 |
| 21 content::ProfilerController* content::ProfilerController::GetInstance() { |
| 22 return ProfilerControllerImpl::GetInstance(); |
| 23 } |
| 24 |
| 25 ProfilerControllerImpl* ProfilerControllerImpl::GetInstance() { |
| 26 return Singleton<ProfilerControllerImpl>::get(); |
| 27 } |
| 28 |
| 29 ProfilerControllerImpl::ProfilerControllerImpl() : subscriber_(NULL) { |
| 30 } |
| 31 |
| 32 ProfilerControllerImpl::~ProfilerControllerImpl() { |
| 33 } |
| 34 |
| 35 void ProfilerControllerImpl::OnPendingProcesses(int sequence_number, |
| 36 int pending_processes, |
| 37 bool end) { |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 39 if (subscriber_) |
| 40 subscriber_->OnPendingProcesses(sequence_number, pending_processes, end); |
| 41 } |
| 42 |
| 43 void ProfilerControllerImpl::OnProfilerDataCollected( |
| 44 int sequence_number, |
| 45 base::DictionaryValue* profiler_data) { |
| 46 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 47 BrowserThread::PostTask( |
| 48 BrowserThread::UI, FROM_HERE, |
| 49 base::Bind(&ProfilerControllerImpl::OnProfilerDataCollected, |
| 50 base::Unretained(this), |
| 51 sequence_number, |
| 52 profiler_data)); |
| 53 return; |
| 54 } |
| 55 |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 if (subscriber_) |
| 58 subscriber_->OnProfilerDataCollected(sequence_number, profiler_data); |
| 59 } |
| 60 |
| 61 void ProfilerControllerImpl::Register(ProfilerSubscriber* subscriber) { |
| 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 63 DCHECK(!subscriber_); |
| 64 subscriber_ = subscriber; |
| 65 } |
| 66 |
| 67 void ProfilerControllerImpl::Unregister(ProfilerSubscriber* subscriber) { |
| 68 if (subscriber == subscriber_) |
| 69 subscriber_ = NULL; |
| 70 } |
| 71 |
| 72 void ProfilerControllerImpl::GetProfilerDataFromChildProcesses( |
| 73 int sequence_number) { |
| 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 75 |
| 76 int pending_processes = 0; |
| 77 for (BrowserChildProcessHost::Iterator child_process_host; |
| 78 !child_process_host.Done(); ++child_process_host) { |
| 79 const std::string process_type = |
| 80 content::GetProcessTypeNameInEnglish(child_process_host->type()); |
| 81 ++pending_processes; |
| 82 if (!child_process_host->Send(new ChildProcessMsg_GetChildProfilerData( |
| 83 sequence_number, process_type))) { |
| 84 --pending_processes; |
| 85 } |
| 86 } |
| 87 |
| 88 BrowserThread::PostTask( |
| 89 BrowserThread::UI, |
| 90 FROM_HERE, |
| 91 base::Bind( |
| 92 &ProfilerControllerImpl::OnPendingProcesses, |
| 93 base::Unretained(this), |
| 94 sequence_number, |
| 95 pending_processes, |
| 96 true)); |
| 97 } |
| 98 |
| 99 void ProfilerControllerImpl::GetProfilerData(int sequence_number) { |
| 100 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 101 |
| 102 int pending_processes = 0; |
| 103 const std::string render_process_type = |
| 104 content::GetProcessTypeNameInEnglish(content::PROCESS_TYPE_RENDERER); |
| 105 |
| 106 for (RenderProcessHost::iterator it(RenderProcessHost::AllHostsIterator()); |
| 107 !it.IsAtEnd(); it.Advance()) { |
| 108 ++pending_processes; |
| 109 if (!it.GetCurrentValue()->Send(new ChildProcessMsg_GetChildProfilerData( |
| 110 sequence_number, render_process_type))) { |
| 111 --pending_processes; |
| 112 } |
| 113 } |
| 114 OnPendingProcesses(sequence_number, pending_processes, false); |
| 115 |
| 116 BrowserThread::PostTask( |
| 117 BrowserThread::IO, |
| 118 FROM_HERE, |
| 119 base::Bind(&ProfilerControllerImpl::GetProfilerDataFromChildProcesses, |
| 120 base::Unretained(this), |
| 121 sequence_number)); |
| 122 } |
| 123 |
| 124 void ProfilerControllerImpl::SetProfilerStatusInChildProcesses(bool enable) { |
| 125 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 126 |
| 127 for (BrowserChildProcessHost::Iterator child_process_host; |
| 128 !child_process_host.Done(); ++child_process_host) { |
| 129 child_process_host->Send(new ChildProcessMsg_SetProfilerStatus(enable)); |
| 130 } |
| 131 } |
| 132 |
| 133 void ProfilerControllerImpl::SetProfilerStatus(bool enable) { |
| 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 135 |
| 136 BrowserThread::PostTask( |
| 137 BrowserThread::IO, |
| 138 FROM_HERE, |
| 139 base::Bind(&ProfilerControllerImpl::SetProfilerStatusInChildProcesses, |
| 140 base::Unretained(this), |
| 141 enable)); |
| 142 |
| 143 for (content::RenderProcessHost::iterator it( |
| 144 content::RenderProcessHost::AllHostsIterator()); |
| 145 !it.IsAtEnd(); it.Advance()) { |
| 146 it.GetCurrentValue()->Send(new ChildProcessMsg_SetProfilerStatus(enable)); |
| 147 } |
| 148 } |
| 149 } // namespace content |
OLD | NEW |