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 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 } | 308 } |
309 return CloudPrintURLFetcher::STOP_PROCESSING; | 309 return CloudPrintURLFetcher::STOP_PROCESSING; |
310 } | 310 } |
311 | 311 |
312 CloudPrintURLFetcher::ResponseAction | 312 CloudPrintURLFetcher::ResponseAction |
313 PrinterJobHandler::HandlePrintDataResponse(const net::URLFetcher* source, | 313 PrinterJobHandler::HandlePrintDataResponse(const net::URLFetcher* source, |
314 const GURL& url, | 314 const GURL& url, |
315 const std::string& data) { | 315 const std::string& data) { |
316 VLOG(1) << "CP_CONNECTOR: Handling print data response" | 316 VLOG(1) << "CP_CONNECTOR: Handling print data response" |
317 << ", printer id: " << printer_info_cloud_.printer_id; | 317 << ", printer id: " << printer_info_cloud_.printer_id; |
318 base::Closure next_task; | |
319 if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) { | 318 if (file_util::CreateTemporaryFile(&job_details_.print_data_file_path_)) { |
320 int ret = file_util::WriteFile(job_details_.print_data_file_path_, | 319 int ret = file_util::WriteFile(job_details_.print_data_file_path_, |
321 data.c_str(), | 320 data.c_str(), |
322 data.length()); | 321 data.length()); |
323 source->GetResponseHeaders()->GetMimeType( | 322 source->GetResponseHeaders()->GetMimeType( |
324 &job_details_.print_data_mime_type_); | 323 &job_details_.print_data_mime_type_); |
325 DCHECK(ret == static_cast<int>(data.length())); | 324 DCHECK(ret == static_cast<int>(data.length())); |
326 if (ret == static_cast<int>(data.length())) { | 325 if (ret == static_cast<int>(data.length())) { |
327 next_task = base::Bind(&PrinterJobHandler::StartPrinting, this); | 326 UpdateJobStatus(PRINT_JOB_STATUS_IN_PROGRESS, SUCCESS); |
| 327 return CloudPrintURLFetcher::STOP_PROCESSING; |
328 } | 328 } |
329 } | 329 } |
330 // If there was no task allocated above, then there was an error in | 330 |
331 // saving the print data, bail out here. | 331 // If we are here, then there was an error in saving the print data, bail out |
332 if (next_task.is_null()) { | 332 // here. |
333 VLOG(1) << "CP_CONNECTOR: Error saving print data" | 333 VLOG(1) << "CP_CONNECTOR: Error saving print data" |
334 << ", printer id: " << printer_info_cloud_.printer_id; | 334 << ", printer id: " << printer_info_cloud_.printer_id; |
335 next_task = base::Bind(&PrinterJobHandler::JobFailed, this, | 335 base::MessageLoop::current()->PostTask( |
336 JOB_DOWNLOAD_FAILED); | 336 FROM_HERE, base::Bind(&PrinterJobHandler::JobFailed, this, |
337 } | 337 JOB_DOWNLOAD_FAILED)); |
338 base::MessageLoop::current()->PostTask(FROM_HERE, next_task); | |
339 return CloudPrintURLFetcher::STOP_PROCESSING; | 338 return CloudPrintURLFetcher::STOP_PROCESSING; |
340 } | 339 } |
341 | 340 |
342 CloudPrintURLFetcher::ResponseAction | 341 CloudPrintURLFetcher::ResponseAction |
343 PrinterJobHandler::HandleSuccessStatusUpdateResponse( | 342 PrinterJobHandler::HandleInProgressStatusUpdateResponse( |
344 const net::URLFetcher* source, | 343 const net::URLFetcher* source, |
345 const GURL& url, | 344 const GURL& url, |
346 DictionaryValue* json_data, | 345 DictionaryValue* json_data, |
347 bool succeeded) { | 346 bool succeeded) { |
348 VLOG(1) << "CP_CONNECTOR: Handling success status update response" | 347 VLOG(1) << "CP_CONNECTOR: Handling success status update response" |
349 << ", printer id: " << printer_info_cloud_.printer_id; | 348 << ", printer id: " << printer_info_cloud_.printer_id; |
350 // The print job has been spooled locally. We now need to create an object | |
351 // that monitors the status of the job and updates the server. | |
352 scoped_refptr<JobStatusUpdater> job_status_updater( | |
353 new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_, | |
354 local_job_id_, cloud_print_server_url_, | |
355 print_system_.get(), this)); | |
356 job_status_updater_list_.push_back(job_status_updater); | |
357 base::MessageLoop::current()->PostTask( | 349 base::MessageLoop::current()->PostTask( |
358 FROM_HERE, | 350 FROM_HERE, base::Bind(&PrinterJobHandler::StartPrinting, this)); |
359 base::Bind(&JobStatusUpdater::UpdateStatus, job_status_updater.get())); | |
360 if (succeeded) { | |
361 // Since we just printed successfully, we want to look for more jobs. | |
362 CheckForJobs(kJobFetchReasonQueryMore); | |
363 } | |
364 VLOG(1) << "CP_CONNECTOR: Stopping printer job handler" | |
365 << ", printer id: " << printer_info_cloud_.printer_id; | |
366 base::MessageLoop::current()->PostTask( | |
367 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); | |
368 return CloudPrintURLFetcher::STOP_PROCESSING; | 351 return CloudPrintURLFetcher::STOP_PROCESSING; |
369 } | 352 } |
370 | 353 |
371 CloudPrintURLFetcher::ResponseAction | 354 CloudPrintURLFetcher::ResponseAction |
372 PrinterJobHandler::HandleFailureStatusUpdateResponse( | 355 PrinterJobHandler::HandleFailureStatusUpdateResponse( |
373 const net::URLFetcher* source, | 356 const net::URLFetcher* source, |
374 const GURL& url, | 357 const GURL& url, |
375 DictionaryValue* json_data, | 358 DictionaryValue* json_data, |
376 bool succeeded) { | 359 bool succeeded) { |
377 VLOG(1) << "CP_CONNECTOR: Handling failure status update response" | 360 VLOG(1) << "CP_CONNECTOR: Handling failure status update response" |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 << ", job id: " << job_details_.job_id_; | 460 << ", job id: " << job_details_.job_id_; |
478 return; | 461 return; |
479 } | 462 } |
480 if (job_details_.job_id_.empty()) { | 463 if (job_details_.job_id_.empty()) { |
481 VLOG(1) << "CP_CONNECTOR: Job status update aborted (empty job id)" | 464 VLOG(1) << "CP_CONNECTOR: Job status update aborted (empty job id)" |
482 << ", printer id: " << printer_info_cloud_.printer_id; | 465 << ", printer id: " << printer_info_cloud_.printer_id; |
483 return; | 466 return; |
484 } | 467 } |
485 | 468 |
486 if (error == SUCCESS) { | 469 if (error == SUCCESS) { |
| 470 DCHECK_EQ(status, PRINT_JOB_STATUS_IN_PROGRESS); |
487 SetNextJSONHandler( | 471 SetNextJSONHandler( |
488 &PrinterJobHandler::HandleSuccessStatusUpdateResponse); | 472 &PrinterJobHandler::HandleInProgressStatusUpdateResponse); |
489 } else { | 473 } else { |
490 SetNextJSONHandler( | 474 SetNextJSONHandler( |
491 &PrinterJobHandler::HandleFailureStatusUpdateResponse); | 475 &PrinterJobHandler::HandleFailureStatusUpdateResponse); |
492 } | 476 } |
493 request_ = CloudPrintURLFetcher::Create(); | 477 request_ = CloudPrintURLFetcher::Create(); |
494 request_->StartGetRequest(GetUrlForJobStatusUpdate(cloud_print_server_url_, | 478 request_->StartGetRequest( |
495 job_details_.job_id_, | 479 GetUrlForJobStatusUpdate(cloud_print_server_url_, job_details_.job_id_, |
496 status), | 480 status), |
497 this, | 481 this, kCloudPrintAPIMaxRetryCount, std::string()); |
498 kCloudPrintAPIMaxRetryCount, | |
499 std::string()); | |
500 } | 482 } |
501 | 483 |
502 void PrinterJobHandler::RunScheduledJobCheck() { | 484 void PrinterJobHandler::RunScheduledJobCheck() { |
503 CheckForJobs(kJobFetchReasonRetry); | 485 CheckForJobs(kJobFetchReasonRetry); |
504 } | 486 } |
505 | 487 |
506 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { | 488 void PrinterJobHandler::SetNextJSONHandler(JSONDataHandler handler) { |
507 next_json_data_handler_ = handler; | 489 next_json_data_handler_ = handler; |
508 next_data_handler_ = NULL; | 490 next_data_handler_ = NULL; |
509 } | 491 } |
(...skipping 13 matching lines...) Expand all Loading... |
523 // This job failed, but others may be pending. Schedule a check. | 505 // This job failed, but others may be pending. Schedule a check. |
524 job_check_pending_ = true; | 506 job_check_pending_ = true; |
525 job_fetch_reason_ = kJobFetchReasonFailure; | 507 job_fetch_reason_ = kJobFetchReasonFailure; |
526 } | 508 } |
527 } | 509 } |
528 | 510 |
529 void PrinterJobHandler::JobSpooled(PlatformJobId local_job_id) { | 511 void PrinterJobHandler::JobSpooled(PlatformJobId local_job_id) { |
530 VLOG(1) << "CP_CONNECTOR: Job spooled" | 512 VLOG(1) << "CP_CONNECTOR: Job spooled" |
531 << ", printer id: " << printer_info_cloud_.printer_id | 513 << ", printer id: " << printer_info_cloud_.printer_id |
532 << ", job id: " << local_job_id; | 514 << ", job id: " << local_job_id; |
533 if (!shutting_down_) { | 515 if (shutting_down_) |
534 local_job_id_ = local_job_id; | 516 return; |
535 UpdateJobStatus(PRINT_JOB_STATUS_IN_PROGRESS, SUCCESS); | 517 |
536 print_thread_.Stop(); | 518 local_job_id_ = local_job_id; |
537 } | 519 print_thread_.Stop(); |
| 520 |
| 521 // The print job has been spooled locally. We now need to create an object |
| 522 // that monitors the status of the job and updates the server. |
| 523 scoped_refptr<JobStatusUpdater> job_status_updater( |
| 524 new JobStatusUpdater(printer_info_.printer_name, job_details_.job_id_, |
| 525 local_job_id_, cloud_print_server_url_, |
| 526 print_system_.get(), this)); |
| 527 job_status_updater_list_.push_back(job_status_updater); |
| 528 base::MessageLoop::current()->PostTask( |
| 529 FROM_HERE, |
| 530 base::Bind(&JobStatusUpdater::UpdateStatus, job_status_updater.get())); |
| 531 |
| 532 CheckForJobs(kJobFetchReasonQueryMore); |
| 533 |
| 534 VLOG(1) << "CP_CONNECTOR: Stopping printer job handler" |
| 535 << ", printer id: " << printer_info_cloud_.printer_id; |
| 536 base::MessageLoop::current()->PostTask( |
| 537 FROM_HERE, base::Bind(&PrinterJobHandler::Stop, this)); |
538 } | 538 } |
539 | 539 |
540 bool PrinterJobHandler::UpdatePrinterInfo() { | 540 bool PrinterJobHandler::UpdatePrinterInfo() { |
541 if (!printer_watcher_.get()) { | 541 if (!printer_watcher_.get()) { |
542 LOG(ERROR) << "CP_CONNECTOR: Printer watcher is missing." | 542 LOG(ERROR) << "CP_CONNECTOR: Printer watcher is missing." |
543 << " Check printer server url for printer id: " | 543 << " Check printer server url for printer id: " |
544 << printer_info_cloud_.printer_id; | 544 << printer_info_cloud_.printer_id; |
545 return false; | 545 return false; |
546 } | 546 } |
547 | 547 |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 job_details.print_data_mime_type_, | 681 job_details.print_data_mime_type_, |
682 printer_name, | 682 printer_name, |
683 UTF16ToUTF8(document_name), | 683 UTF16ToUTF8(document_name), |
684 job_details.tags_, | 684 job_details.tags_, |
685 this)) { | 685 this)) { |
686 OnJobSpoolFailed(); | 686 OnJobSpoolFailed(); |
687 } | 687 } |
688 } | 688 } |
689 | 689 |
690 } // namespace cloud_print | 690 } // namespace cloud_print |
OLD | NEW |