OLD | NEW |
(Empty) | |
| 1 // Copyright 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_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_BACKEND
_H_ |
| 6 #define CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_BACKEND
_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <set> |
| 10 |
| 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/observer_list.h" |
| 14 #include "base/threading/thread.h" |
| 15 #include "chrome/browser/chrome_to_mobile/common/cloud_print_request.h" |
| 16 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_print_to_phon
e_jobs_fetcher.h" |
| 17 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_devic
e_manager.h" |
| 18 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_job_o
bserver.h" |
| 19 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_snapshots_fet
cher.h" |
| 20 #include "googleurl/src/gurl.h" |
| 21 #include "net/url_request/url_request_context_getter.h" |
| 22 |
| 23 class OAuth2TokenService; |
| 24 |
| 25 namespace chrome_to_mobile_receive { |
| 26 |
| 27 // Abstract class for chrome-to-mobile frontend. |
| 28 class ChromeToMobileReceiveFrontend { |
| 29 public: |
| 30 // Callback of |ChromeToMobileReceiveBackend::StartDevice()| when the |
| 31 // |ChromeToMobileReceiveBackend| has started the device. |
| 32 virtual void OnStartDeviceComplete(bool success, |
| 33 std::string printer_id) = 0; |
| 34 // Callback of |ChromeToMobileReceiveBackend::StopDevice()| when the |
| 35 // |ChromeToMobileReceiveBackend| has stopped the device. |
| 36 virtual void OnStopDeviceComplete(bool success) = 0; |
| 37 // Callback of |ChromeToMobileReceiveBackend::CancelAllPendingOperations()| |
| 38 // when the |ChromeToMobileReceiveBackend| has cancelled all the pending |
| 39 // operations. |
| 40 virtual void OnCancelAllPendingOperationsComplete() = 0; |
| 41 // Called when there is cloud print authentication error. |
| 42 virtual void OnCloudPrintAuthError() = 0; |
| 43 // Called when there is oauth token fetch failure. |
| 44 virtual void OnOAuth2AccessTokenFetchError() = 0; |
| 45 // Returns the observers that observe job fetch status. |
| 46 virtual ObserverList<ChromeToMobileReceiveJobObserver>* GetJobObservers() = 0; |
| 47 }; |
| 48 |
| 49 // This class provides |ChromeToMobileReceiveFrontend| APIs to start / stop the |
| 50 // device to receive chrome-to-mobile jobs and APIs to handle chrome-to-mobile |
| 51 // jobs. |
| 52 // |
| 53 // It accepts method calls in the frontend thread where an instance of this |
| 54 // class is created; the requests can be handled in a designated thread. The |
| 55 // frontend is called back in the frontend thread. |
| 56 class ChromeToMobileReceiveBackend |
| 57 : public base::RefCountedThreadSafe<ChromeToMobileReceiveBackend>, |
| 58 public chrome_to_mobile::CloudPrintRequest::Delegate, |
| 59 public ChromeToMobileReceiveDeviceManager::Delegate, |
| 60 public ChromeToMobileSnapshotsFetcher::Consumer, |
| 61 public ChromeToMobilePrintToPhoneJobsFetcher::Consumer { |
| 62 public: |
| 63 // Constructs an instant to handle chrome-to-mobile jobs at the cloud print |
| 64 // server |cloud_print_server_url|. The communication happens in the |
| 65 // context given by |request_context_getter|. Authentication is done by |
| 66 // |oauth2_token_service|. |
| 67 ChromeToMobileReceiveBackend( |
| 68 ChromeToMobileReceiveFrontend* frontend, |
| 69 const GURL& cloud_print_server_url, |
| 70 net::URLRequestContextGetter* request_context_getter, |
| 71 OAuth2TokenService* oauth2_token_service); |
| 72 |
| 73 // Stops the device with |printer_id|; the device will be no longer available |
| 74 // to receive chrome-to-mobile jobs. |
| 75 // This method needs authentication and a request that fetches oauth2 access |
| 76 // token using |oauth2_token_service_| has been sent before this method |
| 77 // returns. |
| 78 virtual void StopDevice(const std::string& printer_id); |
| 79 // Starts the device to receive chrome-to-mobile jobs. It aims to associate |
| 80 // this device with a printer that is identified by |printer_id| if possible; |
| 81 // otherwise a new printer is created. The associated printer will have |
| 82 // |printer_tags|. |
| 83 // This method needs authentication. The requests are handled in a task posted |
| 84 // to a designated thread and thus the authentication state can be different |
| 85 // when the requests actually happen. If there is change in authentication, |
| 86 // |StopDevice()| can be called to cancel this request. |
| 87 virtual void StartDevice( |
| 88 const std::string& printer_id, |
| 89 const std::map<std::string, std::string>& printer_tags); |
| 90 // Fetches chrome-to-mobile jobs for printer identified by |printer_id|. |
| 91 // This method needs authentication. The requests are handled in a task posted |
| 92 // to a designated thread and thus the authentication state can be different |
| 93 // when the requests actually happen. If there is change in authentication, |
| 94 // |StopDevice()| can be called to cancel this request. |
| 95 virtual void FetchJobs(const std::string& printer_id); |
| 96 // Cancels all the in-flight requests. It does not need authentication. |
| 97 virtual void CancelAllPendingOperations(); |
| 98 |
| 99 // Called to start the designated thread for backend operations. |
| 100 virtual void Start(); |
| 101 // Called to shut down the designated thread for backend operations. |
| 102 virtual void ShutDown(); |
| 103 // Returns if the designated thread is running. |
| 104 virtual bool IsRunning() const; |
| 105 |
| 106 protected: |
| 107 virtual ~ChromeToMobileReceiveBackend(); |
| 108 |
| 109 // |ChromeToMobileReceiveDeviceManager::Delegate| |
| 110 virtual void OnStartDeviceComplete(ChromeToMobileReceiveDeviceManager* source) |
| 111 OVERRIDE; |
| 112 // |CloudPrintDeleteRequest::Delegate| |
| 113 virtual void OnRequestComplete(chrome_to_mobile::CloudPrintRequest* source) |
| 114 OVERRIDE; |
| 115 // |ChromeToMobileSnapshotsFetcher::Consumer| |
| 116 virtual void OnSnapshotUrlFetched(const std::string& snapshot_id, |
| 117 const std::string& original_url, |
| 118 const std::string& title, |
| 119 const bool& is_offline_data_expected, |
| 120 const std::string& create_time) OVERRIDE; |
| 121 virtual void OnSnapshotDataDownloadComplete(const std::string& snapshot_id, |
| 122 const bool& success, |
| 123 const std::string& data_mime_type, |
| 124 const std::string& data) OVERRIDE; |
| 125 virtual void OnSnapshotsFetchComplete(ChromeToMobileSnapshotsFetcher * source) |
| 126 OVERRIDE; |
| 127 // |ChromeToMobilePrintToPhoneJobsFetcher::Consumer| |
| 128 virtual void OnPrintToPhoneJobDownloadStart( |
| 129 const std::string& job_id, |
| 130 const std::string& title) OVERRIDE; |
| 131 virtual void OnPrintToPhoneJobDownloadComplete( |
| 132 const std::string& job_id, |
| 133 const bool& success, |
| 134 const std::string& data_mime_type, |
| 135 const std::string& data) OVERRIDE; |
| 136 virtual void OnPrintJobsFetchComplete( |
| 137 ChromeToMobilePrintToPhoneJobsFetcher* fetcher) OVERRIDE; |
| 138 |
| 139 private: |
| 140 friend class base::RefCountedThreadSafe<ChromeToMobileReceiveBackend>; |
| 141 |
| 142 // Method posted to |chrome_to_mobile_thread_| when API |StartDevice()| is |
| 143 // called in |frontend_loop_|. This method will trigger callback |
| 144 // |ChromeToMobileReceiveDeviceManager::Delegate::OnStartDeviceComplete()| in |
| 145 // |chrome_to_mobile_thread_|. |
| 146 void DoStartDevice( |
| 147 std::string printer_id, |
| 148 std::map<std::string, std::string> printer_tags); |
| 149 // Method posted to |chrome_to_mobile_thread_| when API |FetchJobs()| is |
| 150 // called in |frontend_loop_|. This method will trigger callbacks defined in |
| 151 // |ChromeToMobileSnapshotsFetcher::Consumer| and |
| 152 // |ChromeToMobilePrintToPhoneJobsFetcher::Consumer| in |
| 153 // |chrome_to_mobile_thread_|. |
| 154 void DoFetchJobs(std::string printer_id); |
| 155 // Method posted to |chrome_to_mobile_thread_| when API |
| 156 // |CancelAllPendingOperations()| is called in |frontend_loop_|. |
| 157 void DoCancelAllPendingOperations(); |
| 158 |
| 159 // Methods called on |frontend_loop_| to notified job state observers changes |
| 160 // in job status. |
| 161 void DoOnSnapshotUrlFetched(const std::string& snapshot_id, |
| 162 const std::string& original_url, |
| 163 const std::string& title, |
| 164 const bool& is_offline_data_expected, |
| 165 const std::string& create_time); |
| 166 void DoOnSnapshotDataDownloadComplete(const std::string& snapshot_id, |
| 167 const bool& success, |
| 168 const std::string& data_mime_type, |
| 169 const std::string& data); |
| 170 void DoOnPrintToPhoneJobDownloadStart(const std::string& job_id, |
| 171 const std::string& title); |
| 172 void DoOnPrintToPhoneJobDownloadComplete( |
| 173 const std::string& job_id, |
| 174 const bool& success, |
| 175 const std::string& data_mime_type, |
| 176 const std::string& data); |
| 177 |
| 178 // A task to be posted to stops the chrome_to_mobile thread. |
| 179 void StopThread(); |
| 180 |
| 181 // Handles cloud print auth error. |
| 182 void HandleCloudPrintAuthError(); |
| 183 // Handles failure when fetching OAuth2 access token. |
| 184 void HandleGetOAuth2AccessTokenFailure(); |
| 185 // Cancels all the requests on the |chrome_to_mobile_thread_|; note this |
| 186 // method should run in |chrome_to_mobile_thread_|. |
| 187 void CancelAllPendingRequestsOnBackendThreads(); |
| 188 // Helper methods for posting tasks. |
| 189 void PostToChromeToMobileThread(const base::Closure& task); |
| 190 void PostToFrontendThread(const base::Closure& task); |
| 191 |
| 192 ChromeToMobileReceiveFrontend* const frontend_; |
| 193 // The message loop where the communication to |frontend_| should happen. |
| 194 MessageLoop* const frontend_loop_; |
| 195 // Thread where the chrome-to-mobile operations happen. |
| 196 base::Thread chrome_to_mobile_thread_; |
| 197 const GURL cloud_print_server_url_; |
| 198 net::URLRequestContextGetter* const request_context_getter_; |
| 199 OAuth2TokenService* const oauth2_token_service_; |
| 200 |
| 201 // Request to stop the device; it should be accessed in |frontend_loop_|. |
| 202 scoped_ptr<chrome_to_mobile::CloudPrintRequest> stop_device_request_; |
| 203 // Backend requests that should be accessed in |chrome_to_mobile_thread_|. |
| 204 scoped_ptr<ChromeToMobileReceiveDeviceManager> device_manager_; |
| 205 scoped_ptr<ChromeToMobileSnapshotsFetcher> snapshots_fetcher_; |
| 206 scoped_ptr<ChromeToMobilePrintToPhoneJobsFetcher> print_jobs_fetcher_; |
| 207 |
| 208 DISALLOW_COPY_AND_ASSIGN(ChromeToMobileReceiveBackend); |
| 209 }; |
| 210 |
| 211 } // namespace chrome_to_mobile_receive |
| 212 |
| 213 #endif // CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_BACK
END_H_ |
OLD | NEW |