Index: chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_service.h |
diff --git a/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_service.h b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_service.h |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ae658adf7f205fcc9f7465fdea81fcc1789e97ba |
--- /dev/null |
+++ b/chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_service.h |
@@ -0,0 +1,202 @@ |
+// 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_SERVICE_H_ |
+#define CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_SERVICE_H_ |
+ |
+#include <map> |
+#include <string> |
+#include <vector> |
+ |
+#include "base/basictypes.h" |
+#include "base/memory/ref_counted.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "chrome/browser/chrome_to_mobile/receive/chrome_to_mobile_receive_backend.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_receive_service_observer.h" |
+#include "chrome/browser/profiles/profile_keyed_service.h" |
+#include "content/public/browser/notification_observer.h" |
+#include "content/public/browser/notification_registrar.h" |
+ |
+namespace chrome_to_mobile_receive { |
+class ChromeToMobileReceivingDeviceInfoHandler; |
+} // namespace chrome_to_mobile_receive |
+class Profile; |
+ |
+// Class for the service that receives chrome-to-mobile jobs. |
+class ChromeToMobileReceiveService |
+ : public ProfileKeyedService, |
+ public chrome_to_mobile_receive::ChromeToMobileReceiveFrontend, |
+ public content::NotificationObserver { |
+ public: |
+ // Enum for the type of failures observed by this service. When failures |
+ // defined here are observed, the chrome-to-mobile preference in profile will |
+ // be turned off. |
+ enum FailureType { |
+ // Failure in getting oauth token. |
+ kOAuthTokenFetchFailure, |
+ // Failure in getting the required device information. |
+ kDeviceInfoFetchFailure, |
+ // Failure in starting the device to receive chrome-to-mobile service. |
+ kStartDeviceFailure, |
+ // Authentication errors received from cloud print server. |
+ kClouPrintAuthError, |
+ // No failure. |
+ kNoFailure, |
+ }; |
+ |
+ explicit ChromeToMobileReceiveService(Profile* profile); |
+ virtual ~ChromeToMobileReceiveService(); |
+ |
+ // Authentication, profile preference and device information are required to |
+ // start receiving chrome-to-mobile jobs. The three methods below can be used |
+ // to pass in such information, which will also start fetching pending jobs if |
+ // all the required information is ready. |
+ // Note users can act on authentication (by signing in or out) and profile |
+ // preference (via an UI such as Settings panel or a flag); device information |
+ // fetch should be started by the application. |
+ // |
+ // |NotificationObserver| on authentication status changes. |
+ virtual void Observe(int type, |
+ const content::NotificationSource& source, |
+ const content::NotificationDetails& details) OVERRIDE; |
+ // Changes the chrome-to-mobile option in the profile preference. |
+ void TurnOnPreference(bool on); |
+ // Passes in device information required by the cloud print server as |
+ // a map. This will be the printer tags sent to the cloud print server. |
+ void SetDeviceTags(const std::map<std::string, std::string>& printer_tags); |
+ |
+ // Fetches all the pending chrome-to-mobile jobs. It assumes the requirement |
+ // on authentication and profile preference are met; it fetches device |
+ // information if needed. It returns false if authentication or profile |
+ // preference is not ready. |
+ bool FetchPendingJobs(); |
+ |
+ // Called when fail to get device information. |
+ void OnFailToFetchDeviceInfo(); |
+ |
+ // Returns true if chrome-to-mobile jobs can be received if the |
+ // chrome-to-mobile option is on in the profile preference. This requires |
+ // authentication and probably also some requirements on the device; for |
+ // example, push notification needs to be enabled to receive chrome-to-mobile |
+ // jobs on an iOS device. |
+ // |
+ // This method can be used to decide if the chrome-to-mobile option should be |
+ // enabled for the user to turn on or off in a UI. |
+ bool CanGetStartedIfTurnedOn() const; |
+ // Returns true if the chrome-to-mobile option is on in the profile |
+ // preference. |
+ bool IsTurnedOnInPreference() const; |
+ // Returns true if the authentication information is ready for receiving |
+ // chrome-to-mobile jobs. |
+ bool IsAuthenticationReady() const; |
+ // Returns the failure status of this service. |
+ FailureType GetFailureType() const; |
+ |
+ // Adds and removes an observer for job fetching events. |
+ void AddJobObserver(ChromeToMobileReceiveJobObserver* observer); |
+ void RemoveJobObserver(ChromeToMobileReceiveJobObserver* observer); |
+ // Adds and removes an observer for service status changes. |
+ void AddServiceObserver(ChromeToMobileReceiveServiceObserver* observer); |
+ void RemoveServiceObserver(ChromeToMobileReceiveServiceObserver* observer); |
+ |
+ protected: |
+ // |ChromeToMobileReceiveFrontend| |
+ virtual void OnStartDeviceComplete(bool success, |
+ std::string printer_id) OVERRIDE; |
+ virtual void OnStopDeviceComplete(bool success) OVERRIDE; |
+ virtual void OnCancelAllPendingOperationsComplete() OVERRIDE; |
+ virtual void OnCloudPrintAuthError() OVERRIDE; |
+ virtual void OnOAuth2AccessTokenFetchError() OVERRIDE; |
+ virtual ObserverList<ChromeToMobileReceiveJobObserver>* GetJobObservers() |
+ OVERRIDE; |
+ |
+ private: |
+ friend class ChromeToMobileReceiveServiceTest; |
+ |
+ // Starts the device and fetches all the pending jobs based on the assumption |
+ // that all the required information (authentication, profile preference and |
+ // device information) is ready. |
+ void StartDeviceAndFetchAll(); |
+ // Turns off the chrome-to-mobile option in the preference due to failure. |
+ void TurnOffPreferenceDueToFailure(FailureType failure_type); |
+ // Informs the service observers on the service status changes. |
+ void UpdateServiceObservers(); |
+ |
+ // Methods that call |backend_|'s APIs and update this service's |status_| and |
+ // variables that schedules tasks when |status_| changes (that is, variables |
+ // |should_start_device_when_not_started_| and |
+ // |should_fetch_jobs_when_started_|, see below). |
+ // |
+ // Starts the device. |
+ void StartDevice(); |
+ // Stops the device. This will cancel all the pending operations and delete |
+ // the printer registered for this device. |
+ void StopDevice(); |
+ // Cancels all the pending operations. |
+ // Note it is differnet from |StopDevice()| which also deletes the printer and |
+ // requires communication with cloud print server. |
+ // This method is used in case of failure before turning off the |
+ // chrome-to-mobile option in profile preference; |StopDevice()| is not used |
+ // in this case as the same failure could happen when stopping the device. |
+ void CancelAllPendingOperations(); |
+ // Fetch pending jobs. |
+ void FetchJobs(); |
+ // Sets the |status_| to be |kStarted| and performs scheduled actions. |
+ void SetStatusStarted(); |
+ // Sets the |status_| to be |kNotStarted| and performs scheduled actions. |
+ void SetStatusNotStarted(); |
+ |
+ // Returns true if the service is ready to be started, that is, the |
+ // requirements on authentication, profile preference and device information |
+ // are met. |
+ bool IsServiceReadyToBeStarted() const; |
+ // Returns if the device information is ready. |
+ bool IsDeviceInfoReady() const; |
+ // Returns true if the service should start fetching device information. |
+ bool ShouldFetchDeviceInformation() const; |
+ |
+ // The key of this service. |
+ Profile* const profile_; |
+ // Backend that handles the chrome_to_mobile receiving operations. |
+ scoped_refptr<chrome_to_mobile_receive::ChromeToMobileReceiveBackend> |
+ backend_; |
+ // Handles the required device information. |
+ scoped_ptr<chrome_to_mobile_receive::ChromeToMobileReceivingDeviceInfoHandler> |
+ device_info_handler_; |
+ content::NotificationRegistrar registrar_; |
+ // Observers on the job status. |
+ ObserverList<ChromeToMobileReceiveJobObserver> job_observers_; |
+ // Observers on the service status. |
+ ObserverList<ChromeToMobileReceiveServiceObserver> service_observers_; |
+ |
+ // The id of the printer that is associated with this device to receive |
+ // chrome-to-mobile jobs. |
+ std::string printer_id_; |
+ // Map from printer tag name to tag value required by cloud print server when |
+ // updating or registering this device as a printer. |
+ std::map<std::string, std::string> printer_tags_; |
+ // Returns true if the printer has been started since the last time the |
+ // authentication gets ready. |
+ bool authenticated_; |
+ // The failure status of this service. |
+ FailureType failure_type_; |
+ |
+ // The status of the service, which are changed when |backend_| operations or |
+ // the callbacks defined in |ChromeToMobileReceiveFrontend| are called. |
+ enum ServiceStatus { |
+ kNotStarted, // The device has not been started. |
+ kStarting, // The device is being started. |
+ kStarted, // The device has been started. |
+ kStopping // The device is being stopped. |
+ } status_; |
+ // Booleans to schedule backend operations in status |kNotStarted| and |
+ // |kStarted|. |
+ bool should_start_device_when_not_started_; |
+ bool should_fetch_jobs_when_started_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(ChromeToMobileReceiveService); |
+}; |
+ |
+#endif // CHROME_BROWSER_CHROME_TO_MOBILE_RECEIVE_CHROME_TO_MOBILE_RECEIVE_SERVICE_H_ |