| 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" |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 153 MessageLoop::current()->PostTask( | 153 MessageLoop::current()->PostTask( |
| 154 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start)); | 154 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Start)); |
| 155 } | 155 } |
| 156 } | 156 } |
| 157 | 157 |
| 158 bool PrinterJobHandler::UpdatePrinterInfo() { | 158 bool PrinterJobHandler::UpdatePrinterInfo() { |
| 159 VLOG(1) << "CP_PROXY: Update printer info, id: " | 159 VLOG(1) << "CP_PROXY: Update printer info, id: " |
| 160 << printer_info_cloud_.printer_id; | 160 << printer_info_cloud_.printer_id; |
| 161 // We need to update the parts of the printer info that have changed | 161 // We need to update the parts of the printer info that have changed |
| 162 // (could be printer name, description, status or capabilities). | 162 // (could be printer name, description, status or capabilities). |
| 163 // First asynchronously fetch the capabilities. |
| 163 printing::PrinterBasicInfo printer_info; | 164 printing::PrinterBasicInfo printer_info; |
| 164 printer_watcher_->GetCurrentPrinterInfo(&printer_info); | 165 printer_watcher_->GetCurrentPrinterInfo(&printer_info); |
| 165 printing::PrinterCapsAndDefaults printer_caps; | 166 cloud_print::PrintSystem::PrinterCapsAndDefaultsCallback* callback = |
| 167 NewCallback(this, |
| 168 &PrinterJobHandler::OnReceivePrinterCaps); |
| 169 // Asnchronously fetch the printer caps and defaults. The story will |
| 170 // continue in OnReceivePrinterCaps. |
| 171 print_system_->GetPrinterCapsAndDefaults( |
| 172 printer_info.printer_name.c_str(), callback); |
| 173 // While we are waiting for the data, pretend we have work to do and return |
| 174 // true. |
| 175 return true; |
| 176 } |
| 177 |
| 178 void PrinterJobHandler::OnReceivePrinterCaps( |
| 179 bool succeeded, |
| 180 const std::string& printer_name, |
| 181 const printing::PrinterCapsAndDefaults& caps_and_defaults) { |
| 182 printing::PrinterBasicInfo printer_info; |
| 183 printer_watcher_->GetCurrentPrinterInfo(&printer_info); |
| 184 |
| 166 std::string post_data; | 185 std::string post_data; |
| 167 std::string mime_boundary; | 186 std::string mime_boundary; |
| 168 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); | 187 CloudPrintHelpers::CreateMimeBoundaryForUpload(&mime_boundary); |
| 169 if (print_system_->GetPrinterCapsAndDefaults( | 188 |
| 170 printer_info.printer_name, &printer_caps)) { | 189 if (succeeded) { |
| 171 std::string caps_hash = MD5String(printer_caps.printer_capabilities); | 190 std::string caps_hash = MD5String(caps_and_defaults.printer_capabilities); |
| 172 if (caps_hash != printer_info_cloud_.caps_hash) { | 191 if (caps_hash != printer_info_cloud_.caps_hash) { |
| 173 // Hashes don't match, we need to upload new capabilities (the defaults | 192 // Hashes don't match, we need to upload new capabilities (the defaults |
| 174 // go for free along with the capabilities) | 193 // go for free along with the capabilities) |
| 175 printer_info_cloud_.caps_hash = caps_hash; | 194 printer_info_cloud_.caps_hash = caps_hash; |
| 176 CloudPrintHelpers::AddMultipartValueForUpload( | 195 CloudPrintHelpers::AddMultipartValueForUpload( |
| 177 kPrinterCapsValue, printer_caps.printer_capabilities, | 196 kPrinterCapsValue, caps_and_defaults.printer_capabilities, |
| 178 mime_boundary, printer_caps.caps_mime_type, &post_data); | 197 mime_boundary, caps_and_defaults.caps_mime_type, &post_data); |
| 179 CloudPrintHelpers::AddMultipartValueForUpload( | 198 CloudPrintHelpers::AddMultipartValueForUpload( |
| 180 kPrinterDefaultsValue, printer_caps.printer_defaults, | 199 kPrinterDefaultsValue, caps_and_defaults.printer_defaults, |
| 181 mime_boundary, printer_caps.defaults_mime_type, | 200 mime_boundary, caps_and_defaults.defaults_mime_type, |
| 182 &post_data); | 201 &post_data); |
| 183 CloudPrintHelpers::AddMultipartValueForUpload( | 202 CloudPrintHelpers::AddMultipartValueForUpload( |
| 184 kPrinterCapsHashValue, caps_hash, mime_boundary, std::string(), | 203 kPrinterCapsHashValue, caps_hash, mime_boundary, std::string(), |
| 185 &post_data); | 204 &post_data); |
| 186 } | 205 } |
| 206 } else { |
| 207 LOG(ERROR) << "Failed to get printer caps and defaults for printer: " |
| 208 << printer_name; |
| 187 } | 209 } |
| 210 |
| 188 std::string tags_hash = | 211 std::string tags_hash = |
| 189 CloudPrintHelpers::GenerateHashOfStringMap(printer_info.options); | 212 CloudPrintHelpers::GenerateHashOfStringMap(printer_info.options); |
| 190 if (tags_hash != printer_info_cloud_.tags_hash) { | 213 if (tags_hash != printer_info_cloud_.tags_hash) { |
| 191 printer_info_cloud_.tags_hash = tags_hash; | 214 printer_info_cloud_.tags_hash = tags_hash; |
| 192 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags( | 215 CloudPrintHelpers::GenerateMultipartPostDataForPrinterTags( |
| 193 printer_info.options, mime_boundary, &post_data); | 216 printer_info.options, mime_boundary, &post_data); |
| 194 // Remove all the exising proxy tags. | 217 // Remove all the exising proxy tags. |
| 195 std::string cp_tag_wildcard(kProxyTagPrefix); | 218 std::string cp_tag_wildcard(kProxyTagPrefix); |
| 196 cp_tag_wildcard += ".*"; | 219 cp_tag_wildcard += ".*"; |
| 197 CloudPrintHelpers::AddMultipartValueForUpload( | 220 CloudPrintHelpers::AddMultipartValueForUpload( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 209 CloudPrintHelpers::AddMultipartValueForUpload( | 232 CloudPrintHelpers::AddMultipartValueForUpload( |
| 210 kPrinterDescValue, printer_info.printer_description, mime_boundary, | 233 kPrinterDescValue, printer_info.printer_description, mime_boundary, |
| 211 std::string() , &post_data); | 234 std::string() , &post_data); |
| 212 } | 235 } |
| 213 if (printer_info.printer_status != printer_info_.printer_status) { | 236 if (printer_info.printer_status != printer_info_.printer_status) { |
| 214 CloudPrintHelpers::AddMultipartValueForUpload( | 237 CloudPrintHelpers::AddMultipartValueForUpload( |
| 215 kPrinterStatusValue, StringPrintf("%d", printer_info.printer_status), | 238 kPrinterStatusValue, StringPrintf("%d", printer_info.printer_status), |
| 216 mime_boundary, std::string(), &post_data); | 239 mime_boundary, std::string(), &post_data); |
| 217 } | 240 } |
| 218 printer_info_ = printer_info; | 241 printer_info_ = printer_info; |
| 219 bool ret = false; | |
| 220 if (!post_data.empty()) { | 242 if (!post_data.empty()) { |
| 221 // Terminate the request body | 243 // Terminate the request body |
| 222 post_data.append("--" + mime_boundary + "--\r\n"); | 244 post_data.append("--" + mime_boundary + "--\r\n"); |
| 223 std::string mime_type("multipart/form-data; boundary="); | 245 std::string mime_type("multipart/form-data; boundary="); |
| 224 mime_type += mime_boundary; | 246 mime_type += mime_boundary; |
| 225 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); | 247 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); |
| 226 request_ = new CloudPrintURLFetcher; | 248 request_ = new CloudPrintURLFetcher; |
| 227 request_->StartPostRequest( | 249 request_->StartPostRequest( |
| 228 CloudPrintHelpers::GetUrlForPrinterUpdate( | 250 CloudPrintHelpers::GetUrlForPrinterUpdate( |
| 229 cloud_print_server_url_, printer_info_cloud_.printer_id), | 251 cloud_print_server_url_, printer_info_cloud_.printer_id), |
| 230 this, auth_token_, kCloudPrintAPIMaxRetryCount, mime_type, post_data); | 252 this, auth_token_, kCloudPrintAPIMaxRetryCount, mime_type, post_data); |
| 231 ret = true; | 253 } else { |
| 254 // We are done here. Go to the Stop state |
| 255 MessageLoop::current()->PostTask( |
| 256 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); |
| 232 } | 257 } |
| 233 return ret; | |
| 234 } | 258 } |
| 235 | 259 |
| 236 // CloudPrintURLFetcher::Delegate implementation. | 260 // CloudPrintURLFetcher::Delegate implementation. |
| 237 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( | 261 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( |
| 238 const URLFetcher* source, | 262 const URLFetcher* source, |
| 239 const GURL& url, | 263 const GURL& url, |
| 240 const std::string& data) { | 264 const std::string& data) { |
| 241 if (!next_data_handler_) | 265 if (!next_data_handler_) |
| 242 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 266 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
| 243 return (this->*next_data_handler_)(source, url, data); | 267 return (this->*next_data_handler_)(source, url, data); |
| (...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 608 } | 632 } |
| 609 | 633 |
| 610 void PrinterJobHandler::OnJobSpoolFailed() { | 634 void PrinterJobHandler::OnJobSpoolFailed() { |
| 611 DCHECK(MessageLoop::current() == print_thread_.message_loop()); | 635 DCHECK(MessageLoop::current() == print_thread_.message_loop()); |
| 612 job_spooler_ = NULL; | 636 job_spooler_ = NULL; |
| 613 job_handler_message_loop_proxy_->PostTask(FROM_HERE, | 637 job_handler_message_loop_proxy_->PostTask(FROM_HERE, |
| 614 NewRunnableMethod(this, | 638 NewRunnableMethod(this, |
| 615 &PrinterJobHandler::JobFailed, | 639 &PrinterJobHandler::JobFailed, |
| 616 PRINT_FAILED)); | 640 PRINT_FAILED)); |
| 617 } | 641 } |
| OLD | NEW |