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

Side by Side Diff: chrome/browser/password_manager/password_manager_migration_service.cc

Issue 1256803002: [Smart Lock, Prefs reconciliation] Prefs migration logic for desktop platforms. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix mac Created 5 years, 4 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
OLDNEW
(Empty)
1 // Copyright 2015 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 #include "chrome/browser/password_manager/password_manager_migration_service.h"
6
7 #include "base/bind.h"
8 #include "chrome/browser/password_manager/password_manager_migration_service.h"
9 #include "chrome/browser/prefs/pref_service_syncable.h"
10 #include "chrome/browser/profiles/incognito_helpers.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/browser/search_engines/template_url_service_factory.h"
13 #include "chrome/browser/sync/profile_sync_service.h"
14 #include "chrome/browser/sync/profile_sync_service_factory.h"
15 #include "components/keyed_service/content/browser_context_dependency_manager.h"
16 #include "components/password_manager/core/common/password_manager_pref_names.h"
17 #include "components/sync_driver/sync_service_observer.h"
18
19 namespace password_manager {
20
21 PasswordSettingsMigrationService::PasswordSettingsMigrationService(
22 Profile* profile)
23 : profile_(profile), profile_sync_service_(nullptr), weak_factory_(this) {
24 if (ProfileSyncServiceFactory::HasProfileSyncService(profile_))
25 profile_sync_service_ = ProfileSyncServiceFactory::GetForProfile(profile_);
26 InitObservers();
27 }
28
29 PasswordSettingsMigrationService::PasswordSettingsMigrationService(
30 Profile* profile,
31 ProfileSyncService* profile_sync_service)
32 : profile_(profile),
33 profile_sync_service_(profile_sync_service),
34 weak_factory_(this) {}
35
36 PasswordSettingsMigrationService::~PasswordSettingsMigrationService() {}
37
38 void PasswordSettingsMigrationService::InitObservers() {
39 pref_change_registrar_.Init(profile_->GetPrefs());
40 pref_change_registrar_.Add(
41 prefs::kCredentialEnableService,
42 base::Bind(&PasswordSettingsMigrationService::OnPrefChanged,
43 base::Unretained(this), prefs::kPasswordManagerSavingEnabled));
44 pref_change_registrar_.Add(
45 prefs::kPasswordManagerSavingEnabled,
46 base::Bind(&PasswordSettingsMigrationService::OnPrefChanged,
47 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.
48 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
49 if (profile_sync_service_ && profile_sync_service_->CanSyncStart()) {
50 // This causes OnIsSyncingChanged to be called when the value of
51 // PrefService::IsSyncing() changes.
52 prefs->AddObserver(this);
53 } else {
54 PasswordSettingsMigrationService::Reconcile(prefs);
55 }
56 }
57
58 void PasswordSettingsMigrationService::OnPrefChanged(
59 const std::string& other_pref_name,
60 const std::string& changed_pref_name) {
61 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
62 bool changed_pref = prefs->GetBoolean(changed_pref_name.c_str());
63 bool other_pref = prefs->GetBoolean(other_pref_name.c_str());
64 if (changed_pref != other_pref)
65 prefs->SetBoolean(other_pref_name, changed_pref);
66 // TODO(melandory): add histograms in order to track when we can stop
67 // migration.
68 }
69
70 void PasswordSettingsMigrationService::OnIsSyncingChanged() {
71 PrefServiceSyncable* prefs = PrefServiceSyncable::FromProfile(profile_);
72 if (prefs->IsSyncing()) {
73 Reconcile(prefs);
74 prefs->RemoveObserver(this);
75 }
76 }
77
78 // static
79 void PasswordSettingsMigrationService::Reconcile(PrefService* prefs) {
80 bool old_pref = prefs->GetBoolean(prefs::kPasswordManagerSavingEnabled);
81 if (!old_pref) {
82 prefs->SetBoolean(prefs::kCredentialEnableService, false);
83 }
84 }
85
86 // static
87 PasswordSettingsMigrationService::Factory*
88 PasswordSettingsMigrationService::Factory::GetInstance() {
89 return Singleton<PasswordSettingsMigrationService::Factory>::get();
90 }
91
92 // static
93 PasswordSettingsMigrationService*
94 PasswordSettingsMigrationService::Factory::GetForProfile(Profile* profile) {
95 return static_cast<PasswordSettingsMigrationService*>(
96 GetInstance()->GetServiceForBrowserContext(profile, true));
97 }
98
99 PasswordSettingsMigrationService::Factory::Factory()
100 : BrowserContextKeyedServiceFactory(
101 "PasswordSettingsMigrationService",
102 BrowserContextDependencyManager::GetInstance()) {
103 DependsOn(ProfileSyncServiceFactory::GetInstance());
104 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.
105 }
106
107 PasswordSettingsMigrationService::Factory::~Factory() {}
108
109 KeyedService*
110 PasswordSettingsMigrationService::Factory::BuildServiceInstanceFor(
111 content::BrowserContext* profile) const {
112 return new PasswordSettingsMigrationService(static_cast<Profile*>(profile));
113 }
114
115 bool PasswordSettingsMigrationService::Factory::
116 ServiceIsCreatedWithBrowserContext() const {
117 return true;
118 }
119
120 bool PasswordSettingsMigrationService::Factory::ServiceIsNULLWhileTesting()
121 const {
122 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.
123 }
124
125 content::BrowserContext*
126 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.
127 content::BrowserContext* context) const {
128 return chrome::GetBrowserContextRedirectedInIncognito(context);
129 }
130
131 } // namespace password_manager
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698