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 // Returns true if |profile_dir| is the "Default" folder in Chrome's default | |
47 // user data directory, and false otherwise. | |
48 static bool IsDefaultProfileDir(const FilePath& profile_dir); | |
49 | |
50 // Asynchronously looks for a cached credential file in the alternate profile | |
51 // and initiates start up using cached credentials if the file was found. | |
52 // Called by ProfileSyncService when it tries to start up on Windows 8 and | |
53 // cannot auto-start. | |
54 void LookForCachedCredentialsInAlternateProfile(); | |
55 | |
56 // ProfileKeyedService implementation. | |
57 virtual void Shutdown() OVERRIDE; | |
58 | |
59 // PrefStore::Observer implementation. | |
60 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE; | |
61 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE; | |
62 | |
63 // content::NotificationObserver implementation. | |
64 virtual void Observe(int type, | |
65 const content::NotificationSource& source, | |
66 const content::NotificationDetails& details) OVERRIDE; | |
67 | |
68 protected: | |
69 // Returns true if the credential cache represented by |store| contains a | |
70 // value for |pref_name|. | |
71 bool HasPref(scoped_refptr<JsonPrefStore> store, | |
72 const std::string& pref_name); | |
73 | |
74 // Encrypts and base 64 encodes |credential|, converts the result to a | |
75 // StringValue, and returns the result. Caller owns the StringValue returned. | |
76 static base::StringValue* PackCredential(const std::string& credential); | |
77 | |
78 // Extracts a string from the Value |packed|, base 64 decodes and decrypts it, | |
79 // and returns the result. | |
80 static std::string UnpackCredential(const base::Value& packed); | |
81 | |
82 // Updates the value of |pref_name| to |new_value|, unless the user has signed | |
83 // out, in which case we write an empty string value to |pref_name|. | |
84 void PackAndUpdateStringPref(const std::string& pref_name, | |
85 const std::string& new_value); | |
86 | |
87 // Updates the value of |pref_name| to |new_value|, unless the user has signed | |
88 // out, in which case we write "false" to |pref_name|. | |
89 void UpdateBooleanPref(const std::string& pref_name, bool new_value); | |
90 | |
91 // Returns the string pref value contained in |store| for |pref_name|. Assumes | |
92 // that |store| contains a value for |pref_name|. | |
93 std::string GetAndUnpackStringPref(scoped_refptr<JsonPrefStore> store, | |
94 const std::string& pref_name); | |
95 | |
96 // Returns the boolean pref value contained in |store| for |pref_name|. | |
97 // Assumes that |store| contains a value for |pref_name|. | |
98 bool GetBooleanPref(scoped_refptr<JsonPrefStore> store, | |
99 const std::string& pref_name); | |
100 | |
101 // Getter for unit tests. | |
102 scoped_refptr<JsonPrefStore> local_store() const { return local_store_; } | |
Roger Tawa OOO till Jul 10th
2012/07/24 14:13:42
should this be a const ref ?
Raghu Simha
2012/07/24 17:19:56
Done.
| |
103 | |
104 // Setter for unit tests | |
105 void set_local_store(JsonPrefStore* new_local_store) { | |
106 local_store_ = new_local_store; | |
107 } | |
108 | |
109 private: | |
110 // Returns the path to the sync credentials file in the current profile | |
111 // directory. | |
112 FilePath GetCredentialPathInCurrentProfile() const; | |
113 | |
114 // Returns the path to the sync credentials file in the default Desktop | |
115 // profile directory if we are running in Metro mode, and vice versa. | |
116 FilePath GetCredentialPathInAlternateProfile() const; | |
117 | |
118 // Initializes the JsonPrefStore object for the local profile directory. | |
119 void InitializeLocalCredentialCacheWriter(); | |
120 | |
121 // Initializes the JsonPrefStore object for the alternate profile directory | |
122 // if |should_initialize| is true. We take a bool* instead of a bool since | |
123 // this is a callback, and base::Owned needs to clean up the flag. | |
124 void InitializeAlternateCredentialCacheReader(bool* should_initialize); | |
125 | |
126 // Returns true if there is an empty value for kGoogleServicesUsername in the | |
127 // credential cache for the local profile (indicating that the user first | |
128 // signed in and then signed out). Returns false if there's no value at all | |
129 // (indicating that the user has never signed in) or if there's a non-empty | |
130 // value (indicating that the user is currently signed in). | |
131 bool HasUserSignedOut(); | |
132 | |
133 // Loads cached sync credentials from the alternate profile and calls | |
134 // ApplyCachedCredentials if the load was successful. | |
135 void ReadCachedCredentialsFromAlternateProfile(); | |
136 | |
137 // Applies the credentials read from the alternate profile to the PrefStore | |
138 // and TokenService of the local profile and then notifies listeners. | |
139 void ApplyCachedCredentials(const std::string& google_services_username, | |
140 const std::string& lsid, | |
141 const std::string& sid, | |
142 const std::string& encryption_bootstrap_token, | |
143 bool keep_everything_synced, | |
144 const bool datatype_prefs[]); | |
145 | |
146 // Profile for which credentials are being cached. | |
147 Profile* profile_; | |
148 | |
149 // Used for write operations to the credential cache file in the local profile | |
150 // directory. This is separate from the chrome pref store. Protected so that | |
151 // it can be accessed by unit tests. | |
152 scoped_refptr<JsonPrefStore> local_store_; | |
153 | |
154 // Used for read operations on the credential cache file in the alternate | |
155 // profile directory. This is separate from the chrome pref store. | |
156 scoped_refptr<JsonPrefStore> alternate_store_; | |
157 | |
158 // Registrar for notifications from the PrefService. | |
159 PrefChangeRegistrar pref_registrar_; | |
160 | |
161 // Registrar for notifications from the TokenService. | |
162 content::NotificationRegistrar registrar_; | |
163 | |
164 // WeakPtr implementation. | |
165 base::WeakPtrFactory<CredentialCacheService> weak_factory_; | |
166 | |
167 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService); | |
168 }; | |
169 | |
170 } // namespace syncer | |
171 | |
172 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_ | |
OLD | NEW |