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