Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / content / browser / utility_process_host_impl.cc
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.
4
5 #include "content/browser/utility_process_host_impl.h"
6
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"
28
29 #if defined(OS_WIN)
30 #include "content/public/common/sandboxed_process_launcher_delegate.h"
31 #endif
32
33 namespace content {
34
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() {}
43
44   virtual void PreSandbox(bool* disable_default_policy,
45                           base::FilePath* exposed_dir) OVERRIDE {
46     *exposed_dir = exposed_dir_;
47   }
48
49 private:
50   base::FilePath exposed_dir_;
51 };
52 #endif
53
54
55 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL;
56
57 UtilityProcessHost* UtilityProcessHost::Create(
58     UtilityProcessHostClient* client,
59     base::SequencedTaskRunner* client_task_runner) {
60   return new UtilityProcessHostImpl(client, client_task_runner);
61 }
62
63 void UtilityProcessHost::RegisterUtilityMainThreadFactory(
64     UtilityMainThreadFactoryFunction create) {
65   g_utility_main_thread_factory = create;
66 }
67
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_WIN)
77       run_elevated_(false),
78 #endif
79 #if defined(OS_LINUX)
80       child_flags_(ChildProcessHost::CHILD_ALLOW_SELF),
81 #else
82       child_flags_(ChildProcessHost::CHILD_NORMAL),
83 #endif
84       started_(false) {
85 }
86
87 UtilityProcessHostImpl::~UtilityProcessHostImpl() {
88   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
89   if (is_batch_mode_)
90     EndBatchMode();
91 }
92
93 bool UtilityProcessHostImpl::Send(IPC::Message* message) {
94   if (!StartProcess())
95     return false;
96
97   return process_->Send(message);
98 }
99
100 bool UtilityProcessHostImpl::StartBatchMode()  {
101   CHECK(!is_batch_mode_);
102   is_batch_mode_ = StartProcess();
103   Send(new UtilityMsg_BatchMode_Started());
104   return is_batch_mode_;
105 }
106
107 void UtilityProcessHostImpl::EndBatchMode()  {
108   CHECK(is_batch_mode_);
109   is_batch_mode_ = false;
110   Send(new UtilityMsg_BatchMode_Finished());
111 }
112
113 void UtilityProcessHostImpl::SetExposedDir(const base::FilePath& dir) {
114   exposed_dir_ = dir;
115 }
116
117 void UtilityProcessHostImpl::EnableMDns() {
118   is_mdns_enabled_ = true;
119 }
120
121 void UtilityProcessHostImpl::DisableSandbox() {
122   no_sandbox_ = true;
123 }
124
125 #if defined(OS_WIN)
126 void UtilityProcessHostImpl::ElevatePrivileges() {
127   no_sandbox_ = true;
128   run_elevated_ = true;
129 }
130 #endif
131
132 const ChildProcessData& UtilityProcessHostImpl::GetData() {
133   return process_->GetData();
134 }
135
136 #if defined(OS_POSIX)
137
138 void UtilityProcessHostImpl::SetEnv(const base::EnvironmentMap& env) {
139   env_ = env;
140 }
141
142 #endif  // OS_POSIX
143
144 bool UtilityProcessHostImpl::StartProcess() {
145   if (started_)
146     return true;
147   started_ = true;
148
149   if (is_batch_mode_)
150     return true;
151
152   // Name must be set or metrics_service will crash in any test which
153   // launches a UtilityProcessHost.
154   process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this));
155   process_->SetName(base::ASCIIToUTF16("utility process"));
156
157   std::string channel_id = process_->GetHost()->CreateChannel();
158   if (channel_id.empty())
159     return false;
160
161   if (RenderProcessHost::run_renderer_in_process()) {
162     DCHECK(g_utility_main_thread_factory);
163     // See comment in RenderProcessHostImpl::Init() for the background on why we
164     // support single process mode this way.
165     in_process_thread_.reset(g_utility_main_thread_factory(channel_id));
166     in_process_thread_->Start();
167   } else {
168     const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess();
169     int child_flags = child_flags_;
170
171 #if defined(OS_POSIX)
172     bool has_cmd_prefix = browser_command_line.HasSwitch(
173         switches::kUtilityCmdPrefix);
174
175     // When running under gdb, forking /proc/self/exe ends up forking the gdb
176     // executable instead of Chromium. It is almost safe to assume that no
177     // updates will happen while a developer is running with
178     // |switches::kUtilityCmdPrefix|. See ChildProcessHost::GetChildPath() for
179     // a similar case with Valgrind.
180     if (has_cmd_prefix)
181       child_flags = ChildProcessHost::CHILD_NORMAL;
182 #endif
183
184     base::FilePath exe_path = ChildProcessHost::GetChildPath(child_flags);
185     if (exe_path.empty()) {
186       NOTREACHED() << "Unable to get utility process binary name.";
187       return false;
188     }
189
190     CommandLine* cmd_line = new CommandLine(exe_path);
191     cmd_line->AppendSwitchASCII(switches::kProcessType,
192                                 switches::kUtilityProcess);
193     cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id);
194     std::string locale = GetContentClient()->browser()->GetApplicationLocale();
195     cmd_line->AppendSwitchASCII(switches::kLang, locale);
196
197     if (no_sandbox_ || browser_command_line.HasSwitch(switches::kNoSandbox))
198       cmd_line->AppendSwitch(switches::kNoSandbox);
199 #if defined(OS_MACOSX)
200     if (browser_command_line.HasSwitch(switches::kEnableSandboxLogging))
201       cmd_line->AppendSwitch(switches::kEnableSandboxLogging);
202 #endif
203     if (browser_command_line.HasSwitch(switches::kDebugPluginLoading))
204       cmd_line->AppendSwitch(switches::kDebugPluginLoading);
205
206 #if defined(OS_POSIX)
207     if (has_cmd_prefix) {
208       // Launch the utility child process with some prefix
209       // (usually "xterm -e gdb --args").
210       cmd_line->PrependWrapper(browser_command_line.GetSwitchValueNative(
211           switches::kUtilityCmdPrefix));
212     }
213
214     if (!exposed_dir_.empty()) {
215       cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir,
216                                  exposed_dir_);
217     }
218 #endif
219
220     if (is_mdns_enabled_)
221       cmd_line->AppendSwitch(switches::kUtilityProcessEnableMDns);
222
223 #if defined(OS_WIN)
224     // Let the utility process know if it is intended to be elevated.
225     if (run_elevated_)
226       cmd_line->AppendSwitch(switches::kUtilityProcessRunningElevated);
227 #endif
228
229     bool use_zygote = false;
230
231 #if defined(OS_LINUX)
232     // The Linux sandbox does not support granting access to a single directory,
233     // so we need to bypass the zygote in that case.
234     use_zygote = !no_sandbox_ && exposed_dir_.empty();
235 #endif
236
237     process_->Launch(
238 #if defined(OS_WIN)
239         new UtilitySandboxedProcessLauncherDelegate(exposed_dir_),
240         run_elevated_,
241 #elif defined(OS_POSIX)
242         use_zygote,
243         env_,
244 #endif
245         cmd_line);
246   }
247
248   return true;
249 }
250
251 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) {
252   client_task_runner_->PostTask(
253       FROM_HERE,
254       base::Bind(base::IgnoreResult(
255           &UtilityProcessHostClient::OnMessageReceived), client_.get(),
256           message));
257   return true;
258 }
259
260 void UtilityProcessHostImpl::OnProcessLaunchFailed() {
261   client_task_runner_->PostTask(
262       FROM_HERE,
263       base::Bind(&UtilityProcessHostClient::OnProcessLaunchFailed,
264                  client_.get()));
265 }
266
267 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) {
268   client_task_runner_->PostTask(
269       FROM_HERE,
270       base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(),
271             exit_code));
272 }
273
274 }  // namespace content