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
3 // found in the LICENSE file.
5 #include "chrome/service/cloud_print/job_status_updater.h"
8 #include "base/json/json_reader.h"
9 #include "base/metrics/histogram.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/values.h"
13 #include "chrome/common/cloud_print/cloud_print_constants.h"
14 #include "chrome/service/cloud_print/cloud_print_service_helpers.h"
17 namespace cloud_print
{
21 bool IsTerminalJobState(PrintJobStatus status
) {
22 return status
== PRINT_JOB_STATUS_ERROR
||
23 status
== PRINT_JOB_STATUS_COMPLETED
;
28 JobStatusUpdater::JobStatusUpdater(const std::string
& printer_name
,
29 const std::string
& job_id
,
30 PlatformJobId
& local_job_id
,
31 const GURL
& cloud_print_server_url
,
32 PrintSystem
* print_system
,
34 : start_time_(base::Time::Now()),
35 printer_name_(printer_name
),
37 local_job_id_(local_job_id
),
38 cloud_print_server_url_(cloud_print_server_url
),
39 print_system_(print_system
),
45 // Start checking the status of the local print job.
46 void JobStatusUpdater::UpdateStatus() {
47 // It does not matter if we had already sent out an update and are waiting for
48 // a response. This is a new update and we will simply cancel the old request
49 // and send a new one.
51 bool need_update
= false;
52 // If the job has already been completed, we just need to update the server
53 // with that status. The *only* reason we would come back here in that case
54 // is if our last server update attempt failed.
55 if (IsTerminalJobState(last_job_details_
.status
)) {
58 PrintJobDetails details
;
59 if (print_system_
->GetJobDetails(printer_name_
, local_job_id_
,
61 if (details
!= last_job_details_
) {
62 last_job_details_
= details
;
66 // If GetJobDetails failed, the most likely case is that the job no
67 // longer exists in the OS queue. We are going to assume it is done in
69 last_job_details_
.Clear();
70 last_job_details_
.status
= PRINT_JOB_STATUS_COMPLETED
;
73 UMA_HISTOGRAM_ENUMERATION("CloudPrint.NativeJobStatus",
74 last_job_details_
.status
, PRINT_JOB_STATUS_MAX
);
77 request_
= CloudPrintURLFetcher::Create();
78 request_
->StartGetRequest(
79 CloudPrintURLFetcher::REQUEST_UPDATE_JOB
,
80 GetUrlForJobStatusUpdate(
81 cloud_print_server_url_
, job_id_
, last_job_details_
),
83 kCloudPrintAPIMaxRetryCount
,
89 void JobStatusUpdater::Stop() {
93 delegate_
->OnJobCompleted(this);
96 // CloudPrintURLFetcher::Delegate implementation.
97 CloudPrintURLFetcher::ResponseAction
JobStatusUpdater::HandleJSONData(
98 const net::URLFetcher
* source
,
100 base::DictionaryValue
* json_data
,
102 if (IsTerminalJobState(last_job_details_
.status
)) {
103 base::MessageLoop::current()->PostTask(
104 FROM_HERE
, base::Bind(&JobStatusUpdater::Stop
, this));
106 return CloudPrintURLFetcher::STOP_PROCESSING
;
109 CloudPrintURLFetcher::ResponseAction
JobStatusUpdater::OnRequestAuthError() {
110 // We got an Auth error and have no idea how long it will take to refresh
111 // auth information (may take forever). We'll drop current request and
112 // propagate this error to the upper level. After auth issues will be
113 // resolved, GCP connector will restart.
115 delegate_
->OnAuthError();
116 return CloudPrintURLFetcher::STOP_PROCESSING
;
119 std::string
JobStatusUpdater::GetAuthHeader() {
120 return GetCloudPrintAuthHeaderFromStore();
123 JobStatusUpdater::~JobStatusUpdater() {}
125 } // namespace cloud_print