summaryrefslogtreecommitdiffstats
path: root/chrome/service/cloud_print/job_status_updater.cc
blob: f717f29a9a695b85c94a85c92b211cc4803e0306 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/service/cloud_print/job_status_updater.h"

#include "base/json/json_reader.h"
#include "base/string_util.h"
#include "base/utf_string_conversions.h"
#include "chrome/common/net/http_return.h"
#include "chrome/service/cloud_print/cloud_print_consts.h"
#include "chrome/service/cloud_print/cloud_print_helpers.h"
#include "googleurl/src/gurl.h"

JobStatusUpdater::JobStatusUpdater(const std::string& printer_name,
                           const std::string& job_id,
                           cloud_print::PlatformJobId& local_job_id,
                           const std::string& auth_token,
                           const GURL& cloud_print_server_url,
                           cloud_print::PrintSystem* print_system,
                           Delegate* delegate)
    : printer_name_(printer_name), job_id_(job_id),
      local_job_id_(local_job_id), auth_token_(auth_token),
      cloud_print_server_url_(cloud_print_server_url),
      print_system_(print_system), delegate_(delegate), stopped_(false) {
  DCHECK(delegate_);
}

JobStatusUpdater::~JobStatusUpdater() {}

// Start checking the status of the local print job.
void JobStatusUpdater::UpdateStatus() {
  // It does not matter if we had already sent out an update and are waiting for
  // a response. This is a new update and we will simply cancel the old request
  // and send a new one.
  if (!stopped_) {
    bool need_update = false;
    // If the job has already been completed, we just need to update the server
    // with that status. The *only* reason we would come back here in that case
    // is if our last server update attempt failed.
    if (last_job_details_.status == cloud_print::PRINT_JOB_STATUS_COMPLETED) {
      need_update = true;
    } else {
      cloud_print::PrintJobDetails details;
      if (print_system_->GetJobDetails(printer_name_, local_job_id_,
              &details)) {
        if (details != last_job_details_) {
          last_job_details_ = details;
          need_update = true;
        }
      } else {
        // If GetJobDetails failed, the most likely case is that the job no
        // longer exists in the OS queue. We are going to assume it is done in
        // this case.
        last_job_details_.Clear();
        last_job_details_.status = cloud_print::PRINT_JOB_STATUS_COMPLETED;
        need_update = true;
      }
    }
    if (need_update) {
      request_ = new CloudPrintURLFetcher;
      request_->StartGetRequest(
          CloudPrintHelpers::GetUrlForJobStatusUpdate(
              cloud_print_server_url_, job_id_, last_job_details_),
          this, auth_token_, kCloudPrintAPIMaxRetryCount);
    }
  }
}

void JobStatusUpdater::Stop() {
  request_ = NULL;
  DCHECK(delegate_);
  stopped_ = true;
  delegate_->OnJobCompleted(this);
}

// CloudPrintURLFetcher::Delegate implementation.
CloudPrintURLFetcher::ResponseAction JobStatusUpdater::HandleJSONData(
      const URLFetcher* source,
      const GURL& url,
      DictionaryValue* json_data,
      bool succeeded) {
  if (last_job_details_.status == cloud_print::PRINT_JOB_STATUS_COMPLETED) {
    MessageLoop::current()->PostTask(
        FROM_HERE, NewRunnableMethod(this, &JobStatusUpdater::Stop));
  }
  return CloudPrintURLFetcher::STOP_PROCESSING;
}

void JobStatusUpdater::OnRequestAuthError() {
  if (delegate_)
    delegate_->OnAuthError();
}