Respond with QuotaExceededError when IndexedDB has no disk space on open.
[chromium-blink-merge.git] / content / browser / utility_process_host_impl.cc
blobf2b3bc31deeaa3ff2b0fe94726a0309ce4e6c8d3
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/utility_process_host_impl.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/lazy_instance.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/run_loop.h"
13 #include "base/sequenced_task_runner.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/synchronization/lock.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "content/browser/browser_child_process_host_impl.h"
18 #include "content/browser/renderer_host/render_process_host_impl.h"
19 #include "content/common/child_process_host_impl.h"
20 #include "content/common/utility_messages.h"
21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/content_browser_client.h"
23 #include "content/public/browser/utility_process_host_client.h"
24 #include "content/public/common/content_switches.h"
25 #include "content/public/common/process_type.h"
26 #include "ipc/ipc_switches.h"
27 #include "ui/base/ui_base_switches.h"
29 #if defined(OS_WIN)
30 #include "content/public/common/sandboxed_process_launcher_delegate.h"
31 #endif
33 namespace content {
35 #if defined(OS_WIN)
36 // NOTE: changes to this class need to be reviewed by the security team.
37 class UtilitySandboxedProcessLauncherDelegate
38 : public SandboxedProcessLauncherDelegate {
39 public:
40 explicit UtilitySandboxedProcessLauncherDelegate(
41 const base::FilePath& exposed_dir) : exposed_dir_(exposed_dir) {}
42 virtual ~UtilitySandboxedProcessLauncherDelegate() {}
44 virtual void PreSandbox(bool* disable_default_policy,
45 base::FilePath* exposed_dir) OVERRIDE {
46 *exposed_dir = exposed_dir_;
49 private:
50 base::FilePath exposed_dir_;
52 #endif
55 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL;
57 UtilityProcessHost* UtilityProcessHost::Create(
58 UtilityProcessHostClient* client,
59 base::SequencedTaskRunner* client_task_runner) {
60 return new UtilityProcessHostImpl(client, client_task_runner);
63 void UtilityProcessHost::RegisterUtilityMainThreadFactory(
64 UtilityMainThreadFactoryFunction create) {
65 g_utility_main_thread_factory = create;
68 UtilityProcessHostImpl::UtilityProcessHostImpl(
69 UtilityProcessHostClient* client,
70 base::SequencedTaskRunner* client_task_runner)
71 : client_(client),
72 client_task_runner_(client_task_runner),
73 is_batch_mode_(false),
74 is_mdns_enabled_(false),
75 no_sandbox_(false),
76 #if defined(OS_LINUX)
77 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF),
78 #else
79 child_flags_(ChildProcessHost::CHILD_NORMAL),
80 #endif
81 use_linux_zygote_(false),
82 started_(false) {
85 UtilityProcessHostImpl::~UtilityProcessHostImpl() {
86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
87 if (is_batch_mode_)
88 EndBatchMode();
91 bool UtilityProcessHostImpl::Send(IPC::Message* message) {
92 if (!StartProcess())
93 return false;
95 return process_->Send(message);
98 bool UtilityProcessHostImpl::StartBatchMode() {
99 CHECK(!is_batch_mode_);
100 is_batch_mode_ = StartProcess();
101 Send(new UtilityMsg_BatchMode_Started());
102 return is_batch_mode_;
105 void UtilityProcessHostImpl::EndBatchMode() {
106 CHECK(is_batch_mode_);
107 is_batch_mode_ = false;
108 Send(new UtilityMsg_BatchMode_Finished());
111 void UtilityProcessHostImpl::SetExposedDir(const base::FilePath& dir) {
112 exposed_dir_ = dir;
115 void UtilityProcessHostImpl::EnableMDns() {
116 is_mdns_enabled_ = true;
119 void UtilityProcessHostImpl::DisableSandbox() {
120 no_sandbox_ = true;
123 void UtilityProcessHostImpl::EnableZygote() {
124 use_linux_zygote_ = true;
127 const ChildProcessData& UtilityProcessHostImpl::GetData() {
128 return process_->GetData();
131 #if defined(OS_POSIX)
133 void UtilityProcessHostImpl::SetEnv(const base::EnvironmentMap& env) {
134 env_ = env;
137 #endif // OS_POSIX
139 bool UtilityProcessHostImpl::StartProcess() {
140 if (started_)
141 return true;
142 started_ = true;
144 if (is_batch_mode_)
145 return true;
147 // Name must be set or metrics_service will crash in any test which
148 // launches a UtilityProcessHost.
149 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this));
150 process_->SetName(ASCIIToUTF16("utility process"));
152 std::string channel_id = process_->GetHost()->CreateChannel();
153 if (channel_id.empty())
154 return false;
156 // Single process not supported in multiple dll mode currently.
157 if (RenderProcessHost::run_renderer_in_process() &&
158 g_utility_main_thread_factory) {
159 // See comment in RenderProcessHostImpl::Init() for the background on why we
160 // support single process mode this way.
161 in_process_thread_.reset(g_utility_main_thread_factory(channel_id));
162 in_process_thread_->Start();
163 } else {
164 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
165 int child_flags = child_flags_;
167 #if defined(OS_POSIX)
168 bool has_cmd_prefix = browser_command_line.HasSwitch(
169 switches::kUtilityCmdPrefix);
171 // When running under gdb, forking /proc/self/exe ends up forking the gdb
172 // executable instead of Chromium. It is almost safe to assume that no
173 // updates will happen while a developer is running with
174 // |switches::kUtilityCmdPrefix|. See ChildProcessHost::GetChildPath() for
175 // a similar case with Valgrind.
176 if (has_cmd_prefix)
177 child_flags = ChildProcessHost::CHILD_NORMAL;
178 #endif
180 base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
181 if (exe_path.empty()) {
182 NOTREACHED() << "Unable to get utility process binary name.";
183 return false;
186 CommandLine* cmd_line = new CommandLine(exe_path);
187 cmd_line->AppendSwitchASCII(switches::kProcessType,
188 switches::kUtilityProcess);
189 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
190 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
191 cmd_line->AppendSwitchASCII(switches::kLang, locale);
193 if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox))
194 cmd_line->AppendSwitch(switches::kNoSandbox);
195 #if defined(OS_MACOSX)
196 if (browser_command_line.HasSwitch(switches::kEnableSandboxLogging))
197 cmd_line->AppendSwitch(switches::kEnableSandboxLogging);
198 #endif
199 if (browser_command_line.HasSwitch(switches::kDebugPluginLoading))
200 cmd_line->AppendSwitch(switches::kDebugPluginLoading);
202 #if defined(OS_POSIX)
203 // TODO(port): Sandbox this on Linux. Also, zygote this to work with
204 // Linux updating.
205 if (has_cmd_prefix) {
206 // launch the utility child process with some prefix
207 // (usually "xterm -e gdb --args").
208 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
209 switches::kUtilityCmdPrefix));
212 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir,
213 exposed_dir_);
214 #endif
216 if (is_mdns_enabled_)
217 cmd_line->AppendSwitch(switches::kUtilityProcessEnableMDns);
219 bool use_zygote = false;
221 #if defined(OS_LINUX)
222 use_zygote = !no_sandbox_ && use_linux_zygote_;
223 #endif
225 process_->Launch(
226 #if defined(OS_WIN)
227 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_),
228 #elif defined(OS_POSIX)
229 use_zygote,
230 env_,
231 #endif
232 cmd_line);
235 return true;
238 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) {
239 client_task_runner_->PostTask(
240 FROM_HERE,
241 base::Bind(base::IgnoreResult(
242 &UtilityProcessHostClient::OnMessageReceived), client_.get(),
243 message));
244 return true;
247 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) {
248 client_task_runner_->PostTask(
249 FROM_HERE,
250 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(),
251 exit_code));
254 } // namespace content