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_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ | 5 #ifndef CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
6 #define CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ | 6 #define CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 21 matching lines...) Expand all Loading... |
32 // Desktop modes. When the user signs in to sync in one of the modes, we would | 32 // Desktop modes. When the user signs in to sync in one of the modes, we would |
33 // like to automatically start sync in the other mode. | 33 // like to automatically start sync in the other mode. |
34 // | 34 // |
35 // This class implements a caching service for sync credentials. It listens for | 35 // This class implements a caching service for sync credentials. It listens for |
36 // updates to the PrefService and TokenService that pertain to the user | 36 // updates to the PrefService and TokenService that pertain to the user |
37 // signing in and out of sync, and persists the credentials to a separate file | 37 // signing in and out of sync, and persists the credentials to a separate file |
38 // in the default profile directory. It also contains functionality to bootstrap | 38 // in the default profile directory. It also contains functionality to bootstrap |
39 // sync using credentials that were cached due to signing in on the other | 39 // sync using credentials that were cached due to signing in on the other |
40 // (alternate) mode. | 40 // (alternate) mode. |
41 class CredentialCacheService : public ProfileKeyedService, | 41 class CredentialCacheService : public ProfileKeyedService, |
42 public content::NotificationObserver, | 42 public content::NotificationObserver { |
43 public PrefStore::Observer { | |
44 public: | 43 public: |
45 explicit CredentialCacheService(Profile* profile); | 44 explicit CredentialCacheService(Profile* profile); |
46 virtual ~CredentialCacheService(); | 45 virtual ~CredentialCacheService(); |
47 | 46 |
48 // ProfileKeyedService implementation. | 47 // ProfileKeyedService implementation. |
49 virtual void Shutdown() OVERRIDE; | 48 virtual void Shutdown() OVERRIDE; |
50 | 49 |
51 // PrefStore::Observer implementation. | |
52 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
53 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | |
54 | |
55 // content::NotificationObserver implementation. | 50 // content::NotificationObserver implementation. |
56 virtual void Observe(int type, | 51 virtual void Observe(int type, |
57 const content::NotificationSource& source, | 52 const content::NotificationSource& source, |
58 const content::NotificationDetails& details) OVERRIDE; | 53 const content::NotificationDetails& details) OVERRIDE; |
59 | 54 |
| 55 // Loads cached sync credentials from the alternate profile and applies them |
| 56 // to the local profile if the load was successful. |
| 57 void ReadCachedCredentialsFromAlternateProfile(); |
| 58 |
| 59 // Populates a new local credential cache file if the user is already signed |
| 60 // in to the local profile, and there is no existing local credential cache. |
| 61 // Used in scenarios where a user upgraded from an older version of Chrome |
| 62 // that didn't support credential caching. This method is a no-op if local |
| 63 // sync prefs have already been written to the local cache. |
| 64 void WriteExistingSyncPrefsToLocalCache(); |
| 65 |
| 66 // Resets |alternate_store_| and schedules the next read from the alternate |
| 67 // credential cache. |
| 68 void ScheduleNextReadFromAlternateCredentialCache(); |
| 69 |
60 protected: | 70 protected: |
61 // Returns true if the credential cache represented by |store| contains a | 71 // Returns true if the credential cache represented by |store| contains a |
62 // value for |pref_name|. | 72 // value for |pref_name|. |
63 bool HasPref(scoped_refptr<JsonPrefStore> store, | 73 bool HasPref(scoped_refptr<JsonPrefStore> store, |
64 const std::string& pref_name); | 74 const std::string& pref_name); |
65 | 75 |
66 // Encrypts and base 64 encodes |credential|, converts the result to a | 76 // Encrypts and base 64 encodes |credential|, converts the result to a |
67 // StringValue, and returns the result. Caller owns the StringValue returned. | 77 // StringValue, and returns the result. Caller owns the StringValue returned. |
68 static base::StringValue* PackCredential(const std::string& credential); | 78 static base::StringValue* PackCredential(const std::string& credential); |
69 | 79 |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
104 const scoped_refptr<JsonPrefStore>& local_store() const { | 114 const scoped_refptr<JsonPrefStore>& local_store() const { |
105 return local_store_; | 115 return local_store_; |
106 } | 116 } |
107 | 117 |
108 // Setter for unit tests | 118 // Setter for unit tests |
109 void set_local_store(JsonPrefStore* new_local_store) { | 119 void set_local_store(JsonPrefStore* new_local_store) { |
110 local_store_ = new_local_store; | 120 local_store_ = new_local_store; |
111 } | 121 } |
112 | 122 |
113 private: | 123 private: |
| 124 // Used to track the initialization of the local credential cache. |
| 125 class LocalStoreObserver |
| 126 : public base::RefCounted<LocalStoreObserver>, |
| 127 public PrefStore::Observer { |
| 128 public: |
| 129 LocalStoreObserver(CredentialCacheService* service, |
| 130 scoped_refptr<JsonPrefStore> local_store); |
| 131 virtual ~LocalStoreObserver(); |
| 132 |
| 133 // PrefStore::Observer implementation. |
| 134 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| 135 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| 136 |
| 137 protected: |
| 138 friend class base::RefCounted<LocalStoreObserver>; |
| 139 |
| 140 private: |
| 141 CredentialCacheService* service_; |
| 142 scoped_refptr<JsonPrefStore> local_store_; |
| 143 DISALLOW_COPY_AND_ASSIGN(LocalStoreObserver); |
| 144 }; |
| 145 |
| 146 // Used to track the initialization of the alternate credential cache. |
| 147 class AlternateStoreObserver |
| 148 : public base::RefCounted<AlternateStoreObserver>, |
| 149 public PrefStore::Observer { |
| 150 public: |
| 151 AlternateStoreObserver(CredentialCacheService* service, |
| 152 scoped_refptr<JsonPrefStore> alternate_store); |
| 153 virtual ~AlternateStoreObserver(); |
| 154 |
| 155 // PrefStore::Observer implementation. |
| 156 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| 157 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| 158 |
| 159 protected: |
| 160 friend class base::RefCounted<AlternateStoreObserver>; |
| 161 |
| 162 private: |
| 163 CredentialCacheService* service_; |
| 164 scoped_refptr<JsonPrefStore> alternate_store_; |
| 165 DISALLOW_COPY_AND_ASSIGN(AlternateStoreObserver); |
| 166 }; |
| 167 |
114 // Returns the path to the sync credentials file in the current profile | 168 // Returns the path to the sync credentials file in the current profile |
115 // directory. | 169 // directory. |
116 FilePath GetCredentialPathInCurrentProfile() const; | 170 FilePath GetCredentialPathInCurrentProfile() const; |
117 | 171 |
118 // Returns the path to the sync credentials file in the default Desktop | 172 // Returns the path to the sync credentials file in the default Desktop |
119 // profile directory if we are running in Metro mode, and vice versa. | 173 // profile directory if we are running in Metro mode, and vice versa. |
120 FilePath GetCredentialPathInAlternateProfile() const; | 174 FilePath GetCredentialPathInAlternateProfile() const; |
121 | 175 |
122 // Determines if the local credential cache writer should be initialized, | 176 // Determines if the local credential cache writer should be initialized, |
123 // based on the OS version and relevant sync preferences. Returns true if the | 177 // based on the OS version and relevant sync preferences. Returns true if the |
124 // writer must be initialized, and false if not. | 178 // writer must be initialized, and false if not. |
125 bool ShouldInitializeLocalCredentialCacheWriter() const; | 179 bool ShouldInitializeLocalCredentialCacheWriter() const; |
126 | 180 |
127 // Determines if we must look for credentials in the alternate profile, based | |
128 // on relevant sync preferences, in addition the to conditions in | |
129 // ShouldInitializeLocalCredentialCacheWriter(). Returns true if we must look | |
130 // for cached credentials, and false if not. | |
131 bool ShouldLookForCachedCredentialsInAlternateProfile() const; | |
132 | |
133 // Initializes the JsonPrefStore object for the local profile directory. | 181 // Initializes the JsonPrefStore object for the local profile directory. |
134 void InitializeLocalCredentialCacheWriter(); | 182 void InitializeLocalCredentialCacheWriter(); |
135 | 183 |
136 // Initializes the JsonPrefStore object for the alternate profile directory | |
137 // if |should_initialize| is true. We take a bool* instead of a bool since | |
138 // this is a callback, and base::Owned needs to clean up the flag. | |
139 void InitializeAlternateCredentialCacheReader(bool* should_initialize); | |
140 | |
141 // Returns true if there is an empty value for kGoogleServicesUsername in the | 184 // Returns true if there is an empty value for kGoogleServicesUsername in the |
142 // credential cache for the local profile (indicating that the user first | 185 // credential cache for the local profile (indicating that the user first |
143 // signed in and then signed out). Returns false if there's no value at all | 186 // signed in and then signed out). Returns false if there's no value at all |
144 // (indicating that the user has never signed in) or if there's a non-empty | 187 // (indicating that the user has never signed in) or if there's a non-empty |
145 // value (indicating that the user is currently signed in). | 188 // value (indicating that the user is currently signed in). |
146 bool HasUserSignedOut(); | 189 bool HasUserSignedOut(); |
147 | 190 |
148 // Asynchronously looks for a cached credential file in the alternate profile | 191 // Asynchronously looks for a cached credential file in the alternate profile |
149 // and initiates start up using cached credentials if the file was found. | 192 // and initiates start up using cached credentials if the file was found. |
150 // Called by ProfileSyncService when it tries to start up on Windows 8 and | 193 // Called by ProfileSyncService when it tries to start up on Windows 8 and |
151 // cannot auto-start. | 194 // cannot auto-start. |
152 void LookForCachedCredentialsInAlternateProfile(); | 195 void LookForCachedCredentialsInAlternateProfile(); |
153 | 196 |
154 // Loads cached sync credentials from the alternate profile and calls | |
155 // ApplyCachedCredentials if the load was successful. | |
156 void ReadCachedCredentialsFromAlternateProfile(); | |
157 | |
158 // Initiates sync sign in using credentials read from the alternate profile by | 197 // Initiates sync sign in using credentials read from the alternate profile by |
159 // persisting |google_services_username|, |encryption_bootstrap_token|, | 198 // persisting |google_services_username|, |encryption_bootstrap_token|, |
160 // |keep_everything_synced| and |preferred_types| to the local pref store, and | 199 // |keep_everything_synced| and |preferred_types| to the local pref store, and |
161 // preparing ProfileSyncService for sign in. | 200 // preparing ProfileSyncService for sign in. |
162 void InitiateSignInWithCachedCredentials( | 201 void InitiateSignInWithCachedCredentials( |
163 const std::string& google_services_username, | 202 const std::string& google_services_username, |
164 const std::string& encryption_bootstrap_token, | 203 const std::string& encryption_bootstrap_token, |
165 bool keep_everything_synced, | 204 bool keep_everything_synced, |
166 ModelTypeSet preferred_types); | 205 ModelTypeSet preferred_types); |
167 | 206 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 // Determines if the user must be signed in to the local profile or not. | 243 // Determines if the user must be signed in to the local profile or not. |
205 // Called when updated settings are noticed in the alternate credential cache | 244 // Called when updated settings are noticed in the alternate credential cache |
206 // for |google_services_username|, with new values for |lsid|, |sid| and | 245 // for |google_services_username|, with new values for |lsid|, |sid| and |
207 // |encryption_bootstrap_token|. Returns true if we should sign in, and | 246 // |encryption_bootstrap_token|. Returns true if we should sign in, and |
208 // false if not. | 247 // false if not. |
209 bool ShouldSignInToSync(const std::string& google_services_username, | 248 bool ShouldSignInToSync(const std::string& google_services_username, |
210 const std::string& lsid, | 249 const std::string& lsid, |
211 const std::string& sid, | 250 const std::string& sid, |
212 const std::string& encryption_bootstrap_token); | 251 const std::string& encryption_bootstrap_token); |
213 | 252 |
214 // Resets |alternate_store_| and schedules the next read from the alternate | |
215 // credential cache. | |
216 void ScheduleNextReadFromAlternateCredentialCache(); | |
217 | |
218 // Profile for which credentials are being cached. | 253 // Profile for which credentials are being cached. |
219 Profile* profile_; | 254 Profile* profile_; |
220 | 255 |
221 // Used to access sync specific preferences in the PrefStore of |profile_|. | 256 // Used to access sync specific preferences in the PrefStore of |profile_|. |
222 browser_sync::SyncPrefs sync_prefs_; | 257 browser_sync::SyncPrefs sync_prefs_; |
223 | 258 |
224 // Used for write operations to the credential cache file in the local profile | 259 // Used for write operations to the credential cache file in the local profile |
225 // directory. This is separate from the chrome pref store. Protected so that | 260 // directory. This is separate from the chrome pref store. Protected so that |
226 // it can be accessed by unit tests. | 261 // it can be accessed by unit tests. |
227 scoped_refptr<JsonPrefStore> local_store_; | 262 scoped_refptr<JsonPrefStore> local_store_; |
228 | 263 |
| 264 // Used to respond to the initialization of |local_store_|. |
| 265 scoped_refptr<LocalStoreObserver> local_store_observer_; |
| 266 |
229 // Used for read operations on the credential cache file in the alternate | 267 // Used for read operations on the credential cache file in the alternate |
230 // profile directory. This is separate from the chrome pref store. | 268 // profile directory. This is separate from the chrome pref store. |
231 scoped_refptr<JsonPrefStore> alternate_store_; | 269 scoped_refptr<JsonPrefStore> alternate_store_; |
232 | 270 |
| 271 // Used to respond to the initialization of |alternate_store_|. |
| 272 scoped_refptr<AlternateStoreObserver> alternate_store_observer_; |
| 273 |
233 // Registrar for notifications from the PrefService. | 274 // Registrar for notifications from the PrefService. |
234 PrefChangeRegistrar pref_registrar_; | 275 PrefChangeRegistrar pref_registrar_; |
235 | 276 |
236 // Registrar for notifications from the TokenService. | 277 // Registrar for notifications from the TokenService. |
237 content::NotificationRegistrar registrar_; | 278 content::NotificationRegistrar registrar_; |
238 | 279 |
239 // WeakPtr implementation. | 280 // WeakPtr implementation. |
240 base::WeakPtrFactory<CredentialCacheService> weak_factory_; | 281 base::WeakPtrFactory<CredentialCacheService> weak_factory_; |
241 | 282 |
242 // Used to make sure that there is always at most one future read scheduled | 283 // Used to make sure that there is always at most one future read scheduled |
243 // on the alternate credential cache. | 284 // on the alternate credential cache. |
244 base::CancelableClosure next_read_; | 285 base::CancelableClosure next_read_; |
245 | 286 |
246 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService); | 287 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService); |
247 }; | 288 }; |
248 | 289 |
249 } // namespace syncer | 290 } // namespace syncer |
250 | 291 |
251 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ | 292 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
OLD | NEW |