Index: chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_print_to_phone_jobs_fetcher.h |
diff --git a/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_print_to_phone_jobs_fetcher.h b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_print_to_phone_jobs_fetcher.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..534101ec7bb07baa09cad8278d0d93bcebc6ed63 |
--- /dev/null |
+++ b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_print_to_phone_jobs_fetcher.h |
@@ -0,0 +1,128 @@ |
+// Copyright 2012 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. |
+ |
+#ifndef CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_PRINT_TO_PHONE_JOBS_FETCHER_H_ |
+#define CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_PRINT_TO_PHONE_JOBS_FETCHER_H_ |
+ |
+#include <string> |
+ |
+#include "base/threading/non_thread_safe.h" |
+#include "chrome/browser/chrome_to_mobile/common/cloud_print_request.h" |
+#include "googleurl/src/gurl.h" |
+ |
+namespace chrome_to_mobile_receive { |
+ |
+// This class fetches pending print-to-phone jobs for the chrome-to-mobile |
+// feature. |
+// |
+// To use it, construct an instance |fetcher| and call |fetcher->Fetch()|. The |
+// constructor takes a |consumer| of type |
+// |ChromeToMobilePrintToPhoneJobsFetcher::Consumer|, which is called back when |
+// a job download starts or completes. It guarantees the following: |
+// |
+// for any |std::string| |str|, if |
+// |consumer->OnPrintToPhoneJobDownloadStart()| is invoked with |
+// |job_id| = |str| at time |t|, then there exists time |t'| > |t| such as |
+// |consumer->OnPrintToPhoneJobDownloadComplete()| is invoked with |
+// |job_id| = |str| at time |t'|. |
+// |
+// No callback after the |fetcher| is destroyed. |
+// Note the above condition is still guaranteed in this case and if |
+// |consumer->OnPrintToPhoneJobDownloadComplete()| has not been called when |
+// |fetcher| is destroyed, it will be invoked with !|success|. |
+// |
+// Method |fetcher->Fetch()| can be called multiple times to fetch newly |
+// arriving jobs. |
+// |
+// This class is not thread safe. |fetcher->Fetch()| should be called in the |
+// same thread where |fetcher| is created. |consumer|'s callbacks are also |
+// called in the same thread. |
+class ChromeToMobilePrintToPhoneJobsFetcher |
+ : public base::NonThreadSafe, |
+ public chrome_to_mobile::CloudPrintRequest::Delegate { |
+ public: |
+ class Consumer { |
+ public: |
+ // This method is called by |ChromeToMobilePrintToPhoneJobsFetcher| when it |
+ // starts downloading a print-to-phone job. |
+ virtual void OnPrintToPhoneJobDownloadStart( |
+ const std::string& job_id, |
+ const std::string& title) = 0; |
+ // This method is called by |ChromeToMobilePrintToPhoneJobsFetcher| when it |
+ // completes downloading a print-to-phone job. |
+ virtual void OnPrintToPhoneJobDownloadComplete( |
+ const std::string& job_id, |
+ const bool& success, |
+ const std::string& data_mime_type, |
+ const std::string& data) = 0; |
+ // This method is called by |ChromeToMobilePrintToPhoneJobsFetcher| when it |
+ // finishes processing all the |Fetch()| invocations. Once this method is |
+ // called, no |OnPrintToPhoneJobDownloadStart()| or |
+ // |OnPrintToPhoneJobDownloadComplete()| will be called before |
+ // |fetcher->Fetch()| is called again. |
+ virtual void OnPrintJobsFetchComplete( |
+ ChromeToMobilePrintToPhoneJobsFetcher* fetcher) = 0; |
+ }; |
+ |
+ // Constructs an instance to fetch print-to-phone jobs for the printer that is |
+ // identified by |printer_id| at cloud print server |cloud_print_server_url| |
+ // using the cloud print request settings given by |settins|. |consumer| will |
+ // be called back when job downloading status changes. |
+ ChromeToMobilePrintToPhoneJobsFetcher( |
+ const GURL& cloud_print_server_url, |
+ const std::string& printer_id, |
+ const chrome_to_mobile::CloudPrintRequest::Settings& settings, |
+ ChromeToMobilePrintToPhoneJobsFetcher::Consumer* consumer); |
+ virtual ~ChromeToMobilePrintToPhoneJobsFetcher(); |
+ |
+ // Starts fetching pending print-to-phone jobs. |
+ virtual void Fetch(); |
+ |
+ // Returns if it fails to fetch an oauth2 access token required to |
+ // authenticate the requests. |
+ virtual bool HasOAuth2AccessTokenFailure() const; |
+ // Returns if any authentication error is returned by cloud print server. |
+ virtual bool HasCloudPrintAuthError() const; |
+ |
+ protected: |
+ // |CloudPrintRequest::Delegate| |
+ virtual void OnRequestComplete(chrome_to_mobile::CloudPrintRequest* source) |
+ OVERRIDE; |
+ |
+ private: |
+ // Map from a job id to a |chrome_to_mobile::CloudPrintRequest|. |
+ typedef std::map<std::string, chrome_to_mobile::CloudPrintRequest*> |
+ RequestMap; |
+ |
+ // Methods that process a |chrome_to_mobile::CloudPrintRequest| when it |
+ // completes. These methods return |true| if it has processed |source|. |
+ bool HandleFetchComplete(chrome_to_mobile::CloudPrintRequest* source); |
+ bool HandleDownloadComplete(chrome_to_mobile::CloudPrintRequest* source); |
+ bool HandleJobUpdateComplete(chrome_to_mobile::CloudPrintRequest* source); |
+ |
+ // Returns if all the |Fetch()| invocations have been processed. |
+ bool HasProcessedAllFetchInvocations() const; |
+ |
+ // Cancels all the pending requests. |
+ void CancelAllRequests(); |
+ |
+ // Variables passed into the constructor. |
+ const GURL cloud_print_server_url_; |
+ const std::string printer_id_; |
+ const chrome_to_mobile::CloudPrintRequest::Settings settings_; |
+ ChromeToMobilePrintToPhoneJobsFetcher::Consumer* const consumer_; |
+ |
+ // Variables that keep the state of job fetching. |
+ scoped_ptr<chrome_to_mobile::CloudPrintRequest> fetch_request_; |
+ RequestMap pending_download_requests_; |
+ RequestMap pending_job_update_requests_; |
+ bool has_oauth2_token_fetch_failure_; |
+ bool has_cloud_print_auth_error_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChromeToMobilePrintToPhoneJobsFetcher); |
+}; |
+ |
+} // namespace chrome_to_mobile_receive |
+ |
+#endif // CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_PRINT_TO_PHONE_JOBS_FETCHER_H_ |