| 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_ && job_check_pending_) { | 100 if (!task_in_progress_ && job_check_pending_) { |
| 101 task_in_progress_ = true; | 101 task_in_progress_ = true; |
| 102 job_check_pending_ = false; | 102 job_check_pending_ = 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 job_fetch_reason_), | 109 job_fetch_reason_), |
| 110 this, auth_token_, kCloudPrintAPIRetryPolicy); | 110 this, auth_token_, kCloudPrintAPIMaxRetryCount); |
| 111 last_job_fetch_time_ = base::TimeTicks::Now(); | 111 last_job_fetch_time_ = base::TimeTicks::Now(); |
| 112 VLOG(1) << "Last job fetch time for printer " | 112 VLOG(1) << "Last job fetch time for printer " |
| 113 << printer_info_.printer_name.c_str() << " is " | 113 << printer_info_.printer_name.c_str() << " is " |
| 114 << last_job_fetch_time_.ToInternalValue(); | 114 << last_job_fetch_time_.ToInternalValue(); |
| 115 job_fetch_reason_.clear(); | 115 job_fetch_reason_.clear(); |
| 116 } | 116 } |
| 117 } | 117 } |
| 118 } | 118 } |
| 119 } | 119 } |
| 120 | 120 |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 207 if (!post_data.empty()) { | 207 if (!post_data.empty()) { |
| 208 // Terminate the request body | 208 // Terminate the request body |
| 209 post_data.append("--" + mime_boundary + "--\r\n"); | 209 post_data.append("--" + mime_boundary + "--\r\n"); |
| 210 std::string mime_type("multipart/form-data; boundary="); | 210 std::string mime_type("multipart/form-data; boundary="); |
| 211 mime_type += mime_boundary; | 211 mime_type += mime_boundary; |
| 212 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); | 212 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); |
| 213 request_ = new CloudPrintURLFetcher; | 213 request_ = new CloudPrintURLFetcher; |
| 214 request_->StartPostRequest( | 214 request_->StartPostRequest( |
| 215 CloudPrintHelpers::GetUrlForPrinterUpdate( | 215 CloudPrintHelpers::GetUrlForPrinterUpdate( |
| 216 cloud_print_server_url_, printer_info_cloud_.printer_id), | 216 cloud_print_server_url_, printer_info_cloud_.printer_id), |
| 217 this, auth_token_, kCloudPrintAPIRetryPolicy, mime_type, post_data); | 217 this, auth_token_, kCloudPrintAPIMaxRetryCount, mime_type, post_data); |
| 218 ret = true; | 218 ret = true; |
| 219 } | 219 } |
| 220 return ret; | 220 return ret; |
| 221 } | 221 } |
| 222 | 222 |
| 223 // CloudPrintURLFetcher::Delegate implementation. | 223 // CloudPrintURLFetcher::Delegate implementation. |
| 224 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( | 224 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( |
| 225 const URLFetcher* source, | 225 const URLFetcher* source, |
| 226 const GURL& url, | 226 const GURL& url, |
| 227 const std::string& data) { | 227 const std::string& data) { |
| (...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 348 job_data->GetString(kIdValue, &job_details_.job_id_); | 348 job_data->GetString(kIdValue, &job_details_.job_id_); |
| 349 job_data->GetString(kTitleValue, &job_details_.job_title_); | 349 job_data->GetString(kTitleValue, &job_details_.job_title_); |
| 350 std::string print_ticket_url; | 350 std::string print_ticket_url; |
| 351 job_data->GetString(kTicketUrlValue, &print_ticket_url); | 351 job_data->GetString(kTicketUrlValue, &print_ticket_url); |
| 352 job_data->GetString(kFileUrlValue, &print_data_url_); | 352 job_data->GetString(kFileUrlValue, &print_data_url_); |
| 353 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); | 353 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); |
| 354 request_ = new CloudPrintURLFetcher; | 354 request_ = new CloudPrintURLFetcher; |
| 355 request_->StartGetRequest(GURL(print_ticket_url.c_str()), | 355 request_->StartGetRequest(GURL(print_ticket_url.c_str()), |
| 356 this, | 356 this, |
| 357 auth_token_, | 357 auth_token_, |
| 358 kCloudPrintAPIRetryPolicy); | 358 kCloudPrintAPIMaxRetryCount); |
| 359 } | 359 } |
| 360 } | 360 } |
| 361 } | 361 } |
| 362 // If no jobs are available, go to the Stop state. | 362 // If no jobs are available, go to the Stop state. |
| 363 if (!job_available) | 363 if (!job_available) |
| 364 MessageLoop::current()->PostTask( | 364 MessageLoop::current()->PostTask( |
| 365 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); | 365 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); |
| 366 return CloudPrintURLFetcher::STOP_PROCESSING; | 366 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 367 } | 367 } |
| 368 | 368 |
| 369 CloudPrintURLFetcher::ResponseAction | 369 CloudPrintURLFetcher::ResponseAction |
| 370 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, | 370 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, |
| 371 const GURL& url, | 371 const GURL& url, |
| 372 const std::string& data) { | 372 const std::string& data) { |
| 373 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " | 373 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " |
| 374 << printer_info_cloud_.printer_id; | 374 << printer_info_cloud_.printer_id; |
| 375 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { | 375 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { |
| 376 job_details_.print_ticket_ = data; | 376 job_details_.print_ticket_ = data; |
| 377 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); | 377 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); |
| 378 request_ = new CloudPrintURLFetcher; | 378 request_ = new CloudPrintURLFetcher; |
| 379 request_->StartGetRequest(GURL(print_data_url_.c_str()), | 379 request_->StartGetRequest(GURL(print_data_url_.c_str()), |
| 380 this, | 380 this, |
| 381 auth_token_, | 381 auth_token_, |
| 382 kJobDataRetryPolicy); | 382 kJobDataMaxRetryCount); |
| 383 } else { | 383 } else { |
| 384 // The print ticket was not valid. We are done here. | 384 // The print ticket was not valid. We are done here. |
| 385 FailedFetchingJobData(); | 385 FailedFetchingJobData(); |
| 386 } | 386 } |
| 387 return CloudPrintURLFetcher::STOP_PROCESSING; | 387 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 388 } | 388 } |
| 389 | 389 |
| 390 CloudPrintURLFetcher::ResponseAction | 390 CloudPrintURLFetcher::ResponseAction |
| 391 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, | 391 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, |
| 392 const GURL& url, | 392 const GURL& url, |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 520 SetNextJSONHandler( | 520 SetNextJSONHandler( |
| 521 &PrinterJobHandler::HandleFailureStatusUpdateResponse); | 521 &PrinterJobHandler::HandleFailureStatusUpdateResponse); |
| 522 } | 522 } |
| 523 request_ = new CloudPrintURLFetcher; | 523 request_ = new CloudPrintURLFetcher; |
| 524 request_->StartGetRequest( | 524 request_->StartGetRequest( |
| 525 CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_, | 525 CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_, |
| 526 job_details_.job_id_, | 526 job_details_.job_id_, |
| 527 status), | 527 status), |
| 528 this, | 528 this, |
| 529 auth_token_, | 529 auth_token_, |
| 530 kCloudPrintAPIRetryPolicy); | 530 kCloudPrintAPIMaxRetryCount); |
| 531 } | 531 } |
| 532 } | 532 } |
| 533 } | 533 } |
| 534 | 534 |
| 535 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { | 535 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { |
| 536 next_json_data_handler_ = handler; | 536 next_json_data_handler_ = handler; |
| 537 next_data_handler_ = NULL; | 537 next_data_handler_ = NULL; |
| 538 } | 538 } |
| 539 | 539 |
| 540 void PrinterJobHandler::SetNextDataHandler(DataHandler handler) { | 540 void PrinterJobHandler::SetNextDataHandler(DataHandler handler) { |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 583 } | 583 } |
| 584 | 584 |
| 585 void PrinterJobHandler::OnJobSpoolFailed() { | 585 void PrinterJobHandler::OnJobSpoolFailed() { |
| 586 DCHECK(MessageLoop::current() == print_thread_.message_loop()); | 586 DCHECK(MessageLoop::current() == print_thread_.message_loop()); |
| 587 job_spooler_ = NULL; | 587 job_spooler_ = NULL; |
| 588 job_handler_message_loop_proxy_->PostTask(FROM_HERE, | 588 job_handler_message_loop_proxy_->PostTask(FROM_HERE, |
| 589 NewRunnableMethod(this, | 589 NewRunnableMethod(this, |
| 590 &PrinterJobHandler::JobFailed, | 590 &PrinterJobHandler::JobFailed, |
| 591 PRINT_FAILED)); | 591 PRINT_FAILED)); |
| 592 } | 592 } |
| OLD | NEW |