| 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 "mojo/edk/embedder/embedder.h" | 49 #include "mojo/edk/embedder/embedder.h" |
| 50 #include "services/service_manager/public/cpp/interface_registry.h" |
| 47 | 51 |
| 48 #if defined(OS_MACOSX) | 52 #if defined(OS_MACOSX) |
| 49 #include "content/browser/mach_broker_mac.h" | 53 #include "content/browser/mach_broker_mac.h" |
| 50 #endif | 54 #endif |
| 51 | 55 |
| 52 namespace content { | 56 namespace content { |
| 53 namespace { | 57 namespace { |
| 54 | 58 |
| 55 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList> | 59 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList> |
| 56 g_child_process_list = LAZY_INSTANCE_INITIALIZER; | 60 g_child_process_list = LAZY_INSTANCE_INITIALIZER; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 76 void NotifyProcessCrashed(const ChildProcessData& data, int exit_code) { | 80 void NotifyProcessCrashed(const ChildProcessData& data, int exit_code) { |
| 77 for (auto& observer : g_observers.Get()) | 81 for (auto& observer : g_observers.Get()) |
| 78 observer.BrowserChildProcessCrashed(data, exit_code); | 82 observer.BrowserChildProcessCrashed(data, exit_code); |
| 79 } | 83 } |
| 80 | 84 |
| 81 void NotifyProcessKilled(const ChildProcessData& data, int exit_code) { | 85 void NotifyProcessKilled(const ChildProcessData& data, int exit_code) { |
| 82 for (auto& observer : g_observers.Get()) | 86 for (auto& observer : g_observers.Get()) |
| 83 observer.BrowserChildProcessKilled(data, exit_code); | 87 observer.BrowserChildProcessKilled(data, exit_code); |
| 84 } | 88 } |
| 85 | 89 |
| 90 class ConnectionFilterImpl : public ConnectionFilter { |
| 91 public: |
| 92 ConnectionFilterImpl() {} |
| 93 |
| 94 private: |
| 95 // ConnectionFilter: |
| 96 bool OnConnect(const service_manager::Identity& remote_identity, |
| 97 service_manager::InterfaceRegistry* registry, |
| 98 service_manager::Connector* connector) override { |
| 99 registry->AddInterface(base::Bind(&PowerMonitorMessageBroadcaster::Create)); |
| 100 return true; |
| 101 } |
| 102 |
| 103 DISALLOW_COPY_AND_ASSIGN(ConnectionFilterImpl); |
| 104 }; |
| 105 |
| 86 } // namespace | 106 } // namespace |
| 87 | 107 |
| 88 BrowserChildProcessHost* BrowserChildProcessHost::Create( | 108 BrowserChildProcessHost* BrowserChildProcessHost::Create( |
| 89 content::ProcessType process_type, | 109 content::ProcessType process_type, |
| 90 BrowserChildProcessHostDelegate* delegate) { | 110 BrowserChildProcessHostDelegate* delegate) { |
| 91 return Create(process_type, delegate, std::string()); | 111 return Create(process_type, delegate, std::string()); |
| 92 } | 112 } |
| 93 | 113 |
| 94 BrowserChildProcessHost* BrowserChildProcessHost::Create( | 114 BrowserChildProcessHost* BrowserChildProcessHost::Create( |
| 95 content::ProcessType process_type, | 115 content::ProcessType process_type, |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 g_observers.Get().RemoveObserver(observer); | 155 g_observers.Get().RemoveObserver(observer); |
| 136 } | 156 } |
| 137 | 157 |
| 138 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( | 158 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl( |
| 139 content::ProcessType process_type, | 159 content::ProcessType process_type, |
| 140 BrowserChildProcessHostDelegate* delegate, | 160 BrowserChildProcessHostDelegate* delegate, |
| 141 const std::string& service_name) | 161 const std::string& service_name) |
| 142 : data_(process_type), | 162 : data_(process_type), |
| 143 delegate_(delegate), | 163 delegate_(delegate), |
| 144 child_token_(mojo::edk::GenerateRandomToken()), | 164 child_token_(mojo::edk::GenerateRandomToken()), |
| 145 power_monitor_message_broadcaster_(this), | |
| 146 is_channel_connected_(false), | 165 is_channel_connected_(false), |
| 147 notify_child_disconnected_(false), | 166 notify_child_disconnected_(false), |
| 148 weak_factory_(this) { | 167 weak_factory_(this) { |
| 149 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); | 168 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId(); |
| 150 | 169 |
| 151 child_process_host_.reset(ChildProcessHost::Create(this)); | 170 child_process_host_.reset(ChildProcessHost::Create(this)); |
| 152 AddFilter(new TraceMessageFilter(data_.id)); | 171 AddFilter(new TraceMessageFilter(data_.id)); |
| 153 AddFilter(new ProfilerMessageFilter(process_type)); | 172 AddFilter(new ProfilerMessageFilter(process_type)); |
| 154 AddFilter(new HistogramMessageFilter); | 173 AddFilter(new HistogramMessageFilter); |
| 155 AddFilter(new MemoryMessageFilter(this, process_type)); | 174 AddFilter(new MemoryMessageFilter(this, process_type)); |
| 156 | 175 |
| 157 g_child_process_list.Get().push_back(this); | 176 g_child_process_list.Get().push_back(this); |
| 158 GetContentClient()->browser()->BrowserChildProcessHostCreated(this); | 177 GetContentClient()->browser()->BrowserChildProcessHostCreated(this); |
| 159 | 178 |
| 160 power_monitor_message_broadcaster_.Init(); | |
| 161 | |
| 162 if (!service_name.empty()) { | 179 if (!service_name.empty()) { |
| 163 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 180 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 164 child_connection_.reset(new ChildConnection( | 181 child_connection_.reset(new ChildConnection( |
| 165 service_name, base::StringPrintf("%d", data_.id), child_token_, | 182 service_name, base::StringPrintf("%d", data_.id), child_token_, |
| 166 ServiceManagerContext::GetConnectorForIOThread(), | 183 ServiceManagerContext::GetConnectorForIOThread(), |
| 167 base::ThreadTaskRunnerHandle::Get())); | 184 base::ThreadTaskRunnerHandle::Get())); |
| 168 } | 185 } |
| 169 | 186 |
| 187 // May be null during test execution. |
| 188 if (ServiceManagerConnection::GetForProcess()) { |
| 189 ServiceManagerConnection::GetForProcess()->AddConnectionFilter( |
| 190 base::MakeUnique<ConnectionFilterImpl>()); |
| 191 } |
| 192 |
| 170 // Create a persistent memory segment for subprocess histograms. | 193 // Create a persistent memory segment for subprocess histograms. |
| 171 CreateMetricsAllocator(); | 194 CreateMetricsAllocator(); |
| 172 } | 195 } |
| 173 | 196 |
| 174 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { | 197 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() { |
| 175 g_child_process_list.Get().remove(this); | 198 g_child_process_list.Get().remove(this); |
| 176 | 199 |
| 177 if (notify_child_disconnected_) { | 200 if (notify_child_disconnected_) { |
| 178 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 201 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 179 base::Bind(&NotifyProcessHostDisconnected, data_)); | 202 base::Bind(&NotifyProcessHostDisconnected, data_)); |
| (...skipping 401 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 581 | 604 |
| 582 #if defined(OS_WIN) | 605 #if defined(OS_WIN) |
| 583 | 606 |
| 584 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { | 607 void BrowserChildProcessHostImpl::OnObjectSignaled(HANDLE object) { |
| 585 OnChildDisconnected(); | 608 OnChildDisconnected(); |
| 586 } | 609 } |
| 587 | 610 |
| 588 #endif | 611 #endif |
| 589 | 612 |
| 590 } // namespace content | 613 } // namespace content |
| OLD | NEW |