| OLD | NEW | 
|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be | 
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. | 
| 4 | 4 | 
| 5 #ifndef CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ | 5 #ifndef CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ | 
| 6 #define CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ | 6 #define CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ | 
| 7 | 7 | 
| 8 #include <map> | 8 #include <map> | 
| 9 #include <set> | 9 #include <set> | 
|  | 10 #include <queue> | 
| 10 #include <string> | 11 #include <string> | 
| 11 #include <vector> | 12 #include <vector> | 
| 12 | 13 | 
| 13 #include "base/file_path.h" | 14 #include "base/file_path.h" | 
| 14 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" | 
| 15 #include "base/string16.h" | 16 #include "base/string16.h" | 
| 16 #include "base/timer.h" | 17 #include "base/timer.h" | 
| 17 #include "base/values.h" | 18 #include "base/values.h" | 
| 18 #include "chrome/browser/profiles/profile_keyed_service.h" | 19 #include "chrome/browser/profiles/profile_keyed_service.h" | 
| 19 #include "chrome/browser/sessions/session_id.h" | 20 #include "chrome/browser/sessions/session_id.h" | 
| 20 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | 21 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | 
| 21 #include "content/public/browser/notification_observer.h" | 22 #include "content/public/browser/notification_observer.h" | 
| 22 #include "content/public/browser/notification_registrar.h" | 23 #include "content/public/browser/notification_registrar.h" | 
| 23 #include "googleurl/src/gurl.h" | 24 #include "googleurl/src/gurl.h" | 
| 24 #include "net/url_request/url_fetcher_delegate.h" | 25 #include "net/url_request/url_fetcher_delegate.h" | 
|  | 26 #include "sync/notifier/sync_notifier_observer.h" | 
| 25 | 27 | 
| 26 class OAuth2AccessTokenFetcher; | 28 class OAuth2AccessTokenFetcher; | 
| 27 class Browser; | 29 class Browser; | 
| 28 class CloudPrintURL; | 30 class CloudPrintURL; | 
| 29 class MockChromeToMobileService; | 31 class MockChromeToMobileService; | 
| 30 class PrefService; | 32 class PrefService; | 
| 31 class Profile; | 33 class Profile; | 
| 32 | 34 | 
| 33 namespace net { | 35 namespace net { | 
| 34 class URLFetcher; | 36 class URLFetcher; | 
| 35 } | 37 } | 
| 36 | 38 | 
| 37 // ChromeToMobileService connects to the cloud print service to enumerate | 39 // ChromeToMobileService connects to the cloud print service to enumerate | 
| 38 // compatible mobiles owned by its profile and send URLs and MHTML snapshots. | 40 // compatible mobiles owned by its profile and send URLs and MHTML snapshots. | 
| 39 class ChromeToMobileService : public ProfileKeyedService, | 41 class ChromeToMobileService : public ProfileKeyedService, | 
| 40                               public net::URLFetcherDelegate, | 42                               public net::URLFetcherDelegate, | 
| 41                               public content::NotificationObserver, | 43                               public content::NotificationObserver, | 
| 42                               public OAuth2AccessTokenConsumer { | 44                               public OAuth2AccessTokenConsumer, | 
|  | 45                               public syncer::SyncNotifierObserver { | 
| 43  public: | 46  public: | 
| 44   class Observer { | 47   class Observer { | 
| 45    public: | 48    public: | 
| 46     virtual ~Observer(); | 49     virtual ~Observer(); | 
| 47 | 50 | 
| 48     // Called on generation of the page's MHTML snapshot. | 51     // Called on generation of the page's MHTML snapshot. | 
| 49     virtual void SnapshotGenerated(const FilePath& path, int64 bytes) = 0; | 52     virtual void SnapshotGenerated(const FilePath& path, int64 bytes) = 0; | 
| 50 | 53 | 
| 51     // Called after URLFetcher responses from sending the URL (and snapshot). | 54     // Called after URLFetcher responses from sending the URL (and snapshot). | 
| 52     virtual void OnSendComplete(bool success) = 0; | 55     virtual void OnSendComplete(bool success) = 0; | 
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 105 | 108 | 
| 106   // Returns true if the service has found any registered mobile devices. | 109   // Returns true if the service has found any registered mobile devices. | 
| 107   bool HasMobiles() const; | 110   bool HasMobiles() const; | 
| 108 | 111 | 
| 109   // Get the non-NULL ListValue of mobile devices from the cloud print service. | 112   // Get the non-NULL ListValue of mobile devices from the cloud print service. | 
| 110   // The list is owned by PrefService, which outlives ChromeToMobileService. | 113   // The list is owned by PrefService, which outlives ChromeToMobileService. | 
| 111   // Each device DictionaryValue contains strings "type", "name", and "id". | 114   // Each device DictionaryValue contains strings "type", "name", and "id". | 
| 112   // Virtual for unit test mocking. | 115   // Virtual for unit test mocking. | 
| 113   virtual const base::ListValue* GetMobiles() const; | 116   virtual const base::ListValue* GetMobiles() const; | 
| 114 | 117 | 
| 115   // Request an updated mobile device list, request auth first if needed. |  | 
| 116   // Virtual for unit test mocking. |  | 
| 117   virtual void RequestMobileListUpdate(); |  | 
| 118 |  | 
| 119   // Callback with an MHTML snapshot of the browser's selected WebContents. | 118   // Callback with an MHTML snapshot of the browser's selected WebContents. | 
| 120   // Virtual for unit test mocking. | 119   // Virtual for unit test mocking. | 
| 121   virtual void GenerateSnapshot(Browser* browser, | 120   virtual void GenerateSnapshot(Browser* browser, | 
| 122                                 base::WeakPtr<Observer> observer); | 121                                 base::WeakPtr<Observer> observer); | 
| 123 | 122 | 
| 124   // Send the browser's selected WebContents to the specified mobile device. | 123   // Send the browser's selected WebContents to the specified mobile device. | 
| 125   // Virtual for unit test mocking. | 124   // Virtual for unit test mocking. | 
| 126   virtual void SendToMobile(const base::DictionaryValue& mobile, | 125   virtual void SendToMobile(const base::DictionaryValue* mobile, | 
| 127                             const FilePath& snapshot, | 126                             const FilePath& snapshot, | 
| 128                             Browser* browser, | 127                             Browser* browser, | 
| 129                             base::WeakPtr<Observer> observer); | 128                             base::WeakPtr<Observer> observer); | 
| 130 | 129 | 
| 131   // Delete the snapshot file (should be called on observer destruction). | 130   // Delete the snapshot file (should be called on observer destruction). | 
| 132   // Virtual for unit test mocking. | 131   // Virtual for unit test mocking. | 
| 133   virtual void DeleteSnapshot(const FilePath& snapshot); | 132   virtual void DeleteSnapshot(const FilePath& snapshot); | 
| 134 | 133 | 
| 135   // Log a metric for the "ChromeToMobile.Service" histogram. | 134   // Log a metric for the "ChromeToMobile.Service" histogram. | 
| 136   // Virtual for unit test mocking. | 135   // Virtual for unit test mocking. | 
| 137   virtual void LogMetric(Metric metric) const; | 136   virtual void LogMetric(Metric metric) const; | 
| 138 | 137 | 
| 139   // Opens the "Learn More" help article link in the supplied |browser|. | 138   // Opens the "Learn More" help article link in the supplied |browser|. | 
| 140   void LearnMore(Browser* browser) const; | 139   void LearnMore(Browser* browser) const; | 
| 141 | 140 | 
| 142   // net::URLFetcherDelegate method. | 141   // net::URLFetcherDelegate method. | 
| 143   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 142   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | 
| 144 | 143 | 
| 145   // content::NotificationObserver method. | 144   // content::NotificationObserver method. | 
| 146   virtual void Observe(int type, | 145   virtual void Observe(int type, | 
| 147                        const content::NotificationSource& source, | 146                        const content::NotificationSource& source, | 
| 148                        const content::NotificationDetails& details) OVERRIDE; | 147                        const content::NotificationDetails& details) OVERRIDE; | 
| 149 | 148 | 
| 150   // OAuth2AccessTokenConsumer methods. | 149   // OAuth2AccessTokenConsumer methods. | 
| 151   virtual void OnGetTokenSuccess(const std::string& access_token, | 150   virtual void OnGetTokenSuccess(const std::string& access_token, | 
| 152                                  const base::Time& expiration_time) OVERRIDE; | 151                                  const base::Time& expiration_time) OVERRIDE; | 
| 153   virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 152   virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | 
| 154 | 153 | 
|  | 154   // syncer::SyncNotifierObserver implementation. | 
|  | 155   virtual void OnNotificationsEnabled() OVERRIDE; | 
|  | 156   virtual void OnNotificationsDisabled( | 
|  | 157       syncer::NotificationsDisabledReason reason) OVERRIDE; | 
|  | 158   virtual void OnIncomingNotification( | 
|  | 159       const syncer::ObjectIdPayloadMap& id_payloads, | 
|  | 160       syncer::IncomingNotificationSource source) OVERRIDE; | 
|  | 161 | 
| 155  private: | 162  private: | 
| 156   friend class MockChromeToMobileService; | 163   friend class MockChromeToMobileService; | 
| 157 | 164 | 
| 158   // Enable or disable Chrome To Mobile with the browsers' command controllers. | 165   // Enable or disable Chrome To Mobile with the browsers' command controllers. | 
| 159   // The feature state is automatically derived from internal conditions. | 166   // The feature state is automatically derived from internal conditions. | 
| 160   void UpdateCommandState() const; | 167   void UpdateCommandState() const; | 
| 161 | 168 | 
| 162   // Handle the attempted creation of a temporary file for snapshot generation. | 169   // Handle the attempted creation of a temporary file for snapshot generation. | 
| 163   // Alert the observer of failure or generate MHTML with an observer callback. | 170   // Alert the observer of failure or generate MHTML with an observer callback. | 
| 164   void SnapshotFileCreated(base::WeakPtr<Observer> observer, | 171   void SnapshotFileCreated(base::WeakPtr<Observer> observer, | 
| 165                            SessionID::id_type browser_id, | 172                            SessionID::id_type browser_id, | 
| 166                            const FilePath& path, | 173                            const FilePath& path, | 
| 167                            bool success); | 174                            bool success); | 
| 168 | 175 | 
| 169   // Create a cloud print job submission request for a URL or snapshot. | 176   // Create a cloud print job submission request for a URL or snapshot. | 
| 170   net::URLFetcher* CreateRequest(const JobData& data); | 177   net::URLFetcher* CreateRequest(const JobData& data); | 
| 171 | 178 | 
| 172   // Initialize cloud print URLFetcher requests. | 179   // Initialize cloud print URLFetcher requests. | 
| 173   void InitRequest(net::URLFetcher* request); | 180   void InitRequest(net::URLFetcher* request); | 
| 174 | 181 | 
| 175   // Submit a cloud print job request with the requisite data. | 182   // Submit a cloud print job request with the requisite data. | 
| 176   void SendRequest(net::URLFetcher* request, const JobData& data); | 183   void SendRequest(net::URLFetcher* request, const JobData& data); | 
| 177 | 184 | 
|  | 185   // Register to handle cloud print device list invalidations. | 
|  | 186   virtual void RegisterForDeviceListInvalidations(); | 
|  | 187 | 
| 178   // Send the OAuth2AccessTokenFetcher request. | 188   // Send the OAuth2AccessTokenFetcher request. | 
| 179   // Virtual for unit test mocking. | 189   // Virtual for unit test mocking. | 
| 180   virtual void RequestAccessToken(); | 190   virtual void RequestAccessToken(); | 
| 181 | 191 | 
| 182   // Request account information to limit cloud print access to existing users. | 192   // Send the cloud print URLFetcher device search request. | 
| 183   void RequestAccountInfo(); | 193   // Virtual for unit test mocking. | 
|  | 194   virtual void RequestDeviceSearch(); | 
| 184 | 195 | 
| 185   // Send the cloud print URLFetcher device search request. |  | 
| 186   void RequestDeviceSearch(); |  | 
| 187 |  | 
| 188   void HandleAccountInfoResponse(); |  | 
| 189   void HandleSearchResponse(); | 196   void HandleSearchResponse(); | 
| 190   void HandleSubmitResponse(const net::URLFetcher* source); | 197   void HandleSubmitResponse(const net::URLFetcher* source); | 
| 191 | 198 | 
| 192   base::WeakPtrFactory<ChromeToMobileService> weak_ptr_factory_; | 199   base::WeakPtrFactory<ChromeToMobileService> weak_ptr_factory_; | 
| 193 | 200 | 
| 194   Profile* profile_; | 201   Profile* profile_; | 
| 195 | 202 | 
|  | 203   // Sync invalidation service state. Chrome To Mobile requires this service to | 
|  | 204   // keep its mobile device list up to date and prevent page send failures. | 
|  | 205   bool sync_invalidation_enabled_; | 
|  | 206 | 
| 196   // Used to recieve TokenService notifications for GaiaOAuth2LoginRefreshToken. | 207   // Used to recieve TokenService notifications for GaiaOAuth2LoginRefreshToken. | 
| 197   content::NotificationRegistrar registrar_; | 208   content::NotificationRegistrar registrar_; | 
| 198 | 209 | 
| 199   // Cloud print helper class and auth token. | 210   // Cloud print helper class and auth token. | 
| 200   scoped_ptr<CloudPrintURL> cloud_print_url_; | 211   scoped_ptr<CloudPrintURL> cloud_print_url_; | 
| 201   std::string access_token_; | 212   std::string access_token_; | 
| 202 | 213 | 
| 203   // The set of snapshots currently available. | 214   // The set of snapshots currently available. | 
| 204   std::set<FilePath> snapshots_; | 215   std::set<FilePath> snapshots_; | 
| 205 | 216 | 
| 206   // Map URLFetchers to observers for reporting OnSendComplete. | 217   // Map URLFetchers to observers for reporting OnSendComplete. | 
| 207   typedef std::map<const net::URLFetcher*, base::WeakPtr<Observer> > | 218   typedef std::map<const net::URLFetcher*, base::WeakPtr<Observer> > | 
| 208       RequestObserverMap; | 219       RequestObserverMap; | 
| 209   RequestObserverMap request_observer_map_; | 220   RequestObserverMap request_observer_map_; | 
| 210 | 221 | 
| 211   // The pending OAuth access token request and a timer for retrying on failure. | 222   // The pending OAuth access token request and a timer for retrying on failure. | 
| 212   scoped_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_; | 223   scoped_ptr<OAuth2AccessTokenFetcher> access_token_fetcher_; | 
| 213   base::OneShotTimer<ChromeToMobileService> auth_retry_timer_; | 224   base::OneShotTimer<ChromeToMobileService> auth_retry_timer_; | 
| 214 | 225 | 
| 215   // The pending account information request and the cloud print access flag. |  | 
| 216   scoped_ptr<net::URLFetcher> account_info_request_; |  | 
| 217   bool cloud_print_accessible_; |  | 
| 218 |  | 
| 219   // The pending mobile device search request. | 226   // The pending mobile device search request. | 
| 220   scoped_ptr<net::URLFetcher> search_request_; | 227   scoped_ptr<net::URLFetcher> search_request_; | 
| 221 | 228 | 
|  | 229   // A queue of tasks to perform after an access token is lazily initialized. | 
|  | 230   std::queue<base::Closure> task_queue_; | 
|  | 231 | 
| 222   DISALLOW_COPY_AND_ASSIGN(ChromeToMobileService); | 232   DISALLOW_COPY_AND_ASSIGN(ChromeToMobileService); | 
| 223 }; | 233 }; | 
| 224 | 234 | 
| 225 #endif  // CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ | 235 #endif  // CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_ | 
| OLD | NEW | 
|---|