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/cloud_print/printer_job_handler.h" | 5 #include "chrome/service/cloud_print/printer_job_handler.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/file_util.h" | 9 #include "base/file_util.h" |
10 #include "base/json/json_reader.h" | 10 #include "base/json/json_reader.h" |
11 #include "base/md5.h" | 11 #include "base/md5.h" |
12 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
13 #include "base/utf_string_conversions.h" | 13 #include "base/utf_string_conversions.h" |
14 #include "base/values.h" | 14 #include "base/values.h" |
| 15 #include "chrome/common/cloud_print/cloud_print_helpers.h" |
15 #include "chrome/service/cloud_print/cloud_print_consts.h" | 16 #include "chrome/service/cloud_print/cloud_print_consts.h" |
16 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 17 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
17 #include "chrome/service/cloud_print/job_status_updater.h" | 18 #include "chrome/service/cloud_print/job_status_updater.h" |
18 #include "googleurl/src/gurl.h" | 19 #include "googleurl/src/gurl.h" |
19 #include "net/http/http_response_headers.h" | 20 #include "net/http/http_response_headers.h" |
20 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
21 | 22 |
22 PrinterJobHandler::JobDetails::JobDetails() {} | 23 PrinterJobHandler::JobDetails::JobDetails() {} |
23 | 24 |
24 PrinterJobHandler::JobDetails::~JobDetails() {} | 25 PrinterJobHandler::JobDetails::~JobDetails() {} |
(...skipping 153 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
178 void PrinterJobHandler::OnReceivePrinterCaps( | 179 void PrinterJobHandler::OnReceivePrinterCaps( |
179 bool succeeded, | 180 bool succeeded, |
180 const std::string& printer_name, | 181 const std::string& printer_name, |
181 const printing::PrinterCapsAndDefaults& caps_and_defaults) { | 182 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
182 printing::PrinterBasicInfo printer_info; | 183 printing::PrinterBasicInfo printer_info; |
183 if (printer_watcher_) | 184 if (printer_watcher_) |
184 printer_watcher_->GetCurrentPrinterInfo(&printer_info); | 185 printer_watcher_->GetCurrentPrinterInfo(&printer_info); |
185 | 186 |
186 std::string post_data; | 187 std::string post_data; |
187 std::string mime_boundary; | 188 std::string mime_boundary; |
188 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | 189 cloud_print::CreateMimeBoundaryForUpload(&mime_boundary); |
189 | 190 |
190 if (succeeded) { | 191 if (succeeded) { |
191 std::string caps_hash = | 192 std::string caps_hash = |
192 base::MD5String(caps_and_defaults.printer_capabilities); | 193 base::MD5String(caps_and_defaults.printer_capabilities); |
193 if (caps_hash != printer_info_cloud_.caps_hash) { | 194 if (caps_hash != printer_info_cloud_.caps_hash) { |
194 // Hashes don't match, we need to upload new capabilities (the defaults | 195 // Hashes don't match, we need to upload new capabilities (the defaults |
195 // go for free along with the capabilities) | 196 // go for free along with the capabilities) |
196 printer_info_cloud_.caps_hash = caps_hash; | 197 printer_info_cloud_.caps_hash = caps_hash; |
197 CloudPrintHelpers::AddMultipartValueForUpload( | 198 cloud_print::AddMultipartValueForUpload(kPrinterCapsValue, |
198 kPrinterCapsValue, caps_and_defaults.printer_capabilities, | 199 caps_and_defaults.printer_capabilities, mime_boundary, |
199 mime_boundary, caps_and_defaults.caps_mime_type, &post_data); | 200 caps_and_defaults.caps_mime_type, &post_data); |
200 CloudPrintHelpers::AddMultipartValueForUpload( | 201 cloud_print::AddMultipartValueForUpload(kPrinterDefaultsValue, |
201 kPrinterDefaultsValue, caps_and_defaults.printer_defaults, | 202 caps_and_defaults.printer_defaults, mime_boundary, |
202 mime_boundary, caps_and_defaults.defaults_mime_type, | 203 caps_and_defaults.defaults_mime_type, &post_data); |
203 &post_data); | 204 cloud_print::AddMultipartValueForUpload(kPrinterCapsHashValue, |
204 CloudPrintHelpers::AddMultipartValueForUpload( | 205 caps_hash, mime_boundary, std::string(), &post_data); |
205 kPrinterCapsHashValue, caps_hash, mime_boundary, std::string(), | |
206 &post_data); | |
207 } | 206 } |
208 } else { | 207 } else { |
209 LOG(ERROR) << "Failed to get printer caps and defaults for printer: " | 208 LOG(ERROR) << "Failed to get printer caps and defaults for printer: " |
210 << printer_name; | 209 << printer_name; |
211 } | 210 } |
212 | 211 |
213 std::string tags_hash = | 212 std::string tags_hash = |
214 CloudPrintHelpers::GenerateHashOfStringMap(printer_info.options); | 213 CloudPrintHelpers::GenerateHashOfStringMap(printer_info.options); |
215 if (tags_hash != printer_info_cloud_.tags_hash) { | 214 if (tags_hash != printer_info_cloud_.tags_hash) { |
216 printer_info_cloud_.tags_hash = tags_hash; | 215 printer_info_cloud_.tags_hash = tags_hash; |
217 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags( | 216 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags( |
218 printer_info.options, mime_boundary, &post_data); | 217 printer_info.options, mime_boundary, &post_data); |
219 // Remove all the exising proxy tags. | 218 // Remove all the exising proxy tags. |
220 std::string cp_tag_wildcard(kProxyTagPrefix); | 219 std::string cp_tag_wildcard(kProxyTagPrefix); |
221 cp_tag_wildcard += ".*"; | 220 cp_tag_wildcard += ".*"; |
222 CloudPrintHelpers::AddMultipartValueForUpload( | 221 cloud_print::AddMultipartValueForUpload(kPrinterRemoveTagValue, |
223 kPrinterRemoveTagValue, cp_tag_wildcard, mime_boundary, std::string(), | 222 cp_tag_wildcard, mime_boundary, std::string(), &post_data); |
224 &post_data); | |
225 } | 223 } |
226 | 224 |
227 if (printer_info.printer_name != printer_info_.printer_name) { | 225 if (printer_info.printer_name != printer_info_.printer_name) { |
228 CloudPrintHelpers::AddMultipartValueForUpload(kPrinterNameValue, | 226 cloud_print::AddMultipartValueForUpload(kPrinterNameValue, |
229 printer_info.printer_name, | 227 printer_info.printer_name, mime_boundary, std::string(), &post_data); |
230 mime_boundary, | |
231 std::string(), &post_data); | |
232 } | 228 } |
233 if (printer_info.printer_description != printer_info_.printer_description) { | 229 if (printer_info.printer_description != printer_info_.printer_description) { |
234 CloudPrintHelpers::AddMultipartValueForUpload( | 230 cloud_print::AddMultipartValueForUpload(kPrinterDescValue, |
235 kPrinterDescValue, printer_info.printer_description, mime_boundary, | 231 printer_info.printer_description, mime_boundary, |
236 std::string() , &post_data); | 232 std::string(), &post_data); |
237 } | 233 } |
238 if (printer_info.printer_status != printer_info_.printer_status) { | 234 if (printer_info.printer_status != printer_info_.printer_status) { |
239 CloudPrintHelpers::AddMultipartValueForUpload( | 235 cloud_print::AddMultipartValueForUpload(kPrinterStatusValue, |
240 kPrinterStatusValue, | 236 base::StringPrintf("%d", printer_info.printer_status), mime_boundary, |
241 base::StringPrintf("%d", printer_info.printer_status), | 237 std::string(), &post_data); |
242 mime_boundary, | |
243 std::string(), | |
244 &post_data); | |
245 } | 238 } |
246 printer_info_ = printer_info; | 239 printer_info_ = printer_info; |
247 if (!post_data.empty()) { | 240 if (!post_data.empty()) { |
248 // Terminate the request body | 241 // Terminate the request body |
249 post_data.append("--" + mime_boundary + "--\r\n"); | 242 post_data.append("--" + mime_boundary + "--\r\n"); |
250 std::string mime_type("multipart/form-data; boundary="); | 243 std::string mime_type("multipart/form-data; boundary="); |
251 mime_type += mime_boundary; | 244 mime_type += mime_boundary; |
252 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); | 245 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); |
253 request_ = new CloudPrintURLFetcher; | 246 request_ = new CloudPrintURLFetcher; |
254 request_->StartPostRequest( | 247 request_->StartPostRequest( |
(...skipping 387 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 job_handler_message_loop_proxy_->PostTask( | 635 job_handler_message_loop_proxy_->PostTask( |
643 FROM_HERE, base::Bind(&PrinterJobHandler::JobSpooled, this, job_id)); | 636 FROM_HERE, base::Bind(&PrinterJobHandler::JobSpooled, this, job_id)); |
644 } | 637 } |
645 | 638 |
646 void PrinterJobHandler::OnJobSpoolFailed() { | 639 void PrinterJobHandler::OnJobSpoolFailed() { |
647 DCHECK(MessageLoop::current() == print_thread_.message_loop()); | 640 DCHECK(MessageLoop::current() == print_thread_.message_loop()); |
648 job_spooler_ = NULL; | 641 job_spooler_ = NULL; |
649 job_handler_message_loop_proxy_->PostTask( | 642 job_handler_message_loop_proxy_->PostTask( |
650 FROM_HERE, base::Bind(&PrinterJobHandler::JobFailed, this, PRINT_FAILED)); | 643 FROM_HERE, base::Bind(&PrinterJobHandler::JobFailed, this, PRINT_FAILED)); |
651 } | 644 } |
OLD | NEW |