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 "chrome/common/chrome_switches.h" | 17 #include "chrome/common/chrome_switches.h" |
18 #include "chrome/common/chrome_utility_printing_messages.h" | 18 #include "chrome/common/chrome_utility_printing_messages.h" |
19 #include "content/public/common/child_process_host.h" | 19 #include "content/public/common/child_process_host.h" |
20 #include "content/public/common/result_codes.h" | 20 #include "content/public/common/result_codes.h" |
21 #include "content/public/common/sandbox_init.h" | 21 #include "content/public/common/sandbox_init.h" |
| 22 #include "content/public/common/sandboxed_process_launcher_delegate.h" |
22 #include "ipc/ipc_switches.h" | 23 #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" | 24 #include "printing/emf_win.h" |
35 #include "sandbox/win/src/sandbox_policy_base.h" | 25 #include "sandbox/win/src/sandbox_policy_base.h" |
| 26 #include "ui/base/ui_base_switches.h" |
36 | 27 |
37 namespace { | 28 namespace { |
38 | 29 |
39 // NOTE: changes to this class need to be reviewed by the security team. | |
40 class ServiceSandboxedProcessLauncherDelegate | |
41 : public content::SandboxedProcessLauncherDelegate { | |
42 public: | |
43 explicit 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 | |
53 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, | |
54 bool* success) OVERRIDE { | |
55 // Service process may run as windows service and it fails to create a | |
56 // window station. | |
57 policy->SetAlternateDesktop(false); | |
58 } | |
59 | |
60 private: | |
61 base::FilePath exposed_dir_; | |
62 }; | |
63 | |
64 } // namespace | |
65 | |
66 #endif // OS_WIN | |
67 | |
68 using content::ChildProcessHost; | 30 using content::ChildProcessHost; |
69 | 31 |
70 namespace { | 32 const int kMaxNumberOfTempFilesPerDocument = 3; |
| 33 |
71 enum ServiceUtilityProcessHostEvent { | 34 enum ServiceUtilityProcessHostEvent { |
72 SERVICE_UTILITY_STARTED, | 35 SERVICE_UTILITY_STARTED, |
73 SERVICE_UTILITY_DISCONNECTED, | 36 SERVICE_UTILITY_DISCONNECTED, |
74 SERVICE_UTILITY_METAFILE_REQUEST, | 37 SERVICE_UTILITY_METAFILE_REQUEST, |
75 SERVICE_UTILITY_METAFILE_SUCCEEDED, | 38 SERVICE_UTILITY_METAFILE_SUCCEEDED, |
76 SERVICE_UTILITY_METAFILE_FAILED, | 39 SERVICE_UTILITY_METAFILE_FAILED, |
77 SERVICE_UTILITY_CAPS_REQUEST, | 40 SERVICE_UTILITY_CAPS_REQUEST, |
78 SERVICE_UTILITY_CAPS_SUCCEEDED, | 41 SERVICE_UTILITY_CAPS_SUCCEEDED, |
79 SERVICE_UTILITY_CAPS_FAILED, | 42 SERVICE_UTILITY_CAPS_FAILED, |
80 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | 43 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, |
81 SERVICE_UTILITY_SEMANTIC_CAPS_SUCCEEDED, | 44 SERVICE_UTILITY_SEMANTIC_CAPS_SUCCEEDED, |
82 SERVICE_UTILITY_SEMANTIC_CAPS_FAILED, | 45 SERVICE_UTILITY_SEMANTIC_CAPS_FAILED, |
83 SERVICE_UTILITY_EVENT_MAX, | 46 SERVICE_UTILITY_EVENT_MAX, |
84 }; | 47 }; |
| 48 |
| 49 // NOTE: changes to this class need to be reviewed by the security team. |
| 50 class ServiceSandboxedProcessLauncherDelegate |
| 51 : public content::SandboxedProcessLauncherDelegate { |
| 52 public: |
| 53 ServiceSandboxedProcessLauncherDelegate() {} |
| 54 |
| 55 virtual void PreSpawnTarget(sandbox::TargetPolicy* policy, |
| 56 bool* success) OVERRIDE { |
| 57 // Service process may run as windows service and it fails to create a |
| 58 // window station. |
| 59 policy->SetAlternateDesktop(false); |
| 60 } |
| 61 |
| 62 private: |
| 63 }; |
| 64 |
| 65 base::File CreateTempFile() { |
| 66 base::FilePath path; |
| 67 if (!base::CreateTemporaryFile(&path)) |
| 68 return base::File(); |
| 69 return base::File(path, |
| 70 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE | |
| 71 base::File::FLAG_READ | |
| 72 base::File::FLAG_DELETE_ON_CLOSE | |
| 73 base::File::FLAG_TEMPORARY); |
| 74 } |
| 75 |
85 } // namespace | 76 } // namespace |
86 | 77 |
87 ServiceUtilityProcessHost::ServiceUtilityProcessHost( | 78 ServiceUtilityProcessHost::ServiceUtilityProcessHost( |
88 Client* client, base::MessageLoopProxy* client_message_loop_proxy) | 79 Client* client, |
89 : handle_(base::kNullProcessHandle), | 80 base::MessageLoopProxy* client_message_loop_proxy) |
90 client_(client), | 81 : handle_(base::kNullProcessHandle), |
91 client_message_loop_proxy_(client_message_loop_proxy), | 82 client_(client), |
92 waiting_for_reply_(false) { | 83 client_message_loop_proxy_(client_message_loop_proxy), |
| 84 waiting_for_reply_(false), |
| 85 number_of_emf_in_progress_(0), |
| 86 create_file_reply_msg_(NULL), |
| 87 weak_ptr_factory_(this) { |
93 child_process_host_.reset(ChildProcessHost::Create(this)); | 88 child_process_host_.reset(ChildProcessHost::Create(this)); |
94 } | 89 } |
95 | 90 |
96 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { | 91 ServiceUtilityProcessHost::~ServiceUtilityProcessHost() { |
97 // We need to kill the child process when the host dies. | 92 // We need to kill the child process when the host dies. |
98 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); | 93 base::KillProcess(handle_, content::RESULT_CODE_NORMAL_EXIT, false); |
99 } | 94 } |
100 | 95 |
101 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( | 96 bool ServiceUtilityProcessHost::StartRenderPDFPagesToMetafile( |
102 const base::FilePath& pdf_path, | 97 const base::FilePath& pdf_path, |
103 const printing::PdfRenderSettings& render_settings, | 98 const printing::PdfRenderSettings& render_settings) { |
104 const std::vector<printing::PageRange>& page_ranges) { | |
105 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 99 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
106 SERVICE_UTILITY_METAFILE_REQUEST, | 100 SERVICE_UTILITY_METAFILE_REQUEST, |
107 SERVICE_UTILITY_EVENT_MAX); | 101 SERVICE_UTILITY_EVENT_MAX); |
108 start_time_ = base::Time::Now(); | 102 start_time_ = base::Time::Now(); |
109 #if !defined(OS_WIN) | 103 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 | 104 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; | 105 return false; |
121 | 106 |
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_); | 107 DCHECK(!waiting_for_reply_); |
126 waiting_for_reply_ = true; | 108 waiting_for_reply_ = true; |
127 return child_process_host_->Send( | 109 return Send(new ChromeUtilityMsg_RenderPDFPagesToMetafiles( |
128 new ChromeUtilityMsg_RenderPDFPagesToMetafiles( | 110 IPC::TakeFileHandleForProcess(pdf_file.Pass(), handle()), |
129 IPC::TakeFileHandleForProcess(pdf_file.Pass(), handle()), | 111 render_settings)); |
130 metafile_path_, | |
131 render_settings, | |
132 page_ranges)); | |
133 #endif // !defined(OS_WIN) | |
134 } | 112 } |
135 | 113 |
136 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( | 114 bool ServiceUtilityProcessHost::StartGetPrinterCapsAndDefaults( |
137 const std::string& printer_name) { | 115 const std::string& printer_name) { |
138 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 116 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
139 SERVICE_UTILITY_CAPS_REQUEST, | 117 SERVICE_UTILITY_CAPS_REQUEST, |
140 SERVICE_UTILITY_EVENT_MAX); | 118 SERVICE_UTILITY_EVENT_MAX); |
141 start_time_ = base::Time::Now(); | 119 start_time_ = base::Time::Now(); |
142 base::FilePath exposed_path; | 120 if (!StartProcess(true)) |
143 if (!StartProcess(true, exposed_path)) | |
144 return false; | 121 return false; |
145 DCHECK(!waiting_for_reply_); | 122 DCHECK(!waiting_for_reply_); |
146 waiting_for_reply_ = true; | 123 waiting_for_reply_ = true; |
147 return child_process_host_->Send( | 124 return Send(new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); |
148 new ChromeUtilityMsg_GetPrinterCapsAndDefaults(printer_name)); | |
149 } | 125 } |
150 | 126 |
151 bool ServiceUtilityProcessHost::StartGetPrinterSemanticCapsAndDefaults( | 127 bool ServiceUtilityProcessHost::StartGetPrinterSemanticCapsAndDefaults( |
152 const std::string& printer_name) { | 128 const std::string& printer_name) { |
153 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 129 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
154 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, | 130 SERVICE_UTILITY_SEMANTIC_CAPS_REQUEST, |
155 SERVICE_UTILITY_EVENT_MAX); | 131 SERVICE_UTILITY_EVENT_MAX); |
156 start_time_ = base::Time::Now(); | 132 start_time_ = base::Time::Now(); |
157 base::FilePath exposed_path; | 133 if (!StartProcess(true)) |
158 if (!StartProcess(true, exposed_path)) | |
159 return false; | 134 return false; |
160 DCHECK(!waiting_for_reply_); | 135 DCHECK(!waiting_for_reply_); |
161 waiting_for_reply_ = true; | 136 waiting_for_reply_ = true; |
162 return child_process_host_->Send( | 137 return Send( |
163 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); | 138 new ChromeUtilityMsg_GetPrinterSemanticCapsAndDefaults(printer_name)); |
164 } | 139 } |
165 | 140 |
166 bool ServiceUtilityProcessHost::StartProcess( | 141 bool ServiceUtilityProcessHost::Send(IPC::Message* msg) { |
167 bool no_sandbox, | 142 if (child_process_host_) |
168 const base::FilePath& exposed_dir) { | 143 return child_process_host_->Send(msg); |
| 144 delete msg; |
| 145 return false; |
| 146 } |
| 147 |
| 148 bool ServiceUtilityProcessHost::StartProcess(bool no_sandbox) { |
169 std::string channel_id = child_process_host_->CreateChannel(); | 149 std::string channel_id = child_process_host_->CreateChannel(); |
170 if (channel_id.empty()) | 150 if (channel_id.empty()) |
171 return false; | 151 return false; |
172 | 152 |
173 base::FilePath exe_path = GetUtilityProcessCmd(); | 153 base::FilePath exe_path = GetUtilityProcessCmd(); |
174 if (exe_path.empty()) { | 154 if (exe_path.empty()) { |
175 NOTREACHED() << "Unable to get utility process binary name."; | 155 NOTREACHED() << "Unable to get utility process binary name."; |
176 return false; | 156 return false; |
177 } | 157 } |
178 | 158 |
179 CommandLine cmd_line(exe_path); | 159 CommandLine cmd_line(exe_path); |
180 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); | 160 cmd_line.AppendSwitchASCII(switches::kProcessType, switches::kUtilityProcess); |
181 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); | 161 cmd_line.AppendSwitchASCII(switches::kProcessChannelID, channel_id); |
182 cmd_line.AppendSwitch(switches::kLang); | 162 cmd_line.AppendSwitch(switches::kLang); |
183 | 163 |
184 if (Launch(&cmd_line, no_sandbox, exposed_dir)) { | 164 if (Launch(&cmd_line, no_sandbox)) { |
185 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 165 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
186 SERVICE_UTILITY_STARTED, | 166 SERVICE_UTILITY_STARTED, |
187 SERVICE_UTILITY_EVENT_MAX); | 167 SERVICE_UTILITY_EVENT_MAX); |
188 return true; | 168 return true; |
189 } | 169 } |
190 return false; | 170 return false; |
191 } | 171 } |
192 | 172 |
193 bool ServiceUtilityProcessHost::Launch(CommandLine* cmd_line, | 173 bool ServiceUtilityProcessHost::Launch(CommandLine* cmd_line, bool no_sandbox) { |
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) { | 174 if (no_sandbox) { |
203 base::ProcessHandle process = base::kNullProcessHandle; | 175 base::ProcessHandle process = base::kNullProcessHandle; |
204 cmd_line->AppendSwitch(switches::kNoSandbox); | 176 cmd_line->AppendSwitch(switches::kNoSandbox); |
205 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); | 177 base::LaunchProcess(*cmd_line, base::LaunchOptions(), &handle_); |
206 } else { | 178 } else { |
207 ServiceSandboxedProcessLauncherDelegate delegate(exposed_dir); | 179 ServiceSandboxedProcessLauncherDelegate delegate; |
208 handle_ = content::StartSandboxedProcess(&delegate, cmd_line); | 180 handle_ = content::StartSandboxedProcess(&delegate, cmd_line); |
209 } | 181 } |
210 return (handle_ != base::kNullProcessHandle); | 182 return (handle_ != base::kNullProcessHandle); |
211 #endif // !defined(OS_WIN) | |
212 } | 183 } |
213 | 184 |
214 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { | 185 base::FilePath ServiceUtilityProcessHost::GetUtilityProcessCmd() { |
215 #if defined(OS_LINUX) | 186 #if defined(OS_LINUX) |
216 int flags = ChildProcessHost::CHILD_ALLOW_SELF; | 187 int flags = ChildProcessHost::CHILD_ALLOW_SELF; |
217 #else | 188 #else |
218 int flags = ChildProcessHost::CHILD_NORMAL; | 189 int flags = ChildProcessHost::CHILD_NORMAL; |
219 #endif | 190 #endif |
220 return ChildProcessHost::GetChildPath(flags); | 191 return ChildProcessHost::GetChildPath(flags); |
221 } | 192 } |
222 | 193 |
223 void ServiceUtilityProcessHost::OnChildDisconnected() { | 194 void ServiceUtilityProcessHost::OnChildDisconnected() { |
224 if (waiting_for_reply_) { | 195 if (waiting_for_reply_) { |
225 // If we are yet to receive a reply then notify the client that the | 196 // If we are yet to receive a reply then notify the client that the |
226 // child died. | 197 // child died. |
227 client_message_loop_proxy_->PostTask( | 198 client_message_loop_proxy_->PostTask( |
228 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); | 199 FROM_HERE, base::Bind(&Client::OnChildDied, client_.get())); |
229 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 200 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
230 SERVICE_UTILITY_DISCONNECTED, | 201 SERVICE_UTILITY_DISCONNECTED, |
231 SERVICE_UTILITY_EVENT_MAX); | 202 SERVICE_UTILITY_EVENT_MAX); |
232 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", | 203 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityDisconnectTime", |
233 base::Time::Now() - start_time_); | 204 base::Time::Now() - start_time_); |
234 } | 205 } |
235 delete this; | 206 delete this; |
236 } | 207 } |
237 | 208 |
238 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { | 209 bool ServiceUtilityProcessHost::OnMessageReceived(const IPC::Message& message) { |
239 bool handled = true; | 210 bool handled = true; |
240 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) | 211 IPC_BEGIN_MESSAGE_MAP(ServiceUtilityProcessHost, message) |
241 #if defined(OS_WIN) | 212 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageCount, |
242 IPC_MESSAGE_HANDLER( | 213 OnRenderPDFPagesToMetafilesPageCount) |
243 ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_Succeeded, | 214 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_PageDone, |
244 OnRenderPDFPagesToMetafilesSucceeded) | 215 OnRenderPDFPagesToMetafilesPageDone) |
245 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_RenderPDFPagesToMetafile_Failed, | |
246 OnRenderPDFPagesToMetafileFailed) | |
247 #endif | |
248 IPC_MESSAGE_HANDLER( | 216 IPC_MESSAGE_HANDLER( |
249 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, | 217 ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Succeeded, |
250 OnGetPrinterCapsAndDefaultsSucceeded) | 218 OnGetPrinterCapsAndDefaultsSucceeded) |
251 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, | 219 IPC_MESSAGE_HANDLER(ChromeUtilityHostMsg_GetPrinterCapsAndDefaults_Failed, |
252 OnGetPrinterCapsAndDefaultsFailed) | 220 OnGetPrinterCapsAndDefaultsFailed) |
253 IPC_MESSAGE_HANDLER( | 221 IPC_MESSAGE_HANDLER( |
254 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, | 222 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Succeeded, |
255 OnGetPrinterSemanticCapsAndDefaultsSucceeded) | 223 OnGetPrinterSemanticCapsAndDefaultsSucceeded) |
256 IPC_MESSAGE_HANDLER( | 224 IPC_MESSAGE_HANDLER( |
257 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, | 225 ChromeUtilityHostMsg_GetPrinterSemanticCapsAndDefaults_Failed, |
258 OnGetPrinterSemanticCapsAndDefaultsFailed) | 226 OnGetPrinterSemanticCapsAndDefaultsFailed) |
259 IPC_MESSAGE_UNHANDLED(handled = false) | 227 IPC_MESSAGE_UNHANDLED(handled = false) |
260 IPC_END_MESSAGE_MAP() | 228 IPC_END_MESSAGE_MAP() |
261 return handled; | 229 return handled; |
262 } | 230 } |
263 | 231 |
264 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { | 232 base::ProcessHandle ServiceUtilityProcessHost::GetHandle() const { |
265 return handle_; | 233 return handle_; |
266 } | 234 } |
267 | 235 |
268 #if defined(OS_WIN) | 236 void ServiceUtilityProcessHost::OnMetafileSpooled() { |
269 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesSucceeded( | 237 --number_of_emf_in_progress_; |
270 const std::vector<printing::PageRange>& page_ranges, | 238 // ReplayCreateFileIfReady(); |
| 239 } |
| 240 |
| 241 // void ServiceUtilityProcessHost::ReplayCreateFileIfReady() { |
| 242 // if (!create_file_reply_msg_ || |
| 243 // number_of_emf_in_progress_ >= kMaxNumberOfTempFilesPerDocument) { |
| 244 // return; |
| 245 // } |
| 246 // DCHECK(waiting_for_reply_); |
| 247 // if (emf_file_.IsValid()) { |
| 248 // NOTREACHED() << "Utility can't request more than one file."; |
| 249 // return FailReplyOnCreate(); |
| 250 // } |
| 251 // emf_file_ = CreateTempFile(); |
| 252 // DCHECK(emf_file_.IsValid()); |
| 253 // /*OnRenderPDFPagesToMetafilesCreateFileReply(IPC::GetFileHandleForProcess( |
| 254 // emf_file_.GetPlatformFile(), handle(), false));*/ |
| 255 //} |
| 256 |
| 257 // void ServiceUtilityProcessHost::FailReplyOnCreate() { |
| 258 // OnRenderPDFPagesToMetafilesCreateFileReply( |
| 259 // IPC::InvalidPlatformFileForTransit()); |
| 260 // return OnRenderPDFPagesToMetafileFailed(); |
| 261 //} |
| 262 // |
| 263 // void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesCreateFile( |
| 264 // IPC::Message* reply_msg) { |
| 265 // DCHECK(waiting_for_reply_); |
| 266 // if (create_file_reply_msg_) { |
| 267 // NOTREACHED() << "Utility can't create more than one request."; |
| 268 // // Fail both. |
| 269 // FailReplyOnCreate(); |
| 270 // create_file_reply_msg_ = reply_msg; |
| 271 // FailReplyOnCreate(); |
| 272 // return; |
| 273 // } |
| 274 // create_file_reply_msg_ = reply_msg; |
| 275 // ReplayCreateFileIfReady(); |
| 276 //} |
| 277 // |
| 278 // void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesCreateFileReply( |
| 279 // IPC::PlatformFileForTransit file) { |
| 280 // DCHECK(waiting_for_reply_); |
| 281 // ChromeUtilityHostMsg_RenderPDFPagesToMetafiles_CreateFile::WriteReplyParams( |
| 282 // create_file_reply_msg_, file); |
| 283 // Send(create_file_reply_msg_); |
| 284 // create_file_reply_msg_ = NULL; |
| 285 //} |
| 286 |
| 287 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesPageDone( |
| 288 bool success, |
271 double scale_factor) { | 289 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_); | 290 DCHECK(waiting_for_reply_); |
278 waiting_for_reply_ = false; | 291 ++number_of_emf_in_progress_; |
279 // If the metafile was successfully created, we need to take our hands off the | 292 client_message_loop_proxy_->PostTaskAndReply( |
280 // scratch metafile directory. The client will delete it when it is done with | |
281 // metafile. | |
282 scratch_metafile_dir_->Take(); | |
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, | 293 FROM_HERE, |
292 base::Bind(&Client::MetafileAvailable, | 294 base::Bind(&Client::MetafileAvailable, |
293 client_.get(), | 295 client_.get(), |
294 metafile_path_.InsertBeforeExtensionASCII( | 296 scale_factor, |
295 base::StringPrintf(".%d", page_number)), | 297 base::Passed(&emf_file_)), |
296 page_number, | 298 base::Bind(&ServiceUtilityProcessHost::OnMetafileSpooled, |
297 scale_factor)); | 299 weak_ptr_factory_.GetWeakPtr())); |
298 } | 300 } |
299 | 301 |
300 void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileFailed() { | 302 // void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafilesSucceeded() { |
301 DCHECK(waiting_for_reply_); | 303 // DCHECK(waiting_for_reply_); |
302 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 304 // UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
303 SERVICE_UTILITY_METAFILE_FAILED, | 305 // SERVICE_UTILITY_METAFILE_SUCCEEDED, |
304 SERVICE_UTILITY_EVENT_MAX); | 306 // SERVICE_UTILITY_EVENT_MAX); |
305 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", | 307 // UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileTime", |
306 base::Time::Now() - start_time_); | 308 // base::Time::Now() - start_time_); |
307 waiting_for_reply_ = false; | 309 // waiting_for_reply_ = false; |
308 client_message_loop_proxy_->PostTask( | 310 // client_message_loop_proxy_->PostTask( |
309 FROM_HERE, | 311 // FROM_HERE, |
310 base::Bind(&Client::OnRenderPDFPagesToMetafileFailed, client_.get())); | 312 // base::Bind(&Client::OnRenderPDFPagesToMetafileSucceeded, |
311 } | 313 // client_.get())); |
312 #endif // defined(OS_WIN) | 314 //} |
| 315 // |
| 316 // void ServiceUtilityProcessHost::OnRenderPDFPagesToMetafileFailed() { |
| 317 // if (!waiting_for_reply_) |
| 318 // return; |
| 319 // waiting_for_reply_ = false; |
| 320 // UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
| 321 // SERVICE_UTILITY_METAFILE_FAILED, |
| 322 // SERVICE_UTILITY_EVENT_MAX); |
| 323 // UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityMetafileFailTime", |
| 324 // base::Time::Now() - start_time_); |
| 325 // client_message_loop_proxy_->PostTask( |
| 326 // FROM_HERE, |
| 327 // base::Bind(&Client::OnRenderPDFPagesToMetafileFailed, client_.get())); |
| 328 //} |
313 | 329 |
314 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded( | 330 void ServiceUtilityProcessHost::OnGetPrinterCapsAndDefaultsSucceeded( |
315 const std::string& printer_name, | 331 const std::string& printer_name, |
316 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 332 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
317 DCHECK(waiting_for_reply_); | 333 DCHECK(waiting_for_reply_); |
318 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", | 334 UMA_HISTOGRAM_ENUMERATION("CloudPrint.ServiceUtilityProcessHostEvent", |
319 SERVICE_UTILITY_CAPS_SUCCEEDED, | 335 SERVICE_UTILITY_CAPS_SUCCEEDED, |
320 SERVICE_UTILITY_EVENT_MAX); | 336 SERVICE_UTILITY_EVENT_MAX); |
321 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityCapsTime", | 337 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilityCapsTime", |
322 base::Time::Now() - start_time_); | 338 base::Time::Now() - start_time_); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
367 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilitySemanticCapsFailTime", | 383 UMA_HISTOGRAM_TIMES("CloudPrint.ServiceUtilitySemanticCapsFailTime", |
368 base::Time::Now() - start_time_); | 384 base::Time::Now() - start_time_); |
369 waiting_for_reply_ = false; | 385 waiting_for_reply_ = false; |
370 client_message_loop_proxy_->PostTask( | 386 client_message_loop_proxy_->PostTask( |
371 FROM_HERE, | 387 FROM_HERE, |
372 base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, | 388 base::Bind(&Client::OnGetPrinterSemanticCapsAndDefaults, |
373 client_.get(), false, printer_name, | 389 client_.get(), false, printer_name, |
374 printing::PrinterSemanticCapsAndDefaults())); | 390 printing::PrinterSemanticCapsAndDefaults())); |
375 } | 391 } |
376 | 392 |
377 void ServiceUtilityProcessHost::Client::MetafileAvailable( | 393 void ServiceUtilityProcessHost::Client::MetafileAvailable(double scale_factor, |
378 const base::FilePath& metafile_path, | 394 base::File file) { |
379 int highest_rendered_page_number, | 395 file.Seek(base::File::FROM_BEGIN, 0); |
380 double scale_factor) { | 396 int64 size = file.GetLength(); |
381 // The metafile was created in a temp folder which needs to get deleted after | 397 if (size <= 0) |
382 // we have processed it. | 398 return OnRenderPDFPagesToMetafileFailed(); |
383 base::ScopedTempDir scratch_metafile_dir; | 399 std::vector<char> data(size); |
384 if (!scratch_metafile_dir.Set(metafile_path.DirName())) | 400 if (file.ReadAtCurrentPos(&data[0], data.size()) != size) |
385 LOG(WARNING) << "Unable to set scratch metafile directory"; | 401 return OnRenderPDFPagesToMetafileFailed(); |
386 #if defined(OS_WIN) | 402 printing::Emf emf; |
387 // It's important that metafile is declared after scratch_metafile_dir so | 403 if (!emf.InitFromData(&data[0], data.size())) |
388 // that the metafile destructor closes the file before the base::ScopedTempDir | 404 return OnRenderPDFPagesToMetafileFailed(); |
389 // destructor tries to remove the directory. | 405 OnRenderPDFPagesToMetafilePageDone(scale_factor, emf); |
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 } | |
398 #endif // defined(OS_WIN) | |
399 } | 406 } |
400 | |
OLD | NEW |