| Index: chrome/browser/password_manager/password_manager_setting_migrater_service.h
|
| diff --git a/chrome/browser/password_manager/password_manager_setting_migrater_service.h b/chrome/browser/password_manager/password_manager_setting_migrater_service.h
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..c7ac379dd96dc0c27f2c11b3ba59985aea3a9d59
|
| --- /dev/null
|
| +++ b/chrome/browser/password_manager/password_manager_setting_migrater_service.h
|
| @@ -0,0 +1,164 @@
|
| +// Copyright 2015 (c) 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_MIGRATER_SERVICE_H_
|
| +#define CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATER_SERVICE_H_
|
| +
|
| +#include <vector>
|
| +
|
| +#include "base/memory/singleton.h"
|
| +#include "base/prefs/pref_change_registrar.h"
|
| +#include "chrome/browser/prefs/pref_service_syncable_observer.h"
|
| +#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
|
| +#include "components/keyed_service/core/keyed_service.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 a startup of a
|
| +// service. This users can be in following states:
|
| +// 1. N = 0, L = 0
|
| +// 2. N = 1, L = 1
|
| +// 3. N = 1, L = 0
|
| +//
|
| +// Nothing should be done in case 1 and 2 since settings are already in sync;
|
| +// in case 3 we change value of N to 0.
|
| +// The service also observes changes to both preferences, so if one of the
|
| +// preferences is changed locally, then this change is propagated to the other
|
| +// preference.
|
| +//
|
| +// For users who are syncing we save the values for the new and legacy
|
| +// preference on service start up (|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 equals to 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 equals to 0 and old
|
| +// preference equals to 1, if we blindly propagate these values we first get
|
| +// both preferences equals to 0 after priority pref model was associated and
|
| +// then both preferences equals to 1 after preferences model was associated.
|
| +// But correct final value in this case is 0.
|
| +//
|
| +// On the finish of model association step we derive the new values for both
|
| +// settings using following table (first column contains local values for the
|
| +// preferences, first row contains values which has came from sync, where _ that
|
| +// value doesn't came):
|
| +//
|
| +// NL* 0_ 1_ _0 _1 00 01 10 11
|
| +//
|
| +// 00 00 11 00 11 00 11 11 11
|
| +// 01 x x x x x x x x # impossible state
|
| +// 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 propagate the change to the other preference if needed.
|
| +//
|
| +// After service is created it waits until Profile and profile related services
|
| +// (such as ProfileSyncService) are initialized and after that observers for
|
| +// the sync state change and preferences changes are registered.
|
| +//
|
| +// Note: componetization of this service currently is impossible, because it
|
| +// depends on PrefServiceSyncable.
|
| +class PasswordManagerSettingMigraterService
|
| + : public KeyedService,
|
| + public PrefServiceSyncableObserver,
|
| + public content::NotificationObserver {
|
| + public:
|
| + class Factory : public BrowserContextKeyedServiceFactory {
|
| + public:
|
| + static Factory* GetInstance();
|
| + static PasswordManagerSettingMigraterService* GetForProfile(
|
| + Profile* profile);
|
| +
|
| + private:
|
| + friend struct base::DefaultSingletonTraits<Factory>;
|
| + friend class PasswordManagerSettingMigraterServiceTest;
|
| +
|
| + Factory();
|
| + ~Factory() override;
|
| +
|
| + // BrowserContextKeyedServiceFactory:
|
| + KeyedService* BuildServiceInstanceFor(
|
| + content::BrowserContext* profile) const override;
|
| + bool ServiceIsCreatedWithBrowserContext() const override;
|
| + };
|
| +
|
| + explicit PasswordManagerSettingMigraterService(Profile* profile);
|
| + ~PasswordManagerSettingMigraterService() override;
|
| +
|
| + void Shutdown() override;
|
| +
|
| + // PrefServiceSyncableObserver:
|
| + void OnIsSyncingChanged() override;
|
| +
|
| + // content::NotificationObserver:
|
| + void Observe(int type,
|
| + const content::NotificationSource& source,
|
| + const content::NotificationDetails& details) override;
|
| +
|
| + virtual bool CanSyncStart();
|
| +
|
| + void InitObservers();
|
| +
|
| + private:
|
| + // Initializes the observers: preferences observers and sync status observers.
|
| +
|
| + // 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);
|
| +
|
| + 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_;
|
| +
|
| + // Contains values which has came from sync during model association step.
|
| + // After both preferences data types are associated, new value for the both
|
| + // preferences will be calculated.
|
| + std::vector<bool> sync_data_;
|
| +
|
| + // This vector contains initial value for both preferences. The vector maximum
|
| + // size is two, where first element is new kCredentialsEnableService
|
| + // preference and second element is legacy kPasswordManagerSavingEnabled
|
| + // preference.
|
| + bool initial_new_pref_value_;
|
| + bool initial_legacy_pref_value_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(PasswordManagerSettingMigraterService);
|
| +};
|
| +
|
| +#endif // CHROME_BROWSER_PASSWORD_MANAGER_PASSWORD_MANAGER_SETTING_MIGRATER_SERVICE_H_
|
|
|