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.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/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "content/browser/browser_child_process_host_impl.h" | 12 #include "content/browser/browser_child_process_host_impl.h" |
13 #include "content/common/child_process_host_impl.h" | 13 #include "content/common/child_process_host_impl.h" |
14 #include "content/common/utility_messages.h" | 14 #include "content/common/utility_messages.h" |
15 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
| 16 #include "content/public/browser/utility_process_host_client.h" |
16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
17 #include "ipc/ipc_switches.h" | 18 #include "ipc/ipc_switches.h" |
18 #include "ui/base/ui_base_switches.h" | 19 #include "ui/base/ui_base_switches.h" |
19 #include "webkit/plugins/plugin_switches.h" | 20 #include "webkit/plugins/plugin_switches.h" |
20 | 21 |
21 using content::BrowserThread; | 22 using content::BrowserThread; |
22 using content::ChildProcessHost; | 23 using content::ChildProcessHost; |
| 24 using content::UtilityProcessHostClient; |
23 | 25 |
24 UtilityProcessHost::Client::Client() { | 26 namespace content { |
| 27 |
| 28 UtilityProcessHost* UtilityProcessHost::Create( |
| 29 UtilityProcessHostClient* client, |
| 30 BrowserThread::ID client_thread_id) { |
| 31 return new UtilityProcessHostImpl(client, client_thread_id); |
25 } | 32 } |
26 | 33 |
27 UtilityProcessHost::Client::~Client() { | |
28 } | 34 } |
29 | 35 |
30 void UtilityProcessHost::Client::OnProcessCrashed(int exit_code) { | 36 UtilityProcessHostImpl::UtilityProcessHostImpl( |
31 } | 37 UtilityProcessHostClient* client, |
32 | 38 BrowserThread::ID client_thread_id) |
33 bool UtilityProcessHost::Client::OnMessageReceived( | |
34 const IPC::Message& message) { | |
35 return false; | |
36 } | |
37 | |
38 UtilityProcessHost::UtilityProcessHost(Client* client, | |
39 BrowserThread::ID client_thread_id) | |
40 : client_(client), | 39 : client_(client), |
41 client_thread_id_(client_thread_id), | 40 client_thread_id_(client_thread_id), |
42 is_batch_mode_(false), | 41 is_batch_mode_(false), |
43 no_sandbox_(false), | 42 no_sandbox_(false), |
44 #if defined(OS_LINUX) | 43 #if defined(OS_LINUX) |
45 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | 44 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), |
46 #else | 45 #else |
47 child_flags_(ChildProcessHost::CHILD_NORMAL), | 46 child_flags_(ChildProcessHost::CHILD_NORMAL), |
48 #endif | 47 #endif |
49 use_linux_zygote_(false), | 48 use_linux_zygote_(false), |
50 started_(false) { | 49 started_(false) { |
51 process_.reset( | 50 process_.reset( |
52 new BrowserChildProcessHostImpl(content::PROCESS_TYPE_UTILITY, this)); | 51 new BrowserChildProcessHostImpl(content::PROCESS_TYPE_UTILITY, this)); |
53 } | 52 } |
54 | 53 |
55 UtilityProcessHost::~UtilityProcessHost() { | 54 UtilityProcessHostImpl::~UtilityProcessHostImpl() { |
56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 55 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
57 DCHECK(!is_batch_mode_); | 56 DCHECK(!is_batch_mode_); |
58 } | 57 } |
59 | 58 |
60 bool UtilityProcessHost::Send(IPC::Message* message) { | 59 bool UtilityProcessHostImpl::Send(IPC::Message* message) { |
61 if (!StartProcess()) | 60 if (!StartProcess()) |
62 return false; | 61 return false; |
63 | 62 |
64 return process_->Send(message); | 63 return process_->Send(message); |
65 } | 64 } |
66 | 65 |
67 bool UtilityProcessHost::StartBatchMode() { | 66 bool UtilityProcessHostImpl::StartBatchMode() { |
68 CHECK(!is_batch_mode_); | 67 CHECK(!is_batch_mode_); |
69 is_batch_mode_ = StartProcess(); | 68 is_batch_mode_ = StartProcess(); |
70 Send(new UtilityMsg_BatchMode_Started()); | 69 Send(new UtilityMsg_BatchMode_Started()); |
71 return is_batch_mode_; | 70 return is_batch_mode_; |
72 } | 71 } |
73 | 72 |
74 void UtilityProcessHost::EndBatchMode() { | 73 void UtilityProcessHostImpl::EndBatchMode() { |
75 CHECK(is_batch_mode_); | 74 CHECK(is_batch_mode_); |
76 is_batch_mode_ = false; | 75 is_batch_mode_ = false; |
77 Send(new UtilityMsg_BatchMode_Finished()); | 76 Send(new UtilityMsg_BatchMode_Finished()); |
78 } | 77 } |
79 | 78 |
80 FilePath UtilityProcessHost::GetUtilityProcessCmd() { | 79 void UtilityProcessHostImpl::SetExposedDir(const FilePath& dir) { |
81 return ChildProcessHost::GetChildPath(child_flags_); | 80 exposed_dir_ = dir; |
82 } | 81 } |
83 | 82 |
84 bool UtilityProcessHost::StartProcess() { | 83 void UtilityProcessHostImpl::DisableSandbox() { |
| 84 no_sandbox_ = true; |
| 85 } |
| 86 |
| 87 void UtilityProcessHostImpl::EnableZygote() { |
| 88 use_linux_zygote_ = true; |
| 89 } |
| 90 |
| 91 #if defined(OS_POSIX) |
| 92 |
| 93 void UtilityProcessHostImpl::SetEnv(const base::environment_vector& env) { |
| 94 env_ = env; |
| 95 } |
| 96 |
| 97 #endif // OS_POSIX |
| 98 |
| 99 bool UtilityProcessHostImpl::StartProcess() { |
85 if (started_) | 100 if (started_) |
86 return true; | 101 return true; |
87 started_ = true; | 102 started_ = true; |
88 | 103 |
89 if (is_batch_mode_) | 104 if (is_batch_mode_) |
90 return true; | 105 return true; |
91 // Name must be set or metrics_service will crash in any test which | 106 // Name must be set or metrics_service will crash in any test which |
92 // launches a UtilityProcessHost. | 107 // launches a UtilityProcessHost. |
93 process_->SetName(ASCIIToUTF16("utility process")); | 108 process_->SetName(ASCIIToUTF16("utility process")); |
94 | 109 |
95 std::string channel_id = process_->GetHost()->CreateChannel(); | 110 std::string channel_id = process_->GetHost()->CreateChannel(); |
96 if (channel_id.empty()) | 111 if (channel_id.empty()) |
97 return false; | 112 return false; |
98 | 113 |
99 FilePath exe_path = GetUtilityProcessCmd(); | 114 FilePath exe_path = ChildProcessHost::GetChildPath(child_flags_); |
100 if (exe_path.empty()) { | 115 if (exe_path.empty()) { |
101 NOTREACHED() << "Unable to get utility process binary name."; | 116 NOTREACHED() << "Unable to get utility process binary name."; |
102 return false; | 117 return false; |
103 } | 118 } |
104 | 119 |
105 CommandLine* cmd_line = new CommandLine(exe_path); | 120 CommandLine* cmd_line = new CommandLine(exe_path); |
106 cmd_line->AppendSwitchASCII(switches::kProcessType, | 121 cmd_line->AppendSwitchASCII(switches::kProcessType, |
107 switches::kUtilityProcess); | 122 switches::kUtilityProcess); |
108 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); | 123 cmd_line->AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
109 std::string locale = | 124 std::string locale = |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 exposed_dir_, | 159 exposed_dir_, |
145 #elif defined(OS_POSIX) | 160 #elif defined(OS_POSIX) |
146 use_zygote, | 161 use_zygote, |
147 env_, | 162 env_, |
148 #endif | 163 #endif |
149 cmd_line); | 164 cmd_line); |
150 | 165 |
151 return true; | 166 return true; |
152 } | 167 } |
153 | 168 |
154 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 169 bool UtilityProcessHostImpl::OnMessageReceived(const IPC::Message& message) { |
155 BrowserThread::PostTask( | 170 BrowserThread::PostTask( |
156 client_thread_id_, FROM_HERE, | 171 client_thread_id_, FROM_HERE, |
157 base::Bind(base::IgnoreResult(&Client::OnMessageReceived), | 172 base::Bind(base::IgnoreResult( |
158 client_.get(), message)); | 173 &UtilityProcessHostClient::OnMessageReceived), client_.get(), |
| 174 message)); |
159 return true; | 175 return true; |
160 } | 176 } |
161 | 177 |
162 void UtilityProcessHost::OnProcessCrashed(int exit_code) { | 178 void UtilityProcessHostImpl::OnProcessCrashed(int exit_code) { |
163 BrowserThread::PostTask( | 179 BrowserThread::PostTask( |
164 client_thread_id_, FROM_HERE, | 180 client_thread_id_, FROM_HERE, |
165 base::Bind(&Client::OnProcessCrashed, client_.get(), exit_code)); | 181 base::Bind(&UtilityProcessHostClient::OnProcessCrashed, client_.get(), |
| 182 exit_code)); |
166 } | 183 } |
OLD | NEW |