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/location.h"
10 #include "base/metrics/histogram.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_util.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "base/thread_task_runner_handle.h"
15 #include "base/values.h"
16 #include "chrome/common/cloud_print/cloud_print_constants.h"
17 #include "chrome/service/cloud_print/cloud_print_service_helpers.h"
20 namespace cloud_print
{
24 bool IsTerminalJobState(PrintJobStatus status
) {
25 return status
== PRINT_JOB_STATUS_ERROR
||
26 status
== PRINT_JOB_STATUS_COMPLETED
;
31 JobStatusUpdater::JobStatusUpdater(const std::string
& printer_name
,
32 const std::string
& job_id
,
33 PlatformJobId
& local_job_id
,
34 const GURL
& cloud_print_server_url
,
35 PrintSystem
* print_system
,
37 : start_time_(base::Time::Now()),
38 printer_name_(printer_name
),
40 local_job_id_(local_job_id
),
41 cloud_print_server_url_(cloud_print_server_url
),
42 print_system_(print_system
),
48 // Start checking the status of the local print job.
49 void JobStatusUpdater::UpdateStatus() {
50 // It does not matter if we had already sent out an update and are waiting for
51 // a response. This is a new update and we will simply cancel the old request
52 // and send a new one.
54 bool need_update
= false;
55 // If the job has already been completed, we just need to update the server
56 // with that status. The *only* reason we would come back here in that case
57 // is if our last server update attempt failed.
58 if (IsTerminalJobState(last_job_details_
.status
)) {
61 PrintJobDetails details
;
62 if (print_system_
->GetJobDetails(printer_name_
, local_job_id_
,
64 if (details
!= last_job_details_
) {
65 last_job_details_
= details
;
69 // If GetJobDetails failed, the most likely case is that the job no
70 // longer exists in the OS queue. We are going to assume it is done in
72 last_job_details_
.Clear();
73 last_job_details_
.status
= PRINT_JOB_STATUS_COMPLETED
;
76 UMA_HISTOGRAM_ENUMERATION("CloudPrint.NativeJobStatus",
77 last_job_details_
.status
, PRINT_JOB_STATUS_MAX
);
80 request_
= CloudPrintURLFetcher::Create();
81 request_
->StartGetRequest(
82 CloudPrintURLFetcher::REQUEST_UPDATE_JOB
,
83 GetUrlForJobStatusUpdate(
84 cloud_print_server_url_
, job_id_
, last_job_details_
),
86 kCloudPrintAPIMaxRetryCount
,
92 void JobStatusUpdater::Stop() {
96 delegate_
->OnJobCompleted(this);
99 // CloudPrintURLFetcher::Delegate implementation.
100 CloudPrintURLFetcher::ResponseAction
JobStatusUpdater::HandleJSONData(
101 const net::URLFetcher
* source
,
103 base::DictionaryValue
* json_data
,
105 if (IsTerminalJobState(last_job_details_
.status
)) {
106 base::ThreadTaskRunnerHandle::Get()->PostTask(
107 FROM_HERE
, base::Bind(&JobStatusUpdater::Stop
, this));
109 return CloudPrintURLFetcher::STOP_PROCESSING
;
112 CloudPrintURLFetcher::ResponseAction
JobStatusUpdater::OnRequestAuthError() {
113 // We got an Auth error and have no idea how long it will take to refresh
114 // auth information (may take forever). We'll drop current request and
115 // propagate this error to the upper level. After auth issues will be
116 // resolved, GCP connector will restart.
118 delegate_
->OnAuthError();
119 return CloudPrintURLFetcher::STOP_PROCESSING
;
122 std::string
JobStatusUpdater::GetAuthHeader() {
123 return GetCloudPrintAuthHeaderFromStore();
126 JobStatusUpdater::~JobStatusUpdater() {}
128 } // namespace cloud_print