Index: chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_backend.h |
diff --git a/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_backend.h b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_backend.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2e72e38353a531b8b4be480384aaf6d284e78691 |
--- /dev/null |
+++ b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_backend.h |
@@ -0,0 +1,213 @@ |
+// 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_RECEIVE_BACKEND_H_ |
+#define CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_BACKEND_H_ |
+ |
+#include <map> |
+#include <set> |
+ |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "base/observer_list.h" |
+#include "base/threading/thread.h" |
+#include "chrome/browser/chrome_to_mobile/common/cloud_print_request.h" |
+#include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_print_to_phone_jobs_fetcher.h" |
+#include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_device_manager.h" |
+#include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_job_observer.h" |
+#include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_snapshots_fetcher.h" |
+#include "googleurl/src/gurl.h" |
+#include "net/url_request/url_request_context_getter.h" |
+ |
+class OAuth2TokenService; |
+ |
+namespace chrome_to_mobile_receive { |
+ |
+// Abstract class for chrome-to-mobile frontend. |
+class ChromeToMobileReceiveFrontend { |
+ public: |
+ // Callback of |ChromeToMobileReceiveBackend::StartDevice()| when the |
+ // |ChromeToMobileReceiveBackend| has started the device. |
+ virtual void OnStartDeviceComplete(bool success, |
+ std::string printer_id) = 0; |
+ // Callback of |ChromeToMobileReceiveBackend::StopDevice()| when the |
+ // |ChromeToMobileReceiveBackend| has stopped the device. |
+ virtual void OnStopDeviceComplete(bool success) = 0; |
+ // Callback of |ChromeToMobileReceiveBackend::CancelAllPendingOperations()| |
+ // when the |ChromeToMobileReceiveBackend| has cancelled all the pending |
+ // operations. |
+ virtual void OnCancelAllPendingOperationsComplete() = 0; |
+ // Called when there is cloud print authentication error. |
+ virtual void OnCloudPrintAuthError() = 0; |
+ // Called when there is oauth token fetch failure. |
+ virtual void OnOAuth2AccessTokenFetchError() = 0; |
+ // Returns the observers that observe job fetch status. |
+ virtual ObserverList<ChromeToMobileReceiveJobObserver>* GetJobObservers() = 0; |
+}; |
+ |
+// This class provides |ChromeToMobileReceiveFrontend| APIs to start / stop the |
+// device to receive chrome-to-mobile jobs and APIs to handle chrome-to-mobile |
+// jobs. |
+// |
+// It accepts method calls in the frontend thread where an instance of this |
+// class is created; the requests can be handled in a designated thread. The |
+// frontend is called back in the frontend thread. |
+class ChromeToMobileReceiveBackend |
+ : public base::RefCountedThreadSafe<ChromeToMobileReceiveBackend>, |
+ public chrome_to_mobile::CloudPrintRequest::Delegate, |
+ public ChromeToMobileReceiveDeviceManager::Delegate, |
+ public ChromeToMobileSnapshotsFetcher::Consumer, |
+ public ChromeToMobilePrintToPhoneJobsFetcher::Consumer { |
+ public: |
+ // Constructs an instant to handle chrome-to-mobile jobs at the cloud print |
+ // server |cloud_print_server_url|. The communication happens in the |
+ // context given by |request_context_getter|. Authentication is done by |
+ // |oauth2_token_service|. |
+ ChromeToMobileReceiveBackend( |
+ ChromeToMobileReceiveFrontend* frontend, |
+ const GURL& cloud_print_server_url, |
+ net::URLRequestContextGetter* request_context_getter, |
+ OAuth2TokenService* oauth2_token_service); |
+ |
+ // Stops the device with |printer_id|; the device will be no longer available |
+ // to receive chrome-to-mobile jobs. |
+ // This method needs authentication and a request that fetches oauth2 access |
+ // token using |oauth2_token_service_| has been sent before this method |
+ // returns. |
+ virtual void StopDevice(const std::string& printer_id); |
+ // Starts the device to receive chrome-to-mobile jobs. It aims to associate |
+ // this device with a printer that is identified by |printer_id| if possible; |
+ // otherwise a new printer is created. The associated printer will have |
+ // |printer_tags|. |
+ // This method needs authentication. The requests are handled in a task posted |
+ // to a designated thread and thus the authentication state can be different |
+ // when the requests actually happen. If there is change in authentication, |
+ // |StopDevice()| can be called to cancel this request. |
+ virtual void StartDevice( |
+ const std::string& printer_id, |
+ const std::map<std::string, std::string>& printer_tags); |
+ // Fetches chrome-to-mobile jobs for printer identified by |printer_id|. |
+ // This method needs authentication. The requests are handled in a task posted |
+ // to a designated thread and thus the authentication state can be different |
+ // when the requests actually happen. If there is change in authentication, |
+ // |StopDevice()| can be called to cancel this request. |
+ virtual void FetchJobs(const std::string& printer_id); |
+ // Cancels all the in-flight requests. It does not need authentication. |
+ virtual void CancelAllPendingOperations(); |
+ |
+ // Called to start the designated thread for backend operations. |
+ virtual void Start(); |
+ // Called to shut down the designated thread for backend operations. |
+ virtual void ShutDown(); |
+ // Returns if the designated thread is running. |
+ virtual bool IsRunning() const; |
+ |
+ protected: |
+ virtual ~ChromeToMobileReceiveBackend(); |
+ |
+ // |ChromeToMobileReceiveDeviceManager::Delegate| |
+ virtual void OnStartDeviceComplete(ChromeToMobileReceiveDeviceManager* source) |
+ OVERRIDE; |
+ // |CloudPrintDeleteRequest::Delegate| |
+ virtual void OnRequestComplete(chrome_to_mobile::CloudPrintRequest* source) |
+ OVERRIDE; |
+ // |ChromeToMobileSnapshotsFetcher::Consumer| |
+ virtual void OnSnapshotUrlFetched(const std::string& snapshot_id, |
+ const std::string& original_url, |
+ const std::string& title, |
+ const bool& is_offline_data_expected, |
+ const std::string& create_time) OVERRIDE; |
+ virtual void OnSnapshotDataDownloadComplete(const std::string& snapshot_id, |
+ const bool& success, |
+ const std::string& data_mime_type, |
+ const std::string& data) OVERRIDE; |
+ virtual void OnSnapshotsFetchComplete(ChromeToMobileSnapshotsFetcher * source) |
+ OVERRIDE; |
+ // |ChromeToMobilePrintToPhoneJobsFetcher::Consumer| |
+ virtual void OnPrintToPhoneJobDownloadStart( |
+ const std::string& job_id, |
+ const std::string& title) OVERRIDE; |
+ virtual void OnPrintToPhoneJobDownloadComplete( |
+ const std::string& job_id, |
+ const bool& success, |
+ const std::string& data_mime_type, |
+ const std::string& data) OVERRIDE; |
+ virtual void OnPrintJobsFetchComplete( |
+ ChromeToMobilePrintToPhoneJobsFetcher* fetcher) OVERRIDE; |
+ |
+ private: |
+ friend class base::RefCountedThreadSafe<ChromeToMobileReceiveBackend>; |
+ |
+ // Method posted to |chrome_to_mobile_thread_| when API |StartDevice()| is |
+ // called in |frontend_loop_|. This method will trigger callback |
+ // |ChromeToMobileReceiveDeviceManager::Delegate::OnStartDeviceComplete()| in |
+ // |chrome_to_mobile_thread_|. |
+ void DoStartDevice( |
+ std::string printer_id, |
+ std::map<std::string, std::string> printer_tags); |
+ // Method posted to |chrome_to_mobile_thread_| when API |FetchJobs()| is |
+ // called in |frontend_loop_|. This method will trigger callbacks defined in |
+ // |ChromeToMobileSnapshotsFetcher::Consumer| and |
+ // |ChromeToMobilePrintToPhoneJobsFetcher::Consumer| in |
+ // |chrome_to_mobile_thread_|. |
+ void DoFetchJobs(std::string printer_id); |
+ // Method posted to |chrome_to_mobile_thread_| when API |
+ // |CancelAllPendingOperations()| is called in |frontend_loop_|. |
+ void DoCancelAllPendingOperations(); |
+ |
+ // Methods called on |frontend_loop_| to notified job state observers changes |
+ // in job status. |
+ void DoOnSnapshotUrlFetched(const std::string& snapshot_id, |
+ const std::string& original_url, |
+ const std::string& title, |
+ const bool& is_offline_data_expected, |
+ const std::string& create_time); |
+ void DoOnSnapshotDataDownloadComplete(const std::string& snapshot_id, |
+ const bool& success, |
+ const std::string& data_mime_type, |
+ const std::string& data); |
+ void DoOnPrintToPhoneJobDownloadStart(const std::string& job_id, |
+ const std::string& title); |
+ void DoOnPrintToPhoneJobDownloadComplete( |
+ const std::string& job_id, |
+ const bool& success, |
+ const std::string& data_mime_type, |
+ const std::string& data); |
+ |
+ // A task to be posted to stops the chrome_to_mobile thread. |
+ void StopThread(); |
+ |
+ // Handles cloud print auth error. |
+ void HandleCloudPrintAuthError(); |
+ // Handles failure when fetching OAuth2 access token. |
+ void HandleGetOAuth2AccessTokenFailure(); |
+ // Cancels all the requests on the |chrome_to_mobile_thread_|; note this |
+ // method should run in |chrome_to_mobile_thread_|. |
+ void CancelAllPendingRequestsOnBackendThreads(); |
+ // Helper methods for posting tasks. |
+ void PostToChromeToMobileThread(const base::Closure& task); |
+ void PostToFrontendThread(const base::Closure& task); |
+ |
+ ChromeToMobileReceiveFrontend* const frontend_; |
+ // The message loop where the communication to |frontend_| should happen. |
+ MessageLoop* const frontend_loop_; |
+ // Thread where the chrome-to-mobile operations happen. |
+ base::Thread chrome_to_mobile_thread_; |
+ const GURL cloud_print_server_url_; |
+ net::URLRequestContextGetter* const request_context_getter_; |
+ OAuth2TokenService* const oauth2_token_service_; |
+ |
+ // Request to stop the device; it should be accessed in |frontend_loop_|. |
+ scoped_ptr<chrome_to_mobile::CloudPrintRequest> stop_device_request_; |
+ // Backend requests that should be accessed in |chrome_to_mobile_thread_|. |
+ scoped_ptr<ChromeToMobileReceiveDeviceManager> device_manager_; |
+ scoped_ptr<ChromeToMobileSnapshotsFetcher> snapshots_fetcher_; |
+ scoped_ptr<ChromeToMobilePrintToPhoneJobsFetcher> print_jobs_fetcher_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChromeToMobileReceiveBackend); |
+}; |
+ |
+} // namespace chrome_to_mobile_receive |
+ |
+#endif // CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_BACKEND_H_ |