Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(797)

Side by Side Diff: chrome/browser/sync/credential_cache_service_win.h

Issue 10656033: [sync] Automatic bootstrapping of Sync on Win 8 from cached credentials (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix unit tests Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 // Field in which the sync service token is cached.
30 extern const char kSyncServiceToken[];
31
32 // On Windows 8, Chrome must maintain separate profile directories for Metro and
33 // Desktop modes. When the user signs in to sync in one of the modes, we would
34 // like to automatically start sync in the other mode.
35 //
36 // This class implements a caching service for sync credentials. It listens for
37 // updates to the pref service and token service that pertain to the user
Andrew T Wilson (Slow) 2012/07/20 01:04:05 token/pref service -> TokenService/PrefService sin
Raghu Simha 2012/07/20 23:35:22 Done.
38 // signing in and out of sync, and persists the credentials to a separate file
39 // in the default profile directory. It also contains functionality to bootstrap
40 // sync using credentials that were cached due to signing in on the other
41 // (alternate) mode.
42 class CredentialCacheService : public ProfileKeyedService,
43 public content::NotificationObserver,
44 public PrefStore::Observer {
45 public:
46 explicit CredentialCacheService(Profile* profile);
47 virtual ~CredentialCacheService();
48
49 // ProfileKeyedService implementation.
50 virtual void Shutdown() OVERRIDE;
51
52 // Returns true if |profile_dir| is the "Default" folder in Chrome's default
53 // user data directory, and false otherwise.
54 static bool IsDefaultProfileDir(const FilePath& profile_dir);
55
56 // Encrypts and base 64 encodes |credential|, converts the result to a
57 // StringValue, and returns the result. Caller owns the StringValue returned.
58 static base::StringValue* PackCredential(const std::string& credential);
59
60 // Extracts a string from the Value |packed|, base 64 decodes and decrypts it,
61 // and returns the result.
62 static std::string UnpackCredential(const base::Value& packed);
63
64 // PrefStore::Observer implementation.
65 virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
66 virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
67
68 // Asynchronously looks for a cached credential file in the alternate profile
69 // and initiates start up using cached credentials if the file was found.
70 // Called by ProfileSyncService when it tries to start up on Windows 8 and
71 // cannot auto-start.
72 void LookForCachedCredentialsInAlternateProfile();
73
74 // Returns true if the service was able to successfully load credentials from
75 // the alternate profile.
76 bool SuccessfullyLoadedCredentials() const;
77
78 // Returns true if the credential cache represented by |store| contains a
79 // value for |pref_name|.
80 bool HasPref(scoped_refptr<JsonPrefStore> store,
81 const std::string& pref_name);
82
83 // Returns true if there is an empty value for kGoogleServicesUsername in the
84 // credential cache for the local profile (indicating that the user first
85 // signed in and then signed out). Returns false if there's no value at all
86 // (indicating that the user has never signed in) or if there's a non-empty
87 // value (indicating that the user is currently signed in).
88 bool HasUserSignedOut();
89
90 // Updates the value of |pref_name| to |new_value|, unless the user has signed
91 // out, in which case we write an empty string value to |pref_name|.
92 void UpdateStringPref(const std::string& pref_name,
93 const std::string& new_value);
94
95 // Updates the value of |pref_name| to |new_value|, unless the user has signed
96 // out, in which case we write "false" to |pref_name|.
97 void UpdateBooleanPref(const std::string& pref_name, bool new_value);
98
99 // Returns the string pref value contained in |store| for |pref_name|. Assumes
100 // that |store| contains a value for |pref_name|.
101 std::string GetStringPref(scoped_refptr<JsonPrefStore> store,
102 const std::string& pref_name);
103
104 // Returns the boolean pref value contained in |store| for |pref_name|.
105 // Assumes that |store| contains a value for |pref_name|.
106 bool GetBooleanPref(scoped_refptr<JsonPrefStore> store,
107 const std::string& pref_name);
108
109 // content::NotificationObserver implementation.
110 virtual void Observe(int type,
111 const content::NotificationSource& source,
112 const content::NotificationDetails& details) OVERRIDE;
113
114 protected:
115 // Used for write operations to the credential cache file in the local profile
116 // directory. This is separate from the chrome pref store. Protected so that
117 // it can be accessed by unit tests.
118 scoped_refptr<JsonPrefStore> local_store_;
119
120 private:
121 // Returns the path to the sync credentials file in the current profile
122 // directory.
123 FilePath GetCredentialPathInCurrentProfile() const;
124
125 // Returns the path to the sync credentials file in the default Desktop
126 // profile directory if we are running in Metro mode, and vice versa.
127 FilePath GetCredentialPathInAlternateProfile() const;
128
129 // Initializes the JsonPrefStore object for the local profile directory.
130 void InitializeLocalCredentialCacheWriter();
131
132 // Initializes the JsonPrefStore object for the alternate profile directory
133 // if |should_initialize| is true.
134 void InitializeAlternateCredentialCacheReader(bool* should_initialize);
135
136 // Loads cached sync credentials from the alternate profile and calls
137 // ApplyCachedCredentials if the load was successful.
138 void ReadCachedCredentialsFromAlternateProfile();
139
140 // Applies the credentials read from the alternate profile to the pref store
141 // and token service of the local profile and then notifies listeners.
142 void ApplyCachedCredentials(const std::string& google_services_username,
143 const std::string& sync_service_token,
144 const std::string& encryption_bootstrap_token,
145 bool keep_everything_synced,
146 const bool datatype_prefs[]);
147
148 // Keeps track of when we were able to successfully load credentials from
149 // the alternate profile.
150 bool loaded_credentials_;
151
152 // Profile for which credentials are being cached.
153 Profile* profile_;
154
155 // WeakPtr implementation.
156 base::WeakPtrFactory<CredentialCacheService> weak_factory_;
157
158 // Used for read operations on the credential cache file in the alternate
159 // profile directory. This is separate from the chrome pref store.
160 scoped_refptr<JsonPrefStore> alternate_store_;
161
162 // Registrar for notifications from the PrefService.
163 PrefChangeRegistrar pref_registrar_;
164
165 // Registrar for notifications from the TokenService.
166 content::NotificationRegistrar token_service_registrar_;
Andrew T Wilson (Slow) 2012/07/20 01:04:05 I'd just call this "registrar_" since it's a gener
Raghu Simha 2012/07/20 23:35:22 Done.
167
168 DISALLOW_COPY_AND_ASSIGN(CredentialCacheService);
169 };
170
171 } // namespace syncer
172
173 #endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698