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#ifndef CHROME_SERVICE_CLOUD_PRINT_JOB_STATUS_UPDATER_H_
6#define CHROME_SERVICE_CLOUD_PRINT_JOB_STATUS_UPDATER_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/memory/ref_counted.h"
12#include "base/threading/thread.h"
13#include "chrome/service/cloud_print/cloud_print_url_fetcher.h"
14#include "chrome/service/cloud_print/print_system.h"
15#include "net/url_request/url_request_status.h"
16#include "url/gurl.h"
17
18namespace cloud_print {
19
20// Periodically monitors the status of a local print job and updates the
21// cloud print server accordingly. When the job has been completed this
22// object releases the reference to itself which should cause it to
23// self-destruct.
24class JobStatusUpdater : public base::RefCountedThreadSafe<JobStatusUpdater>,
25                         public CloudPrintURLFetcherDelegate {
26 public:
27  class Delegate {
28   public:
29    virtual bool OnJobCompleted(JobStatusUpdater* updater) = 0;
30    virtual void OnAuthError() = 0;
31
32   protected:
33    virtual ~Delegate() {}
34  };
35
36  JobStatusUpdater(const std::string& printer_name,
37                   const std::string& job_id,
38                   PlatformJobId& local_job_id,
39                   const GURL& cloud_print_server_url,
40                   PrintSystem* print_system,
41                   Delegate* delegate);
42
43  // Checks the status of the local print job and sends an update.
44  void UpdateStatus();
45  void Stop();
46
47  // CloudPrintURLFetcher::Delegate implementation.
48  virtual CloudPrintURLFetcher::ResponseAction HandleJSONData(
49      const net::URLFetcher* source,
50      const GURL& url,
51      base::DictionaryValue* json_data,
52      bool succeeded) OVERRIDE;
53  virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() OVERRIDE;
54  virtual std::string GetAuthHeader() OVERRIDE;
55
56  base::Time start_time() const {
57    return start_time_;
58  }
59
60 private:
61  friend class base::RefCountedThreadSafe<JobStatusUpdater>;
62  virtual ~JobStatusUpdater();
63
64  base::Time start_time_;
65  std::string printer_name_;
66  std::string job_id_;
67  PlatformJobId local_job_id_;
68  PrintJobDetails last_job_details_;
69  scoped_refptr<CloudPrintURLFetcher> request_;
70  GURL cloud_print_server_url_;
71  scoped_refptr<PrintSystem> print_system_;
72  Delegate* delegate_;
73  // A flag that is set to true in Stop() and will ensure the next scheduled
74  // task will do nothing.
75  bool stopped_;
76  DISALLOW_COPY_AND_ASSIGN(JobStatusUpdater);
77};
78
79// This typedef is to workaround the issue with certain versions of
80// Visual Studio where it gets confused between multiple Delegate
81// classes and gives a C2500 error. (I saw this error on the try bots -
82// the workaround was not needed for my machine).
83typedef JobStatusUpdater::Delegate JobStatusUpdaterDelegate;
84
85}  // namespace cloud_print
86
87#endif  // CHROME_SERVICE_CLOUD_PRINT_JOB_STATUS_UPDATER_H_
88