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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 // Check if we have work to do. | 102 // Check if we have work to do. |
103 if (HavePendingTasks()) { | 103 if (HavePendingTasks()) { |
104 if (printer_delete_pending_) { | 104 if (printer_delete_pending_) { |
105 printer_delete_pending_ = false; | 105 printer_delete_pending_ = false; |
106 task_in_progress_ = true; | 106 task_in_progress_ = true; |
107 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterDeleteResponse); | 107 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterDeleteResponse); |
108 request_ = new CloudPrintURLFetcher; | 108 request_ = new CloudPrintURLFetcher; |
109 request_->StartGetRequest( | 109 request_->StartGetRequest( |
110 CloudPrintHelpers::GetUrlForPrinterDelete( | 110 CloudPrintHelpers::GetUrlForPrinterDelete( |
111 cloud_print_server_url_, printer_info_cloud_.printer_id), | 111 cloud_print_server_url_, printer_info_cloud_.printer_id), |
112 this, auth_token_, kCloudPrintAPIMaxRetryCount); | 112 this, |
| 113 auth_token_, |
| 114 kCloudPrintAPIMaxRetryCount, |
| 115 std::string()); |
113 } | 116 } |
114 if (!task_in_progress_ && printer_update_pending_) { | 117 if (!task_in_progress_ && printer_update_pending_) { |
115 printer_update_pending_ = false; | 118 printer_update_pending_ = false; |
116 task_in_progress_ = UpdatePrinterInfo(); | 119 task_in_progress_ = UpdatePrinterInfo(); |
117 } | 120 } |
118 if (!task_in_progress_ && job_check_pending_) { | 121 if (!task_in_progress_ && job_check_pending_) { |
119 task_in_progress_ = true; | 122 task_in_progress_ = true; |
120 job_check_pending_ = false; | 123 job_check_pending_ = false; |
121 // We need to fetch any pending jobs for this printer | 124 // We need to fetch any pending jobs for this printer |
122 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); | 125 SetNextJSONHandler(&PrinterJobHandler::HandleJobMetadataResponse); |
123 request_ = new CloudPrintURLFetcher; | 126 request_ = new CloudPrintURLFetcher; |
124 request_->StartGetRequest( | 127 request_->StartGetRequest( |
125 CloudPrintHelpers::GetUrlForJobFetch( | 128 CloudPrintHelpers::GetUrlForJobFetch( |
126 cloud_print_server_url_, printer_info_cloud_.printer_id, | 129 cloud_print_server_url_, printer_info_cloud_.printer_id, |
127 job_fetch_reason_), | 130 job_fetch_reason_), |
128 this, auth_token_, kCloudPrintAPIMaxRetryCount); | 131 this, |
| 132 auth_token_, |
| 133 kCloudPrintAPIMaxRetryCount, |
| 134 std::string()); |
129 last_job_fetch_time_ = base::TimeTicks::Now(); | 135 last_job_fetch_time_ = base::TimeTicks::Now(); |
130 VLOG(1) << "Last job fetch time for printer " | 136 VLOG(1) << "Last job fetch time for printer " |
131 << printer_info_.printer_name.c_str() << " is " | 137 << printer_info_.printer_name.c_str() << " is " |
132 << last_job_fetch_time_.ToInternalValue(); | 138 << last_job_fetch_time_.ToInternalValue(); |
133 job_fetch_reason_.clear(); | 139 job_fetch_reason_.clear(); |
134 } | 140 } |
135 } | 141 } |
136 } | 142 } |
137 } | 143 } |
138 | 144 |
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
247 if (!post_data.empty()) { | 253 if (!post_data.empty()) { |
248 // Terminate the request body | 254 // Terminate the request body |
249 post_data.append("--" + mime_boundary + "--\r\n"); | 255 post_data.append("--" + mime_boundary + "--\r\n"); |
250 std::string mime_type("multipart/form-data; boundary="); | 256 std::string mime_type("multipart/form-data; boundary="); |
251 mime_type += mime_boundary; | 257 mime_type += mime_boundary; |
252 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); | 258 SetNextJSONHandler(&PrinterJobHandler::HandlePrinterUpdateResponse); |
253 request_ = new CloudPrintURLFetcher; | 259 request_ = new CloudPrintURLFetcher; |
254 request_->StartPostRequest( | 260 request_->StartPostRequest( |
255 CloudPrintHelpers::GetUrlForPrinterUpdate( | 261 CloudPrintHelpers::GetUrlForPrinterUpdate( |
256 cloud_print_server_url_, printer_info_cloud_.printer_id), | 262 cloud_print_server_url_, printer_info_cloud_.printer_id), |
257 this, auth_token_, kCloudPrintAPIMaxRetryCount, mime_type, post_data); | 263 this, |
| 264 auth_token_, |
| 265 kCloudPrintAPIMaxRetryCount, |
| 266 mime_type, |
| 267 post_data, |
| 268 std::string()); |
258 } else { | 269 } else { |
259 // We are done here. Go to the Stop state | 270 // We are done here. Go to the Stop state |
260 MessageLoop::current()->PostTask( | 271 MessageLoop::current()->PostTask( |
261 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); | 272 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); |
262 } | 273 } |
263 } | 274 } |
264 | 275 |
265 // CloudPrintURLFetcher::Delegate implementation. | 276 // CloudPrintURLFetcher::Delegate implementation. |
266 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( | 277 CloudPrintURLFetcher::ResponseAction PrinterJobHandler::HandleRawData( |
267 const URLFetcher* source, | 278 const URLFetcher* source, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
401 std::string value; | 412 std::string value; |
402 if (tags->GetString(i, &value)) | 413 if (tags->GetString(i, &value)) |
403 job_details_.tags_.push_back(value); | 414 job_details_.tags_.push_back(value); |
404 } | 415 } |
405 } | 416 } |
406 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); | 417 SetNextDataHandler(&PrinterJobHandler::HandlePrintTicketResponse); |
407 request_ = new CloudPrintURLFetcher; | 418 request_ = new CloudPrintURLFetcher; |
408 request_->StartGetRequest(GURL(print_ticket_url.c_str()), | 419 request_->StartGetRequest(GURL(print_ticket_url.c_str()), |
409 this, | 420 this, |
410 auth_token_, | 421 auth_token_, |
411 kCloudPrintAPIMaxRetryCount); | 422 kCloudPrintAPIMaxRetryCount, |
| 423 std::string()); |
412 } | 424 } |
413 } | 425 } |
414 } | 426 } |
415 // If no jobs are available, go to the Stop state. | 427 // If no jobs are available, go to the Stop state. |
416 if (!job_available) | 428 if (!job_available) |
417 MessageLoop::current()->PostTask( | 429 MessageLoop::current()->PostTask( |
418 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); | 430 FROM_HERE, NewRunnableMethod(this, &PrinterJobHandler::Stop)); |
419 return CloudPrintURLFetcher::STOP_PROCESSING; | 431 return CloudPrintURLFetcher::STOP_PROCESSING; |
420 } | 432 } |
421 | 433 |
422 CloudPrintURLFetcher::ResponseAction | 434 CloudPrintURLFetcher::ResponseAction |
423 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, | 435 PrinterJobHandler::HandlePrintTicketResponse(const URLFetcher* source, |
424 const GURL& url, | 436 const GURL& url, |
425 const std::string& data) { | 437 const std::string& data) { |
426 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " | 438 VLOG(1) << "CP_PROXY: Handle print ticket response, id: " |
427 << printer_info_cloud_.printer_id; | 439 << printer_info_cloud_.printer_id; |
428 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { | 440 if (print_system_->ValidatePrintTicket(printer_info_.printer_name, data)) { |
429 job_details_.print_ticket_ = data; | 441 job_details_.print_ticket_ = data; |
430 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); | 442 SetNextDataHandler(&PrinterJobHandler::HandlePrintDataResponse); |
431 request_ = new CloudPrintURLFetcher; | 443 request_ = new CloudPrintURLFetcher; |
| 444 std::string accept_headers = "Accept: "; |
| 445 accept_headers += print_system_->GetSupportedMimeTypes(); |
432 request_->StartGetRequest(GURL(print_data_url_.c_str()), | 446 request_->StartGetRequest(GURL(print_data_url_.c_str()), |
433 this, | 447 this, |
434 auth_token_, | 448 auth_token_, |
435 kJobDataMaxRetryCount); | 449 kJobDataMaxRetryCount, |
| 450 accept_headers); |
436 } else { | 451 } else { |
437 // The print ticket was not valid. We are done here. | 452 // The print ticket was not valid. We are done here. |
438 FailedFetchingJobData(); | 453 FailedFetchingJobData(); |
439 } | 454 } |
440 return CloudPrintURLFetcher::STOP_PROCESSING; | 455 return CloudPrintURLFetcher::STOP_PROCESSING; |
441 } | 456 } |
442 | 457 |
443 CloudPrintURLFetcher::ResponseAction | 458 CloudPrintURLFetcher::ResponseAction |
444 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, | 459 PrinterJobHandler::HandlePrintDataResponse(const URLFetcher* source, |
445 const GURL& url, | 460 const GURL& url, |
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
573 SetNextJSONHandler( | 588 SetNextJSONHandler( |
574 &PrinterJobHandler::HandleFailureStatusUpdateResponse); | 589 &PrinterJobHandler::HandleFailureStatusUpdateResponse); |
575 } | 590 } |
576 request_ = new CloudPrintURLFetcher; | 591 request_ = new CloudPrintURLFetcher; |
577 request_->StartGetRequest( | 592 request_->StartGetRequest( |
578 CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_, | 593 CloudPrintHelpers::GetUrlForJobStatusUpdate(cloud_print_server_url_, |
579 job_details_.job_id_, | 594 job_details_.job_id_, |
580 status), | 595 status), |
581 this, | 596 this, |
582 auth_token_, | 597 auth_token_, |
583 kCloudPrintAPIMaxRetryCount); | 598 kCloudPrintAPIMaxRetryCount, |
| 599 std::string()); |
584 } | 600 } |
585 } | 601 } |
586 } | 602 } |
587 | 603 |
588 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { | 604 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { |
589 next_json_data_handler_ = handler; | 605 next_json_data_handler_ = handler; |
590 next_data_handler_ = NULL; | 606 next_data_handler_ = NULL; |
591 } | 607 } |
592 | 608 |
593 void PrinterJobHandler::SetNextDataHandler(DataHandler handler) { | 609 void PrinterJobHandler::SetNextDataHandler(DataHandler handler) { |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
637 } | 653 } |
638 | 654 |
639 void PrinterJobHandler::OnJobSpoolFailed() { | 655 void PrinterJobHandler::OnJobSpoolFailed() { |
640 DCHECK(MessageLoop::current() == print_thread_.message_loop()); | 656 DCHECK(MessageLoop::current() == print_thread_.message_loop()); |
641 job_spooler_ = NULL; | 657 job_spooler_ = NULL; |
642 job_handler_message_loop_proxy_->PostTask(FROM_HERE, | 658 job_handler_message_loop_proxy_->PostTask(FROM_HERE, |
643 NewRunnableMethod(this, | 659 NewRunnableMethod(this, |
644 &PrinterJobHandler::JobFailed, | 660 &PrinterJobHandler::JobFailed, |
645 PRINT_FAILED)); | 661 PRINT_FAILED)); |
646 } | 662 } |
OLD | NEW |