Chromium Code Reviews| 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" |
| (...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 274 job_details_.tags_.clear(); | 274 job_details_.tags_.clear(); |
| 275 ListValue* tags = NULL; | 275 ListValue* tags = NULL; |
| 276 if (job_data->GetList(kTagsValue, &tags)) { | 276 if (job_data->GetList(kTagsValue, &tags)) { |
| 277 for (size_t i = 0; i < tags->GetSize(); i++) { | 277 for (size_t i = 0; i < tags->GetSize(); i++) { |
| 278 std::string value; | 278 std::string value; |
| 279 if (tags->GetString(i, &value)) | 279 if (tags->GetString(i, &value)) |
| 280 job_details_.tags_.push_back(value); | 280 job_details_.tags_.push_back(value); |
| 281 } | 281 } |
| 282 } | 282 } |
| 283 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); | 283 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); |
| 284 request_ = new CloudPrintURLFetcher; | 284 request_ = CreateCloudPrintURLFetcher(); |
| 285 request_->StartGetRequest(GURL(print_ticket_url.c_str()), | 285 request_->StartGetRequest(GURL(print_ticket_url.c_str()), |
| 286 this, | 286 this, |
| 287 kCloudPrintAPIMaxRetryCount, | 287 kCloudPrintAPIMaxRetryCount, |
| 288 std::string()); | 288 std::string()); |
| 289 } | 289 } |
| 290 } | 290 } |
| 291 } | 291 } |
| 292 // If no jobs are available, go to the Stop state. | 292 // If no jobs are available, go to the Stop state. |
| 293 if (!job_available) { | 293 if (!job_available) { |
| 294 VLOG(1) << "CP_CONNECTOR: Stopping printer job handler" | 294 VLOG(1) << "CP_CONNECTOR: Stopping printer job handler" |
| 295 << ", printer id: " << printer_info_cloud_.printer_id; | 295 << ", printer id: " << printer_info_cloud_.printer_id; |
| 296 MessageLoop::current()->PostTask( | 296 MessageLoop::current()->PostTask( |
| 297 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); | 297 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); |
| 298 } | 298 } |
| 299 return CloudPrintURLFetcher::STOP_PROCESSING; | 299 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 300 } | 300 } |
| 301 | 301 |
| 302 CloudPrintURLFetcher::ResponseAction | 302 CloudPrintURLFetcher::ResponseAction |
| 303 PrinterJobHandler::HandlePrintTicketResponse(const net::URLFetcher* source, | 303 PrinterJobHandler::HandlePrintTicketResponse(const net::URLFetcher* source, |
| 304 const GURL& url, | 304 const GURL& url, |
| 305 const std::string& data) { | 305 const std::string& data) { |
| 306 VLOG(1) << "CP_CONNECTOR: Handling print ticket response" | 306 VLOG(1) << "CP_CONNECTOR: Handling print ticket response" |
| 307 << ", printer id: " << printer_info_cloud_.printer_id; | 307 << ", printer id: " << printer_info_cloud_.printer_id; |
| 308 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { | 308 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { |
| 309 job_details_.print_ticket_ = data; | 309 job_details_.print_ticket_ = data; |
| 310 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); | 310 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); |
| 311 request_ = new CloudPrintURLFetcher; | 311 request_ = CreateCloudPrintURLFetcher(); |
| 312 std::string accept_headers = "Accept: "; | 312 std::string accept_headers = "Accept: "; |
| 313 accept_headers += print_system_->GetSupportedMimeTypes(); | 313 accept_headers += print_system_->GetSupportedMimeTypes(); |
| 314 request_->StartGetRequest(GURL(print_data_url_.c_str()), | 314 request_->StartGetRequest(GURL(print_data_url_.c_str()), |
| 315 this, | 315 this, |
| 316 kJobDataMaxRetryCount, | 316 kJobDataMaxRetryCount, |
| 317 accept_headers); | 317 accept_headers); |
| 318 } else { | 318 } else { |
| 319 // The print ticket was not valid. We are done here. | 319 // The print ticket was not valid. We are done here. |
| 320 FailedFetchingJobData(); | 320 FailedFetchingJobData(); |
| 321 } | 321 } |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 415 << ", task in progress: " << task_in_progress_; | 415 << ", task in progress: " << task_in_progress_; |
| 416 } | 416 } |
| 417 if (!task_in_progress_ && job_check_pending_) { | 417 if (!task_in_progress_ && job_check_pending_) { |
| 418 task_in_progress_ = true; | 418 task_in_progress_ = true; |
| 419 VLOG(1) << "CP_CONNECTOR: Changed task in progress" | 419 VLOG(1) << "CP_CONNECTOR: Changed task in progress" |
| 420 ", printer id: " << printer_info_cloud_.printer_id | 420 ", printer id: " << printer_info_cloud_.printer_id |
| 421 << ", task in progress: " << task_in_progress_; | 421 << ", task in progress: " << task_in_progress_; |
| 422 job_check_pending_ = false; | 422 job_check_pending_ = false; |
| 423 // We need to fetch any pending jobs for this printer | 423 // We need to fetch any pending jobs for this printer |
| 424 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); | 424 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); |
| 425 request_ = new CloudPrintURLFetcher; | 425 request_ = CreateCloudPrintURLFetcher(); |
| 426 request_->StartGetRequest( | 426 request_->StartGetRequest( |
| 427 GetUrlForJobFetch( | 427 GetUrlForJobFetch( |
| 428 cloud_print_server_url_, printer_info_cloud_.printer_id, | 428 cloud_print_server_url_, printer_info_cloud_.printer_id, |
| 429 job_fetch_reason_), | 429 job_fetch_reason_), |
| 430 this, | 430 this, |
| 431 kCloudPrintAPIMaxRetryCount, | 431 kCloudPrintAPIMaxRetryCount, |
| 432 std::string()); | 432 std::string()); |
| 433 last_job_fetch_time_ = base::TimeTicks::Now(); | 433 last_job_fetch_time_ = base::TimeTicks::Now(); |
| 434 VLOG(1) << "CP_CONNECTOR: Last job fetch time" | 434 VLOG(1) << "CP_CONNECTOR: Last job fetch time" |
| 435 << ", printer name: " << printer_info_.printer_name.c_str() | 435 << ", printer name: " << printer_info_.printer_name.c_str() |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 497 return; | 497 return; |
| 498 } | 498 } |
| 499 | 499 |
| 500 if (error == SUCCESS) { | 500 if (error == SUCCESS) { |
| 501 SetNextJSONHandler( | 501 SetNextJSONHandler( |
| 502 &PrinterJobHandler::HandleSuccessStatusUpdateResponse); | 502 &PrinterJobHandler::HandleSuccessStatusUpdateResponse); |
| 503 } else { | 503 } else { |
| 504 SetNextJSONHandler( | 504 SetNextJSONHandler( |
| 505 &PrinterJobHandler::HandleFailureStatusUpdateResponse); | 505 &PrinterJobHandler::HandleFailureStatusUpdateResponse); |
| 506 } | 506 } |
| 507 request_ = new CloudPrintURLFetcher; | 507 request_ = CreateCloudPrintURLFetcher(); |
| 508 request_->StartGetRequest(GetUrlForJobStatusUpdate(cloud_print_server_url_, | 508 request_->StartGetRequest(GetUrlForJobStatusUpdate(cloud_print_server_url_, |
| 509 job_details_.job_id_, | 509 job_details_.job_id_, |
| 510 status), | 510 status), |
| 511 this, | 511 this, |
| 512 kCloudPrintAPIMaxRetryCount, | 512 kCloudPrintAPIMaxRetryCount, |
| 513 std::string()); | 513 std::string()); |
| 514 } | 514 } |
| 515 | 515 |
| 516 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { | 516 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { |
| 517 next_json_data_handler_ = handler; | 517 next_json_data_handler_ = handler; |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 645 base::StringPrintf("%d", printer_info.printer_status), mime_boundary, | 645 base::StringPrintf("%d", printer_info.printer_status), mime_boundary, |
| 646 std::string(), &post_data); | 646 std::string(), &post_data); |
| 647 } | 647 } |
| 648 printer_info_ = printer_info; | 648 printer_info_ = printer_info; |
| 649 if (!post_data.empty()) { | 649 if (!post_data.empty()) { |
| 650 // Terminate the request body | 650 // Terminate the request body |
| 651 post_data.append("--" + mime_boundary + "--\r\n"); | 651 post_data.append("--" + mime_boundary + "--\r\n"); |
| 652 std::string mime_type("multipart/form-data; boundary="); | 652 std::string mime_type("multipart/form-data; boundary="); |
| 653 mime_type += mime_boundary; | 653 mime_type += mime_boundary; |
| 654 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); | 654 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); |
| 655 request_ = new CloudPrintURLFetcher; | 655 request_ = CreateCloudPrintURLFetcher(); |
| 656 request_->StartPostRequest( | 656 request_->StartPostRequest( |
| 657 GetUrlForPrinterUpdate( | 657 GetUrlForPrinterUpdate( |
| 658 cloud_print_server_url_, printer_info_cloud_.printer_id), | 658 cloud_print_server_url_, printer_info_cloud_.printer_id), |
| 659 this, | 659 this, |
| 660 kCloudPrintAPIMaxRetryCount, | 660 kCloudPrintAPIMaxRetryCount, |
| 661 mime_type, | 661 mime_type, |
| 662 post_data, | 662 post_data, |
| 663 std::string()); | 663 std::string()); |
| 664 } else { | 664 } else { |
| 665 // We are done here. Go to the Stop state | 665 // We are done here. Go to the Stop state |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 690 job_details.print_data_file_path_, | 690 job_details.print_data_file_path_, |
| 691 job_details.print_data_mime_type_, | 691 job_details.print_data_mime_type_, |
| 692 printer_name, | 692 printer_name, |
| 693 UTF16ToUTF8(document_name), | 693 UTF16ToUTF8(document_name), |
| 694 job_details.tags_, | 694 job_details.tags_, |
| 695 this)) { | 695 this)) { |
| 696 OnJobSpoolFailed(); | 696 OnJobSpoolFailed(); |
| 697 } | 697 } |
| 698 } | 698 } |
| 699 | 699 |
| 700 CloudPrintURLFetcher* PrinterJobHandler::CreateCloudPrintURLFetcher() { | |
|
Vitaly Buka (NO REVIEWS)
2013/02/08 22:38:56
Maybe also to use static factory for tests?
| |
| 701 return new CloudPrintURLFetcher; | |
| 702 } | |
| 703 | |
| 700 } // namespace cloud_print | 704 } // namespace cloud_print |
| OLD | NEW |