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