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 "chrome/service/service_utility_process_host.h" | 5 #include "chrome/service/service_utility_process_host.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/command_line.h" | 8 #include "base/command_line.h" |
9 #include "base/files/file.h" | |
10 #include "base/files/file_path.h" | |
9 #include "base/files/file_util.h" | 11 #include "base/files/file_util.h" |
10 #include "base/files/scoped_temp_dir.h" | |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/message_loop/message_loop.h" | |
13 #include "base/message_loop/message_loop_proxy.h" | 13 #include "base/message_loop/message_loop_proxy.h" |
14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
15 #include "base/process/kill.h" | 15 #include "base/process/kill.h" |
16 #include "base/strings/utf_string_conversions.h" | 16 #include "base/process/launch.h" |
17 #include "base/task_runner_util.h" | |
17 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/chrome_utility_printing_messages.h" | 19 #include "chrome/common/chrome_utility_printing_messages.h" |
19 #include "content/public/common/child_process_host.h" | 20 #include "content/public/common/child_process_host.h" |
20 #include "content/public/common/result_codes.h" | 21 #include "content/public/common/result_codes.h" |
21 #include "content/public/common/sandbox_init.h" | 22 #include "content/public/common/sandbox_init.h" |
23 #include "content/public/common/sandboxed_process_launcher_delegate.h" | |
22 #include "ipc/ipc_switches.h" | 24 #include "ipc/ipc_switches.h" |
23 #include "printing/page_range.h" | |
24 #include "ui/base/ui_base_switches.h" | |
25 #include "ui/gfx/rect.h" | |
26 | |
27 #if defined(OS_WIN) | |
28 #include "base/files/file_path.h" | |
29 #include "base/memory/scoped_ptr.h" | |
30 #include "base/process/launch.h" | |
31 #include "base/win/scoped_handle.h" | |
32 #include "content/public/common/sandbox_init.h" | |
33 #include "content/public/common/sandboxed_process_launcher_delegate.h" | |
34 #include "printing/emf_win.h" | 25 #include "printing/emf_win.h" |
35 #include "sandbox/win/src/sandbox_policy_base.h" | 26 #include "sandbox/win/src/sandbox_policy_base.h" |
27 #include "ui/base/ui_base_switches.h" | |
36 | 28 |
37 namespace { | 29 namespace { |
38 | 30 |
31 using content::ChildProcessHost; | |
32 | |
33 const int kMaxNumberOfTempFilesPerDocument = 3; | |
34 | |
35 enum ServiceUtilityProcessHostEvent { | |
36 SERVICE_UTILITY_STARTED, | |
37 SERVICE_UTILITY_DISCONNECTED, | |
38 SERVICE_UTILITY_METAFILE_REQUEST, | |
39 SERVICE_UTILITY_METAFILE_SUCCEEDED, | |
40 SERVICE_UTILITY_METAFILE_FAILED, | |
41 SERVICE_UTILITY_CAPS_REQUEST, | |
42 SERVICE_UTILITY_CAPS_SUCCEEDED, | |
43 SERVICE_UTILITY_CAPS_FAILED, | |
44 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | |
45 SERVICE_UTILITY_SEMANTIC_CAPS_SUCCEEDED, | |
46 SERVICE_UTILITY_SEMANTIC_CAPS_FAILED, | |
47 SERVICE_UTILITY_FAILED_TO_START, | |
48 SERVICE_UTILITY_EVENT_MAX, | |
49 }; | |
50 | |
39 // NOTE: changes to this class need to be reviewed by the security team. | 51 // NOTE: changes to this class need to be reviewed by the security team. |
40 class ServiceSandboxedProcessLauncherDelegate | 52 class ServiceSandboxedProcessLauncherDelegate |
41 : public content::SandboxedProcessLauncherDelegate { | 53 : public content::SandboxedProcessLauncherDelegate { |
42 public: | 54 public: |
43 explicit ServiceSandboxedProcessLauncherDelegate( | 55 ServiceSandboxedProcessLauncherDelegate() {} |
44 const base::FilePath& exposed_dir) | |
45 : exposed_dir_(exposed_dir) { | |
46 } | |
47 | |
48 virtual void PreSandbox(bool* disable_default_policy, | |
49 base::FilePath* exposed_dir) OVERRIDE { | |
50 *exposed_dir = exposed_dir_; | |
51 } | |
52 | 56 |
53 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, | 57 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, |
54 bool* success) OVERRIDE { | 58 bool* success) OVERRIDE { |
55 // Service process may run as windows service and it fails to create a | 59 // Service process may run as windows service and it fails to create a |
56 // window station. | 60 // window station. |
57 policy->SetAlternateDesktop(false); | 61 policy->SetAlternateDesktop(false); |
58 } | 62 } |
59 | 63 |
60 private: | 64 private: |
Lei Zhang
2014/09/16 03:36:55
private block looks empty, DISALLOW_COPY_AND_ASSIG
Vitaly Buka (NO REVIEWS)
2014/09/16 07:50:36
Done.
| |
61 base::FilePath exposed_dir_; | |
62 }; | 65 }; |
63 | 66 |
67 base::File CreateTempFile() { | |
68 base::FilePath path; | |
69 if (!base::CreateTemporaryFile(&path)) | |
70 return base::File(); | |
71 return base::File(path, | |
72 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | | |
73 base::File::FLAG_READ | | |
74 base::File::FLAG_DELETE_ON_CLOSE | | |
75 base::File::FLAG_TEMPORARY); | |
76 } | |
77 | |
64 } // namespace | 78 } // namespace |
65 | 79 |
66 #endif // OS_WIN | 80 class ServiceUtilityProcessHost::PdfToEmfState { |
81 public: | |
82 explicit PdfToEmfState(ServiceUtilityProcessHost* host) | |
83 : host_(host), page_count_(0), current_page_(0), pages_in_progress_(0) {} | |
84 ~PdfToEmfState() { Stop(); } | |
67 | 85 |
68 using content::ChildProcessHost; | 86 bool Start(base::File pdf_file, |
87 const printing::PdfRenderSettings& conversion_settings) { | |
88 return host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | |
89 IPC::TakeFileHandleForProcess(pdf_file.Pass(), host_->handle()), | |
90 conversion_settings)); | |
91 } | |
69 | 92 |
70 namespace { | 93 void GetMorePages() { |
71 enum ServiceUtilityProcessHostEvent { | 94 while (pages_in_progress_ < kMaxNumberOfTempFilesPerDocument && |
72 SERVICE_UTILITY_STARTED, | 95 current_page_ < page_count_) { |
73 SERVICE_UTILITY_DISCONNECTED, | 96 ++pages_in_progress_; |
74 SERVICE_UTILITY_METAFILE_REQUEST, | 97 emf_files_.push_back(CreateTempFile()); |
Lei Zhang
2014/09/16 03:36:55
Do you care if CreateTempFile() fails?
Vitaly Buka (NO REVIEWS)
2014/09/16 07:50:36
I don't. Invalid handle will be passed to utility
Lei Zhang
2014/09/16 19:57:36
I don't think CreateTemporaryFile() will have coll
Vitaly Buka (NO REVIEWS)
2014/09/16 22:10:37
If some process, not even Chrome leak files after
| |
75 SERVICE_UTILITY_METAFILE_SUCCEEDED, | 98 host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_GetPage( |
76 SERVICE_UTILITY_METAFILE_FAILED, | 99 current_page_++, |
77 SERVICE_UTILITY_CAPS_REQUEST, | 100 IPC::GetFileHandleForProcess( |
78 SERVICE_UTILITY_CAPS_SUCCEEDED, | 101 emf_files_.back().GetPlatformFile(), host_->handle(), false))); |
79 SERVICE_UTILITY_CAPS_FAILED, | 102 } |
80 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | 103 } |
81 SERVICE_UTILITY_SEMANTIC_CAPS_SUCCEEDED, | 104 |
82 SERVICE_UTILITY_SEMANTIC_CAPS_FAILED, | 105 bool OnPageProcessed() { |
Lei Zhang
2014/09/16 03:36:54
document return value
Vitaly Buka (NO REVIEWS)
2014/09/16 07:50:36
Done.
| |
83 SERVICE_UTILITY_EVENT_MAX, | 106 --pages_in_progress_; |
107 GetMorePages(); | |
108 if (pages_in_progress_ || current_page_ < page_count_) | |
109 return false; | |
110 Stop(); | |
111 return true; | |
112 } | |
113 | |
114 base::File TakeNextFile() { | |
115 DCHECK(!emf_files_.empty()); | |
116 base::File file; | |
117 if (!emf_files_.empty()) | |
118 file = emf_files_.front().Pass(); | |
119 emf_files_.pop_front(); | |
120 return file.Pass(); | |
121 } | |
122 | |
123 void set_page_count(int page_count) { page_count_ = page_count; } | |
124 | |
125 private: | |
126 void Stop() { | |
127 host_->Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles_Stop()); | |
128 } | |
129 ServiceUtilityProcessHost* host_; | |
130 std::deque<base::File> emf_files_; | |
131 int page_count_; | |
132 int current_page_; | |
133 int pages_in_progress_; | |
84 }; | 134 }; |
85 } // namespace | |
86 | 135 |
87 ServiceUtilityProcessHost::ServiceUtilityProcessHost( | 136 ServiceUtilityProcessHost::ServiceUtilityProcessHost( |
88 Client* client, base::MessageLoopProxy* client_message_loop_proxy) | 137 Client* client, |
89 : handle_(base::kNullProcessHandle), | 138 base::MessageLoopProxy* client_message_loop_proxy) |
90 client_(client), | 139 : handle_(base::kNullProcessHandle), |
91 client_message_loop_proxy_(client_message_loop_proxy), | 140 client_(client), |
92 waiting_for_reply_(false) { | 141 client_message_loop_proxy_(client_message_loop_proxy), |
142 waiting_for_reply_(false), | |
143 weak_ptr_factory_(this) { | |
93 child_process_host_.reset(ChildProcessHost::Create(this)); | 144 child_process_host_.reset(ChildProcessHost::Create(this)); |
94 } | 145 } |
95 | 146 |
96 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { | 147 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { |
97 // We need to kill the child process when the host dies. | 148 // We need to kill the child process when the host dies. |
98 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); | 149 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); |
99 } | 150 } |
100 | 151 |
101 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( | 152 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( |
102 const base::FilePath& pdf_path, | 153 const base::FilePath& pdf_path, |
103 const printing::PdfRenderSettings& render_settings, | 154 const printing::PdfRenderSettings& render_settings) { |
104 const std::vector<printing::PageRange>& page_ranges) { | |
105 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 155 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
106 SERVICE_UTILITY_METAFILE_REQUEST, | 156 SERVICE_UTILITY_METAFILE_REQUEST, |
107 SERVICE_UTILITY_EVENT_MAX); | 157 SERVICE_UTILITY_EVENT_MAX); |
108 start_time_ = base::Time::Now(); | 158 start_time_ = base::Time::Now(); |
109 #if !defined(OS_WIN) | 159 base::File pdf_file(pdf_path, base::File::FLAG_OPEN | base::File::FLAG_READ); |
110 // This is only implemented on Windows (because currently it is only needed | 160 if (!pdf_file.IsValid() || !StartProcess(false)) |
111 // on Windows). Will add implementations on other platforms when needed. | |
112 NOTIMPLEMENTED(); | |
113 return false; | |
114 #else // !defined(OS_WIN) | |
115 scratch_metafile_dir_.reset(new base::ScopedTempDir); | |
116 if (!scratch_metafile_dir_->CreateUniqueTempDir()) | |
117 return false; | |
118 metafile_path_ = scratch_metafile_dir_->path().AppendASCII("output.emf"); | |
119 if (!StartProcess(false, scratch_metafile_dir_->path())) | |
120 return false; | 161 return false; |
121 | 162 |
122 base::File pdf_file( | |
123 pdf_path, | |
124 base::File::FLAG_OPEN | base::File::FLAG_READ | base::File::FLAG_WRITE); | |
125 DCHECK(!waiting_for_reply_); | 163 DCHECK(!waiting_for_reply_); |
126 waiting_for_reply_ = true; | 164 waiting_for_reply_ = true; |
127 return child_process_host_->Send( | 165 |
128 new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | 166 pdf_to_emf_state_.reset(new PdfToEmfState(this)); |
129 IPC::TakeFileHandleForProcess(pdf_file.Pass(), handle()), | 167 return pdf_to_emf_state_->Start(pdf_file.Pass(), render_settings); |
130 metafile_path_, | |
131 render_settings, | |
132 page_ranges)); | |
133 #endif // !defined(OS_WIN) | |
134 } | 168 } |
135 | 169 |
136 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( | 170 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( |
137 const std::string& printer_name) { | 171 const std::string& printer_name) { |
138 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 172 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
139 SERVICE_UTILITY_CAPS_REQUEST, | 173 SERVICE_UTILITY_CAPS_REQUEST, |
140 SERVICE_UTILITY_EVENT_MAX); | 174 SERVICE_UTILITY_EVENT_MAX); |
141 start_time_ = base::Time::Now(); | 175 start_time_ = base::Time::Now(); |
142 base::FilePath exposed_path; | 176 if (!StartProcess(true)) |
143 if (!StartProcess(true, exposed_path)) | |
144 return false; | 177 return false; |
145 DCHECK(!waiting_for_reply_); | 178 DCHECK(!waiting_for_reply_); |
146 waiting_for_reply_ = true; | 179 waiting_for_reply_ = true; |
147 return child_process_host_->Send( | 180 return Send(new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); |
148 new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); | |
149 } | 181 } |
150 | 182 |
151 bool ServiceUtilityProcessHost::StartGetPrinterSemanticCapsAndDefaults( | 183 bool ServiceUtilityProcessHost::StartGetPrinterSemanticCapsAndDefaults( |
152 const std::string& printer_name) { | 184 const std::string& printer_name) { |
153 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 185 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
154 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | 186 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, |
155 SERVICE_UTILITY_EVENT_MAX); | 187 SERVICE_UTILITY_EVENT_MAX); |
156 start_time_ = base::Time::Now(); | 188 start_time_ = base::Time::Now(); |
157 base::FilePath exposed_path; | 189 if (!StartProcess(true)) |
158 if (!StartProcess(true, exposed_path)) | |
159 return false; | 190 return false; |
160 DCHECK(!waiting_for_reply_); | 191 DCHECK(!waiting_for_reply_); |
161 waiting_for_reply_ = true; | 192 waiting_for_reply_ = true; |
162 return child_process_host_->Send( | 193 return Send( |
163 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); | 194 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); |
164 } | 195 } |
165 | 196 |
166 bool ServiceUtilityProcessHost::StartProcess( | 197 bool ServiceUtilityProcessHost::Send(IPC::Message* msg) { |
167 bool no_sandbox, | 198 if (child_process_host_) |
168 const base::FilePath& exposed_dir) { | 199 return child_process_host_->Send(msg); |
200 delete msg; | |
201 return false; | |
202 } | |
203 | |
204 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox) { | |
169 std::string channel_id = child_process_host_->CreateChannel(); | 205 std::string channel_id = child_process_host_->CreateChannel(); |
170 if (channel_id.empty()) | 206 if (channel_id.empty()) |
171 return false; | 207 return false; |
172 | 208 |
173 base::FilePath exe_path = GetUtilityProcessCmd(); | 209 base::FilePath exe_path = GetUtilityProcessCmd(); |
174 if (exe_path.empty()) { | 210 if (exe_path.empty()) { |
175 NOTREACHED() << "Unable to get utility process binary name."; | 211 NOTREACHED() << "Unable to get utility process binary name."; |
176 return false; | 212 return false; |
177 } | 213 } |
178 | 214 |
179 CommandLine cmd_line(exe_path); | 215 CommandLine cmd_line(exe_path); |
180 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); | 216 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); |
181 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); | 217 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
182 cmd_line.AppendSwitch(switches::kLang); | 218 cmd_line.AppendSwitch(switches::kLang); |
183 | 219 |
184 if (Launch(&cmd_line, no_sandbox, exposed_dir)) { | 220 if (Launch(&cmd_line, no_sandbox)) { |
185 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 221 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
186 SERVICE_UTILITY_STARTED, | 222 SERVICE_UTILITY_STARTED, |
187 SERVICE_UTILITY_EVENT_MAX); | 223 SERVICE_UTILITY_EVENT_MAX); |
188 return true; | 224 return true; |
189 } | 225 } |
226 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | |
227 SERVICE_UTILITY_FAILED_TO_START, | |
228 SERVICE_UTILITY_EVENT_MAX); | |
190 return false; | 229 return false; |
191 } | 230 } |
192 | 231 |
193 bool ServiceUtilityProcessHost::Launch(CommandLine* cmd_line, | 232 bool ServiceUtilityProcessHost::Launch(CommandLine* cmd_line, bool no_sandbox) { |
Lei Zhang
2014/09/16 03:36:55
nit: base::CommandLine
Vitaly Buka (NO REVIEWS)
2014/09/16 07:50:36
Done.
| |
194 bool no_sandbox, | |
195 const base::FilePath& exposed_dir) { | |
196 #if !defined(OS_WIN) | |
197 // TODO(sanjeevr): Implement for non-Windows OSes. | |
198 NOTIMPLEMENTED(); | |
199 return false; | |
200 #else // !defined(OS_WIN) | |
201 | |
202 if (no_sandbox) { | 233 if (no_sandbox) { |
203 base::ProcessHandle process = base::kNullProcessHandle; | 234 base::ProcessHandle process = base::kNullProcessHandle; |
204 cmd_line->AppendSwitch(switches::kNoSandbox); | 235 cmd_line->AppendSwitch(switches::kNoSandbox); |
205 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); | 236 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); |
206 } else { | 237 } else { |
207 ServiceSandboxedProcessLauncherDelegate delegate(exposed_dir); | 238 ServiceSandboxedProcessLauncherDelegate delegate; |
208 handle_ = content::StartSandboxedProcess(&delegate, cmd_line); | 239 handle_ = content::StartSandboxedProcess(&delegate, cmd_line); |
209 } | 240 } |
210 return (handle_ != base::kNullProcessHandle); | 241 return (handle_ != base::kNullProcessHandle); |
211 #endif // !defined(OS_WIN) | |
212 } | 242 } |
213 | 243 |
214 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { | 244 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { |
215 #if defined(OS_LINUX) | 245 #if defined(OS_LINUX) |
216 int flags = ChildProcessHost::CHILD_ALLOW_SELF; | 246 int flags = ChildProcessHost::CHILD_ALLOW_SELF; |
217 #else | 247 #else |
218 int flags = ChildProcessHost::CHILD_NORMAL; | 248 int flags = ChildProcessHost::CHILD_NORMAL; |
219 #endif | 249 #endif |
220 return ChildProcessHost::GetChildPath(flags); | 250 return ChildProcessHost::GetChildPath(flags); |
221 } | 251 } |
222 | 252 |
223 void ServiceUtilityProcessHost::OnChildDisconnected() { | 253 void ServiceUtilityProcessHost::OnChildDisconnected() { |
224 if (waiting_for_reply_) { | 254 if (waiting_for_reply_) { |
225 // If we are yet to receive a reply then notify the client that the | 255 // If we are yet to receive a reply then notify the client that the |
226 // child died. | 256 // child died. |
227 client_message_loop_proxy_->PostTask( | 257 client_message_loop_proxy_->PostTask( |
228 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); | 258 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); |
229 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 259 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
230 SERVICE_UTILITY_DISCONNECTED, | 260 SERVICE_UTILITY_DISCONNECTED, |
231 SERVICE_UTILITY_EVENT_MAX); | 261 SERVICE_UTILITY_EVENT_MAX); |
232 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", | 262 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", |
233 base::Time::Now() - start_time_); | 263 base::Time::Now() - start_time_); |
234 } | 264 } |
235 delete this; | 265 delete this; |
236 } | 266 } |
237 | 267 |
238 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 268 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
239 bool handled = true; | 269 bool handled = true; |
240 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) | 270 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) |
241 #if defined(OS_WIN) | |
242 IPC_MESSAGE_HANDLER( | 271 IPC_MESSAGE_HANDLER( |
243 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded, | 272 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, |
244 OnRenderPDFPagesToMetafilesSucceeded) | 273 OnRenderPDFPagesToMetafilesPageCount) |
245 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, | 274 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, |
246 OnRenderPDFPagesToMetafileFailed) | 275 OnRenderPDFPagesToMetafilesPageDone) |
247 #endif | |
248 IPC_MESSAGE_HANDLER( | 276 IPC_MESSAGE_HANDLER( |
249 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, | 277 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, |
250 OnGetPrinterCapsAndDefaultsSucceeded) | 278 OnGetPrinterCapsAndDefaultsSucceeded) |
251 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, | 279 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, |
252 OnGetPrinterCapsAndDefaultsFailed) | 280 OnGetPrinterCapsAndDefaultsFailed) |
253 IPC_MESSAGE_HANDLER( | 281 IPC_MESSAGE_HANDLER( |
254 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, | 282 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, |
255 OnGetPrinterSemanticCapsAndDefaultsSucceeded) | 283 OnGetPrinterSemanticCapsAndDefaultsSucceeded) |
256 IPC_MESSAGE_HANDLER( | 284 IPC_MESSAGE_HANDLER( |
257 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, | 285 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, |
258 OnGetPrinterSemanticCapsAndDefaultsFailed) | 286 OnGetPrinterSemanticCapsAndDefaultsFailed) |
259 IPC_MESSAGE_UNHANDLED(handled = false) | 287 IPC_MESSAGE_UNHANDLED(handled = false) |
260 IPC_END_MESSAGE_MAP() | 288 IPC_END_MESSAGE_MAP() |
261 return handled; | 289 return handled; |
262 } | 290 } |
263 | 291 |
264 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { | 292 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { |
265 return handle_; | 293 return handle_; |
266 } | 294 } |
267 | 295 |
268 #if defined(OS_WIN) | 296 void ServiceUtilityProcessHost::OnMetafileSpooled(bool success) { |
269 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesSucceeded( | 297 if (!success || pdf_to_emf_state_->OnPageProcessed()) |
270 const std::vector<printing::PageRange>& page_ranges, | 298 OnPDFToEmfFinished(success); |
299 } | |
300 | |
301 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesPageCount( | |
302 int page_count) { | |
303 DCHECK(waiting_for_reply_); | |
Lei Zhang
2014/09/16 03:36:55
Add "bool PdfToEmfState::has_page_count()" { retur
Vitaly Buka (NO REVIEWS)
2014/09/16 07:50:36
Done.
| |
304 if (!pdf_to_emf_state_ || page_count <= 0) | |
305 return OnPDFToEmfFinished(false); | |
306 pdf_to_emf_state_->set_page_count(page_count); | |
307 pdf_to_emf_state_->GetMorePages(); | |
308 } | |
309 | |
310 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesPageDone( | |
311 bool success, | |
271 double scale_factor) { | 312 double scale_factor) { |
272 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | |
273 SERVICE_UTILITY_METAFILE_SUCCEEDED, | |
274 SERVICE_UTILITY_EVENT_MAX); | |
275 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", | |
276 base::Time::Now() - start_time_); | |
277 DCHECK(waiting_for_reply_); | 313 DCHECK(waiting_for_reply_); |
278 waiting_for_reply_ = false; | 314 if (!pdf_to_emf_state_ || !success) |
279 // If the metafile was successfully created, we need to take our hands off the | 315 return OnPDFToEmfFinished(false); |
280 // scratch metafile directory. The client will delete it when it is done with | 316 base::File emf_file = pdf_to_emf_state_->TakeNextFile(); |
281 // metafile. | 317 base::PostTaskAndReplyWithResult( |
282 scratch_metafile_dir_->Take(); | 318 client_message_loop_proxy_, |
283 | |
284 // TODO(vitalybuka|scottmg): http://crbug.com/170859: Currently, only one | |
285 // page is printed at a time. This would need to be refactored to change | |
286 // this. | |
287 CHECK_EQ(1u, page_ranges.size()); | |
288 CHECK_EQ(page_ranges[0].from, page_ranges[0].to); | |
289 int page_number = page_ranges[0].from; | |
290 client_message_loop_proxy_->PostTask( | |
291 FROM_HERE, | 319 FROM_HERE, |
292 base::Bind(&Client::MetafileAvailable, | 320 base::Bind(&Client::MetafileAvailable, |
293 client_.get(), | 321 client_.get(), |
294 metafile_path_.InsertBeforeExtensionASCII( | 322 scale_factor, |
295 base::StringPrintf(".%d", page_number)), | 323 base::Passed(&emf_file)), |
296 page_number, | 324 base::Bind(&ServiceUtilityProcessHost::OnMetafileSpooled, |
297 scale_factor)); | 325 weak_ptr_factory_.GetWeakPtr())); |
298 } | 326 } |
299 | 327 |
300 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileFailed() { | 328 void ServiceUtilityProcessHost::OnPDFToEmfFinished(bool success) { |
301 DCHECK(waiting_for_reply_); | 329 if (!waiting_for_reply_) |
302 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 330 return; |
303 SERVICE_UTILITY_METAFILE_FAILED, | |
304 SERVICE_UTILITY_EVENT_MAX); | |
305 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", | |
306 base::Time::Now() - start_time_); | |
307 waiting_for_reply_ = false; | 331 waiting_for_reply_ = false; |
332 if (success) { | |
333 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | |
334 SERVICE_UTILITY_METAFILE_SUCCEEDED, | |
335 SERVICE_UTILITY_EVENT_MAX); | |
336 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", | |
337 base::Time::Now() - start_time_); | |
338 } else { | |
339 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | |
340 SERVICE_UTILITY_METAFILE_FAILED, | |
341 SERVICE_UTILITY_EVENT_MAX); | |
342 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", | |
343 base::Time::Now() - start_time_); | |
344 } | |
308 client_message_loop_proxy_->PostTask( | 345 client_message_loop_proxy_->PostTask( |
309 FROM_HERE, | 346 FROM_HERE, |
310 base::Bind(&Client::OnRenderPDFPagesToMetafileFailed, client_.get())); | 347 base::Bind( |
348 &Client::OnRenderPDFPagesToMetafileDone, client_.get(), success)); | |
349 pdf_to_emf_state_.reset(); | |
311 } | 350 } |
312 #endif // defined(OS_WIN) | |
313 | 351 |
314 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded( | 352 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded( |
315 const std::string& printer_name, | 353 const std::string& printer_name, |
316 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 354 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
317 DCHECK(waiting_for_reply_); | 355 DCHECK(waiting_for_reply_); |
318 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 356 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
319 SERVICE_UTILITY_CAPS_SUCCEEDED, | 357 SERVICE_UTILITY_CAPS_SUCCEEDED, |
320 SERVICE_UTILITY_EVENT_MAX); | 358 SERVICE_UTILITY_EVENT_MAX); |
321 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityCapsTime", | 359 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityCapsTime", |
322 base::Time::Now() - start_time_); | 360 base::Time::Now() - start_time_); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
367 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilitySemanticCapsFailTime", | 405 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilitySemanticCapsFailTime", |
368 base::Time::Now() - start_time_); | 406 base::Time::Now() - start_time_); |
369 waiting_for_reply_ = false; | 407 waiting_for_reply_ = false; |
370 client_message_loop_proxy_->PostTask( | 408 client_message_loop_proxy_->PostTask( |
371 FROM_HERE, | 409 FROM_HERE, |
372 base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, | 410 base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, |
373 client_.get(), false, printer_name, | 411 client_.get(), false, printer_name, |
374 printing::PrinterSemanticCapsAndDefaults())); | 412 printing::PrinterSemanticCapsAndDefaults())); |
375 } | 413 } |
376 | 414 |
377 void ServiceUtilityProcessHost::Client::MetafileAvailable( | 415 bool ServiceUtilityProcessHost::Client::MetafileAvailable(double scale_factor, |
378 const base::FilePath& metafile_path, | 416 base::File file) { |
379 int highest_rendered_page_number, | 417 file.Seek(base::File::FROM_BEGIN, 0); |
380 double scale_factor) { | 418 int64 size = file.GetLength(); |
381 // The metafile was created in a temp folder which needs to get deleted after | 419 if (size <= 0) { |
382 // we have processed it. | 420 OnRenderPDFPagesToMetafileDone(false); |
383 base::ScopedTempDir scratch_metafile_dir; | 421 return false; |
384 if (!scratch_metafile_dir.Set(metafile_path.DirName())) | |
385 LOG(WARNING) << "Unable to set scratch metafile directory"; | |
386 #if defined(OS_WIN) | |
387 // It's important that metafile is declared after scratch_metafile_dir so | |
388 // that the metafile destructor closes the file before the base::ScopedTempDir | |
389 // destructor tries to remove the directory. | |
390 printing::Emf metafile; | |
391 if (!metafile.InitFromFile(metafile_path)) { | |
392 OnRenderPDFPagesToMetafileFailed(); | |
393 } else { | |
394 OnRenderPDFPagesToMetafileSucceeded(metafile, | |
395 highest_rendered_page_number, | |
396 scale_factor); | |
397 } | 422 } |
398 #endif // defined(OS_WIN) | 423 std::vector<char> data(size); |
424 if (file.ReadAtCurrentPos(&data[0], data.size()) != size) { | |
Lei Zhang
2014/09/16 03:36:55
nit: also data.data()
Vitaly Buka (NO REVIEWS)
2014/09/16 07:50:36
Done.
| |
425 OnRenderPDFPagesToMetafileDone(false); | |
426 return false; | |
427 } | |
428 printing::Emf emf; | |
429 if (!emf.InitFromData(&data[0], data.size())) { | |
430 OnRenderPDFPagesToMetafileDone(false); | |
431 return false; | |
432 } | |
433 OnRenderPDFPagesToMetafilePageDone(scale_factor, emf); | |
434 return true; | |
399 } | 435 } |
400 | |
OLD | NEW |