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" |
| 11 #include "base/cancelable_callback.h" |
11 #include "base/file_path.h" | 12 #include "base/file_path.h" |
12 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "chrome/browser/prefs/pref_change_registrar.h" | 15 #include "chrome/browser/prefs/pref_change_registrar.h" |
15 #include "chrome/browser/profiles/profile_keyed_service.h" | 16 #include "chrome/browser/profiles/profile_keyed_service.h" |
16 #include "chrome/browser/sync/sync_prefs.h" | 17 #include "chrome/browser/sync/sync_prefs.h" |
17 #include "chrome/common/json_pref_store.h" | 18 #include "chrome/common/json_pref_store.h" |
18 #include "content/public/browser/notification_observer.h" | 19 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 20 #include "content/public/browser/notification_registrar.h" |
20 | 21 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 | 54 |
54 // content::NotificationObserver implementation. | 55 // content::NotificationObserver implementation. |
55 virtual void Observe(int type, | 56 virtual void Observe(int type, |
56 const content::NotificationSource& source, | 57 const content::NotificationSource& source, |
57 const content::NotificationDetails& details) OVERRIDE; | 58 const content::NotificationDetails& details) OVERRIDE; |
58 | 59 |
59 protected: | 60 protected: |
60 // Returns true if the credential cache represented by |store| contains a | 61 // Returns true if the credential cache represented by |store| contains a |
61 // value for |pref_name|. | 62 // value for |pref_name|. |
62 bool HasPref(scoped_refptr<JsonPrefStore> store, | 63 bool HasPref(scoped_refptr<JsonPrefStore> store, |
63 const std::string& pref_name); | 64 const std::string& pref_name); |
64 | 65 |
65 // Encrypts and base 64 encodes |credential|, converts the result to a | 66 // Encrypts and base 64 encodes |credential|, converts the result to a |
66 // StringValue, and returns the result. Caller owns the StringValue returned. | 67 // StringValue, and returns the result. Caller owns the StringValue returned. |
67 static base::StringValue* PackCredential(const std::string& credential); | 68 static base::StringValue* PackCredential(const std::string& credential); |
68 | 69 |
69 // Extracts a string from the Value |packed|, base 64 decodes and decrypts it, | 70 // Extracts a string from the Value |packed|, base 64 decodes and decrypts it, |
70 // and returns the result. | 71 // and returns the result. |
71 static std::string UnpackCredential(const base::Value& packed); | 72 static std::string UnpackCredential(const base::Value& packed); |
72 | 73 |
| 74 // Writes the timestamp at which the last update was made to the credential |
| 75 // cache of the local profile. Used to make sure that we only copy credentials |
| 76 // from a more recently updated cache to an older cache. |
| 77 void WriteLastUpdatedTime(); |
| 78 |
73 // Updates the value of |pref_name| to |new_value|, unless the user has signed | 79 // Updates the value of |pref_name| to |new_value|, unless the user has signed |
74 // out, in which case we write an empty string value to |pref_name|. | 80 // out, in which case we write an empty string value to |pref_name|. |
75 void PackAndUpdateStringPref(const std::string& pref_name, | 81 void PackAndUpdateStringPref(const std::string& pref_name, |
76 const std::string& new_value); | 82 const std::string& new_value); |
77 | 83 |
78 // Updates the value of |pref_name| to |new_value|, unless the user has signed | 84 // Updates the value of |pref_name| to |new_value|, unless the user has signed |
79 // out, in which case we write "false" to |pref_name|. | 85 // out, in which case we write "false" to |pref_name|. |
80 void UpdateBooleanPref(const std::string& pref_name, bool new_value); | 86 void UpdateBooleanPref(const std::string& pref_name, bool new_value); |
81 | 87 |
| 88 // Returns the time at which the credential cache represented by |store| was |
| 89 // last updated. Used to make sure that we only copy credentials from a more |
| 90 // recently updated cache to an older cache. |
| 91 int64 GetLastUpdatedTime(scoped_refptr<JsonPrefStore> store); |
| 92 |
82 // Returns the string pref value contained in |store| for |pref_name|. Assumes | 93 // Returns the string pref value contained in |store| for |pref_name|. Assumes |
83 // that |store| contains a value for |pref_name|. | 94 // that |store| contains a value for |pref_name|. |
84 std::string GetAndUnpackStringPref(scoped_refptr<JsonPrefStore> store, | 95 std::string GetAndUnpackStringPref(scoped_refptr<JsonPrefStore> store, |
85 const std::string& pref_name); | 96 const std::string& pref_name); |
86 | 97 |
87 // Returns the boolean pref value contained in |store| for |pref_name|. | 98 // Returns the boolean pref value contained in |store| for |pref_name|. |
88 // Assumes that |store| contains a value for |pref_name|. | 99 // Assumes that |store| contains a value for |pref_name|. |
89 bool GetBooleanPref(scoped_refptr<JsonPrefStore> store, | 100 bool GetBooleanPref(scoped_refptr<JsonPrefStore> store, |
90 const std::string& pref_name); | 101 const std::string& pref_name); |
91 | 102 |
92 // Getter for unit tests. | 103 // Getter for unit tests. |
93 const scoped_refptr<JsonPrefStore>& local_store() const { | 104 const scoped_refptr<JsonPrefStore>& local_store() const { |
94 return local_store_; | 105 return local_store_; |
95 } | 106 } |
96 | 107 |
97 // Setter for unit tests | 108 // Setter for unit tests |
98 void set_local_store(JsonPrefStore* new_local_store) { | 109 void set_local_store(JsonPrefStore* new_local_store) { |
99 local_store_ = new_local_store; | 110 local_store_ = new_local_store; |
100 } | 111 } |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 // Asynchronously looks for a cached credential file in the alternate profile | 148 // Asynchronously looks for a cached credential file in the alternate profile |
138 // and initiates start up using cached credentials if the file was found. | 149 // and initiates start up using cached credentials if the file was found. |
139 // Called by ProfileSyncService when it tries to start up on Windows 8 and | 150 // Called by ProfileSyncService when it tries to start up on Windows 8 and |
140 // cannot auto-start. | 151 // cannot auto-start. |
141 void LookForCachedCredentialsInAlternateProfile(); | 152 void LookForCachedCredentialsInAlternateProfile(); |
142 | 153 |
143 // Loads cached sync credentials from the alternate profile and calls | 154 // Loads cached sync credentials from the alternate profile and calls |
144 // ApplyCachedCredentials if the load was successful. | 155 // ApplyCachedCredentials if the load was successful. |
145 void ReadCachedCredentialsFromAlternateProfile(); | 156 void ReadCachedCredentialsFromAlternateProfile(); |
146 | 157 |
147 // Applies the credentials read from the alternate profile to the PrefStore | 158 // Initiates sync sign in using credentials read from the alternate profile by |
148 // and TokenService of the local profile and then notifies listeners. | 159 // persisting |google_services_username|, |encryption_bootstrap_token|, |
149 void ApplyCachedCredentials(const std::string& google_services_username, | 160 // |keep_everything_synced| and |preferred_types| to the local pref store, and |
150 const std::string& lsid, | 161 // preparing ProfileSyncService for sign in. |
151 const std::string& sid, | 162 void InitiateSignInWithCachedCredentials( |
152 const std::string& encryption_bootstrap_token, | 163 const std::string& google_services_username, |
153 bool keep_everything_synced, | 164 const std::string& encryption_bootstrap_token, |
154 const bool datatype_prefs[]); | 165 bool keep_everything_synced, |
| 166 ModelTypeSet preferred_types); |
| 167 |
| 168 // Updates the TokenService credentials with |lsid| and |sid| and triggers the |
| 169 // minting of new tokens for all Chrome services. ProfileSyncService is |
| 170 // automatically notified when tokens are minted, and goes on to consume the |
| 171 // updated credentials. |
| 172 void UpdateTokenServiceCredentials(const std::string& lsid, |
| 173 const std::string& sid); |
| 174 |
| 175 // Initiates a sign out of sync. Called when we notice that the user has |
| 176 // signed out from the alternate mode by reading its credential cache. |
| 177 void InitiateSignOut(); |
| 178 |
| 179 // Compares the sync preferences in the local profile with values that were |
| 180 // read from the alternate profile -- |keep_everything_synced| and |
| 181 // |preferred_types|. Returns true if the prefs have changed, and false |
| 182 // otherwise. |
| 183 bool HaveSyncPrefsChanged(bool keep_everything_synced, |
| 184 ModelTypeSet preferred_types) const; |
| 185 |
| 186 // Compares the token service credentials in the local profile with values |
| 187 // that were read from the alternate profile -- |lsid| and |sid|. Returns true |
| 188 // if the credentials have changed, and false otherwise. |
| 189 bool HaveTokenServiceCredentialsChanged(const std::string& lsid, |
| 190 const std::string& sid); |
| 191 |
| 192 // Determines if the user must be signed out of the local profile or not. |
| 193 // Called when updated settings are noticed in the alternate credential cache |
| 194 // for |google_services_username|. Returns true if we should sign out, and |
| 195 // false if not. |
| 196 bool ShouldSignOutOfSync(const std::string& google_services_username); |
| 197 |
| 198 // Determines if sync settings may be reconfigured or not. Called when |
| 199 // updated settings are noticed in the alternate credential cache for |
| 200 // |google_services_username|. Returns true if we may reconfigure, and false |
| 201 // if not. |
| 202 bool MayReconfigureSync(const std::string& google_services_username); |
| 203 |
| 204 // 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 |
| 206 // for |google_services_username|, with new values for |lsid|, |sid| and |
| 207 // |encryption_bootstrap_token|. Returns true if we should sign in, and |
| 208 // false if not. |
| 209 bool ShouldSignInToSync(const std::string& google_services_username, |
| 210 const std::string& lsid, |
| 211 const std::string& sid, |
| 212 const std::string& encryption_bootstrap_token); |
| 213 |
| 214 // Resets |alternate_store_| and schedules the next read from the alternate |
| 215 // credential cache. |
| 216 void ScheduleNextReadFromAlternateCredentialCache(); |
155 | 217 |
156 // Profile for which credentials are being cached. | 218 // Profile for which credentials are being cached. |
157 Profile* profile_; | 219 Profile* profile_; |
158 | 220 |
159 // Used to access sync specific preferences in the PrefStore of |profile_|. | 221 // Used to access sync specific preferences in the PrefStore of |profile_|. |
160 browser_sync::SyncPrefs sync_prefs_; | 222 browser_sync::SyncPrefs sync_prefs_; |
161 | 223 |
162 // Used for write operations to the credential cache file in the local profile | 224 // Used for write operations to the credential cache file in the local profile |
163 // directory. This is separate from the chrome pref store. Protected so that | 225 // directory. This is separate from the chrome pref store. Protected so that |
164 // it can be accessed by unit tests. | 226 // it can be accessed by unit tests. |
165 scoped_refptr<JsonPrefStore> local_store_; | 227 scoped_refptr<JsonPrefStore> local_store_; |
166 | 228 |
167 // Used for read operations on the credential cache file in the alternate | 229 // Used for read operations on the credential cache file in the alternate |
168 // profile directory. This is separate from the chrome pref store. | 230 // profile directory. This is separate from the chrome pref store. |
169 scoped_refptr<JsonPrefStore> alternate_store_; | 231 scoped_refptr<JsonPrefStore> alternate_store_; |
170 | 232 |
171 // Registrar for notifications from the PrefService. | 233 // Registrar for notifications from the PrefService. |
172 PrefChangeRegistrar pref_registrar_; | 234 PrefChangeRegistrar pref_registrar_; |
173 | 235 |
174 // Registrar for notifications from the TokenService. | 236 // Registrar for notifications from the TokenService. |
175 content::NotificationRegistrar registrar_; | 237 content::NotificationRegistrar registrar_; |
176 | 238 |
177 // WeakPtr implementation. | 239 // WeakPtr implementation. |
178 base::WeakPtrFactory<CredentialCacheService> weak_factory_; | 240 base::WeakPtrFactory<CredentialCacheService> weak_factory_; |
179 | 241 |
| 242 // Used to make sure that there is always at most one future read scheduled |
| 243 // on the alternate credential cache. |
| 244 base::CancelableClosure next_read_; |
| 245 |
180 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService); | 246 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService); |
181 }; | 247 }; |
182 | 248 |
183 } // namespace syncer | 249 } // namespace syncer |
184 | 250 |
185 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ | 251 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
OLD | NEW |