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/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/values.h" | 12 #include "base/values.h" |
13 #include "chrome/common/cloud_print/cloud_print_constants.h" | 13 #include "chrome/common/cloud_print/cloud_print_constants.h" |
14 #include "chrome/service/cloud_print/cloud_print_helpers.h" | 14 #include "chrome/service/cloud_print/cloud_print_helpers.h" |
15 #include "url/gurl.h" | 15 #include "url/gurl.h" |
16 | 16 |
17 namespace cloud_print { | 17 namespace cloud_print { |
18 | 18 |
| 19 namespace { |
| 20 |
| 21 bool IsTerminalJobState(PrintJobStatus status) { |
| 22 return status == PRINT_JOB_STATUS_ERROR || |
| 23 status == PRINT_JOB_STATUS_COMPLETED; |
| 24 } |
| 25 |
| 26 } // namespace |
| 27 |
19 JobStatusUpdater::JobStatusUpdater(const std::string& printer_name, | 28 JobStatusUpdater::JobStatusUpdater(const std::string& printer_name, |
20 const std::string& job_id, | 29 const std::string& job_id, |
21 PlatformJobId& local_job_id, | 30 PlatformJobId& local_job_id, |
22 const GURL& cloud_print_server_url, | 31 const GURL& cloud_print_server_url, |
23 PrintSystem* print_system, | 32 PrintSystem* print_system, |
24 Delegate* delegate) | 33 Delegate* delegate) |
25 : start_time_(base::Time::Now()), | 34 : start_time_(base::Time::Now()), |
26 printer_name_(printer_name), | 35 printer_name_(printer_name), |
27 job_id_(job_id), | 36 job_id_(job_id), |
28 local_job_id_(local_job_id), | 37 local_job_id_(local_job_id), |
29 cloud_print_server_url_(cloud_print_server_url), | 38 cloud_print_server_url_(cloud_print_server_url), |
30 print_system_(print_system), | 39 print_system_(print_system), |
31 delegate_(delegate), | 40 delegate_(delegate), |
32 stopped_(false) { | 41 stopped_(false) { |
33 DCHECK(delegate_); | 42 DCHECK(delegate_); |
34 } | 43 } |
35 | 44 |
36 // Start checking the status of the local print job. | 45 // Start checking the status of the local print job. |
37 void JobStatusUpdater::UpdateStatus() { | 46 void JobStatusUpdater::UpdateStatus() { |
38 // It does not matter if we had already sent out an update and are waiting for | 47 // It does not matter if we had already sent out an update and are waiting for |
39 // a response. This is a new update and we will simply cancel the old request | 48 // a response. This is a new update and we will simply cancel the old request |
40 // and send a new one. | 49 // and send a new one. |
41 if (!stopped_) { | 50 if (!stopped_) { |
42 bool need_update = false; | 51 bool need_update = false; |
43 // If the job has already been completed, we just need to update the server | 52 // If the job has already been completed, we just need to update the server |
44 // with that status. The *only* reason we would come back here in that case | 53 // with that status. The *only* reason we would come back here in that case |
45 // is if our last server update attempt failed. | 54 // is if our last server update attempt failed. |
46 if (last_job_details_.status == PRINT_JOB_STATUS_COMPLETED) { | 55 if (IsTerminalJobState(last_job_details_.status)) { |
47 need_update = true; | 56 need_update = true; |
48 } else { | 57 } else { |
49 PrintJobDetails details; | 58 PrintJobDetails details; |
50 if (print_system_->GetJobDetails(printer_name_, local_job_id_, | 59 if (print_system_->GetJobDetails(printer_name_, local_job_id_, |
51 &details)) { | 60 &details)) { |
52 if (details != last_job_details_) { | 61 if (details != last_job_details_) { |
53 last_job_details_ = details; | 62 last_job_details_ = details; |
54 need_update = true; | 63 need_update = true; |
55 } | 64 } |
56 } else { | 65 } else { |
(...skipping 26 matching lines...) Expand all Loading... |
83 stopped_ = true; | 92 stopped_ = true; |
84 delegate_->OnJobCompleted(this); | 93 delegate_->OnJobCompleted(this); |
85 } | 94 } |
86 | 95 |
87 // CloudPrintURLFetcher::Delegate implementation. | 96 // CloudPrintURLFetcher::Delegate implementation. |
88 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::HandleJSONData( | 97 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::HandleJSONData( |
89 const net::URLFetcher* source, | 98 const net::URLFetcher* source, |
90 const GURL& url, | 99 const GURL& url, |
91 DictionaryValue* json_data, | 100 DictionaryValue* json_data, |
92 bool succeeded) { | 101 bool succeeded) { |
93 if (last_job_details_.status == PRINT_JOB_STATUS_COMPLETED) { | 102 if (IsTerminalJobState(last_job_details_.status)) { |
94 base::MessageLoop::current()->PostTask( | 103 base::MessageLoop::current()->PostTask( |
95 FROM_HERE, base::Bind(&JobStatusUpdater::Stop, this)); | 104 FROM_HERE, base::Bind(&JobStatusUpdater::Stop, this)); |
96 } | 105 } |
97 return CloudPrintURLFetcher::STOP_PROCESSING; | 106 return CloudPrintURLFetcher::STOP_PROCESSING; |
98 } | 107 } |
99 | 108 |
100 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::OnRequestAuthError() { | 109 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::OnRequestAuthError() { |
101 // We got an Auth error and have no idea how long it will take to refresh | 110 // We got an Auth error and have no idea how long it will take to refresh |
102 // auth information (may take forever). We'll drop current request and | 111 // auth information (may take forever). We'll drop current request and |
103 // propagate this error to the upper level. After auth issues will be | 112 // propagate this error to the upper level. After auth issues will be |
104 // resolved, GCP connector will restart. | 113 // resolved, GCP connector will restart. |
105 if (delegate_) | 114 if (delegate_) |
106 delegate_->OnAuthError(); | 115 delegate_->OnAuthError(); |
107 return CloudPrintURLFetcher::STOP_PROCESSING; | 116 return CloudPrintURLFetcher::STOP_PROCESSING; |
108 } | 117 } |
109 | 118 |
110 std::string JobStatusUpdater::GetAuthHeader() { | 119 std::string JobStatusUpdater::GetAuthHeader() { |
111 return GetCloudPrintAuthHeaderFromStore(); | 120 return GetCloudPrintAuthHeaderFromStore(); |
112 } | 121 } |
113 | 122 |
114 JobStatusUpdater::~JobStatusUpdater() {} | 123 JobStatusUpdater::~JobStatusUpdater() {} |
115 | 124 |
116 } // namespace cloud_print | 125 } // namespace cloud_print |
OLD | NEW |