Avoid crashing when going back/forward to debug URLs on a sad WebUI tab.
[chromium-blink-merge.git] / chrome / service / cloud_print / job_status_updater.h
blob169f0e7eb249ffc5361ff4535b7f4c2c180cf5b3
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 #ifndef CHROME_SERVICE_CLOUD_PRINT_JOB_STATUS_UPDATER_H_
6 #define CHROME_SERVICE_CLOUD_PRINT_JOB_STATUS_UPDATER_H_
8 #include <string>
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"
18 namespace cloud_print {
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.
24 class 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;
32 protected:
33 virtual ~Delegate() {}
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);
43 // Checks the status of the local print job and sends an update.
44 void UpdateStatus();
45 void Stop();
47 // CloudPrintURLFetcher::Delegate implementation.
48 CloudPrintURLFetcher::ResponseAction HandleJSONData(
49 const net::URLFetcher* source,
50 const GURL& url,
51 base::DictionaryValue* json_data,
52 bool succeeded) override;
53 CloudPrintURLFetcher::ResponseAction OnRequestAuthError() override;
54 std::string GetAuthHeader() override;
56 base::Time start_time() const {
57 return start_time_;
60 private:
61 friend class base::RefCountedThreadSafe<JobStatusUpdater>;
62 ~JobStatusUpdater() override;
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);
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).
83 typedef JobStatusUpdater::Delegate JobStatusUpdaterDelegate;
85 } // namespace cloud_print
87 #endif // CHROME_SERVICE_CLOUD_PRINT_JOB_STATUS_UPDATER_H_