| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/file_util.h" | 7 #include "base/file_util.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/md5.h" | 9 #include "base/md5.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/common/net/http_return.h" | 13 #include "chrome/common/net/http_return.h" |
| 14 #include "chrome/service/cloud_print/cloud_print_consts.h" | 14 #include "chrome/service/cloud_print/cloud_print_consts.h" |
| 15 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 15 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 16 #include "chrome/service/cloud_print/job_status_updater.h" | 16 #include "chrome/service/cloud_print/job_status_updater.h" |
| 17 #include "googleurl/src/gurl.h" | 17 #include "googleurl/src/gurl.h" |
| 18 #include "net/http/http_response_headers.h" | 18 #include "net/http/http_response_headers.h" |
| 19 | 19 |
| 20 PrinterJobHandler::PrinterJobHandler( | 20 PrinterJobHandler::PrinterJobHandler( |
| 21 const cloud_print::PrinterBasicInfo& printer_info, | 21 const printing::PrinterBasicInfo& printer_info, |
| 22 const PrinterInfoFromCloud& printer_info_cloud, | 22 const PrinterInfoFromCloud& printer_info_cloud, |
| 23 const std::string& auth_token, | 23 const std::string& auth_token, |
| 24 const GURL& cloud_print_server_url, | 24 const GURL& cloud_print_server_url, |
| 25 cloud_print::PrintSystem* print_system, | 25 cloud_print::PrintSystem* print_system, |
| 26 Delegate* delegate) | 26 Delegate* delegate) |
| 27 : print_system_(print_system), | 27 : print_system_(print_system), |
| 28 printer_info_(printer_info), | 28 printer_info_(printer_info), |
| 29 printer_info_cloud_(printer_info_cloud), | 29 printer_info_cloud_(printer_info_cloud), |
| 30 auth_token_(auth_token), | 30 auth_token_(auth_token), |
| 31 cloud_print_server_url_(cloud_print_server_url), | 31 cloud_print_server_url_(cloud_print_server_url), |
| 32 delegate_(delegate), | 32 delegate_(delegate), |
| 33 local_job_id_(-1), | 33 local_job_id_(-1), |
| 34 next_response_handler_(NULL), | 34 next_response_handler_(NULL), |
| 35 next_failure_handler_(NULL), | 35 next_failure_handler_(NULL), |
| 36 server_error_count_(0), | 36 server_error_count_(0), |
| 37 print_thread_("Chrome_CloudPrintJobPrintThread"), | 37 print_thread_("Chrome_CloudPrintJobPrintThread"), |
| 38 job_handler_message_loop_proxy_( | 38 job_handler_message_loop_proxy_( |
| 39 base::MessageLoopProxy::CreateForCurrentThread()), | 39 base::MessageLoopProxy::CreateForCurrentThread()), |
| 40 shutting_down_(false), | 40 shutting_down_(false), |
| 41 server_job_available_(false), | 41 server_job_available_(false), |
| 42 printer_update_pending_(true), | 42 printer_update_pending_(true), |
| 43 printer_delete_pending_(false), | 43 printer_delete_pending_(false), |
| 44 task_in_progress_(false) { | 44 task_in_progress_(false) { |
| 45 } | 45 } |
| 46 | 46 |
| 47 bool PrinterJobHandler::Initialize() { | 47 bool PrinterJobHandler::Initialize() { |
| 48 if (print_system_->IsValidPrinter(printer_info_.printer_name)) { | 48 if (print_system_->GetPrintBackend()->IsValidPrinter( |
| 49 printer_info_.printer_name)) { |
| 49 printer_watcher_ = print_system_->CreatePrinterWatcher( | 50 printer_watcher_ = print_system_->CreatePrinterWatcher( |
| 50 printer_info_.printer_name); | 51 printer_info_.printer_name); |
| 51 printer_watcher_->StartWatching(this); | 52 printer_watcher_->StartWatching(this); |
| 52 NotifyJobAvailable(); | 53 NotifyJobAvailable(); |
| 53 } else { | 54 } else { |
| 54 // This printer does not exist any more. Delete it from the server. | 55 // This printer does not exist any more. Delete it from the server. |
| 55 OnPrinterDeleted(); | 56 OnPrinterDeleted(); |
| 56 } | 57 } |
| 57 return true; | 58 return true; |
| 58 } | 59 } |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 MessageLoop::current()->PostTask( | 130 MessageLoop::current()->PostTask( |
| 130 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start)); | 131 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start)); |
| 131 } | 132 } |
| 132 } | 133 } |
| 133 | 134 |
| 134 bool PrinterJobHandler::UpdatePrinterInfo() { | 135 bool PrinterJobHandler::UpdatePrinterInfo() { |
| 135 VLOG(1) << "CP_PROXY: Update printer info, id: " | 136 VLOG(1) << "CP_PROXY: Update printer info, id: " |
| 136 << printer_info_cloud_.printer_id; | 137 << printer_info_cloud_.printer_id; |
| 137 // We need to update the parts of the printer info that have changed | 138 // We need to update the parts of the printer info that have changed |
| 138 // (could be printer name, description, status or capabilities). | 139 // (could be printer name, description, status or capabilities). |
| 139 cloud_print::PrinterBasicInfo printer_info; | 140 printing::PrinterBasicInfo printer_info; |
| 140 printer_watcher_->GetCurrentPrinterInfo(&printer_info); | 141 printer_watcher_->GetCurrentPrinterInfo(&printer_info); |
| 141 cloud_print::PrinterCapsAndDefaults printer_caps; | 142 printing::PrinterCapsAndDefaults printer_caps; |
| 142 std::string post_data; | 143 std::string post_data; |
| 143 std::string mime_boundary; | 144 std::string mime_boundary; |
| 144 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | 145 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); |
| 145 if (print_system_->GetPrinterCapsAndDefaults(printer_info.printer_name, | 146 if (print_system_->GetPrintBackend()->GetPrinterCapsAndDefaults( |
| 146 &printer_caps)) { | 147 printer_info.printer_name, &printer_caps)) { |
| 147 std::string caps_hash = MD5String(printer_caps.printer_capabilities); | 148 std::string caps_hash = MD5String(printer_caps.printer_capabilities); |
| 148 if (caps_hash != printer_info_cloud_.caps_hash) { | 149 if (caps_hash != printer_info_cloud_.caps_hash) { |
| 149 // Hashes don't match, we need to upload new capabilities (the defaults | 150 // Hashes don't match, we need to upload new capabilities (the defaults |
| 150 // go for free along with the capabilities) | 151 // go for free along with the capabilities) |
| 151 printer_info_cloud_.caps_hash = caps_hash; | 152 printer_info_cloud_.caps_hash = caps_hash; |
| 152 CloudPrintHelpers::AddMultipartValueForUpload( | 153 CloudPrintHelpers::AddMultipartValueForUpload( |
| 153 kPrinterCapsValue, printer_caps.printer_capabilities, | 154 kPrinterCapsValue, printer_caps.printer_capabilities, |
| 154 mime_boundary, printer_caps.caps_mime_type, &post_data); | 155 mime_boundary, printer_caps.caps_mime_type, &post_data); |
| 155 CloudPrintHelpers::AddMultipartValueForUpload( | 156 CloudPrintHelpers::AddMultipartValueForUpload( |
| 156 kPrinterDefaultsValue, printer_caps.printer_defaults, | 157 kPrinterDefaultsValue, printer_caps.printer_defaults, |
| (...skipping 515 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 672 } | 673 } |
| 673 | 674 |
| 674 void PrinterJobHandler::OnJobSpoolFailed() { | 675 void PrinterJobHandler::OnJobSpoolFailed() { |
| 675 DCHECK(MessageLoop::current() == print_thread_.message_loop()); | 676 DCHECK(MessageLoop::current() == print_thread_.message_loop()); |
| 676 job_spooler_ = NULL; | 677 job_spooler_ = NULL; |
| 677 job_handler_message_loop_proxy_->PostTask(FROM_HERE, | 678 job_handler_message_loop_proxy_->PostTask(FROM_HERE, |
| 678 NewRunnableMethod(this, | 679 NewRunnableMethod(this, |
| 679 &PrinterJobHandler::JobFailed, | 680 &PrinterJobHandler::JobFailed, |
| 680 PRINT_FAILED)); | 681 PRINT_FAILED)); |
| 681 } | 682 } |
| 682 | |
| OLD | NEW |