Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / chrome / service / cloud_print / job_status_updater.cc
blob840bfc032e66339e74cf693c72afebe7afda83a9
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"
7 #include "base/bind.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"
18 #include "url/gurl.h"
20 namespace cloud_print {
22 namespace {
24 bool IsTerminalJobState(PrintJobStatus status) {
25 return status == PRINT_JOB_STATUS_ERROR ||
26 status == PRINT_JOB_STATUS_COMPLETED;
29 } // namespace
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,
36 Delegate* delegate)
37 : start_time_(base::Time::Now()),
38 printer_name_(printer_name),
39 job_id_(job_id),
40 local_job_id_(local_job_id),
41 cloud_print_server_url_(cloud_print_server_url),
42 print_system_(print_system),
43 delegate_(delegate),
44 stopped_(false) {
45 DCHECK(delegate_);
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.
53 if (!stopped_) {
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)) {
59 need_update = true;
60 } else {
61 PrintJobDetails details;
62 if (print_system_->GetJobDetails(printer_name_, local_job_id_,
63 &details)) {
64 if (details != last_job_details_) {
65 last_job_details_ = details;
66 need_update = true;
68 } else {
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
71 // this case.
72 last_job_details_.Clear();
73 last_job_details_.status = PRINT_JOB_STATUS_COMPLETED;
74 need_update = true;
76 UMA_HISTOGRAM_ENUMERATION("CloudPrint.NativeJobStatus",
77 last_job_details_.status, PRINT_JOB_STATUS_MAX);
79 if (need_update) {
80 request_ = CloudPrintURLFetcher::Create();
81 request_->StartGetRequest(
82 CloudPrintURLFetcher::REQUEST_UPDATE_JOB,
83 GetUrlForJobStatusUpdate(
84 cloud_print_server_url_, job_id_, last_job_details_),
85 this,
86 kCloudPrintAPIMaxRetryCount,
87 std::string());
92 void JobStatusUpdater::Stop() {
93 request_ = NULL;
94 DCHECK(delegate_);
95 stopped_ = true;
96 delegate_->OnJobCompleted(this);
99 // CloudPrintURLFetcher::Delegate implementation.
100 CloudPrintURLFetcher::ResponseAction JobStatusUpdater::HandleJSONData(
101 const net::URLFetcher* source,
102 const GURL& url,
103 base::DictionaryValue* json_data,
104 bool succeeded) {
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.
117 if (delegate_)
118 delegate_->OnAuthError();
119 return CloudPrintURLFetcher::STOP_PROCESSING;
122 std::string JobStatusUpdater::GetAuthHeader() {
123 return GetCloudPrintAuthHeaderFromStore();
126 JobStatusUpdater::~JobStatusUpdater() {}
128 } // namespace cloud_print