Added SwapInterval to the GPU command buffer
[chromium-blink-merge.git] / content / browser / utility_process_host_impl.cc
blob0d24e3a5e6906b25fe18e6a41e8ee885251439cb
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 "content/public/common/sandboxed_process_launcher_delegate.h"
27 #include "ipc/ipc_switches.h"
28 #include "ui/base/ui_base_switches.h"
30 namespace content {
32 // NOTE: changes to this class need to be reviewed by the security team.
33 class UtilitySandboxedProcessLauncherDelegate
34 : public SandboxedProcessLauncherDelegate {
35 public:
36 UtilitySandboxedProcessLauncherDelegate(const base::FilePath& exposed_dir,
37 bool launch_elevated, bool no_sandbox,
38 base::EnvironmentMap& env,
39 ChildProcessHost* host)
40 : exposed_dir_(exposed_dir),
41 #if defined(OS_WIN)
42 launch_elevated_(launch_elevated)
43 #elif defined(OS_POSIX)
44 env_(env),
45 no_sandbox_(no_sandbox),
46 ipc_fd_(host->TakeClientFileDescriptor())
47 #endif // OS_WIN
50 ~UtilitySandboxedProcessLauncherDelegate() override {}
52 #if defined(OS_WIN)
53 virtual bool ShouldLaunchElevated() override {
54 return launch_elevated_;
56 virtual void PreSandbox(bool* disable_default_policy,
57 base::FilePath* exposed_dir) override {
58 *exposed_dir = exposed_dir_;
60 #elif defined(OS_POSIX)
62 bool ShouldUseZygote() override {
63 return !no_sandbox_ && exposed_dir_.empty();
65 base::EnvironmentMap GetEnvironment() override { return env_; }
66 base::ScopedFD TakeIpcFd() override { return ipc_fd_.Pass(); }
67 #endif // OS_WIN
69 private:
71 base::FilePath exposed_dir_;
73 #if defined(OS_WIN)
74 bool launch_elevated_;
75 #elif defined(OS_POSIX)
76 base::EnvironmentMap env_;
77 bool no_sandbox_;
78 base::ScopedFD ipc_fd_;
79 #endif // OS_WIN
82 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL;
84 UtilityProcessHost* UtilityProcessHost::Create(
85 const scoped_refptr<UtilityProcessHostClient>& client,
86 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner) {
87 return new UtilityProcessHostImpl(client, client_task_runner);
90 void UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
91 UtilityMainThreadFactoryFunction create) {
92 g_utility_main_thread_factory = create;
95 UtilityProcessHostImpl::UtilityProcessHostImpl(
96 const scoped_refptr<UtilityProcessHostClient>& client,
97 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner)
98 : client_(client),
99 client_task_runner_(client_task_runner),
100 is_batch_mode_(false),
101 is_mdns_enabled_(false),
102 no_sandbox_(false),
103 run_elevated_(false),
104 #if defined(OS_LINUX)
105 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF),
106 #else
107 child_flags_(ChildProcessHost::CHILD_NORMAL),
108 #endif
109 started_(false) {
112 UtilityProcessHostImpl::~UtilityProcessHostImpl() {
113 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
114 if (is_batch_mode_)
115 EndBatchMode();
118 bool UtilityProcessHostImpl::Send(IPC::Message* message) {
119 if (!StartProcess())
120 return false;
122 return process_->Send(message);
125 bool UtilityProcessHostImpl::StartBatchMode() {
126 CHECK(!is_batch_mode_);
127 is_batch_mode_ = StartProcess();
128 Send(new UtilityMsg_BatchMode_Started());
129 return is_batch_mode_;
132 void UtilityProcessHostImpl::EndBatchMode() {
133 CHECK(is_batch_mode_);
134 is_batch_mode_ = false;
135 Send(new UtilityMsg_BatchMode_Finished());
138 void UtilityProcessHostImpl::SetExposedDir(const base::FilePath& dir) {
139 exposed_dir_ = dir;
142 void UtilityProcessHostImpl::EnableMDns() {
143 is_mdns_enabled_ = true;
146 void UtilityProcessHostImpl::DisableSandbox() {
147 no_sandbox_ = true;
150 #if defined(OS_WIN)
151 void UtilityProcessHostImpl::ElevatePrivileges() {
152 no_sandbox_ = true;
153 run_elevated_ = true;
155 #endif
157 const ChildProcessData& UtilityProcessHostImpl::GetData() {
158 return process_->GetData();
161 #if defined(OS_POSIX)
163 void UtilityProcessHostImpl::SetEnv(const base::EnvironmentMap& env) {
164 env_ = env;
167 #endif // OS_POSIX
169 bool UtilityProcessHostImpl::StartProcess() {
170 if (started_)
171 return true;
172 started_ = true;
174 if (is_batch_mode_)
175 return true;
177 // Name must be set or metrics_service will crash in any test which
178 // launches a UtilityProcessHost.
179 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this));
180 process_->SetName(base::ASCIIToUTF16("utility process"));
182 std::string channel_id = process_->GetHost()->CreateChannel();
183 if (channel_id.empty())
184 return false;
186 if (RenderProcessHost::run_renderer_in_process()) {
187 DCHECK(g_utility_main_thread_factory);
188 // See comment in RenderProcessHostImpl::Init() for the background on why we
189 // support single process mode this way.
190 in_process_thread_.reset(g_utility_main_thread_factory(channel_id));
191 in_process_thread_->Start();
192 } else {
193 const base::CommandLine& browser_command_line =
194 *base::CommandLine::ForCurrentProcess();
195 int child_flags = child_flags_;
197 #if defined(OS_POSIX)
198 bool has_cmd_prefix = browser_command_line.HasSwitch(
199 switches::kUtilityCmdPrefix);
201 // When running under gdb, forking /proc/self/exe ends up forking the gdb
202 // executable instead of Chromium. It is almost safe to assume that no
203 // updates will happen while a developer is running with
204 // |switches::kUtilityCmdPrefix|. See ChildProcessHost::GetChildPath() for
205 // a similar case with Valgrind.
206 if (has_cmd_prefix)
207 child_flags = ChildProcessHost::CHILD_NORMAL;
208 #endif
210 base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
211 if (exe_path.empty()) {
212 NOTREACHED() << "Unable to get utility process binary name.";
213 return false;
216 base::CommandLine* cmd_line = new base::CommandLine(exe_path);
217 cmd_line->AppendSwitchASCII(switches::kProcessType,
218 switches::kUtilityProcess);
219 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
220 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
221 cmd_line->AppendSwitchASCII(switches::kLang, locale);
223 if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox))
224 cmd_line->AppendSwitch(switches::kNoSandbox);
225 #if defined(OS_MACOSX)
226 if (browser_command_line.HasSwitch(switches::kEnableSandboxLogging))
227 cmd_line->AppendSwitch(switches::kEnableSandboxLogging);
228 #endif
229 if (browser_command_line.HasSwitch(switches::kDebugPluginLoading))
230 cmd_line->AppendSwitch(switches::kDebugPluginLoading);
232 #if defined(OS_POSIX)
233 if (has_cmd_prefix) {
234 // Launch the utility child process with some prefix
235 // (usually "xterm -e gdb --args").
236 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
237 switches::kUtilityCmdPrefix));
240 if (!exposed_dir_.empty()) {
241 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir,
242 exposed_dir_);
244 #endif
246 if (is_mdns_enabled_)
247 cmd_line->AppendSwitch(switches::kUtilityProcessEnableMDns);
249 #if defined(OS_WIN)
250 // Let the utility process know if it is intended to be elevated.
251 if (run_elevated_)
252 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
253 #endif
255 process_->Launch(
256 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_,
257 run_elevated_,
258 no_sandbox_, env_,
259 process_->GetHost()),
260 cmd_line);
263 return true;
266 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) {
267 if (!client_.get())
268 return true;
270 client_task_runner_->PostTask(
271 FROM_HERE,
272 base::Bind(
273 base::IgnoreResult(&UtilityProcessHostClient::OnMessageReceived),
274 client_.get(),
275 message));
277 return true;
280 void UtilityProcessHostImpl::OnProcessLaunchFailed() {
281 if (!client_.get())
282 return;
284 client_task_runner_->PostTask(
285 FROM_HERE,
286 base::Bind(&UtilityProcessHostClient::OnProcessLaunchFailed,
287 client_.get()));
290 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) {
291 if (!client_.get())
292 return;
294 client_task_runner_->PostTask(
295 FROM_HERE,
296 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(),
297 exit_code));
300 } // namespace content