Chromium Code Reviews| Index: chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.cc |
| diff --git a/chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.cc b/chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a5e9ee4d0dbfbec28ff2c3a10d7fef15b375102e |
| --- /dev/null |
| +++ b/chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.cc |
| @@ -0,0 +1,168 @@ |
| +// Copyright 2017 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. |
| + |
| +#include "chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.h" |
| + |
| +#include <algorithm> |
| +#include <iterator> |
| +#include <memory> |
| +#include <utility> |
| +#include <vector> |
| + |
| +#include "base/barrier_closure.h" |
| +#include "base/bind_helpers.h" |
| +#include "base/callback_helpers.h" |
| +#include "base/macros.h" |
| +#include "base/synchronization/lock.h" |
| +#include "base/win/registry.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "chrome/browser/extensions/extension_service.h" |
| +#include "chrome/browser/profile_resetter/profile_resetter.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/profiles/profile_manager.h" |
| +#include "chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_prefs_manager_win.h" |
| +#include "chrome/browser/ui/browser.h" |
| +#include "chrome/browser/ui/browser_finder.h" |
| +#include "components/chrome_cleaner/public/constants/constants.h" |
| + |
| +namespace safe_browsing { |
| + |
| +namespace { |
| + |
| +class SettingsResetter { |
|
alito
2017/05/26 20:58:07
Could you add a comment explaining the lifetime ma
ftirelo
2017/05/30 20:17:39
Done.
|
| + public: |
| + SettingsResetter(Profile* profile, |
| + std::unique_ptr<SettingsResetterDelegate> delegate, |
| + base::OnceClosure done_callback); |
| + virtual ~SettingsResetter(); |
| + |
| + // Resets settings of |profile_| and invokes |done_callback_| when done. |
| + void Run(); |
| + |
| + private: |
| + // Resets settings for |profile| according to default values given by |
| + // |master_settings|. Used as a callback for |
| + // DefaultSettingsFetcher::FetchDefaultSettings(). |
| + void OnFetchCompleted( |
| + std::unique_ptr<BrandcodedDefaultSettings> master_settings); |
| + |
| + // Invokes |done_callback_|, removes the settings reset tag for |profile_|, |
| + // and deletes this object. |
| + void OnResetCompleted(); |
| + |
| + // The profile to be reset. |
| + Profile* profile_; |
| + |
| + // The callback to be invoked once settings reset completes. |
| + base::OnceClosure done_callback_; |
| + |
| + std::unique_ptr<SettingsResetterDelegate> delegate_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(SettingsResetter); |
| +}; |
| + |
| +SettingsResetter::SettingsResetter( |
| + Profile* profile, |
| + std::unique_ptr<SettingsResetterDelegate> delegate, |
| + base::OnceClosure done_callback) |
| + : done_callback_(std::move(done_callback)), |
| + profile_(profile), |
| + delegate_(std::move(delegate)) { |
| + DCHECK(profile_); |
| + DCHECK(done_callback_); |
| + DCHECK(delegate_); |
| +} |
| + |
| +SettingsResetter::~SettingsResetter() { |
| + DCHECK(!done_callback_); |
| +} |
| + |
| +void SettingsResetter::Run() { |
| + delegate_->FetchDefaultSettings( |
| + base::Bind(&SettingsResetter::OnFetchCompleted, base::Unretained(this))); |
| +} |
| + |
| +ProfileResetter::ResettableFlags kSettingsToReset = |
|
alito
2017/05/26 20:58:07
Could this be moved into OnFetchCompleted?
ftirelo
2017/05/30 20:17:39
Done.
|
| + ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE | |
| + ProfileResetter::EXTENSIONS | ProfileResetter::STARTUP_PAGES | |
| + ProfileResetter::SHORTCUTS; |
| + |
| +void SettingsResetter::OnFetchCompleted( |
| + std::unique_ptr<BrandcodedDefaultSettings> master_settings) { |
| + delegate_->GetProfileResetter(profile_)->Reset( |
| + kSettingsToReset, std::move(master_settings), |
| + base::Bind(&SettingsResetter::OnResetCompleted, base::Unretained(this))); |
| +} |
| + |
| +void SettingsResetter::OnResetCompleted() { |
| + ChromeCleanerPrefsManager(profile_).RecordSettingsResetPending(false); |
| + std::move(done_callback_).Run(); |
| + delete this; |
| +} |
| + |
| +// Returns true if there is information of a completed cleanup in the registry. |
| +bool CleanupCompletedFromRegistry() { |
| + base::string16 cleaner_key_path( |
| + chrome_cleaner::kSoftwareRemovalToolRegistryKey); |
| + cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey); |
| + |
| + base::win::RegKey srt_cleaner_key(HKEY_CURRENT_USER, cleaner_key_path.c_str(), |
| + KEY_QUERY_VALUE); |
| + int64_t ts_cleanup_started = 0L, ts_cleanup_completed = 0L; |
| + return srt_cleaner_key.Valid() && |
| + srt_cleaner_key.ReadInt64( |
| + chrome_cleaner::kCleanupStartedTimestampValueName, |
| + &ts_cleanup_started) == ERROR_SUCCESS && |
| + srt_cleaner_key.ReadInt64( |
| + chrome_cleaner::kCleanupConfirmedTimestampValueName, |
| + &ts_cleanup_completed) == ERROR_SUCCESS && |
| + ts_cleanup_started < ts_cleanup_completed; |
| +} |
| + |
| +Profile* GetLastActiveProfile() { |
|
alito
2017/05/26 20:58:07
As discussed, we should maybe change the strategy
robertshield
2017/05/29 01:57:11
+1: I don't think we should use last active, I thi
ftirelo
2017/05/30 20:17:39
Done.
ftirelo
2017/05/30 20:17:39
Done.
|
| + Browser* browser = chrome::FindLastActive(); |
| + return browser ? browser->profile() : nullptr; |
| +} |
| + |
| +} // namespace |
| + |
| +void TagCurrentProfileForResetting() { |
| + Profile* profile = GetLastActiveProfile(); |
| + if (profile) |
| + ChromeCleanerPrefsManager(profile).RecordSettingsResetPending(true); |
| +} |
| + |
| +void ResetCurrentProfilePostCleanupIfTagged( |
| + base::OnceClosure continuation, |
| + std::unique_ptr<SettingsResetterDelegate> delegate) { |
| + Profile* profile = GetLastActiveProfile(); |
| + if (!profile || !ChromeCleanerPrefsManager(profile).SettingsResetPending() || |
| + !CleanupCompletedFromRegistry()) { |
| + std::move(continuation).Run(); |
| + return; |
| + } |
| + |
| + // This object will self-delete once |continuation| is invoked. |
| + (new SettingsResetter(profile, |
| + delegate ? std::move(delegate) |
| + : base::MakeUnique<SettingsResetterDelegate>(), |
| + std::move(continuation))) |
| + ->Run(); |
| +} |
| + |
| +SettingsResetterDelegate::SettingsResetterDelegate() {} |
| + |
| +SettingsResetterDelegate::~SettingsResetterDelegate() {} |
| + |
| +void SettingsResetterDelegate::FetchDefaultSettings( |
| + DefaultSettingsFetcher::SettingsCallback callback) { |
| + DefaultSettingsFetcher::FetchDefaultSettings(std::move(callback)); |
| +} |
| + |
| +std::unique_ptr<ProfileResetter> SettingsResetterDelegate::GetProfileResetter( |
| + Profile* profile) { |
| + return base::MakeUnique<ProfileResetter>(profile); |
| +} |
| + |
| +} // namespace safe_browsing |