Chromium Code Reviews| Index: chrome/browser/password_manager/password_manager_migration_service.cc |
| diff --git a/chrome/browser/password_manager/password_manager_migration_service.cc b/chrome/browser/password_manager/password_manager_migration_service.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..6a514d723b512365abac9f6ebfcfd085986d25b1 |
| --- /dev/null |
| +++ b/chrome/browser/password_manager/password_manager_migration_service.cc |
| @@ -0,0 +1,131 @@ |
| +// Copyright 2015 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. |
| + |
| +#include "chrome/browser/password_manager/password_manager_migration_service.h" |
| + |
| +#include "base/bind.h" |
| +#include "chrome/browser/password_manager/password_manager_migration_service.h" |
| +#include "chrome/browser/prefs/pref_service_syncable.h" |
| +#include "chrome/browser/profiles/incognito_helpers.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/search_engines/template_url_service_factory.h" |
| +#include "chrome/browser/sync/profile_sync_service.h" |
| +#include "chrome/browser/sync/profile_sync_service_factory.h" |
| +#include "components/keyed_service/content/browser_context_dependency_manager.h" |
| +#include "components/password_manager/core/common/password_manager_pref_names.h" |
| +#include "components/sync_driver/sync_service_observer.h" |
| + |
| +namespace password_manager { |
| + |
| +PasswordSettingsMigrationService::PasswordSettingsMigrationService( |
| + Profile* profile) |
| + : profile_(profile), profile_sync_service_(nullptr), weak_factory_(this) { |
| + if (ProfileSyncServiceFactory::HasProfileSyncService(profile_)) |
| + profile_sync_service_ = ProfileSyncServiceFactory::GetForProfile(profile_); |
| + InitObservers(); |
| +} |
| + |
| +PasswordSettingsMigrationService::PasswordSettingsMigrationService( |
| + Profile* profile, |
| + ProfileSyncService* profile_sync_service) |
| + : profile_(profile), |
| + profile_sync_service_(profile_sync_service), |
| + weak_factory_(this) {} |
| + |
| +PasswordSettingsMigrationService::~PasswordSettingsMigrationService() {} |
| + |
| +void PasswordSettingsMigrationService::InitObservers() { |
| + pref_change_registrar_.Init(profile_->GetPrefs()); |
| + pref_change_registrar_.Add( |
| + prefs::kCredentialEnableService, |
| + base::Bind(&PasswordSettingsMigrationService::OnPrefChanged, |
| + base::Unretained(this), prefs::kPasswordManagerSavingEnabled)); |
| + pref_change_registrar_.Add( |
| + prefs::kPasswordManagerSavingEnabled, |
| + base::Bind(&PasswordSettingsMigrationService::OnPrefChanged, |
| + base::Unretained(this), prefs::kCredentialEnableService)); |
|
engedy
2015/08/07 11:41:38
nit: Include bind_helpers.h for base::Unretained.
melandory
2015/08/12 09:03:23
Done.
|
| + PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| + if (profile_sync_service_ && profile_sync_service_->CanSyncStart()) { |
| + // This causes OnIsSyncingChanged to be called when the value of |
| + // PrefService::IsSyncing() changes. |
| + prefs->AddObserver(this); |
| + } else { |
| + PasswordSettingsMigrationService::Reconcile(prefs); |
| + } |
| +} |
| + |
| +void PasswordSettingsMigrationService::OnPrefChanged( |
| + const std::string& other_pref_name, |
| + const std::string& changed_pref_name) { |
| + PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| + bool changed_pref = prefs->GetBoolean(changed_pref_name.c_str()); |
| + bool other_pref = prefs->GetBoolean(other_pref_name.c_str()); |
| + if (changed_pref != other_pref) |
| + prefs->SetBoolean(other_pref_name, changed_pref); |
| + // TODO(melandory): add histograms in order to track when we can stop |
| + // migration. |
| +} |
| + |
| +void PasswordSettingsMigrationService::OnIsSyncingChanged() { |
| + PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_); |
| + if (prefs->IsSyncing()) { |
| + Reconcile(prefs); |
| + prefs->RemoveObserver(this); |
| + } |
| +} |
| + |
| +// static |
| +void PasswordSettingsMigrationService::Reconcile(PrefService* prefs) { |
| + bool old_pref = prefs->GetBoolean(prefs::kPasswordManagerSavingEnabled); |
| + if (!old_pref) { |
| + prefs->SetBoolean(prefs::kCredentialEnableService, false); |
| + } |
| +} |
| + |
| +// static |
| +PasswordSettingsMigrationService::Factory* |
| +PasswordSettingsMigrationService::Factory::GetInstance() { |
| + return Singleton<PasswordSettingsMigrationService::Factory>::get(); |
| +} |
| + |
| +// static |
| +PasswordSettingsMigrationService* |
| +PasswordSettingsMigrationService::Factory::GetForProfile(Profile* profile) { |
| + return static_cast<PasswordSettingsMigrationService*>( |
| + GetInstance()->GetServiceForBrowserContext(profile, true)); |
| +} |
| + |
| +PasswordSettingsMigrationService::Factory::Factory() |
| + : BrowserContextKeyedServiceFactory( |
| + "PasswordSettingsMigrationService", |
| + BrowserContextDependencyManager::GetInstance()) { |
| + DependsOn(ProfileSyncServiceFactory::GetInstance()); |
| + DependsOn(TemplateURLServiceFactory::GetInstance()); |
|
engedy
2015/08/07 11:41:38
I think we don't need this dependency.
melandory
2015/08/12 09:03:23
Done.
|
| +} |
| + |
| +PasswordSettingsMigrationService::Factory::~Factory() {} |
| + |
| +KeyedService* |
| +PasswordSettingsMigrationService::Factory::BuildServiceInstanceFor( |
| + content::BrowserContext* profile) const { |
| + return new PasswordSettingsMigrationService(static_cast<Profile*>(profile)); |
| +} |
| + |
| +bool PasswordSettingsMigrationService::Factory:: |
| + ServiceIsCreatedWithBrowserContext() const { |
| + return true; |
| +} |
| + |
| +bool PasswordSettingsMigrationService::Factory::ServiceIsNULLWhileTesting() |
| + const { |
| + return false; |
|
engedy
2015/08/07 11:41:38
You may omit the override as this is the default b
melandory
2015/08/12 09:03:23
Done.
|
| +} |
| + |
| +content::BrowserContext* |
| +PasswordSettingsMigrationService::Factory::GetBrowserContextToUse( |
|
engedy
2015/08/07 11:41:38
You may omit this override as well. I think it's f
melandory
2015/08/12 09:03:23
Done.
|
| + content::BrowserContext* context) const { |
| + return chrome::GetBrowserContextRedirectedInIncognito(context); |
| +} |
| + |
| +} // namespace password_manager |