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_SERVICE
_H_ |
| 6 #define CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_SERVICE
_H_ |
| 7 |
| 8 #include <map> |
| 9 #include <string> |
| 10 #include <vector> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_backe
nd.h" |
| 16 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_job_o
bserver.h" |
| 17 #include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_servi
ce_observer.h" |
| 18 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 19 #include "content/public/browser/notification_observer.h" |
| 20 #include "content/public/browser/notification_registrar.h" |
| 21 |
| 22 namespace chrome_to_mobile_receive { |
| 23 class ChromeToMobileReceivingDeviceInfoHandler; |
| 24 } // namespace chrome_to_mobile_receive |
| 25 class Profile; |
| 26 |
| 27 // Class for the service that receives chrome-to-mobile jobs. |
| 28 class ChromeToMobileReceiveService |
| 29 : public ProfileKeyedService, |
| 30 public chrome_to_mobile_receive::ChromeToMobileReceiveFrontend, |
| 31 public content::NotificationObserver { |
| 32 public: |
| 33 // Enum for the type of failures observed by this service. When failures |
| 34 // defined here are observed, the chrome-to-mobile preference in profile will |
| 35 // be turned off. |
| 36 enum FailureType { |
| 37 // Failure in getting oauth token. |
| 38 kOAuthTokenFetchFailure, |
| 39 // Failure in getting the required device information. |
| 40 kDeviceInfoFetchFailure, |
| 41 // Failure in starting the device to receive chrome-to-mobile service. |
| 42 kStartDeviceFailure, |
| 43 // Authentication errors received from cloud print server. |
| 44 kClouPrintAuthError, |
| 45 // No failure. |
| 46 kNoFailure, |
| 47 }; |
| 48 |
| 49 explicit ChromeToMobileReceiveService(Profile* profile); |
| 50 virtual ~ChromeToMobileReceiveService(); |
| 51 |
| 52 // Authentication, profile preference and device information are required to |
| 53 // start receiving chrome-to-mobile jobs. The three methods below can be used |
| 54 // to pass in such information, which will also start fetching pending jobs if |
| 55 // all the required information is ready. |
| 56 // Note users can act on authentication (by signing in or out) and profile |
| 57 // preference (via an UI such as Settings panel or a flag); device information |
| 58 // fetch should be started by the application. |
| 59 // |
| 60 // |NotificationObserver| on authentication status changes. |
| 61 virtual void Observe(int type, |
| 62 const content::NotificationSource& source, |
| 63 const content::NotificationDetails& details) OVERRIDE; |
| 64 // Changes the chrome-to-mobile option in the profile preference. |
| 65 void TurnOnPreference(bool on); |
| 66 // Passes in device information required by the cloud print server as |
| 67 // a map. This will be the printer tags sent to the cloud print server. |
| 68 void SetDeviceTags(const std::map<std::string, std::string>& printer_tags); |
| 69 |
| 70 // Fetches all the pending chrome-to-mobile jobs. It assumes the requirement |
| 71 // on authentication and profile preference are met; it fetches device |
| 72 // information if needed. It returns false if authentication or profile |
| 73 // preference is not ready. |
| 74 bool FetchPendingJobs(); |
| 75 |
| 76 // Called when fail to get device information. |
| 77 void OnFailToFetchDeviceInfo(); |
| 78 |
| 79 // Returns true if chrome-to-mobile jobs can be received if the |
| 80 // chrome-to-mobile option is on in the profile preference. This requires |
| 81 // authentication and probably also some requirements on the device; for |
| 82 // example, push notification needs to be enabled to receive chrome-to-mobile |
| 83 // jobs on an iOS device. |
| 84 // |
| 85 // This method can be used to decide if the chrome-to-mobile option should be |
| 86 // enabled for the user to turn on or off in a UI. |
| 87 bool CanGetStartedIfTurnedOn() const; |
| 88 // Returns true if the chrome-to-mobile option is on in the profile |
| 89 // preference. |
| 90 bool IsTurnedOnInPreference() const; |
| 91 // Returns true if the authentication information is ready for receiving |
| 92 // chrome-to-mobile jobs. |
| 93 bool IsAuthenticationReady() const; |
| 94 // Returns the failure status of this service. |
| 95 FailureType GetFailureType() const; |
| 96 |
| 97 // Adds and removes an observer for job fetching events. |
| 98 void AddJobObserver(ChromeToMobileReceiveJobObserver* observer); |
| 99 void RemoveJobObserver(ChromeToMobileReceiveJobObserver* observer); |
| 100 // Adds and removes an observer for service status changes. |
| 101 void AddServiceObserver(ChromeToMobileReceiveServiceObserver* observer); |
| 102 void RemoveServiceObserver(ChromeToMobileReceiveServiceObserver* observer); |
| 103 |
| 104 protected: |
| 105 // |ChromeToMobileReceiveFrontend| |
| 106 virtual void OnStartDeviceComplete(bool success, |
| 107 std::string printer_id) OVERRIDE; |
| 108 virtual void OnStopDeviceComplete(bool success) OVERRIDE; |
| 109 virtual void OnCancelAllPendingOperationsComplete() OVERRIDE; |
| 110 virtual void OnCloudPrintAuthError() OVERRIDE; |
| 111 virtual void OnOAuth2AccessTokenFetchError() OVERRIDE; |
| 112 virtual ObserverList<ChromeToMobileReceiveJobObserver>* GetJobObservers() |
| 113 OVERRIDE; |
| 114 |
| 115 private: |
| 116 friend class ChromeToMobileReceiveServiceTest; |
| 117 |
| 118 // Starts the device and fetches all the pending jobs based on the assumption |
| 119 // that all the required information (authentication, profile preference and |
| 120 // device information) is ready. |
| 121 void StartDeviceAndFetchAll(); |
| 122 // Turns off the chrome-to-mobile option in the preference due to failure. |
| 123 void TurnOffPreferenceDueToFailure(FailureType failure_type); |
| 124 // Informs the service observers on the service status changes. |
| 125 void UpdateServiceObservers(); |
| 126 |
| 127 // Methods that call |backend_|'s APIs and update this service's |status_| and |
| 128 // variables that schedules tasks when |status_| changes (that is, variables |
| 129 // |should_start_device_when_not_started_| and |
| 130 // |should_fetch_jobs_when_started_|, see below). |
| 131 // |
| 132 // Starts the device. |
| 133 void StartDevice(); |
| 134 // Stops the device. This will cancel all the pending operations and delete |
| 135 // the printer registered for this device. |
| 136 void StopDevice(); |
| 137 // Cancels all the pending operations. |
| 138 // Note it is differnet from |StopDevice()| which also deletes the printer and |
| 139 // requires communication with cloud print server. |
| 140 // This method is used in case of failure before turning off the |
| 141 // chrome-to-mobile option in profile preference; |StopDevice()| is not used |
| 142 // in this case as the same failure could happen when stopping the device. |
| 143 void CancelAllPendingOperations(); |
| 144 // Fetch pending jobs. |
| 145 void FetchJobs(); |
| 146 // Sets the |status_| to be |kStarted| and performs scheduled actions. |
| 147 void SetStatusStarted(); |
| 148 // Sets the |status_| to be |kNotStarted| and performs scheduled actions. |
| 149 void SetStatusNotStarted(); |
| 150 |
| 151 // Returns true if the service is ready to be started, that is, the |
| 152 // requirements on authentication, profile preference and device information |
| 153 // are met. |
| 154 bool IsServiceReadyToBeStarted() const; |
| 155 // Returns if the device information is ready. |
| 156 bool IsDeviceInfoReady() const; |
| 157 // Returns true if the service should start fetching device information. |
| 158 bool ShouldFetchDeviceInformation() const; |
| 159 |
| 160 // The key of this service. |
| 161 Profile* const profile_; |
| 162 // Backend that handles the chrome_to_mobile receiving operations. |
| 163 scoped_refptr<chrome_to_mobile_receive::ChromeToMobileReceiveBackend> |
| 164 backend_; |
| 165 // Handles the required device information. |
| 166 scoped_ptr<chrome_to_mobile_receive::ChromeToMobileReceivingDeviceInfoHandler> |
| 167 device_info_handler_; |
| 168 content::NotificationRegistrar registrar_; |
| 169 // Observers on the job status. |
| 170 ObserverList<ChromeToMobileReceiveJobObserver> job_observers_; |
| 171 // Observers on the service status. |
| 172 ObserverList<ChromeToMobileReceiveServiceObserver> service_observers_; |
| 173 |
| 174 // The id of the printer that is associated with this device to receive |
| 175 // chrome-to-mobile jobs. |
| 176 std::string printer_id_; |
| 177 // Map from printer tag name to tag value required by cloud print server when |
| 178 // updating or registering this device as a printer. |
| 179 std::map<std::string, std::string> printer_tags_; |
| 180 // Returns true if the printer has been started since the last time the |
| 181 // authentication gets ready. |
| 182 bool authenticated_; |
| 183 // The failure status of this service. |
| 184 FailureType failure_type_; |
| 185 |
| 186 // The status of the service, which are changed when |backend_| operations or |
| 187 // the callbacks defined in |ChromeToMobileReceiveFrontend| are called. |
| 188 enum ServiceStatus { |
| 189 kNotStarted, // The device has not been started. |
| 190 kStarting, // The device is being started. |
| 191 kStarted, // The device has been started. |
| 192 kStopping // The device is being stopped. |
| 193 } status_; |
| 194 // Booleans to schedule backend operations in status |kNotStarted| and |
| 195 // |kStarted|. |
| 196 bool should_start_device_when_not_started_; |
| 197 bool should_fetch_jobs_when_started_; |
| 198 |
| 199 DISALLOW_COPY_AND_ASSIGN(ChromeToMobileReceiveService); |
| 200 }; |
| 201 |
| 202 #endif // CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_SERV
ICE_H_ |
OLD | NEW |