| 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 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 // Check if we have work to do. | 84 // Check if we have work to do. |
| 85 if (HavePendingTasks()) { | 85 if (HavePendingTasks()) { |
| 86 if (printer_delete_pending_) { | 86 if (printer_delete_pending_) { |
| 87 printer_delete_pending_ = false; | 87 printer_delete_pending_ = false; |
| 88 task_in_progress_ = true; | 88 task_in_progress_ = true; |
| 89 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterDeleteResponse); | 89 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterDeleteResponse); |
| 90 request_ = new CloudPrintURLFetcher; | 90 request_ = new CloudPrintURLFetcher; |
| 91 request_->StartGetRequest( | 91 request_->StartGetRequest( |
| 92 CloudPrintHelpers::GetUrlForPrinterDelete( | 92 CloudPrintHelpers::GetUrlForPrinterDelete( |
| 93 cloud_print_server_url_, printer_info_cloud_.printer_id), | 93 cloud_print_server_url_, printer_info_cloud_.printer_id), |
| 94 this, auth_token_, kCloudPrintAPIRetryPolicy); | 94 this, auth_token_, kCloudPrintAPIMaxRetryCount); |
| 95 } | 95 } |
| 96 if (!task_in_progress_ && printer_update_pending_) { | 96 if (!task_in_progress_ && printer_update_pending_) { |
| 97 printer_update_pending_ = false; | 97 printer_update_pending_ = false; |
| 98 task_in_progress_ = UpdatePrinterInfo(); | 98 task_in_progress_ = UpdatePrinterInfo(); |
| 99 } | 99 } |
| 100 if (!task_in_progress_ && server_job_available_) { | 100 if (!task_in_progress_ && server_job_available_) { |
| 101 task_in_progress_ = true; | 101 task_in_progress_ = true; |
| 102 server_job_available_ = false; | 102 server_job_available_ = false; |
| 103 // We need to fetch any pending jobs for this printer | 103 // We need to fetch any pending jobs for this printer |
| 104 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); | 104 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); |
| 105 request_ = new CloudPrintURLFetcher; | 105 request_ = new CloudPrintURLFetcher; |
| 106 request_->StartGetRequest( | 106 request_->StartGetRequest( |
| 107 CloudPrintHelpers::GetUrlForJobFetch( | 107 CloudPrintHelpers::GetUrlForJobFetch( |
| 108 cloud_print_server_url_, printer_info_cloud_.printer_id), | 108 cloud_print_server_url_, printer_info_cloud_.printer_id), |
| 109 this, auth_token_, kCloudPrintAPIRetryPolicy); | 109 this, auth_token_, kCloudPrintAPIMaxRetryCount); |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 } | 112 } |
| 113 } | 113 } |
| 114 | 114 |
| 115 void PrinterJobHandler::Stop() { | 115 void PrinterJobHandler::Stop() { |
| 116 VLOG(1) << "CP_PROXY: Stop printer job handler, id: " | 116 VLOG(1) << "CP_PROXY: Stop printer job handler, id: " |
| 117 << printer_info_cloud_.printer_id; | 117 << printer_info_cloud_.printer_id; |
| 118 task_in_progress_ = false; | 118 task_in_progress_ = false; |
| 119 Reset(); | 119 Reset(); |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 if (!post_data.empty()) { | 199 if (!post_data.empty()) { |
| 200 // Terminate the request body | 200 // Terminate the request body |
| 201 post_data.append("--" + mime_boundary + "--\r\n"); | 201 post_data.append("--" + mime_boundary + "--\r\n"); |
| 202 std::string mime_type("multipart/form-data; boundary="); | 202 std::string mime_type("multipart/form-data; boundary="); |
| 203 mime_type += mime_boundary; | 203 mime_type += mime_boundary; |
| 204 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); | 204 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); |
| 205 request_ = new CloudPrintURLFetcher; | 205 request_ = new CloudPrintURLFetcher; |
| 206 request_->StartPostRequest( | 206 request_->StartPostRequest( |
| 207 CloudPrintHelpers::GetUrlForPrinterUpdate( | 207 CloudPrintHelpers::GetUrlForPrinterUpdate( |
| 208 cloud_print_server_url_, printer_info_cloud_.printer_id), | 208 cloud_print_server_url_, printer_info_cloud_.printer_id), |
| 209 this, auth_token_, kCloudPrintAPIRetryPolicy, mime_type, post_data); | 209 this, auth_token_, kCloudPrintAPIMaxRetryCount, mime_type, post_data); |
| 210 ret = true; | 210 ret = true; |
| 211 } | 211 } |
| 212 return ret; | 212 return ret; |
| 213 } | 213 } |
| 214 | 214 |
| 215 // CloudPrintURLFetcher::Delegate implementation. | 215 // CloudPrintURLFetcher::Delegate implementation. |
| 216 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( | 216 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( |
| 217 const URLFetcher* source, | 217 const URLFetcher* source, |
| 218 const GURL& url, | 218 const GURL& url, |
| 219 const std::string& data) { | 219 const std::string& data) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 job_data->GetString(kIdValue, &job_details_.job_id_); | 340 job_data->GetString(kIdValue, &job_details_.job_id_); |
| 341 job_data->GetString(kTitleValue, &job_details_.job_title_); | 341 job_data->GetString(kTitleValue, &job_details_.job_title_); |
| 342 std::string print_ticket_url; | 342 std::string print_ticket_url; |
| 343 job_data->GetString(kTicketUrlValue, &print_ticket_url); | 343 job_data->GetString(kTicketUrlValue, &print_ticket_url); |
| 344 job_data->GetString(kFileUrlValue, &print_data_url_); | 344 job_data->GetString(kFileUrlValue, &print_data_url_); |
| 345 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); | 345 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); |
| 346 request_ = new CloudPrintURLFetcher; | 346 request_ = new CloudPrintURLFetcher; |
| 347 request_->StartGetRequest(GURL(print_ticket_url.c_str()), | 347 request_->StartGetRequest(GURL(print_ticket_url.c_str()), |
| 348 this, | 348 this, |
| 349 auth_token_, | 349 auth_token_, |
| 350 kCloudPrintAPIRetryPolicy); | 350 kCloudPrintAPIMaxRetryCount); |
| 351 } | 351 } |
| 352 } | 352 } |
| 353 } | 353 } |
| 354 // If no jobs are available, go to the Stop state. | 354 // If no jobs are available, go to the Stop state. |
| 355 if (!job_available) | 355 if (!job_available) |
| 356 MessageLoop::current()->PostTask( | 356 MessageLoop::current()->PostTask( |
| 357 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); | 357 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); |
| 358 return CloudPrintURLFetcher::STOP_PROCESSING; | 358 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 359 } | 359 } |
| 360 | 360 |
| 361 CloudPrintURLFetcher::ResponseAction | 361 CloudPrintURLFetcher::ResponseAction |
| 362 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, | 362 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, |
| 363 const GURL& url, | 363 const GURL& url, |
| 364 const std::string& data) { | 364 const std::string& data) { |
| 365 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " | 365 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " |
| 366 << printer_info_cloud_.printer_id; | 366 << printer_info_cloud_.printer_id; |
| 367 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { | 367 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { |
| 368 job_details_.print_ticket_ = data; | 368 job_details_.print_ticket_ = data; |
| 369 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); | 369 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); |
| 370 request_ = new CloudPrintURLFetcher; | 370 request_ = new CloudPrintURLFetcher; |
| 371 request_->StartGetRequest(GURL(print_data_url_.c_str()), | 371 request_->StartGetRequest(GURL(print_data_url_.c_str()), |
| 372 this, | 372 this, |
| 373 auth_token_, | 373 auth_token_, |
| 374 kJobDataRetryPolicy); | 374 kJobDataMaxRetryCount); |
| 375 } else { | 375 } else { |
| 376 // The print ticket was not valid. We are done here. | 376 // The print ticket was not valid. We are done here. |
| 377 FailedFetchingJobData(); | 377 FailedFetchingJobData(); |
| 378 } | 378 } |
| 379 return CloudPrintURLFetcher::STOP_PROCESSING; | 379 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 380 } | 380 } |
| 381 | 381 |
| 382 CloudPrintURLFetcher::ResponseAction | 382 CloudPrintURLFetcher::ResponseAction |
| 383 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, | 383 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, |
| 384 const GURL& url, | 384 const GURL& url, |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 512 SetNextJSONHandler( | 512 SetNextJSONHandler( |
| 513 &PrinterJobHandler::HandleFailureStatusUpdateResponse); | 513 &PrinterJobHandler::HandleFailureStatusUpdateResponse); |
| 514 } | 514 } |
| 515 request_ = new CloudPrintURLFetcher; | 515 request_ = new CloudPrintURLFetcher; |
| 516 request_->StartGetRequest( | 516 request_->StartGetRequest( |
| 517 CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_, | 517 CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_, |
| 518 job_details_.job_id_, | 518 job_details_.job_id_, |
| 519 status), | 519 status), |
| 520 this, | 520 this, |
| 521 auth_token_, | 521 auth_token_, |
| 522 kCloudPrintAPIRetryPolicy); | 522 kCloudPrintAPIMaxRetryCount); |
| 523 } | 523 } |
| 524 } | 524 } |
| 525 } | 525 } |
| 526 | 526 |
| 527 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { | 527 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { |
| 528 next_json_data_handler_ = handler; | 528 next_json_data_handler_ = handler; |
| 529 next_data_handler_ = NULL; | 529 next_data_handler_ = NULL; |
| 530 } | 530 } |
| 531 | 531 |
| 532 void PrinterJobHandler::SetNextDataHandler(DataHandler handler) { | 532 void PrinterJobHandler::SetNextDataHandler(DataHandler handler) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 575 } | 575 } |
| 576 | 576 |
| 577 void PrinterJobHandler::OnJobSpoolFailed() { | 577 void PrinterJobHandler::OnJobSpoolFailed() { |
| 578 DCHECK(MessageLoop::current() == print_thread_.message_loop()); | 578 DCHECK(MessageLoop::current() == print_thread_.message_loop()); |
| 579 job_spooler_ = NULL; | 579 job_spooler_ = NULL; |
| 580 job_handler_message_loop_proxy_->PostTask(FROM_HERE, | 580 job_handler_message_loop_proxy_->PostTask(FROM_HERE, |
| 581 NewRunnableMethod(this, | 581 NewRunnableMethod(this, |
| 582 &PrinterJobHandler::JobFailed, | 582 &PrinterJobHandler::JobFailed, |
| 583 PRINT_FAILED)); | 583 PRINT_FAILED)); |
| 584 } | 584 } |
| OLD | NEW |