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
3 // found in the LICENSE file.
5 #ifndef CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_
6 #define CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_
14 #include "base/compiler_specific.h"
15 #include "base/files/file_path.h"
16 #include "base/memory/scoped_vector.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/string16.h"
19 #include "base/timer.h"
20 #include "base/values.h"
21 #include "chrome/browser/profiles/profile_keyed_service.h"
22 #include "chrome/browser/sessions/session_id.h"
23 #include "content/public/browser/notification_observer.h"
24 #include "content/public/browser/notification_registrar.h"
25 #include "google_apis/gaia/oauth2_access_token_consumer.h"
26 #include "googleurl/src/gurl.h"
27 #include "net/url_request/url_fetcher_delegate.h"
28 #include "sync/notifier/invalidation_handler.h"
30 class OAuth2AccessTokenFetcher
;
33 class MockChromeToMobileService
;
34 class PrefRegistrySyncable
;
41 // ChromeToMobileService connects to the cloud print service to enumerate
42 // compatible mobiles owned by its profile and send URLs and MHTML snapshots.
43 class ChromeToMobileService
: public ProfileKeyedService
,
44 public net::URLFetcherDelegate
,
45 public content::NotificationObserver
,
46 public OAuth2AccessTokenConsumer
,
47 public syncer::InvalidationHandler
{
54 // Called on generation of the page's MHTML snapshot.
55 virtual void SnapshotGenerated(const base::FilePath
& path
, int64 bytes
) = 0;
57 // Called after URLFetcher responses from sending the URL (and snapshot).
58 virtual void OnSendComplete(bool success
) = 0;
61 // The supported mobile device operating systems.
67 // The cloud print job types.
74 // The cloud print job submission data.
83 base::FilePath snapshot
;
84 std::string snapshot_id
;
85 std::string snapshot_content
;
89 DISALLOW_COPY_AND_ASSIGN(JobData
);
92 // Returns whether Chrome To Mobile is enabled (gated on the Action Box UI).
93 static bool IsChromeToMobileEnabled();
95 // Update and return the IDC_CHROME_TO_MOBILE_PAGE command enabled state; the
96 // state is derived from commandline, profile, and current page applicability.
97 // NOTE: Call this instead of IsCommandEnabled(IDC_CHROME_TO_MOBILE_PAGE).
98 static bool UpdateAndGetCommandState(Browser
* browser
);
100 // Register the user prefs associated with this service.
101 static void RegisterUserPrefs(PrefRegistrySyncable
* registry
);
103 explicit ChromeToMobileService(Profile
* profile
);
104 virtual ~ChromeToMobileService();
106 // Returns true if the service has found any registered mobile devices.
107 bool HasMobiles() const;
109 // Get the non-NULL ListValue of mobile devices from the cloud print service.
110 // The list is owned by PrefService, which outlives ChromeToMobileService.
111 // Each device DictionaryValue contains strings "type", "name", and "id".
112 // Virtual for unit test mocking.
113 virtual const base::ListValue
* GetMobiles() const;
115 // Callback with an MHTML snapshot of the browser's selected WebContents.
116 // Virtual for unit test mocking.
117 virtual void GenerateSnapshot(Browser
* browser
,
118 base::WeakPtr
<Observer
> observer
);
120 // Send the browser's selected WebContents to the specified mobile device.
121 // Virtual for unit test mocking.
122 virtual void SendToMobile(const base::DictionaryValue
* mobile
,
123 const base::FilePath
& snapshot
,
125 base::WeakPtr
<Observer
> observer
);
127 // Delete the snapshot file (should be called on observer destruction).
128 // Virtual for unit test mocking.
129 virtual void DeleteSnapshot(const base::FilePath
& snapshot
);
131 // Opens the "Learn More" help article link in the supplied |browser|.
132 void LearnMore(Browser
* browser
) const;
134 // ProfileKeyedService method.
135 virtual void Shutdown() OVERRIDE
;
137 // net::URLFetcherDelegate method.
138 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
140 // content::NotificationObserver method.
141 virtual void Observe(int type
,
142 const content::NotificationSource
& source
,
143 const content::NotificationDetails
& details
) OVERRIDE
;
145 // OAuth2AccessTokenConsumer methods.
146 virtual void OnGetTokenSuccess(const std::string
& access_token
,
147 const base::Time
& expiration_time
) OVERRIDE
;
148 virtual void OnGetTokenFailure(const GoogleServiceAuthError
& error
) OVERRIDE
;
150 // syncer::InvalidationHandler implementation.
151 virtual void OnInvalidatorStateChange(
152 syncer::InvalidatorState state
) OVERRIDE
;
153 virtual void OnIncomingInvalidation(
154 const syncer::ObjectIdInvalidationMap
& invalidation_map
) OVERRIDE
;
156 // Expose access token accessors for test purposes.
157 const std::string
& GetAccessTokenForTest() const;
158 void SetAccessTokenForTest(const std::string
& access_token
);
161 friend class MockChromeToMobileService
;
163 // Handle the attempted creation of a temporary file for snapshot generation.
164 void SnapshotFileCreated(base::WeakPtr
<Observer
> observer
,
165 SessionID::id_type browser_id
,
166 const base::FilePath
& path
);
168 // Handle the attempted MHTML snapshot generation; alerts the observer.
169 void SnapshotGenerated(base::WeakPtr
<Observer
> observer
,
170 const base::FilePath
& path
,
173 // Handle the attempted reading of the snapshot file for job submission.
174 // Send valid snapshot contents if available, or log an error.
175 void SnapshotFileRead(base::WeakPtr
<Observer
> observer
,
176 scoped_ptr
<JobData
> data
);
178 // Initialize cloud print URLFetcher requests.
179 void InitRequest(net::URLFetcher
* request
);
181 // Submit a cloud print job request with the requisite data.
182 void SendJobRequest(base::WeakPtr
<Observer
> observer
, const JobData
& data
);
184 // Clear the cached cloud print auth access token.
185 void ClearAccessToken();
187 // Send the OAuth2AccessTokenFetcher request.
188 void RequestAccessToken();
190 // Send the cloud print URLFetcher device search request.
191 // Virtual for unit test mocking.
192 virtual void RequestDeviceSearch();
194 void HandleSearchResponse(const net::URLFetcher
* source
);
195 void HandleSubmitResponse(const net::URLFetcher
* source
);
197 base::WeakPtrFactory
<ChromeToMobileService
> weak_ptr_factory_
;
201 // Sync invalidation service state. Chrome To Mobile requires this service to
202 // to keep the mobile device list up to date and prevent page send failures.
203 bool sync_invalidation_enabled_
;
205 // Used to recieve TokenService notifications for GaiaOAuth2LoginRefreshToken.
206 content::NotificationRegistrar registrar_
;
208 // The cloud print service URL and auth access token.
209 GURL cloud_print_url_
;
210 std::string access_token_
;
212 // The set of snapshots currently available.
213 std::set
<base::FilePath
> snapshots_
;
215 // The list of active URLFetcher requests owned by the service.
216 ScopedVector
<net::URLFetcher
> url_fetchers_
;
218 // Map URLFetchers to observers for reporting OnSendComplete.
219 typedef std::map
<const net::URLFetcher
*, base::WeakPtr
<Observer
> >
221 RequestObserverMap request_observer_map_
;
223 // The pending OAuth access token request and timers for retrying on failure.
224 scoped_ptr
<OAuth2AccessTokenFetcher
> access_token_fetcher_
;
225 base::OneShotTimer
<ChromeToMobileService
> auth_retry_timer_
;
226 base::OneShotTimer
<ChromeToMobileService
> search_retry_timer_
;
228 // A queue of tasks to perform after an access token is lazily initialized.
229 std::queue
<base::Closure
> task_queue_
;
231 DISALLOW_COPY_AND_ASSIGN(ChromeToMobileService
);
234 #endif // CHROME_BROWSER_CHROME_TO_MOBILE_SERVICE_H_