OLD | NEW |
(Empty) | |
| 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. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
| 6 #define CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
| 7 |
| 8 #include <string> |
| 9 |
| 10 #include "base/basictypes.h" |
| 11 #include "base/file_path.h" |
| 12 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/weak_ptr.h" |
| 14 #include "chrome/browser/prefs/pref_change_registrar.h" |
| 15 #include "chrome/browser/profiles/profile_keyed_service.h" |
| 16 #include "chrome/browser/sync/sync_prefs.h" |
| 17 #include "chrome/common/json_pref_store.h" |
| 18 #include "content/public/browser/notification_observer.h" |
| 19 #include "content/public/browser/notification_registrar.h" |
| 20 |
| 21 namespace base { |
| 22 class StringValue; |
| 23 class Value; |
| 24 } |
| 25 |
| 26 class Profile; |
| 27 |
| 28 namespace syncer { |
| 29 |
| 30 // On Windows 8, Chrome must maintain separate profile directories for Metro and |
| 31 // Desktop modes. When the user signs in to sync in one of the modes, we would |
| 32 // like to automatically start sync in the other mode. |
| 33 // |
| 34 // This class implements a caching service for sync credentials. It listens for |
| 35 // updates to the PrefService and TokenService that pertain to the user |
| 36 // signing in and out of sync, and persists the credentials to a separate file |
| 37 // in the default profile directory. It also contains functionality to bootstrap |
| 38 // sync using credentials that were cached due to signing in on the other |
| 39 // (alternate) mode. |
| 40 class CredentialCacheService : public ProfileKeyedService, |
| 41 public content::NotificationObserver, |
| 42 public PrefStore::Observer { |
| 43 public: |
| 44 explicit CredentialCacheService(Profile* profile); |
| 45 virtual ~CredentialCacheService(); |
| 46 |
| 47 // ProfileKeyedService implementation. |
| 48 virtual void Shutdown() OVERRIDE; |
| 49 |
| 50 // PrefStore::Observer implementation. |
| 51 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; |
| 52 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; |
| 53 |
| 54 // content::NotificationObserver implementation. |
| 55 virtual void Observe(int type, |
| 56 const content::NotificationSource& source, |
| 57 const content::NotificationDetails& details) OVERRIDE; |
| 58 |
| 59 protected: |
| 60 // Returns true if the credential cache represented by |store| contains a |
| 61 // value for |pref_name|. |
| 62 bool HasPref(scoped_refptr<JsonPrefStore> store, |
| 63 const std::string& pref_name); |
| 64 |
| 65 // Encrypts and base 64 encodes |credential|, converts the result to a |
| 66 // StringValue, and returns the result. Caller owns the StringValue returned. |
| 67 static base::StringValue* PackCredential(const std::string& credential); |
| 68 |
| 69 // Extracts a string from the Value |packed|, base 64 decodes and decrypts it, |
| 70 // and returns the result. |
| 71 static std::string UnpackCredential(const base::Value& packed); |
| 72 |
| 73 // 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|. |
| 75 void PackAndUpdateStringPref(const std::string& pref_name, |
| 76 const std::string& new_value); |
| 77 |
| 78 // 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|. |
| 80 void UpdateBooleanPref(const std::string& pref_name, bool new_value); |
| 81 |
| 82 // Returns the string pref value contained in |store| for |pref_name|. Assumes |
| 83 // that |store| contains a value for |pref_name|. |
| 84 std::string GetAndUnpackStringPref(scoped_refptr<JsonPrefStore> store, |
| 85 const std::string& pref_name); |
| 86 |
| 87 // Returns the boolean pref value contained in |store| for |pref_name|. |
| 88 // Assumes that |store| contains a value for |pref_name|. |
| 89 bool GetBooleanPref(scoped_refptr<JsonPrefStore> store, |
| 90 const std::string& pref_name); |
| 91 |
| 92 // Getter for unit tests. |
| 93 const scoped_refptr<JsonPrefStore>& local_store() const { |
| 94 return local_store_; |
| 95 } |
| 96 |
| 97 // Setter for unit tests |
| 98 void set_local_store(JsonPrefStore* new_local_store) { |
| 99 local_store_ = new_local_store; |
| 100 } |
| 101 |
| 102 private: |
| 103 // Returns the path to the sync credentials file in the current profile |
| 104 // directory. |
| 105 FilePath GetCredentialPathInCurrentProfile() const; |
| 106 |
| 107 // Returns the path to the sync credentials file in the default Desktop |
| 108 // profile directory if we are running in Metro mode, and vice versa. |
| 109 FilePath GetCredentialPathInAlternateProfile() const; |
| 110 |
| 111 // Determines if the local credential cache writer should be initialized, |
| 112 // based on the OS version and relevant sync preferences. Returns true if the |
| 113 // writer must be initialized, and false if not. |
| 114 bool ShouldInitializeLocalCredentialCacheWriter() const; |
| 115 |
| 116 // Determines if we must look for credentials in the alternate profile, based |
| 117 // on relevant sync preferences, in addition the to conditions in |
| 118 // ShouldInitializeLocalCredentialCacheWriter(). Returns true if we must look |
| 119 // for cached credentials, and false if not. |
| 120 bool ShouldLookForCachedCredentialsInAlternateProfile() const; |
| 121 |
| 122 // Initializes the JsonPrefStore object for the local profile directory. |
| 123 void InitializeLocalCredentialCacheWriter(); |
| 124 |
| 125 // Initializes the JsonPrefStore object for the alternate profile directory |
| 126 // if |should_initialize| is true. We take a bool* instead of a bool since |
| 127 // this is a callback, and base::Owned needs to clean up the flag. |
| 128 void InitializeAlternateCredentialCacheReader(bool* should_initialize); |
| 129 |
| 130 // Returns true if there is an empty value for kGoogleServicesUsername in the |
| 131 // credential cache for the local profile (indicating that the user first |
| 132 // signed in and then signed out). Returns false if there's no value at all |
| 133 // (indicating that the user has never signed in) or if there's a non-empty |
| 134 // value (indicating that the user is currently signed in). |
| 135 bool HasUserSignedOut(); |
| 136 |
| 137 // Asynchronously looks for a cached credential file in the alternate profile |
| 138 // 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 |
| 140 // cannot auto-start. |
| 141 void LookForCachedCredentialsInAlternateProfile(); |
| 142 |
| 143 // Loads cached sync credentials from the alternate profile and calls |
| 144 // ApplyCachedCredentials if the load was successful. |
| 145 void ReadCachedCredentialsFromAlternateProfile(); |
| 146 |
| 147 // Applies the credentials read from the alternate profile to the PrefStore |
| 148 // and TokenService of the local profile and then notifies listeners. |
| 149 void ApplyCachedCredentials(const std::string& google_services_username, |
| 150 const std::string& lsid, |
| 151 const std::string& sid, |
| 152 const std::string& encryption_bootstrap_token, |
| 153 bool keep_everything_synced, |
| 154 const bool datatype_prefs[]); |
| 155 |
| 156 // Profile for which credentials are being cached. |
| 157 Profile* profile_; |
| 158 |
| 159 // Used to access sync specific preferences in the PrefStore of |profile_|. |
| 160 browser_sync::SyncPrefs sync_prefs_; |
| 161 |
| 162 // 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 |
| 164 // it can be accessed by unit tests. |
| 165 scoped_refptr<JsonPrefStore> local_store_; |
| 166 |
| 167 // Used for read operations on the credential cache file in the alternate |
| 168 // profile directory. This is separate from the chrome pref store. |
| 169 scoped_refptr<JsonPrefStore> alternate_store_; |
| 170 |
| 171 // Registrar for notifications from the PrefService. |
| 172 PrefChangeRegistrar pref_registrar_; |
| 173 |
| 174 // Registrar for notifications from the TokenService. |
| 175 content::NotificationRegistrar registrar_; |
| 176 |
| 177 // WeakPtr implementation. |
| 178 base::WeakPtrFactory<CredentialCacheService> weak_factory_; |
| 179 |
| 180 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService); |
| 181 }; |
| 182 |
| 183 } // namespace syncer |
| 184 |
| 185 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ |
OLD | NEW |