ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / content / browser / utility_process_host_impl.cc
blobb859407e702b0e59ab250e821f631af29dd760e2
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 const 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 bool ShouldLaunchElevated() override {
54 return launch_elevated_;
56 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:
70 base::FilePath exposed_dir_;
72 #if defined(OS_WIN)
73 bool launch_elevated_;
74 #elif defined(OS_POSIX)
75 base::EnvironmentMap env_;
76 bool no_sandbox_;
77 base::ScopedFD ipc_fd_;
78 #endif // OS_WIN
81 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL;
83 UtilityProcessHost* UtilityProcessHost::Create(
84 const scoped_refptr<UtilityProcessHostClient>& client,
85 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner) {
86 return new UtilityProcessHostImpl(client, client_task_runner);
89 void UtilityProcessHostImpl::RegisterUtilityMainThreadFactory(
90 UtilityMainThreadFactoryFunction create) {
91 g_utility_main_thread_factory = create;
94 UtilityProcessHostImpl::UtilityProcessHostImpl(
95 const scoped_refptr<UtilityProcessHostClient>& client,
96 const scoped_refptr<base::SequencedTaskRunner>& client_task_runner)
97 : client_(client),
98 client_task_runner_(client_task_runner),
99 is_batch_mode_(false),
100 is_mdns_enabled_(false),
101 no_sandbox_(false),
102 run_elevated_(false),
103 #if defined(OS_LINUX)
104 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF),
105 #else
106 child_flags_(ChildProcessHost::CHILD_NORMAL),
107 #endif
108 started_(false) {
111 UtilityProcessHostImpl::~UtilityProcessHostImpl() {
112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
113 if (is_batch_mode_)
114 EndBatchMode();
117 bool UtilityProcessHostImpl::Send(IPC::Message* message) {
118 if (!StartProcess())
119 return false;
121 return process_->Send(message);
124 bool UtilityProcessHostImpl::StartBatchMode() {
125 CHECK(!is_batch_mode_);
126 is_batch_mode_ = StartProcess();
127 Send(new UtilityMsg_BatchMode_Started());
128 return is_batch_mode_;
131 void UtilityProcessHostImpl::EndBatchMode() {
132 CHECK(is_batch_mode_);
133 is_batch_mode_ = false;
134 Send(new UtilityMsg_BatchMode_Finished());
137 void UtilityProcessHostImpl::SetExposedDir(const base::FilePath& dir) {
138 exposed_dir_ = dir;
141 void UtilityProcessHostImpl::EnableMDns() {
142 is_mdns_enabled_ = true;
145 void UtilityProcessHostImpl::DisableSandbox() {
146 no_sandbox_ = true;
149 #if defined(OS_WIN)
150 void UtilityProcessHostImpl::ElevatePrivileges() {
151 no_sandbox_ = true;
152 run_elevated_ = true;
154 #endif
156 const ChildProcessData& UtilityProcessHostImpl::GetData() {
157 return process_->GetData();
160 #if defined(OS_POSIX)
162 void UtilityProcessHostImpl::SetEnv(const base::EnvironmentMap& env) {
163 env_ = env;
166 #endif // OS_POSIX
168 bool UtilityProcessHostImpl::StartProcess() {
169 if (started_)
170 return true;
171 started_ = true;
173 if (is_batch_mode_)
174 return true;
176 // Name must be set or metrics_service will crash in any test which
177 // launches a UtilityProcessHost.
178 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this));
179 process_->SetName(base::ASCIIToUTF16("utility process"));
181 std::string channel_id = process_->GetHost()->CreateChannel();
182 if (channel_id.empty())
183 return false;
185 if (RenderProcessHost::run_renderer_in_process()) {
186 DCHECK(g_utility_main_thread_factory);
187 // See comment in RenderProcessHostImpl::Init() for the background on why we
188 // support single process mode this way.
189 in_process_thread_.reset(g_utility_main_thread_factory(channel_id));
190 in_process_thread_->Start();
191 } else {
192 const base::CommandLine& browser_command_line =
193 *base::CommandLine::ForCurrentProcess();
194 int child_flags = child_flags_;
196 bool has_cmd_prefix = browser_command_line.HasSwitch(
197 switches::kUtilityCmdPrefix);
199 // When running under gdb, forking /proc/self/exe ends up forking the gdb
200 // executable instead of Chromium. It is almost safe to assume that no
201 // updates will happen while a developer is running with
202 // |switches::kUtilityCmdPrefix|. See ChildProcessHost::GetChildPath() for
203 // a similar case with Valgrind.
204 if (has_cmd_prefix)
205 child_flags = ChildProcessHost::CHILD_NORMAL;
207 base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
208 if (exe_path.empty()) {
209 NOTREACHED() << "Unable to get utility process binary name.";
210 return false;
213 base::CommandLine* cmd_line = new base::CommandLine(exe_path);
214 cmd_line->AppendSwitchASCII(switches::kProcessType,
215 switches::kUtilityProcess);
216 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
217 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
218 cmd_line->AppendSwitchASCII(switches::kLang, locale);
220 if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox))
221 cmd_line->AppendSwitch(switches::kNoSandbox);
222 #if defined(OS_MACOSX)
223 if (browser_command_line.HasSwitch(switches::kEnableSandboxLogging))
224 cmd_line->AppendSwitch(switches::kEnableSandboxLogging);
225 #endif
226 if (browser_command_line.HasSwitch(switches::kDebugPluginLoading))
227 cmd_line->AppendSwitch(switches::kDebugPluginLoading);
229 if (has_cmd_prefix) {
230 // Launch the utility child process with some prefix
231 // (usually "xterm -e gdb --args").
232 cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
233 switches::kUtilityCmdPrefix));
236 if (!exposed_dir_.empty()) {
237 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir,
238 exposed_dir_);
241 if (is_mdns_enabled_)
242 cmd_line->AppendSwitch(switches::kUtilityProcessEnableMDns);
244 #if defined(OS_WIN)
245 // Let the utility process know if it is intended to be elevated.
246 if (run_elevated_)
247 cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
248 #endif
250 process_->Launch(
251 new UtilitySandboxedProcessLauncherDelegate(exposed_dir_,
252 run_elevated_,
253 no_sandbox_, env_,
254 process_->GetHost()),
255 cmd_line);
258 return true;
261 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) {
262 if (!client_.get())
263 return true;
265 client_task_runner_->PostTask(
266 FROM_HERE,
267 base::Bind(
268 base::IgnoreResult(&UtilityProcessHostClient::OnMessageReceived),
269 client_.get(),
270 message));
272 return true;
275 void UtilityProcessHostImpl::OnProcessLaunchFailed() {
276 if (!client_.get())
277 return;
279 client_task_runner_->PostTask(
280 FROM_HERE,
281 base::Bind(&UtilityProcessHostClient::OnProcessLaunchFailed,
282 client_.get()));
285 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) {
286 if (!client_.get())
287 return;
289 client_task_runner_->PostTask(
290 FROM_HERE,
291 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(),
292 exit_code));
295 } // namespace content