Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(226)

Side by Side Diff: chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.cc

Issue 2906103002: Post-cleanup settings reset. (Closed)
Patch Set: Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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 #include "chrome/browser/safe_browsing/chrome_cleaner/settings_resetter_win.h"
6
7 #include <algorithm>
8 #include <iterator>
9 #include <memory>
10 #include <utility>
11 #include <vector>
12
13 #include "base/barrier_closure.h"
14 #include "base/bind_helpers.h"
15 #include "base/callback_helpers.h"
16 #include "base/macros.h"
17 #include "base/synchronization/lock.h"
18 #include "base/win/registry.h"
19 #include "chrome/browser/browser_process.h"
20 #include "chrome/browser/extensions/extension_service.h"
21 #include "chrome/browser/profile_resetter/profile_resetter.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/profile_manager.h"
24 #include "chrome/browser/safe_browsing/chrome_cleaner/chrome_cleaner_prefs_manag er_win.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_finder.h"
27 #include "components/chrome_cleaner/public/constants/constants.h"
28
29 namespace safe_browsing {
30
31 namespace {
32
33 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.
34 public:
35 SettingsResetter(Profile* profile,
36 std::unique_ptr<SettingsResetterDelegate> delegate,
37 base::OnceClosure done_callback);
38 virtual ~SettingsResetter();
39
40 // Resets settings of |profile_| and invokes |done_callback_| when done.
41 void Run();
42
43 private:
44 // Resets settings for |profile| according to default values given by
45 // |master_settings|. Used as a callback for
46 // DefaultSettingsFetcher::FetchDefaultSettings().
47 void OnFetchCompleted(
48 std::unique_ptr<BrandcodedDefaultSettings> master_settings);
49
50 // Invokes |done_callback_|, removes the settings reset tag for |profile_|,
51 // and deletes this object.
52 void OnResetCompleted();
53
54 // The profile to be reset.
55 Profile* profile_;
56
57 // The callback to be invoked once settings reset completes.
58 base::OnceClosure done_callback_;
59
60 std::unique_ptr<SettingsResetterDelegate> delegate_;
61
62 DISALLOW_COPY_AND_ASSIGN(SettingsResetter);
63 };
64
65 SettingsResetter::SettingsResetter(
66 Profile* profile,
67 std::unique_ptr<SettingsResetterDelegate> delegate,
68 base::OnceClosure done_callback)
69 : done_callback_(std::move(done_callback)),
70 profile_(profile),
71 delegate_(std::move(delegate)) {
72 DCHECK(profile_);
73 DCHECK(done_callback_);
74 DCHECK(delegate_);
75 }
76
77 SettingsResetter::~SettingsResetter() {
78 DCHECK(!done_callback_);
79 }
80
81 void SettingsResetter::Run() {
82 delegate_->FetchDefaultSettings(
83 base::Bind(&SettingsResetter::OnFetchCompleted, base::Unretained(this)));
84 }
85
86 ProfileResetter::ResettableFlags kSettingsToReset =
alito 2017/05/26 20:58:07 Could this be moved into OnFetchCompleted?
ftirelo 2017/05/30 20:17:39 Done.
87 ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE |
88 ProfileResetter::EXTENSIONS | ProfileResetter::STARTUP_PAGES |
89 ProfileResetter::SHORTCUTS;
90
91 void SettingsResetter::OnFetchCompleted(
92 std::unique_ptr<BrandcodedDefaultSettings> master_settings) {
93 delegate_->GetProfileResetter(profile_)->Reset(
94 kSettingsToReset, std::move(master_settings),
95 base::Bind(&SettingsResetter::OnResetCompleted, base::Unretained(this)));
96 }
97
98 void SettingsResetter::OnResetCompleted() {
99 ChromeCleanerPrefsManager(profile_).RecordSettingsResetPending(false);
100 std::move(done_callback_).Run();
101 delete this;
102 }
103
104 // Returns true if there is information of a completed cleanup in the registry.
105 bool CleanupCompletedFromRegistry() {
106 base::string16 cleaner_key_path(
107 chrome_cleaner::kSoftwareRemovalToolRegistryKey);
108 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey);
109
110 base::win::RegKey srt_cleaner_key(HKEY_CURRENT_USER, cleaner_key_path.c_str(),
111 KEY_QUERY_VALUE);
112 int64_t ts_cleanup_started = 0L, ts_cleanup_completed = 0L;
113 return srt_cleaner_key.Valid() &&
114 srt_cleaner_key.ReadInt64(
115 chrome_cleaner::kCleanupStartedTimestampValueName,
116 &ts_cleanup_started) == ERROR_SUCCESS &&
117 srt_cleaner_key.ReadInt64(
118 chrome_cleaner::kCleanupConfirmedTimestampValueName,
119 &ts_cleanup_completed) == ERROR_SUCCESS &&
120 ts_cleanup_started < ts_cleanup_completed;
121 }
122
123 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.
124 Browser* browser = chrome::FindLastActive();
125 return browser ? browser->profile() : nullptr;
126 }
127
128 } // namespace
129
130 void TagCurrentProfileForResetting() {
131 Profile* profile = GetLastActiveProfile();
132 if (profile)
133 ChromeCleanerPrefsManager(profile).RecordSettingsResetPending(true);
134 }
135
136 void ResetCurrentProfilePostCleanupIfTagged(
137 base::OnceClosure continuation,
138 std::unique_ptr<SettingsResetterDelegate> delegate) {
139 Profile* profile = GetLastActiveProfile();
140 if (!profile || !ChromeCleanerPrefsManager(profile).SettingsResetPending() ||
141 !CleanupCompletedFromRegistry()) {
142 std::move(continuation).Run();
143 return;
144 }
145
146 // This object will self-delete once |continuation| is invoked.
147 (new SettingsResetter(profile,
148 delegate ? std::move(delegate)
149 : base::MakeUnique<SettingsResetterDelegate>(),
150 std::move(continuation)))
151 ->Run();
152 }
153
154 SettingsResetterDelegate::SettingsResetterDelegate() {}
155
156 SettingsResetterDelegate::~SettingsResetterDelegate() {}
157
158 void SettingsResetterDelegate::FetchDefaultSettings(
159 DefaultSettingsFetcher::SettingsCallback callback) {
160 DefaultSettingsFetcher::FetchDefaultSettings(std::move(callback));
161 }
162
163 std::unique_ptr<ProfileResetter> SettingsResetterDelegate::GetProfileResetter(
164 Profile* profile) {
165 return base::MakeUnique<ProfileResetter>(profile);
166 }
167
168 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698