OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/printing/cloud_print/job_status_updater.h" |
| 6 |
| 7 #include "base/json/json_reader.h" |
| 8 #include "base/string_util.h" |
| 9 #include "base/utf_string_conversions.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/printing/cloud_print/cloud_print_consts.h" |
| 12 #include "chrome/browser/printing/cloud_print/cloud_print_helpers.h" |
| 13 #include "googleurl/src/gurl.h" |
| 14 |
| 15 JobStatusUpdater::JobStatusUpdater(const std::string& printer_name, |
| 16 const std::string& job_id, |
| 17 cloud_print::PlatformJobId& local_job_id, |
| 18 const std::string& auth_token, |
| 19 Delegate* delegate) |
| 20 : printer_name_(printer_name), job_id_(job_id), |
| 21 local_job_id_(local_job_id), auth_token_(auth_token), |
| 22 delegate_(delegate), stopped_(false) { |
| 23 DCHECK(delegate_); |
| 24 } |
| 25 |
| 26 // Start checking the status of the local print job. |
| 27 void JobStatusUpdater::UpdateStatus() { |
| 28 // It does not matter if we had already sent out an update and are waiting for |
| 29 // a response. This is a new update and we will simply cancel the old request |
| 30 // and send a new one. |
| 31 if (!stopped_) { |
| 32 bool need_update = false; |
| 33 // If the job has already been completed, we just need to update the server |
| 34 // with that status. The *only* reason we would come back here in that case |
| 35 // is if our last server update attempt failed. |
| 36 if (last_job_details_.status == cloud_print::PRINT_JOB_STATUS_COMPLETED) { |
| 37 need_update = true; |
| 38 } else { |
| 39 cloud_print::PrintJobDetails details; |
| 40 if (cloud_print::GetJobDetails(printer_name_, local_job_id_, &details)) { |
| 41 if (details != last_job_details_) { |
| 42 last_job_details_ = details; |
| 43 need_update = true; |
| 44 } |
| 45 } else { |
| 46 // If GetJobDetails failed, the most likely case is that the job no |
| 47 // longer exists in the OS queue. We are going to assume it is done in |
| 48 // this case. |
| 49 last_job_details_.Clear(); |
| 50 last_job_details_.status = cloud_print::PRINT_JOB_STATUS_COMPLETED; |
| 51 need_update = true; |
| 52 } |
| 53 } |
| 54 if (need_update) { |
| 55 GURL update_url = CloudPrintHelpers::GetUrlForJobStatusUpdate( |
| 56 job_id_, last_job_details_); |
| 57 request_.reset(new URLFetcher(update_url, URLFetcher::GET, this)); |
| 58 CloudPrintHelpers::PrepCloudPrintRequest(request_.get(), auth_token_); |
| 59 request_->Start(); |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 64 void JobStatusUpdater::Stop() { |
| 65 request_.reset(); |
| 66 DCHECK(delegate_); |
| 67 stopped_ = true; |
| 68 delegate_->OnJobCompleted(this); |
| 69 } |
| 70 |
| 71 // URLFetcher::Delegate implementation. |
| 72 void JobStatusUpdater::OnURLFetchComplete(const URLFetcher* source, |
| 73 const GURL& url, |
| 74 const URLRequestStatus& status, |
| 75 int response_code, |
| 76 const ResponseCookies& cookies, |
| 77 const std::string& data) { |
| 78 int64 next_update_interval = kJobStatusUpdateInterval; |
| 79 if (!status.is_success() || (response_code != 200)) { |
| 80 next_update_interval *= 10; |
| 81 MessageLoop::current()->PostDelayedTask( |
| 82 FROM_HERE, NewRunnableMethod(this, &JobStatusUpdater::UpdateStatus), |
| 83 next_update_interval); |
| 84 } else if (last_job_details_.status == |
| 85 cloud_print::PRINT_JOB_STATUS_COMPLETED) { |
| 86 MessageLoop::current()->PostTask( |
| 87 FROM_HERE, NewRunnableMethod(this, &JobStatusUpdater::Stop)); |
| 88 } |
| 89 } |
| 90 |
OLD | NEW |