| Index: chrome/browser/password_manager/password_manager_setting_migrator_service.h
|
| diff --git a/chrome/browser/password_manager/password_manager_setting_migrator_service.h b/chrome/browser/password_manager/password_manager_setting_migrator_service.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..3399b85035b04c692d5e120d8b29b9381b0311e7
|
| --- /dev/null
|
| +++ b/chrome/browser/password_manager/password_manager_setting_migrator_service.h
|
| @@ -0,0 +1,167 @@
|
| +// Copyright (c) 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.
|
| +
|
| +#ifndef CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATOR_SERVICE_H_
|
| +#define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATOR_SERVICE_H_
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/memory/singleton.h"
|
| +#include "base/prefs/pref_change_registrar.h"
|
| +#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
|
| +#include "components/keyed_service/core/keyed_service.h"
|
| +#include "components/syncable_prefs/pref_service_syncable_observer.h"
|
| +#include "content/public/browser/notification_observer.h"
|
| +#include "content/public/browser/notification_registrar.h"
|
| +
|
| +class Profile;
|
| +
|
| +namespace sync_driver {
|
| +class SyncService;
|
| +}
|
| +
|
| +// Service that is responsible for reconciling the legacy "Offer to save your
|
| +// web passwords" setting (henceforth denoted 'L', for legacy) with the new
|
| +// "Enable Smart Lock for Passwords" setting (henceforth denoted 'N', for new).
|
| +//
|
| +// It works as follows.
|
| +//
|
| +// Users who are not syncing (this is checked on start-up of the service
|
| +// calling to SyncService:CanSyncStart()) are migrated on startup of the
|
| +// service. These users can be in following states:
|
| +// 1. N = 0, L = 0
|
| +// 2. N = 1, L = 1
|
| +// 3. N = 1, L = 0
|
| +// 4. N = 0, L = 1
|
| +//
|
| +// Nothing should be done in case 1 and 2 since settings are already in sync;
|
| +// in case 3,4 we change value of N to 0.
|
| +//
|
| +// For users who are syncing we save the values for the new and legacy
|
| +// preference on service startup (|inital_values_|) and the values that come
|
| +// from sync during models association step (|sync_data_|). Propagating change
|
| +// of one preference to another preference without having this special treatment
|
| +// will not always work. Here is an example which illustrates possible corner
|
| +// case: the client executes the migration code for the first time, legacy
|
| +// preference has a value 1, new preference was registered for the
|
| +// first time and has default value which is also 1, the sync data snapshot
|
| +// which user has on a client contains new preference equal to 0 and old
|
| +// preference equal to 1, if we blindly propagate these values we first get
|
| +// both preferences equal to 0 after priority pref model was associated and
|
| +// then both preferences equal to 1 after preferences model was associated.
|
| +// But the correct final value in this case is 0.
|
| +//
|
| +// At the end of model association step we derive the new values for both
|
| +// settings using the following table (first column contains local values for
|
| +// the preferences, first row contains values which came from sync, where _
|
| +// means that the value did not come):
|
| +//
|
| +// NL* 0_ 1_ _0 _1 00 01 10 11
|
| +//
|
| +// 00 00 11 00 11 00 11 11 11
|
| +// 01 x x x x 00 00 00 11
|
| +// 10 x x x x 00 00 00 11
|
| +// 11 00 11 00 11 00 00 00 11
|
| +//
|
| +// The service observes changes to both preferences (e.g. changes from sync,
|
| +// changes from UI) and propagates the change to the other preference if needed.
|
| +//
|
| +// Note: componetization of this service currently is impossible, because it
|
| +// depends on PrefServiceSyncable https://crbug.com/522536.
|
| +class PasswordManagerSettingMigratorService
|
| + : public KeyedService,
|
| + public syncable_prefs::PrefServiceSyncableObserver,
|
| + public content::NotificationObserver {
|
| + public:
|
| + class Factory : public BrowserContextKeyedServiceFactory {
|
| + public:
|
| + static Factory* GetInstance();
|
| + static PasswordManagerSettingMigratorService* GetForProfile(
|
| + Profile* profile);
|
| +
|
| + private:
|
| + friend struct base::DefaultSingletonTraits<Factory>;
|
| + friend class PasswordManagerSettingMigratorServiceTest;
|
| +
|
| + Factory();
|
| + ~Factory() override;
|
| +
|
| + // BrowserContextKeyedServiceFactory:
|
| + KeyedService* BuildServiceInstanceFor(
|
| + content::BrowserContext* profile) const override;
|
| + bool ServiceIsCreatedWithBrowserContext() const override;
|
| + };
|
| +
|
| + explicit PasswordManagerSettingMigratorService(Profile* profile);
|
| + ~PasswordManagerSettingMigratorService() override;
|
| +
|
| + void Shutdown() override;
|
| +
|
| + // PrefServiceSyncableObserver:
|
| + void OnIsSyncingChanged() override;
|
| +
|
| + // content::NotificationObserver:
|
| + void Observe(int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) override;
|
| +
|
| + private:
|
| + // Initializes the observers: preferences observers and sync status observers.
|
| + void InitObservers();
|
| +
|
| + // Returns true if sync is expected to start for the user.
|
| + bool CanSyncStart();
|
| +
|
| + // Called when the value of the |kCredentialsEnableService| preference
|
| + // changes, and updates the value of |kPasswordManagerSavingEnabled|
|
| + // preference accordingly.
|
| + void OnCredentialsEnableServicePrefChanged(
|
| + const std::string& changed_pref_name);
|
| +
|
| + // Called when the value of the |kPasswordManagerSavingEnabled| preference
|
| + // changes, and updates the value of |kCredentialsEnableService| preference
|
| + // accordingly.
|
| + void OnPasswordManagerSavingEnabledPrefChanged(
|
| + const std::string& changed_pref_name);
|
| +
|
| + // Turns off one pref if another pref is off.
|
| + void MigrateOffState(PrefService* prefs);
|
| +
|
| + // Performs a migration after sync associates models. Migration is performed
|
| + // based on initial values for both preferences and values received from
|
| + // sync.
|
| + void MigrateAfterModelAssociation(PrefService* prefs);
|
| +
|
| + // Contains values which have come from sync during model association step.
|
| + // The vector minimum size is 0 and the vector maximum size is 4:
|
| + // * sync_data_ has length equal to 0, when no change to the preferences has
|
| + // came from sync.
|
| + // * sync_data has length equal to 1, when change to only one preference
|
| + // came from sync and another pref already had this value, e.g local state
|
| + // 01 and N=1 came from sync.
|
| + // * sync_data has length equals to 4, changes to both preference has came
|
| + // from sync, e.g. local state is 11 and 01 came from sync.
|
| + // This way index 0 corresponds to kCredentialsEnableService, last index
|
| + // corresponds to kPasswordManagerSavingEnabled if size of sync_data_ equals
|
| + // to 4, otherwise the vector contains the value only for one preference.
|
| + std::vector<bool> sync_data_;
|
| +
|
| + // The initial value for kCredentialsEnableService.
|
| + bool initial_new_pref_value_;
|
| + // The initial value for kPasswordManagerSavingEnabled.
|
| + bool initial_legacy_pref_value_;
|
| +
|
| + Profile* const profile_;
|
| +
|
| + sync_driver::SyncService* sync_service_;
|
| +
|
| + PrefChangeRegistrar pref_change_registrar_;
|
| +
|
| + // Used to register for notification that profile creation is complete.
|
| + content::NotificationRegistrar registrar_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PasswordManagerSettingMigratorService);
|
| +};
|
| +
|
| +#endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATOR_SERVICE_H_
|
|
|