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