| 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/job_status_updater.h" | 5 #include "chrome/service/cloud_print/job_status_updater.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/json/json_reader.h" | 8 #include "base/json/json_reader.h" |
| 9 #include "base/string_util.h" | 9 #include "base/string_util.h" |
| 10 #include "base/utf_string_conversions.h" | 10 #include "base/utf_string_conversions.h" |
| 11 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "chrome/service/cloud_print/cloud_print_consts.h" | 12 #include "chrome/common/cloud_print/cloud_print_constants.h" |
| 13 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 13 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
| 14 #include "googleurl/src/gurl.h" | 14 #include "googleurl/src/gurl.h" |
| 15 | 15 |
| 16 namespace cloud_print { |
| 17 |
| 16 JobStatusUpdater::JobStatusUpdater(const std::string& printer_name, | 18 JobStatusUpdater::JobStatusUpdater(const std::string& printer_name, |
| 17 const std::string& job_id, | 19 const std::string& job_id, |
| 18 cloud_print::PlatformJobId& local_job_id, | 20 PlatformJobId& local_job_id, |
| 19 const GURL& cloud_print_server_url, | 21 const GURL& cloud_print_server_url, |
| 20 cloud_print::PrintSystem* print_system, | 22 PrintSystem* print_system, |
| 21 Delegate* delegate) | 23 Delegate* delegate) |
| 22 : printer_name_(printer_name), job_id_(job_id), | 24 : printer_name_(printer_name), job_id_(job_id), |
| 23 local_job_id_(local_job_id), | 25 local_job_id_(local_job_id), |
| 24 cloud_print_server_url_(cloud_print_server_url), | 26 cloud_print_server_url_(cloud_print_server_url), |
| 25 print_system_(print_system), delegate_(delegate), stopped_(false) { | 27 print_system_(print_system), delegate_(delegate), stopped_(false) { |
| 26 DCHECK(delegate_); | 28 DCHECK(delegate_); |
| 27 } | 29 } |
| 28 | 30 |
| 29 // Start checking the status of the local print job. | 31 // Start checking the status of the local print job. |
| 30 void JobStatusUpdater::UpdateStatus() { | 32 void JobStatusUpdater::UpdateStatus() { |
| 31 // It does not matter if we had already sent out an update and are waiting for | 33 // It does not matter if we had already sent out an update and are waiting for |
| 32 // a response. This is a new update and we will simply cancel the old request | 34 // a response. This is a new update and we will simply cancel the old request |
| 33 // and send a new one. | 35 // and send a new one. |
| 34 if (!stopped_) { | 36 if (!stopped_) { |
| 35 bool need_update = false; | 37 bool need_update = false; |
| 36 // If the job has already been completed, we just need to update the server | 38 // If the job has already been completed, we just need to update the server |
| 37 // with that status. The *only* reason we would come back here in that case | 39 // with that status. The *only* reason we would come back here in that case |
| 38 // is if our last server update attempt failed. | 40 // is if our last server update attempt failed. |
| 39 if (last_job_details_.status == cloud_print::PRINT_JOB_STATUS_COMPLETED) { | 41 if (last_job_details_.status == PRINT_JOB_STATUS_COMPLETED) { |
| 40 need_update = true; | 42 need_update = true; |
| 41 } else { | 43 } else { |
| 42 cloud_print::PrintJobDetails details; | 44 PrintJobDetails details; |
| 43 if (print_system_->GetJobDetails(printer_name_, local_job_id_, | 45 if (print_system_->GetJobDetails(printer_name_, local_job_id_, |
| 44 &details)) { | 46 &details)) { |
| 45 if (details != last_job_details_) { | 47 if (details != last_job_details_) { |
| 46 last_job_details_ = details; | 48 last_job_details_ = details; |
| 47 need_update = true; | 49 need_update = true; |
| 48 } | 50 } |
| 49 } else { | 51 } else { |
| 50 // If GetJobDetails failed, the most likely case is that the job no | 52 // If GetJobDetails failed, the most likely case is that the job no |
| 51 // longer exists in the OS queue. We are going to assume it is done in | 53 // longer exists in the OS queue. We are going to assume it is done in |
| 52 // this case. | 54 // this case. |
| 53 last_job_details_.Clear(); | 55 last_job_details_.Clear(); |
| 54 last_job_details_.status = cloud_print::PRINT_JOB_STATUS_COMPLETED; | 56 last_job_details_.status = PRINT_JOB_STATUS_COMPLETED; |
| 55 need_update = true; | 57 need_update = true; |
| 56 } | 58 } |
| 57 } | 59 } |
| 58 if (need_update) { | 60 if (need_update) { |
| 59 request_ = new CloudPrintURLFetcher; | 61 request_ = new CloudPrintURLFetcher; |
| 60 request_->StartGetRequest( | 62 request_->StartGetRequest( |
| 61 CloudPrintHelpers::GetUrlForJobStatusUpdate( | 63 GetUrlForJobStatusUpdate( |
| 62 cloud_print_server_url_, job_id_, last_job_details_), | 64 cloud_print_server_url_, job_id_, last_job_details_), |
| 63 this, | 65 this, |
| 64 kCloudPrintAPIMaxRetryCount, | 66 kCloudPrintAPIMaxRetryCount, |
| 65 std::string()); | 67 std::string()); |
| 66 } | 68 } |
| 67 } | 69 } |
| 68 } | 70 } |
| 69 | 71 |
| 70 void JobStatusUpdater::Stop() { | 72 void JobStatusUpdater::Stop() { |
| 71 request_ = NULL; | 73 request_ = NULL; |
| 72 DCHECK(delegate_); | 74 DCHECK(delegate_); |
| 73 stopped_ = true; | 75 stopped_ = true; |
| 74 delegate_->OnJobCompleted(this); | 76 delegate_->OnJobCompleted(this); |
| 75 } | 77 } |
| 76 | 78 |
| 77 // CloudPrintURLFetcher::Delegate implementation. | 79 // CloudPrintURLFetcher::Delegate implementation. |
| 78 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::HandleJSONData( | 80 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::HandleJSONData( |
| 79 const net::URLFetcher* source, | 81 const net::URLFetcher* source, |
| 80 const GURL& url, | 82 const GURL& url, |
| 81 DictionaryValue* json_data, | 83 DictionaryValue* json_data, |
| 82 bool succeeded) { | 84 bool succeeded) { |
| 83 if (last_job_details_.status == cloud_print::PRINT_JOB_STATUS_COMPLETED) { | 85 if (last_job_details_.status == PRINT_JOB_STATUS_COMPLETED) { |
| 84 MessageLoop::current()->PostTask( | 86 MessageLoop::current()->PostTask( |
| 85 FROM_HERE, base::Bind(&JobStatusUpdater::Stop, this)); | 87 FROM_HERE, base::Bind(&JobStatusUpdater::Stop, this)); |
| 86 } | 88 } |
| 87 return CloudPrintURLFetcher::STOP_PROCESSING; | 89 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 88 } | 90 } |
| 89 | 91 |
| 90 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::OnRequestAuthError() { | 92 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::OnRequestAuthError() { |
| 91 // We got an Auth error and have no idea how long it will take to refresh | 93 // We got an Auth error and have no idea how long it will take to refresh |
| 92 // auth information (may take forever). We'll drop current request and | 94 // auth information (may take forever). We'll drop current request and |
| 93 // propagate this error to the upper level. After auth issues will be | 95 // propagate this error to the upper level. After auth issues will be |
| 94 // resolved, GCP connector will restart. | 96 // resolved, GCP connector will restart. |
| 95 if (delegate_) | 97 if (delegate_) |
| 96 delegate_->OnAuthError(); | 98 delegate_->OnAuthError(); |
| 97 return CloudPrintURLFetcher::STOP_PROCESSING; | 99 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 98 } | 100 } |
| 99 | 101 |
| 100 std::string JobStatusUpdater::GetAuthHeader() { | 102 std::string JobStatusUpdater::GetAuthHeader() { |
| 101 return CloudPrintHelpers::GetCloudPrintAuthHeaderFromStore(); | 103 return GetCloudPrintAuthHeaderFromStore(); |
| 102 } | 104 } |
| 103 | 105 |
| 104 JobStatusUpdater::~JobStatusUpdater() {} | 106 JobStatusUpdater::~JobStatusUpdater() {} |
| 107 |
| 108 } // namespace cloud_print |
| OLD | NEW |