OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/browser_child_process_host_impl.h" | 5 #include "content/browser/browser_child_process_host_impl.h" |
6 | 6 |
7 #include "base/base_switches.h" | 7 #include "base/base_switches.h" |
8 #include "base/bind.h" | 8 #include "base/bind.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/debug/dump_without_crashing.h" | 10 #include "base/debug/dump_without_crashing.h" |
11 #include "base/feature_list.h" | 11 #include "base/feature_list.h" |
12 #include "base/files/file_path.h" | 12 #include "base/files/file_path.h" |
13 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/metrics/field_trial.h" | 16 #include "base/metrics/field_trial.h" |
17 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
18 #include "base/metrics/persistent_histogram_allocator.h" | 18 #include "base/metrics/persistent_histogram_allocator.h" |
19 #include "base/metrics/persistent_memory_allocator.h" | 19 #include "base/metrics/persistent_memory_allocator.h" |
20 #include "base/stl_util.h" | 20 #include "base/stl_util.h" |
21 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
22 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
23 #include "base/synchronization/waitable_event.h" | 23 #include "base/synchronization/waitable_event.h" |
24 #include "base/threading/thread_task_runner_handle.h" | 24 #include "base/threading/thread_task_runner_handle.h" |
25 #include "build/build_config.h" | 25 #include "build/build_config.h" |
26 #include "components/tracing/common/tracing_switches.h" | 26 #include "components/tracing/common/tracing_switches.h" |
27 #include "content/browser/histogram_message_filter.h" | 27 #include "content/browser/histogram_message_filter.h" |
28 #include "content/browser/loader/resource_message_filter.h" | 28 #include "content/browser/loader/resource_message_filter.h" |
29 #include "content/browser/memory/memory_message_filter.h" | 29 #include "content/browser/memory/memory_message_filter.h" |
| 30 #include "content/browser/power_monitor_message_broadcaster.h" |
30 #include "content/browser/profiler_message_filter.h" | 31 #include "content/browser/profiler_message_filter.h" |
31 #include "content/browser/service_manager/service_manager_context.h" | 32 #include "content/browser/service_manager/service_manager_context.h" |
32 #include "content/browser/tracing/trace_message_filter.h" | 33 #include "content/browser/tracing/trace_message_filter.h" |
33 #include "content/common/child_process_host_impl.h" | 34 #include "content/common/child_process_host_impl.h" |
34 #include "content/common/child_process_messages.h" | 35 #include "content/common/child_process_messages.h" |
35 #include "content/common/service_manager/child_connection.h" | 36 #include "content/common/service_manager/child_connection.h" |
36 #include "content/public/browser/browser_child_process_host_delegate.h" | 37 #include "content/public/browser/browser_child_process_host_delegate.h" |
37 #include "content/public/browser/browser_child_process_observer.h" | 38 #include "content/public/browser/browser_child_process_observer.h" |
38 #include "content/public/browser/browser_thread.h" | 39 #include "content/public/browser/browser_thread.h" |
39 #include "content/public/browser/child_process_data.h" | 40 #include "content/public/browser/child_process_data.h" |
40 #include "content/public/browser/content_browser_client.h" | 41 #include "content/public/browser/content_browser_client.h" |
| 42 #include "content/public/common/connection_filter.h" |
41 #include "content/public/common/content_features.h" | 43 #include "content/public/common/content_features.h" |
42 #include "content/public/common/content_switches.h" | 44 #include "content/public/common/content_switches.h" |
43 #include "content/public/common/mojo_channel_switches.h" | 45 #include "content/public/common/mojo_channel_switches.h" |
44 #include "content/public/common/process_type.h" | 46 #include "content/public/common/process_type.h" |
45 #include "content/public/common/result_codes.h" | 47 #include "content/public/common/result_codes.h" |
| 48 #include "content/public/common/service_manager_connection.h" |
46 #include "ipc/attachment_broker.h" | 49 #include "ipc/attachment_broker.h" |
47 #include "ipc/attachment_broker_privileged.h" | 50 #include "ipc/attachment_broker_privileged.h" |
48 #include "mojo/edk/embedder/embedder.h" | 51 #include "mojo/edk/embedder/embedder.h" |
| 52 #include "services/service_manager/public/cpp/interface_registry.h" |
49 | 53 |
50 #if defined(OS_MACOSX) | 54 #if defined(OS_MACOSX) |
51 #include "content/browser/mach_broker_mac.h" | 55 #include "content/browser/mach_broker_mac.h" |
52 #endif | 56 #endif |
53 | 57 |
54 namespace content { | 58 namespace content { |
55 namespace { | 59 namespace { |
56 | 60 |
57 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList> | 61 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList> |
58 g_child_process_list = LAZY_INSTANCE_INITIALIZER; | 62 g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
110 } | 114 } |
111 return nullptr; | 115 return nullptr; |
112 } | 116 } |
113 | 117 |
114 #if defined(OS_MACOSX) | 118 #if defined(OS_MACOSX) |
115 base::PortProvider* BrowserChildProcessHost::GetPortProvider() { | 119 base::PortProvider* BrowserChildProcessHost::GetPortProvider() { |
116 return MachBroker::GetInstance(); | 120 return MachBroker::GetInstance(); |
117 } | 121 } |
118 #endif | 122 #endif |
119 | 123 |
| 124 class BrowserChildProcessHost::ConnectionFilterImpl : public ConnectionFilter { |
| 125 public: |
| 126 ConnectionFilterImpl() {} |
| 127 |
| 128 private: |
| 129 // ConnectionFilter: |
| 130 bool OnConnect(const service_manager::Identity& remote_identity, |
| 131 service_manager::InterfaceRegistry* registry, |
| 132 service_manager::Connector* connector) override { |
| 133 registry->AddInterface(base::Bind(&PowerMonitorMessageBroadcaster::Create)); |
| 134 return true; |
| 135 } |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(ConnectionFilterImpl); |
| 138 }; |
| 139 |
120 // static | 140 // static |
121 BrowserChildProcessHostImpl::BrowserChildProcessList* | 141 BrowserChildProcessHostImpl::BrowserChildProcessList* |
122 BrowserChildProcessHostImpl::GetIterator() { | 142 BrowserChildProcessHostImpl::GetIterator() { |
123 return g_child_process_list.Pointer(); | 143 return g_child_process_list.Pointer(); |
124 } | 144 } |
125 | 145 |
126 // static | 146 // static |
127 void BrowserChildProcessHostImpl::AddObserver( | 147 void BrowserChildProcessHostImpl::AddObserver( |
128 BrowserChildProcessObserver* observer) { | 148 BrowserChildProcessObserver* observer) { |
129 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 149 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
130 g_observers.Get().AddObserver(observer); | 150 g_observers.Get().AddObserver(observer); |
131 } | 151 } |
132 | 152 |
133 // static | 153 // static |
134 void BrowserChildProcessHostImpl::RemoveObserver( | 154 void BrowserChildProcessHostImpl::RemoveObserver( |
135 BrowserChildProcessObserver* observer) { | 155 BrowserChildProcessObserver* observer) { |
136 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. | 156 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126. |
137 g_observers.Get().RemoveObserver(observer); | 157 g_observers.Get().RemoveObserver(observer); |
138 } | 158 } |
139 | 159 |
140 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( | 160 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
141 content::ProcessType process_type, | 161 content::ProcessType process_type, |
142 BrowserChildProcessHostDelegate* delegate, | 162 BrowserChildProcessHostDelegate* delegate, |
143 const std::string& service_name) | 163 const std::string& service_name) |
144 : data_(process_type), | 164 : data_(process_type), |
145 delegate_(delegate), | 165 delegate_(delegate), |
146 child_token_(mojo::edk::GenerateRandomToken()), | 166 child_token_(mojo::edk::GenerateRandomToken()), |
147 power_monitor_message_broadcaster_(this), | |
148 is_channel_connected_(false), | 167 is_channel_connected_(false), |
149 notify_child_disconnected_(false), | 168 notify_child_disconnected_(false), |
150 weak_factory_(this) { | 169 weak_factory_(this) { |
151 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); | 170 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
152 | 171 |
153 #if USE_ATTACHMENT_BROKER | 172 #if USE_ATTACHMENT_BROKER |
154 // Construct the privileged attachment broker early in the life cycle of a | 173 // Construct the privileged attachment broker early in the life cycle of a |
155 // child process. This ensures that when a test is being run in one of the | 174 // child process. This ensures that when a test is being run in one of the |
156 // single process modes, the global attachment broker is the privileged | 175 // single process modes, the global attachment broker is the privileged |
157 // attachment broker, rather than an unprivileged attachment broker. | 176 // attachment broker, rather than an unprivileged attachment broker. |
158 #if defined(OS_MACOSX) | 177 #if defined(OS_MACOSX) |
159 IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded( | 178 IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded( |
160 MachBroker::GetInstance()); | 179 MachBroker::GetInstance()); |
161 #else | 180 #else |
162 IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded(); | 181 IPC::AttachmentBrokerPrivileged::CreateBrokerIfNeeded(); |
163 #endif // defined(OS_MACOSX) | 182 #endif // defined(OS_MACOSX) |
164 #endif // USE_ATTACHMENT_BROKER | 183 #endif // USE_ATTACHMENT_BROKER |
165 | 184 |
166 child_process_host_.reset(ChildProcessHost::Create(this)); | 185 child_process_host_.reset(ChildProcessHost::Create(this)); |
167 AddFilter(new TraceMessageFilter(data_.id)); | 186 AddFilter(new TraceMessageFilter(data_.id)); |
168 AddFilter(new ProfilerMessageFilter(process_type)); | 187 AddFilter(new ProfilerMessageFilter(process_type)); |
169 AddFilter(new HistogramMessageFilter); | 188 AddFilter(new HistogramMessageFilter); |
170 AddFilter(new MemoryMessageFilter(this, process_type)); | 189 AddFilter(new MemoryMessageFilter(this, process_type)); |
171 | 190 |
172 g_child_process_list.Get().push_back(this); | 191 g_child_process_list.Get().push_back(this); |
173 GetContentClient()->browser()->BrowserChildProcessHostCreated(this); | 192 GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
174 | 193 |
175 power_monitor_message_broadcaster_.Init(); | |
176 | |
177 if (!service_name.empty()) { | 194 if (!service_name.empty()) { |
178 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 195 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
179 child_connection_.reset(new ChildConnection( | 196 child_connection_.reset(new ChildConnection( |
180 service_name, base::StringPrintf("%d", data_.id), child_token_, | 197 service_name, base::StringPrintf("%d", data_.id), child_token_, |
181 ServiceManagerContext::GetConnectorForIOThread(), | 198 ServiceManagerContext::GetConnectorForIOThread(), |
182 base::ThreadTaskRunnerHandle::Get())); | 199 base::ThreadTaskRunnerHandle::Get())); |
183 } | 200 } |
184 | 201 |
| 202 // May be null during test execution. |
| 203 if (ServiceManagerConnection::GetForProcess()) { |
| 204 ServiceManagerConnection::GetForProcess()->AddConnectionFilter( |
| 205 base::MakeUnique<ConnectionFilterImpl>()); |
| 206 } |
| 207 |
185 // Create a persistent memory segment for subprocess histograms. | 208 // Create a persistent memory segment for subprocess histograms. |
186 CreateMetricsAllocator(); | 209 CreateMetricsAllocator(); |
187 } | 210 } |
188 | 211 |
189 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { | 212 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
190 g_child_process_list.Get().remove(this); | 213 g_child_process_list.Get().remove(this); |
191 | 214 |
192 if (notify_child_disconnected_) { | 215 if (notify_child_disconnected_) { |
193 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 216 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
194 base::Bind(&NotifyProcessHostDisconnected, data_)); | 217 base::Bind(&NotifyProcessHostDisconnected, data_)); |
(...skipping 403 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
598 | 621 |
599 #if defined(OS_WIN) | 622 #if defined(OS_WIN) |
600 | 623 |
601 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { | 624 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
602 OnChildDisconnected(); | 625 OnChildDisconnected(); |
603 } | 626 } |
604 | 627 |
605 #endif | 628 #endif |
606 | 629 |
607 } // namespace content | 630 } // namespace content |
OLD | NEW |