| OLD | NEW |
| (Empty) | |
| 1 // Copyright 2015 (c) 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_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATER_SERVIC
E_H_ |
| 6 #define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATER_SERVIC
E_H_ |
| 7 |
| 8 #include <vector> |
| 9 |
| 10 #include "base/memory/singleton.h" |
| 11 #include "base/prefs/pref_change_registrar.h" |
| 12 #include "chrome/browser/prefs/pref_service_syncable_observer.h" |
| 13 #include "components/keyed_service/content/browser_context_keyed_service_factory
.h" |
| 14 #include "components/keyed_service/core/keyed_service.h" |
| 15 #include "content/public/browser/notification_observer.h" |
| 16 #include "content/public/browser/notification_registrar.h" |
| 17 |
| 18 class Profile; |
| 19 |
| 20 namespace sync_driver { |
| 21 class SyncService; |
| 22 } |
| 23 |
| 24 // Service that is responsible for reconciling the legacy "Offer to save your |
| 25 // web passwords" setting (henceforth denoted 'L', for legacy) with the new |
| 26 // "Enable Smart Lock for Passwords" setting (henceforth denoted 'N', for new). |
| 27 // |
| 28 // It works as follows. |
| 29 // |
| 30 // Users who are not syncing (this is checked on start-up of the service |
| 31 // calling to SyncService:CanSyncStart()) are migrated on a startup of a |
| 32 // service. This users can be in following states: |
| 33 // 1. N = 0, L = 0 |
| 34 // 2. N = 1, L = 1 |
| 35 // 3. N = 1, L = 0 |
| 36 // |
| 37 // Nothing should be done in case 1 and 2 since settings are already in sync; |
| 38 // in case 3 we change value of N to 0. |
| 39 // The service also observes changes to both preferences, so if one of the |
| 40 // preferences is changed locally, then this change is propagated to the other |
| 41 // preference. |
| 42 // |
| 43 // For users who are syncing we save the values for the new and legacy |
| 44 // preference on service start up (|inital_values_|) and the values that come |
| 45 // from sync during models association step (|sync_data_|). Propagating change |
| 46 // of one preference to another preference without having this special treatment |
| 47 // will not always work. Here is an example which illustrates possible corner |
| 48 // case: the client executes the migration code for the first time, legacy |
| 49 // preference has a value equals to 1, new preference was registered for the |
| 50 // first time and has default value which is also 1, the sync data snapshot |
| 51 // which user has on a client contains new preference equals to 0 and old |
| 52 // preference equals to 1, if we blindly propagate these values we first get |
| 53 // both preferences equals to 0 after priority pref model was associated and |
| 54 // then both preferences equals to 1 after preferences model was associated. |
| 55 // But correct final value in this case is 0. |
| 56 // |
| 57 // On the finish of model association step we derive the new values for both |
| 58 // settings using following table (first column contains local values for the |
| 59 // preferences, first row contains values which has came from sync, where _ that |
| 60 // value doesn't came): |
| 61 // |
| 62 // NL* 0_ 1_ _0 _1 00 01 10 11 |
| 63 // |
| 64 // 00 00 11 00 11 00 11 11 11 |
| 65 // 01 x x x x x x x x # impossible state |
| 66 // 10 x x x x 00 00 00 11 |
| 67 // 11 00 11 00 11 00 00 00 11 |
| 68 // |
| 69 // The service observes changes to both preferences (e.g. changes from sync, |
| 70 // changes from UI) and propagate the change to the other preference if needed. |
| 71 // |
| 72 // After service is created it waits until Profile and profile related services |
| 73 // (such as ProfileSyncService) are initialized and after that observers for |
| 74 // the sync state change and preferences changes are registered. |
| 75 // |
| 76 // Note: componetization of this service currently is impossible, because it |
| 77 // depends on PrefServiceSyncable. |
| 78 class PasswordManagerSettingMigraterService |
| 79 : public KeyedService, |
| 80 public PrefServiceSyncableObserver, |
| 81 public content::NotificationObserver { |
| 82 public: |
| 83 class Factory : public BrowserContextKeyedServiceFactory { |
| 84 public: |
| 85 static Factory* GetInstance(); |
| 86 static PasswordManagerSettingMigraterService* GetForProfile( |
| 87 Profile* profile); |
| 88 |
| 89 private: |
| 90 friend struct base::DefaultSingletonTraits<Factory>; |
| 91 friend class PasswordManagerSettingMigraterServiceTest; |
| 92 |
| 93 Factory(); |
| 94 ~Factory() override; |
| 95 |
| 96 // BrowserContextKeyedServiceFactory: |
| 97 KeyedService* BuildServiceInstanceFor( |
| 98 content::BrowserContext* profile) const override; |
| 99 bool ServiceIsCreatedWithBrowserContext() const override; |
| 100 }; |
| 101 |
| 102 explicit PasswordManagerSettingMigraterService(Profile* profile); |
| 103 ~PasswordManagerSettingMigraterService() override; |
| 104 |
| 105 void Shutdown() override; |
| 106 |
| 107 // PrefServiceSyncableObserver: |
| 108 void OnIsSyncingChanged() override; |
| 109 |
| 110 // content::NotificationObserver: |
| 111 void Observe(int type, |
| 112 const content::NotificationSource& source, |
| 113 const content::NotificationDetails& details) override; |
| 114 |
| 115 virtual bool CanSyncStart(); |
| 116 |
| 117 void InitObservers(); |
| 118 |
| 119 private: |
| 120 // Initializes the observers: preferences observers and sync status observers. |
| 121 |
| 122 // Called when the value of the |kCredentialsEnableService| preference |
| 123 // changes, and updates the value of |kPasswordManagerSavingEnabled| |
| 124 // preference accordingly. |
| 125 void OnCredentialsEnableServicePrefChanged( |
| 126 const std::string& changed_pref_name); |
| 127 |
| 128 // Called when the value of the |kPasswordManagerSavingEnabled| preference |
| 129 // changes, and updates the value of |kCredentialsEnableService| preference |
| 130 // accordingly. |
| 131 void OnPasswordManagerSavingEnabledPrefChanged( |
| 132 const std::string& changed_pref_name); |
| 133 |
| 134 // Turns off one pref if another pref is off. |
| 135 void MigrateOffState(PrefService* prefs); |
| 136 |
| 137 // Performs a migration after sync associates models. Migration is performed |
| 138 // based on initial values for both preferences and values received from |
| 139 // sync. |
| 140 void MigrateAfterModelAssociation(PrefService* prefs); |
| 141 |
| 142 Profile* const profile_; |
| 143 sync_driver::SyncService* sync_service_; |
| 144 PrefChangeRegistrar pref_change_registrar_; |
| 145 |
| 146 // Used to register for notification that profile creation is complete. |
| 147 content::NotificationRegistrar registrar_; |
| 148 |
| 149 // Contains values which has came from sync during model association step. |
| 150 // After both preferences data types are associated, new value for the both |
| 151 // preferences will be calculated. |
| 152 std::vector<bool> sync_data_; |
| 153 |
| 154 // This vector contains initial value for both preferences. The vector maximum |
| 155 // size is two, where first element is new kCredentialsEnableService |
| 156 // preference and second element is legacy kPasswordManagerSavingEnabled |
| 157 // preference. |
| 158 bool initial_new_pref_value_; |
| 159 bool initial_legacy_pref_value_; |
| 160 |
| 161 DISALLOW_COPY_AND_ASSIGN(PasswordManagerSettingMigraterService); |
| 162 }; |
| 163 |
| 164 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATER_SER
VICE_H_ |
| OLD | NEW |