Respond with QuotaExceededError when IndexedDB has no disk space on open.
[chromium-blink-merge.git] / content / browser / browser_child_process_host_impl.cc
blob5cc953281d3178a3bb387dfcfb756aef37af9cba
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/browser_child_process_host_impl.h"
7 #include "base/base_switches.h"
8 #include "base/bind.h"
9 #include "base/command_line.h"
10 #include "base/files/file_path.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/metrics/histogram.h"
14 #include "base/path_service.h"
15 #include "base/stl_util.h"
16 #include "base/strings/string_util.h"
17 #include "base/synchronization/waitable_event.h"
18 #include "content/browser/histogram_message_filter.h"
19 #include "content/browser/loader/resource_message_filter.h"
20 #include "content/browser/profiler_message_filter.h"
21 #include "content/browser/tracing/trace_message_filter.h"
22 #include "content/common/child_process_host_impl.h"
23 #include "content/public/browser/browser_child_process_host_delegate.h"
24 #include "content/public/browser/browser_child_process_observer.h"
25 #include "content/public/browser/browser_thread.h"
26 #include "content/public/browser/child_process_data.h"
27 #include "content/public/browser/content_browser_client.h"
28 #include "content/public/common/content_switches.h"
29 #include "content/public/common/process_type.h"
30 #include "content/public/common/result_codes.h"
32 #if defined(OS_MACOSX)
33 #include "content/browser/mach_broker_mac.h"
34 #endif
36 namespace content {
37 namespace {
39 static base::LazyInstance<BrowserChildProcessHostImpl::BrowserChildProcessList>
40 g_child_process_list = LAZY_INSTANCE_INITIALIZER;
42 base::LazyInstance<ObserverList<BrowserChildProcessObserver> >
43 g_observers = LAZY_INSTANCE_INITIALIZER;
45 void NotifyProcessHostConnected(const ChildProcessData& data) {
46 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
47 BrowserChildProcessHostConnected(data));
50 void NotifyProcessHostDisconnected(const ChildProcessData& data) {
51 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
52 BrowserChildProcessHostDisconnected(data));
55 void NotifyProcessCrashed(const ChildProcessData& data) {
56 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
57 BrowserChildProcessCrashed(data));
60 } // namespace
62 BrowserChildProcessHost* BrowserChildProcessHost::Create(
63 int process_type,
64 BrowserChildProcessHostDelegate* delegate) {
65 return new BrowserChildProcessHostImpl(process_type, delegate);
68 #if defined(OS_MACOSX)
69 base::ProcessMetrics::PortProvider* BrowserChildProcessHost::GetPortProvider() {
70 return MachBroker::GetInstance();
72 #endif
74 // static
75 BrowserChildProcessHostImpl::BrowserChildProcessList*
76 BrowserChildProcessHostImpl::GetIterator() {
77 return g_child_process_list.Pointer();
80 // static
81 void BrowserChildProcessHostImpl::AddObserver(
82 BrowserChildProcessObserver* observer) {
83 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
84 g_observers.Get().AddObserver(observer);
87 // static
88 void BrowserChildProcessHostImpl::RemoveObserver(
89 BrowserChildProcessObserver* observer) {
90 // TODO(phajdan.jr): Check thread after fixing http://crbug.com/167126.
91 g_observers.Get().RemoveObserver(observer);
94 BrowserChildProcessHostImpl::BrowserChildProcessHostImpl(
95 int process_type,
96 BrowserChildProcessHostDelegate* delegate)
97 : data_(process_type),
98 delegate_(delegate),
99 power_monitor_message_broadcaster_(this) {
100 data_.id = ChildProcessHostImpl::GenerateChildProcessUniqueId();
102 child_process_host_.reset(ChildProcessHost::Create(this));
103 child_process_host_->AddFilter(new TraceMessageFilter);
104 child_process_host_->AddFilter(new ProfilerMessageFilter(process_type));
105 child_process_host_->AddFilter(new HistogramMessageFilter());
107 g_child_process_list.Get().push_back(this);
108 GetContentClient()->browser()->BrowserChildProcessHostCreated(this);
111 BrowserChildProcessHostImpl::~BrowserChildProcessHostImpl() {
112 g_child_process_list.Get().remove(this);
114 #if defined(OS_WIN)
115 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
116 #endif
119 // static
120 void BrowserChildProcessHostImpl::TerminateAll() {
121 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
122 // Make a copy since the BrowserChildProcessHost dtor mutates the original
123 // list.
124 BrowserChildProcessList copy = g_child_process_list.Get();
125 for (BrowserChildProcessList::iterator it = copy.begin();
126 it != copy.end(); ++it) {
127 delete (*it)->delegate(); // ~*HostDelegate deletes *HostImpl.
131 void BrowserChildProcessHostImpl::Launch(
132 #if defined(OS_WIN)
133 SandboxedProcessLauncherDelegate* delegate,
134 #elif defined(OS_POSIX)
135 bool use_zygote,
136 const base::EnvironmentMap& environ,
137 #endif
138 CommandLine* cmd_line) {
139 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
141 GetContentClient()->browser()->AppendExtraCommandLineSwitches(
142 cmd_line, data_.id);
144 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
145 static const char* kForwardSwitches[] = {
146 switches::kDisableLogging,
147 switches::kEnableDCHECK,
148 switches::kEnableLogging,
149 switches::kLoggingLevel,
150 switches::kTraceToConsole,
151 switches::kV,
152 switches::kVModule,
153 #if defined(OS_POSIX)
154 switches::kChildCleanExit,
155 #endif
156 #if defined(OS_WIN)
157 switches::kEnableHighResolutionTime,
158 #endif
160 cmd_line->CopySwitchesFrom(browser_command_line, kForwardSwitches,
161 arraysize(kForwardSwitches));
163 child_process_.reset(new ChildProcessLauncher(
164 #if defined(OS_WIN)
165 delegate,
166 #elif defined(OS_POSIX)
167 use_zygote,
168 environ,
169 child_process_host_->TakeClientFileDescriptor(),
170 #endif
171 cmd_line,
172 data_.id,
173 this));
176 const ChildProcessData& BrowserChildProcessHostImpl::GetData() const {
177 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
178 return data_;
181 ChildProcessHost* BrowserChildProcessHostImpl::GetHost() const {
182 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
183 return child_process_host_.get();
186 base::ProcessHandle BrowserChildProcessHostImpl::GetHandle() const {
187 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
188 DCHECK(child_process_.get())
189 << "Requesting a child process handle before launching.";
190 DCHECK(child_process_->GetHandle())
191 << "Requesting a child process handle before launch has completed OK.";
192 return child_process_->GetHandle();
195 void BrowserChildProcessHostImpl::SetName(const string16& name) {
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
197 data_.name = name;
200 void BrowserChildProcessHostImpl::SetHandle(base::ProcessHandle handle) {
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
202 data_.handle = handle;
205 void BrowserChildProcessHostImpl::ForceShutdown() {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
207 g_child_process_list.Get().remove(this);
208 child_process_host_->ForceShutdown();
211 void BrowserChildProcessHostImpl::SetBackgrounded(bool backgrounded) {
212 child_process_->SetProcessBackgrounded(backgrounded);
215 void BrowserChildProcessHostImpl::SetTerminateChildOnShutdown(
216 bool terminate_on_shutdown) {
217 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
218 child_process_->SetTerminateChildOnShutdown(terminate_on_shutdown);
221 void BrowserChildProcessHostImpl::NotifyProcessInstanceCreated(
222 const ChildProcessData& data) {
223 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
224 FOR_EACH_OBSERVER(BrowserChildProcessObserver, g_observers.Get(),
225 BrowserChildProcessInstanceCreated(data));
228 base::TerminationStatus BrowserChildProcessHostImpl::GetTerminationStatus(
229 bool known_dead, int* exit_code) {
230 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
231 if (!child_process_) // If the delegate doesn't use Launch() helper.
232 return base::GetTerminationStatus(data_.handle, exit_code);
233 return child_process_->GetChildTerminationStatus(known_dead,
234 exit_code);
237 bool BrowserChildProcessHostImpl::OnMessageReceived(
238 const IPC::Message& message) {
239 return delegate_->OnMessageReceived(message);
242 void BrowserChildProcessHostImpl::OnChannelConnected(int32 peer_pid) {
243 #if defined(OS_WIN)
244 // From this point onward, the exit of the child process is detected by an
245 // error on the IPC channel.
246 DeleteProcessWaitableEvent(early_exit_watcher_.GetWatchedEvent());
247 early_exit_watcher_.StopWatching();
248 #endif
250 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
251 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
252 base::Bind(&NotifyProcessHostConnected, data_));
254 delegate_->OnChannelConnected(peer_pid);
257 void BrowserChildProcessHostImpl::OnChannelError() {
258 delegate_->OnChannelError();
261 bool BrowserChildProcessHostImpl::CanShutdown() {
262 return delegate_->CanShutdown();
265 void BrowserChildProcessHostImpl::OnChildDisconnected() {
266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
267 if (child_process_.get() || data_.handle) {
268 DCHECK(data_.handle != base::kNullProcessHandle);
269 int exit_code;
270 base::TerminationStatus status = GetTerminationStatus(
271 true /* known_dead */, &exit_code);
272 switch (status) {
273 case base::TERMINATION_STATUS_PROCESS_CRASHED:
274 case base::TERMINATION_STATUS_ABNORMAL_TERMINATION: {
275 delegate_->OnProcessCrashed(exit_code);
276 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
277 base::Bind(&NotifyProcessCrashed, data_));
278 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Crashed2",
279 data_.process_type,
280 PROCESS_TYPE_MAX);
281 break;
283 case base::TERMINATION_STATUS_PROCESS_WAS_KILLED: {
284 delegate_->OnProcessCrashed(exit_code);
285 // Report that this child process was killed.
286 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Killed2",
287 data_.process_type,
288 PROCESS_TYPE_MAX);
289 break;
291 case base::TERMINATION_STATUS_STILL_RUNNING: {
292 UMA_HISTOGRAM_ENUMERATION("ChildProcess.DisconnectedAlive2",
293 data_.process_type,
294 PROCESS_TYPE_MAX);
296 default:
297 break;
299 UMA_HISTOGRAM_ENUMERATION("ChildProcess.Disconnected2",
300 data_.process_type,
301 PROCESS_TYPE_MAX);
303 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
304 base::Bind(&NotifyProcessHostDisconnected, data_));
305 delete delegate_; // Will delete us
308 bool BrowserChildProcessHostImpl::Send(IPC::Message* message) {
309 return child_process_host_->Send(message);
312 void BrowserChildProcessHostImpl::OnProcessLaunched() {
313 base::ProcessHandle handle = child_process_->GetHandle();
314 if (!handle) {
315 delete delegate_; // Will delete us
316 return;
319 #if defined(OS_WIN)
320 // Start a WaitableEventWatcher that will invoke OnProcessExitedEarly if the
321 // child process exits. This watcher is stopped once the IPC channel is
322 // connected and the exit of the child process is detecter by an error on the
323 // IPC channel thereafter.
324 DCHECK(!early_exit_watcher_.GetWatchedEvent());
325 early_exit_watcher_.StartWatching(
326 new base::WaitableEvent(handle),
327 base::Bind(&BrowserChildProcessHostImpl::OnProcessExitedEarly,
328 base::Unretained(this)));
329 #endif
331 data_.handle = handle;
332 delegate_->OnProcessLaunched();
335 #if defined(OS_WIN)
337 void BrowserChildProcessHostImpl::DeleteProcessWaitableEvent(
338 base::WaitableEvent* event) {
339 if (!event)
340 return;
342 // The WaitableEvent does not own the process handle so ensure it does not
343 // close it.
344 event->Release();
346 delete event;
349 void BrowserChildProcessHostImpl::OnProcessExitedEarly(
350 base::WaitableEvent* event) {
351 DeleteProcessWaitableEvent(event);
352 OnChildDisconnected();
355 #endif
357 } // namespace content