OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/utility_process_host_impl.h" | 5 #include "content/browser/utility_process_host_impl.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
10 #include "base/lazy_instance.h" | 10 #include "base/lazy_instance.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
46 virtual void PreSandbox(bool* disable_default_policy, | 46 virtual void PreSandbox(bool* disable_default_policy, |
47 base::FilePath* exposed_dir) OVERRIDE { | 47 base::FilePath* exposed_dir) OVERRIDE { |
48 *exposed_dir = exposed_dir_; | 48 *exposed_dir = exposed_dir_; |
49 } | 49 } |
50 | 50 |
51 private: | 51 private: |
52 base::FilePath exposed_dir_; | 52 base::FilePath exposed_dir_; |
53 }; | 53 }; |
54 #endif | 54 #endif |
55 | 55 |
56 // We want to ensure there's only one utility thread running at a time, as there | |
57 // are many globals used in the utility process. | |
58 static base::LazyInstance<base::Lock> g_one_utility_thread_lock; | |
59 | 56 |
60 // Single process not supported in multiple dll mode currently. | 57 UtilityMainThreadFactoryFunction g_utility_main_thread_factory = NULL; |
61 #if !defined(CHROME_MULTIPLE_DLL) | |
62 class UtilityMainThread : public base::Thread { | |
63 public: | |
64 UtilityMainThread(const std::string& channel_id) | |
65 : Thread("Chrome_InProcUtilityThread"), | |
66 channel_id_(channel_id) { | |
67 } | |
68 | |
69 virtual ~UtilityMainThread() { | |
70 Stop(); | |
71 } | |
72 | |
73 private: | |
74 // base::Thread implementation: | |
75 virtual void Init() OVERRIDE { | |
76 // We need to return right away or else the main thread that started us will | |
77 // hang. | |
78 base::MessageLoop::current()->PostTask( | |
79 FROM_HERE, | |
80 base::Bind(&UtilityMainThread::InitInternal, base::Unretained(this))); | |
81 } | |
82 | |
83 virtual void CleanUp() OVERRIDE { | |
84 child_process_.reset(); | |
85 | |
86 // See comment in RendererMainThread. | |
87 SetThreadWasQuitProperly(true); | |
88 g_one_utility_thread_lock.Get().Release(); | |
89 } | |
90 | |
91 void InitInternal() { | |
92 g_one_utility_thread_lock.Get().Acquire(); | |
93 child_process_.reset(new ChildProcess()); | |
94 child_process_->set_main_thread(new UtilityThreadImpl(channel_id_)); | |
95 } | |
96 | |
97 std::string channel_id_; | |
98 scoped_ptr<ChildProcess> child_process_; | |
99 | |
100 DISALLOW_COPY_AND_ASSIGN(UtilityMainThread); | |
101 }; | |
102 #endif // !CHROME_MULTIPLE_DLL | |
103 | 58 |
104 UtilityProcessHost* UtilityProcessHost::Create( | 59 UtilityProcessHost* UtilityProcessHost::Create( |
105 UtilityProcessHostClient* client, | 60 UtilityProcessHostClient* client, |
106 base::SequencedTaskRunner* client_task_runner) { | 61 base::SequencedTaskRunner* client_task_runner) { |
107 return new UtilityProcessHostImpl(client, client_task_runner); | 62 return new UtilityProcessHostImpl(client, client_task_runner); |
108 } | 63 } |
109 | 64 |
| 65 void UtilityProcessHost::RegisterUtilityMainThreadFactory( |
| 66 UtilityMainThreadFactoryFunction create) { |
| 67 g_utility_main_thread_factory = create; |
| 68 } |
| 69 |
110 UtilityProcessHostImpl::UtilityProcessHostImpl( | 70 UtilityProcessHostImpl::UtilityProcessHostImpl( |
111 UtilityProcessHostClient* client, | 71 UtilityProcessHostClient* client, |
112 base::SequencedTaskRunner* client_task_runner) | 72 base::SequencedTaskRunner* client_task_runner) |
113 : client_(client), | 73 : client_(client), |
114 client_task_runner_(client_task_runner), | 74 client_task_runner_(client_task_runner), |
115 is_batch_mode_(false), | 75 is_batch_mode_(false), |
116 is_mdns_enabled_(false), | 76 is_mdns_enabled_(false), |
117 no_sandbox_(false), | 77 no_sandbox_(false), |
118 #if defined(OS_LINUX) | 78 #if defined(OS_LINUX) |
119 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | 79 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 // Name must be set or metrics_service will crash in any test which | 149 // Name must be set or metrics_service will crash in any test which |
190 // launches a UtilityProcessHost. | 150 // launches a UtilityProcessHost. |
191 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this)); | 151 process_.reset(new BrowserChildProcessHostImpl(PROCESS_TYPE_UTILITY, this)); |
192 process_->SetName(ASCIIToUTF16("utility process")); | 152 process_->SetName(ASCIIToUTF16("utility process")); |
193 | 153 |
194 std::string channel_id = process_->GetHost()->CreateChannel(); | 154 std::string channel_id = process_->GetHost()->CreateChannel(); |
195 if (channel_id.empty()) | 155 if (channel_id.empty()) |
196 return false; | 156 return false; |
197 | 157 |
198 // Single process not supported in multiple dll mode currently. | 158 // Single process not supported in multiple dll mode currently. |
199 #if !defined(CHROME_MULTIPLE_DLL) | 159 if (RenderProcessHost::run_renderer_in_process() && |
200 if (RenderProcessHost::run_renderer_in_process()) { | 160 g_utility_main_thread_factory) { |
201 // See comment in RenderProcessHostImpl::Init() for the background on why we | 161 // See comment in RenderProcessHostImpl::Init() for the background on why we |
202 // support single process mode this way. | 162 // support single process mode this way. |
203 in_process_thread_.reset(new UtilityMainThread(channel_id)); | 163 in_process_thread_.reset(g_utility_main_thread_factory(channel_id)); |
204 in_process_thread_->Start(); | 164 in_process_thread_->Start(); |
205 } else | 165 } else { |
206 #endif // !CHROME_MULTIPLE_DLL | |
207 { | |
208 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); | 166 const CommandLine& browser_command_line = *CommandLine::ForCurrentProcess(); |
209 int child_flags = child_flags_; | 167 int child_flags = child_flags_; |
210 | 168 |
211 #if defined(OS_POSIX) | 169 #if defined(OS_POSIX) |
212 bool has_cmd_prefix = browser_command_line.HasSwitch( | 170 bool has_cmd_prefix = browser_command_line.HasSwitch( |
213 switches::kUtilityCmdPrefix); | 171 switches::kUtilityCmdPrefix); |
214 | 172 |
215 // When running under gdb, forking /proc/self/exe ends up forking the gdb | 173 // When running under gdb, forking /proc/self/exe ends up forking the gdb |
216 // executable instead of Chromium. It is almost safe to assume that no | 174 // executable instead of Chromium. It is almost safe to assume that no |
217 // updates will happen while a developer is running with | 175 // updates will happen while a developer is running with |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
288 } | 246 } |
289 | 247 |
290 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) { | 248 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) { |
291 client_task_runner_->PostTask( | 249 client_task_runner_->PostTask( |
292 FROM_HERE, | 250 FROM_HERE, |
293 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(), | 251 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(), |
294 exit_code)); | 252 exit_code)); |
295 } | 253 } |
296 | 254 |
297 } // namespace content | 255 } // namespace content |
OLD | NEW |