Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 #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 1. | |
|
Nicolas Zea
2015/09/03 22:44:16
Doesn't case 3 denote that N's value is already 1?
melandory
2015/09/04 15:35:26
Yep, it should be "we change value of N to 0." Tha
| |
| 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 row contains): | |
| 59 // | |
| 60 // NL* 0_ 1_ _0 _1 00 01 10 11 | |
|
Nicolas Zea
2015/09/03 22:44:16
What are the X and Y axes?
melandory
2015/09/04 15:35:26
Done.
| |
| 61 // | |
| 62 // 00 00 11 00 11 00 11 11 11 | |
| 63 // 01 x x x x x x x x # impossible state | |
| 64 // 10 x x x x 00 00 00 11 | |
| 65 // 11 00 11 00 11 00 00 00 11 | |
| 66 // | |
| 67 // The service observes changes to both preferences (e.g. changes from sync, | |
| 68 // changes from UI) and propagate the change to the other preference if needed. | |
| 69 // | |
| 70 // After service is created it waits until Profile and profile related services | |
| 71 // (such as ProfileSyncService) are initialized and after that observers for | |
| 72 // the sync state change and preferences changes are registered. | |
| 73 // | |
| 74 // Note: componetization of this service currently is impossible, because it | |
| 75 // depends on PrefServiceSyncable. | |
| 76 class PasswordManagerSettingMigraterService | |
| 77 : public KeyedService, | |
| 78 public PrefServiceSyncableObserver, | |
| 79 public content::NotificationObserver { | |
| 80 public: | |
| 81 class Factory : public BrowserContextKeyedServiceFactory { | |
| 82 public: | |
| 83 static Factory* GetInstance(); | |
| 84 static PasswordManagerSettingMigraterService* GetForProfile( | |
| 85 Profile* profile); | |
| 86 | |
| 87 private: | |
| 88 friend struct DefaultSingletonTraits<Factory>; | |
| 89 friend class PasswordManagerSettingMigraterServiceTest; | |
| 90 | |
| 91 Factory(); | |
| 92 ~Factory() override; | |
| 93 | |
| 94 // BrowserContextKeyedServiceFactory: | |
| 95 KeyedService* BuildServiceInstanceFor( | |
| 96 content::BrowserContext* profile) const override; | |
| 97 bool ServiceIsCreatedWithBrowserContext() const override; | |
| 98 }; | |
| 99 | |
| 100 explicit PasswordManagerSettingMigraterService(Profile* profile); | |
| 101 ~PasswordManagerSettingMigraterService() override; | |
| 102 | |
| 103 void Shutdown() override; | |
| 104 | |
| 105 // PrefServiceSyncableObserver: | |
| 106 void OnIsSyncingChanged() override; | |
| 107 | |
| 108 // content::NotificationObserver: | |
| 109 void Observe(int type, | |
| 110 const content::NotificationSource& source, | |
| 111 const content::NotificationDetails& details) override; | |
| 112 | |
| 113 private: | |
| 114 // Initializes the observers: preferences observers and sync status observers. | |
| 115 void InitObservers(); | |
| 116 | |
| 117 // Called when the value of the |kCredentialsEnableService| preference | |
| 118 // changes, and updates the value of |kPasswordManagerSavingEnabled| | |
| 119 // preference accordingly. | |
| 120 void OnCredentialsEnableServicePrefChanged( | |
| 121 const std::string& changed_pref_name); | |
| 122 | |
| 123 // Called when the value of the |kPasswordManagerSavingEnabled| preference | |
| 124 // changes, and updates the value of |kCredentialsEnableService| preference | |
| 125 // accordingly. | |
| 126 void OnPasswordManagerSavingEnabledPrefChanged( | |
| 127 const std::string& changed_pref_name); | |
| 128 | |
| 129 // Turns off one pref if another pref is off. | |
| 130 void MigrateOffState(PrefService* prefs); | |
| 131 | |
| 132 // Performs a migration after sync associates models. Migration is performed | |
| 133 // based on initial values for both preferences and values received from | |
| 134 // sync. | |
| 135 void MigrateAfterModelAssociation(PrefService* prefs); | |
| 136 | |
| 137 Profile* const profile_; | |
| 138 sync_driver::SyncService* sync_service_; | |
| 139 PrefChangeRegistrar pref_change_registrar_; | |
| 140 | |
| 141 // Used to register for notification that profile creation is complete. | |
| 142 content::NotificationRegistrar registrar_; | |
| 143 | |
| 144 std::vector<bool> initial_values_; | |
| 145 | |
| 146 // Contains values which has came from sync during model association step. | |
| 147 // After both preferences data types are associated, new value for the both | |
| 148 // preferences will be calculated. | |
| 149 std::vector<bool> sync_data_; | |
| 150 | |
| 151 // This vector contains initial value for both preferences. The vector maximum | |
| 152 // size is two, where first element is new kCredentialsEnableService | |
| 153 // preference and second element is legacy kPasswordManagerSavingEnabled | |
| 154 // preference. | |
| 155 bool initial_new_pref_value_; | |
| 156 bool initial_legacy_pref_value_; | |
| 157 | |
| 158 DISALLOW_COPY_AND_ASSIGN(PasswordManagerSettingMigraterService); | |
| 159 }; | |
| 160 | |
| 161 #endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATER_SER VICE_H_ | |
| OLD | NEW |