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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/sync/credential_cache_service_win.h
diff --git a/chrome/browser/sync/credential_cache_service_win.h b/chrome/browser/sync/credential_cache_service_win.h
new file mode 100644
index 0000000000000000000000000000000000000000..030bb859e2a1142b7d309a18c87b451b8ecd9670
--- /dev/null
+++ b/chrome/browser/sync/credential_cache_service_win.h
@@ -0,0 +1,173 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_
+#define CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_
+
+#include <string>
+
+#include "base/basictypes.h"
+#include "base/file_path.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "chrome/browser/prefs/pref_change_registrar.h"
+#include "chrome/browser/profiles/profile_keyed_service.h"
+#include "chrome/common/json_pref_store.h"
+#include "content/public/browser/notification_observer.h"
+#include "content/public/browser/notification_registrar.h"
+
+namespace base {
+class StringValue;
+class Value;
+}
+
+class Profile;
+
+namespace syncer {
+
+// Field in which the sync service token is cached.
+extern const char kSyncServiceToken[];
+
+// On Windows 8, Chrome must maintain separate profile directories for Metro and
+// Desktop modes. When the user signs in to sync in one of the modes, we would
+// like to automatically start sync in the other mode.
+//
+// This class implements a caching service for sync credentials. It listens for
+// 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.
+// signing in and out of sync, and persists the credentials to a separate file
+// in the default profile directory. It also contains functionality to bootstrap
+// sync using credentials that were cached due to signing in on the other
+// (alternate) mode.
+class CredentialCacheService : public ProfileKeyedService,
+ public content::NotificationObserver,
+ public PrefStore::Observer {
+ public:
+ explicit CredentialCacheService(Profile* profile);
+ virtual ~CredentialCacheService();
+
+ // ProfileKeyedService implementation.
+ virtual void Shutdown() OVERRIDE;
+
+ // Returns true if |profile_dir| is the "Default" folder in Chrome's default
+ // user data directory, and false otherwise.
+ static bool IsDefaultProfileDir(const FilePath& profile_dir);
+
+ // Encrypts and base 64 encodes |credential|, converts the result to a
+ // StringValue, and returns the result. Caller owns the StringValue returned.
+ static base::StringValue* PackCredential(const std::string& credential);
+
+ // Extracts a string from the Value |packed|, base 64 decodes and decrypts it,
+ // and returns the result.
+ static std::string UnpackCredential(const base::Value& packed);
+
+ // PrefStore::Observer implementation.
+ virtual void OnInitializationCompleted(bool succeeded) OVERRIDE;
+ virtual void OnPrefValueChanged(const std::string& key) OVERRIDE;
+
+ // Asynchronously looks for a cached credential file in the alternate profile
+ // and initiates start up using cached credentials if the file was found.
+ // Called by ProfileSyncService when it tries to start up on Windows 8 and
+ // cannot auto-start.
+ void LookForCachedCredentialsInAlternateProfile();
+
+ // Returns true if the service was able to successfully load credentials from
+ // the alternate profile.
+ bool SuccessfullyLoadedCredentials() const;
+
+ // Returns true if the credential cache represented by |store| contains a
+ // value for |pref_name|.
+ bool HasPref(scoped_refptr<JsonPrefStore> store,
+ const std::string& pref_name);
+
+ // Returns true if there is an empty value for kGoogleServicesUsername in the
+ // credential cache for the local profile (indicating that the user first
+ // signed in and then signed out). Returns false if there's no value at all
+ // (indicating that the user has never signed in) or if there's a non-empty
+ // value (indicating that the user is currently signed in).
+ bool HasUserSignedOut();
+
+ // Updates the value of |pref_name| to |new_value|, unless the user has signed
+ // out, in which case we write an empty string value to |pref_name|.
+ void UpdateStringPref(const std::string& pref_name,
+ const std::string& new_value);
+
+ // Updates the value of |pref_name| to |new_value|, unless the user has signed
+ // out, in which case we write "false" to |pref_name|.
+ void UpdateBooleanPref(const std::string& pref_name, bool new_value);
+
+ // Returns the string pref value contained in |store| for |pref_name|. Assumes
+ // that |store| contains a value for |pref_name|.
+ std::string GetStringPref(scoped_refptr<JsonPrefStore> store,
+ const std::string& pref_name);
+
+ // Returns the boolean pref value contained in |store| for |pref_name|.
+ // Assumes that |store| contains a value for |pref_name|.
+ bool GetBooleanPref(scoped_refptr<JsonPrefStore> store,
+ const std::string& pref_name);
+
+ // content::NotificationObserver implementation.
+ virtual void Observe(int type,
+ const content::NotificationSource& source,
+ const content::NotificationDetails& details) OVERRIDE;
+
+ protected:
+ // Used for write operations to the credential cache file in the local profile
+ // directory. This is separate from the chrome pref store. Protected so that
+ // it can be accessed by unit tests.
+ scoped_refptr<JsonPrefStore> local_store_;
+
+ private:
+ // Returns the path to the sync credentials file in the current profile
+ // directory.
+ FilePath GetCredentialPathInCurrentProfile() const;
+
+ // Returns the path to the sync credentials file in the default Desktop
+ // profile directory if we are running in Metro mode, and vice versa.
+ FilePath GetCredentialPathInAlternateProfile() const;
+
+ // Initializes the JsonPrefStore object for the local profile directory.
+ void InitializeLocalCredentialCacheWriter();
+
+ // Initializes the JsonPrefStore object for the alternate profile directory
+ // if |should_initialize| is true.
+ void InitializeAlternateCredentialCacheReader(bool* should_initialize);
+
+ // Loads cached sync credentials from the alternate profile and calls
+ // ApplyCachedCredentials if the load was successful.
+ void ReadCachedCredentialsFromAlternateProfile();
+
+ // Applies the credentials read from the alternate profile to the pref store
+ // and token service of the local profile and then notifies listeners.
+ void ApplyCachedCredentials(const std::string& google_services_username,
+ const std::string& sync_service_token,
+ const std::string& encryption_bootstrap_token,
+ bool keep_everything_synced,
+ const bool datatype_prefs[]);
+
+ // Keeps track of when we were able to successfully load credentials from
+ // the alternate profile.
+ bool loaded_credentials_;
+
+ // Profile for which credentials are being cached.
+ Profile* profile_;
+
+ // WeakPtr implementation.
+ base::WeakPtrFactory<CredentialCacheService> weak_factory_;
+
+ // Used for read operations on the credential cache file in the alternate
+ // profile directory. This is separate from the chrome pref store.
+ scoped_refptr<JsonPrefStore> alternate_store_;
+
+ // Registrar for notifications from the PrefService.
+ PrefChangeRegistrar pref_registrar_;
+
+ // Registrar for notifications from the TokenService.
+ 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.
+
+ DISALLOW_COPY_AND_ASSIGN(CredentialCacheService);
+};
+
+} // namespace syncer
+
+#endif // CHROME_BROWSER_SYNC_CREDENTIAL_CACHE_SERVICE_WIN_H_

Powered by Google App Engine
This is Rietveld 408576698