OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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.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.h" |
12 #include "content/common/child_process_host_impl.h" | 13 #include "content/common/child_process_host_impl.h" |
13 #include "content/common/utility_messages.h" | 14 #include "content/common/utility_messages.h" |
14 #include "content/public/browser/content_browser_client.h" | 15 #include "content/public/browser/content_browser_client.h" |
15 #include "content/public/common/content_switches.h" | 16 #include "content/public/common/content_switches.h" |
16 #include "ipc/ipc_switches.h" | 17 #include "ipc/ipc_switches.h" |
17 #include "ui/base/ui_base_switches.h" | 18 #include "ui/base/ui_base_switches.h" |
18 #include "webkit/plugins/plugin_switches.h" | 19 #include "webkit/plugins/plugin_switches.h" |
19 | 20 |
20 using content::BrowserThread; | 21 using content::BrowserThread; |
21 using content::ChildProcessHost; | 22 using content::ChildProcessHost; |
22 | 23 |
23 UtilityProcessHost::Client::Client() { | 24 UtilityProcessHost::Client::Client() { |
24 } | 25 } |
25 | 26 |
26 UtilityProcessHost::Client::~Client() { | 27 UtilityProcessHost::Client::~Client() { |
27 } | 28 } |
28 | 29 |
29 void UtilityProcessHost::Client::OnProcessCrashed(int exit_code) { | 30 void UtilityProcessHost::Client::OnProcessCrashed(int exit_code) { |
30 } | 31 } |
31 | 32 |
32 bool UtilityProcessHost::Client::OnMessageReceived( | 33 bool UtilityProcessHost::Client::OnMessageReceived( |
33 const IPC::Message& message) { | 34 const IPC::Message& message) { |
34 return false; | 35 return false; |
35 } | 36 } |
36 | 37 |
37 UtilityProcessHost::UtilityProcessHost(Client* client, | 38 UtilityProcessHost::UtilityProcessHost(Client* client, |
38 BrowserThread::ID client_thread_id) | 39 BrowserThread::ID client_thread_id) |
39 : BrowserChildProcessHost(content::PROCESS_TYPE_UTILITY), | 40 : client_(client), |
40 client_(client), | |
41 client_thread_id_(client_thread_id), | 41 client_thread_id_(client_thread_id), |
42 is_batch_mode_(false), | 42 is_batch_mode_(false), |
43 no_sandbox_(false), | 43 no_sandbox_(false), |
44 #if defined(OS_LINUX) | 44 #if defined(OS_LINUX) |
45 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), | 45 child_flags_(ChildProcessHost::CHILD_ALLOW_SELF), |
46 #else | 46 #else |
47 child_flags_(ChildProcessHost::CHILD_NORMAL), | 47 child_flags_(ChildProcessHost::CHILD_NORMAL), |
48 #endif | 48 #endif |
49 use_linux_zygote_(false), | 49 use_linux_zygote_(false), |
50 started_(false) { | 50 started_(false) { |
| 51 process_.reset(new BrowserChildProcessHost( |
| 52 content::PROCESS_TYPE_UTILITY, this)); |
51 } | 53 } |
52 | 54 |
53 UtilityProcessHost::~UtilityProcessHost() { | 55 UtilityProcessHost::~UtilityProcessHost() { |
54 DCHECK(!is_batch_mode_); | 56 DCHECK(!is_batch_mode_); |
55 } | 57 } |
56 | 58 |
57 bool UtilityProcessHost::Send(IPC::Message* message) { | 59 bool UtilityProcessHost::Send(IPC::Message* message) { |
58 if (!StartProcess()) | 60 if (!StartProcess()) |
59 return false; | 61 return false; |
60 | 62 |
61 return BrowserChildProcessHost::Send(message); | 63 return process_->Send(message); |
62 } | 64 } |
63 | 65 |
64 bool UtilityProcessHost::StartBatchMode() { | 66 bool UtilityProcessHost::StartBatchMode() { |
65 CHECK(!is_batch_mode_); | 67 CHECK(!is_batch_mode_); |
66 is_batch_mode_ = StartProcess(); | 68 is_batch_mode_ = StartProcess(); |
67 Send(new UtilityMsg_BatchMode_Started()); | 69 Send(new UtilityMsg_BatchMode_Started()); |
68 return is_batch_mode_; | 70 return is_batch_mode_; |
69 } | 71 } |
70 | 72 |
71 void UtilityProcessHost::EndBatchMode() { | 73 void UtilityProcessHost::EndBatchMode() { |
72 CHECK(is_batch_mode_); | 74 CHECK(is_batch_mode_); |
73 is_batch_mode_ = false; | 75 is_batch_mode_ = false; |
74 Send(new UtilityMsg_BatchMode_Finished()); | 76 Send(new UtilityMsg_BatchMode_Finished()); |
75 } | 77 } |
76 | 78 |
77 FilePath UtilityProcessHost::GetUtilityProcessCmd() { | 79 FilePath UtilityProcessHost::GetUtilityProcessCmd() { |
78 return ChildProcessHost::GetChildPath(child_flags_); | 80 return ChildProcessHost::GetChildPath(child_flags_); |
79 } | 81 } |
80 | 82 |
81 bool UtilityProcessHost::StartProcess() { | 83 bool UtilityProcessHost::StartProcess() { |
82 if (started_) | 84 if (started_) |
83 return true; | 85 return true; |
84 started_ = true; | 86 started_ = true; |
85 | 87 |
86 if (is_batch_mode_) | 88 if (is_batch_mode_) |
87 return true; | 89 return true; |
88 // Name must be set or metrics_service will crash in any test which | 90 // Name must be set or metrics_service will crash in any test which |
89 // launches a UtilityProcessHost. | 91 // launches a UtilityProcessHost. |
90 SetName(ASCIIToUTF16("utility process")); | 92 process_->SetName(ASCIIToUTF16("utility process")); |
91 | 93 |
92 std::string channel_id = child_process_host()->CreateChannel(); | 94 std::string channel_id = process_->GetHost()->CreateChannel(); |
93 if (channel_id.empty()) | 95 if (channel_id.empty()) |
94 return false; | 96 return false; |
95 | 97 |
96 FilePath exe_path = GetUtilityProcessCmd(); | 98 FilePath exe_path = GetUtilityProcessCmd(); |
97 if (exe_path.empty()) { | 99 if (exe_path.empty()) { |
98 NOTREACHED() << "Unable to get utility process binary name."; | 100 NOTREACHED() << "Unable to get utility process binary name."; |
99 return false; | 101 return false; |
100 } | 102 } |
101 | 103 |
102 CommandLine* cmd_line = new CommandLine(exe_path); | 104 CommandLine* cmd_line = new CommandLine(exe_path); |
(...skipping 26 matching lines...) Expand all Loading... |
129 | 131 |
130 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir_); | 132 cmd_line->AppendSwitchPath(switches::kUtilityProcessAllowedDir, exposed_dir_); |
131 #endif | 133 #endif |
132 | 134 |
133 bool use_zygote = false; | 135 bool use_zygote = false; |
134 | 136 |
135 #if defined(OS_LINUX) | 137 #if defined(OS_LINUX) |
136 use_zygote = !no_sandbox_ && use_linux_zygote_; | 138 use_zygote = !no_sandbox_ && use_linux_zygote_; |
137 #endif | 139 #endif |
138 | 140 |
139 Launch( | 141 process_->Launch( |
140 #if defined(OS_WIN) | 142 #if defined(OS_WIN) |
141 exposed_dir_, | 143 exposed_dir_, |
142 #elif defined(OS_POSIX) | 144 #elif defined(OS_POSIX) |
143 use_zygote, | 145 use_zygote, |
144 env_, | 146 env_, |
145 #endif | 147 #endif |
146 cmd_line); | 148 cmd_line); |
147 | 149 |
148 return true; | 150 return true; |
149 } | 151 } |
150 | 152 |
151 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 153 bool UtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
152 BrowserThread::PostTask( | 154 BrowserThread::PostTask( |
153 client_thread_id_, FROM_HERE, | 155 client_thread_id_, FROM_HERE, |
154 base::Bind(base::IgnoreResult(&Client::OnMessageReceived), | 156 base::Bind(base::IgnoreResult(&Client::OnMessageReceived), |
155 client_.get(), message)); | 157 client_.get(), message)); |
156 return true; | 158 return true; |
157 } | 159 } |
158 | 160 |
159 void UtilityProcessHost::OnProcessCrashed(int exit_code) { | 161 void UtilityProcessHost::OnProcessCrashed(int exit_code) { |
160 BrowserThread::PostTask( | 162 BrowserThread::PostTask( |
161 client_thread_id_, FROM_HERE, | 163 client_thread_id_, FROM_HERE, |
162 base::Bind(&Client::OnProcessCrashed, client_.get(), exit_code)); | 164 base::Bind(&Client::OnProcessCrashed, client_.get(), exit_code)); |
163 } | 165 } |
OLD | NEW |