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

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

Issue 2906103002: Post-cleanup settings reset. (Closed)
Patch Set: Nits Created 3 years, 6 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/memory/ref_counted.h"
18 #include "base/synchronization/lock.h"
19 #include "base/win/registry.h"
20 #include "chrome/browser/browser_process.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/srt_field_trial_win.h"
25 #include "chrome/browser/ui/browser.h"
26 #include "chrome/browser/ui/browser_finder.h"
27 #include "chrome/common/pref_names.h"
28 #include "components/chrome_cleaner/public/constants/constants.h"
29 #include "components/pref_registry/pref_registry_syncable.h"
30 #include "components/prefs/pref_service.h"
31 #include "content/public/browser/browser_thread.h"
32
33 namespace safe_browsing {
34
35 namespace {
36
37 // Manages post-cleanup settings reset for a list of profiles.
38 // An instance of this class is created by ResetPostCleanupSettingsIfTagged()
39 // and will self-delete once all profiles in the list have been reset.
40 class SettingsResetter : public base::RefCounted<SettingsResetter> {
41 public:
42 SettingsResetter(std::vector<Profile*> profiles_to_reset,
43 std::unique_ptr<SettingsResetterDelegate> delegate,
44 base::OnceClosure done_callback);
45 virtual ~SettingsResetter();
46
47 // Resets settings for all profiles in |profiles_to_reset__| and invokes
48 // |done_callback_| when done.
49 void Run();
50
51 private:
52 // Resets settings for |profile| according to default values given by
53 // |master_settings|. Used as a callback for
54 // DefaultSettingsFetcher::FetchDefaultSettings().
55 void OnFetchCompleted(
56 Profile* profile,
57 std::unique_ptr<BrandcodedDefaultSettings> master_settings);
58
59 // Removes the settings reset tag for |profile|. If there are no more
60 // profiles to reset, invokes |done_callback_| and deletes this object.
61 void OnResetCompleted(Profile* profile);
62
63 // The profiles to be reset.
64 std::vector<Profile*> profiles_to_reset_;
65
66 // The number of profiles that need to be reset.
67 int num_pending_resets_;
68
69 // The callback to be invoked once settings reset completes.
70 base::OnceClosure done_callback_;
71
72 std::unique_ptr<SettingsResetterDelegate> delegate_;
73
74 DISALLOW_COPY_AND_ASSIGN(SettingsResetter);
75 };
76
77 SettingsResetter::SettingsResetter(
78 std::vector<Profile*> profiles_to_reset,
79 std::unique_ptr<SettingsResetterDelegate> delegate,
80 base::OnceClosure done_callback)
81 : done_callback_(std::move(done_callback)),
82 profiles_to_reset_(std::move(profiles_to_reset)),
83 num_pending_resets_(profiles_to_reset_.size()),
84 delegate_(std::move(delegate)) {
85 DCHECK_LT(0, num_pending_resets_);
86 DCHECK(done_callback_);
87 DCHECK(delegate_);
88 }
89
90 SettingsResetter::~SettingsResetter() {
91 DCHECK(!done_callback_);
92 DCHECK(!num_pending_resets_);
93 }
94
95 void SettingsResetter::Run() {
96 for (Profile* profile : profiles_to_reset_) {
97 delegate_->FetchDefaultSettings(
98 base::Bind(&SettingsResetter::OnFetchCompleted, this, profile));
99 }
100 }
101
102 void SettingsResetter::OnFetchCompleted(
103 Profile* profile,
104 std::unique_ptr<BrandcodedDefaultSettings> master_settings) {
105 static const ProfileResetter::ResettableFlags kSettingsToReset =
106 ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE |
107 ProfileResetter::EXTENSIONS | ProfileResetter::STARTUP_PAGES |
108 ProfileResetter::SHORTCUTS;
109
110 delegate_->GetProfileResetter(profile)->Reset(
111 kSettingsToReset, std::move(master_settings),
112 base::Bind(&SettingsResetter::OnResetCompleted, base::Unretained(this),
113 profile));
114 }
115
116 void SettingsResetter::OnResetCompleted(Profile* profile) {
117 DCHECK_LT(0, num_pending_resets_);
118
119 RecordPostCleanupSettingsResetPending(false, profile);
120
121 --num_pending_resets_;
122 if (!num_pending_resets_)
123 std::move(done_callback_).Run();
124 }
125
126 // Returns true if there is information of a completed cleanup in the registry.
127 bool CleanupCompletedFromRegistry() {
128 base::string16 cleaner_key_path(
129 chrome_cleaner::kSoftwareRemovalToolRegistryKey);
130 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey);
131
132 base::win::RegKey srt_cleaner_key(HKEY_CURRENT_USER, cleaner_key_path.c_str(),
133 KEY_QUERY_VALUE);
134 int64_t ts_cleanup_started = 0L, ts_cleanup_completed = 0L;
135 return srt_cleaner_key.Valid() &&
136 srt_cleaner_key.ReadInt64(
137 chrome_cleaner::kCleanupStartedTimestampValueName,
138 &ts_cleanup_started) == ERROR_SUCCESS &&
139 srt_cleaner_key.ReadInt64(
140 chrome_cleaner::kCleanupConfirmedTimestampValueName,
141 &ts_cleanup_completed) == ERROR_SUCCESS &&
142 ts_cleanup_started < ts_cleanup_completed;
143 }
144
145 bool CopyProfilesToReset(const std::vector<Profile*>& profiles,
146 std::vector<Profile*>* profiles_to_reset) {
147 std::copy_if(profiles.begin(), profiles.end(),
148 std::back_inserter(*profiles_to_reset),
149 [](Profile* profile) -> bool {
150 return PostCleanupSettingsResetPending(profile);
151 });
152 return profiles_to_reset->size() > 0;
153 }
154
155 } // namespace
156
157 void RegisterChromeCleanerProfilePrefs(
158 user_prefs::PrefRegistrySyncable* registry) {
159 DCHECK(registry);
160 registry->RegisterBooleanPref(prefs::kChromeCleanerResetPending, false);
161 }
162
163 bool PostCleanupSettingsResetPending(Profile* profile) {
164 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
165 DCHECK(profile);
166
167 if (!base::FeatureList::IsEnabled(safe_browsing::kInBrowserCleanerUIFeature))
168 return false;
169
170 PrefService* prefs = profile->GetPrefs();
171 DCHECK(prefs);
172 return prefs->GetBoolean(prefs::kChromeCleanerResetPending);
173 }
174
175 void RecordPostCleanupSettingsResetPending(bool value, Profile* profile) {
176 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
177 DCHECK(profile);
178
179 if (!base::FeatureList::IsEnabled(safe_browsing::kInBrowserCleanerUIFeature))
180 return;
181
182 PrefService* prefs = profile->GetPrefs();
183 DCHECK(prefs);
184 prefs->SetBoolean(prefs::kChromeCleanerResetPending, value);
185 }
186
187 void TagProfileForResetting(Profile* profile) {
188 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
189 DCHECK(profile);
190
191 if (base::FeatureList::IsEnabled(safe_browsing::kInBrowserCleanerUIFeature))
192 RecordPostCleanupSettingsResetPending(true, profile);
193 }
194
195 void ResetPostCleanupSettingsIfTagged(
196 std::vector<Profile*> profiles,
197 base::OnceClosure continuation,
198 std::unique_ptr<SettingsResetterDelegate> delegate) {
199 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
200 DCHECK(continuation);
201
202 std::vector<Profile*> profiles_to_reset;
203 if (!base::FeatureList::IsEnabled(
204 safe_browsing::kInBrowserCleanerUIFeature) ||
205 !CopyProfilesToReset(profiles, &profiles_to_reset) ||
206 !CleanupCompletedFromRegistry()) {
207 std::move(continuation).Run();
208 return;
209 }
210
211 // This object will self-delete once |continuation| is invoked.
212 make_scoped_refptr(
213 new SettingsResetter(std::move(profiles_to_reset),
214 delegate
215 ? std::move(delegate)
216 : base::MakeUnique<SettingsResetterDelegate>(),
217 std::move(continuation)))
218 ->Run();
219 }
220
221 SettingsResetterDelegate::SettingsResetterDelegate() {}
222
223 SettingsResetterDelegate::~SettingsResetterDelegate() {}
224
225 void SettingsResetterDelegate::FetchDefaultSettings(
226 DefaultSettingsFetcher::SettingsCallback callback) {
227 DefaultSettingsFetcher::FetchDefaultSettings(std::move(callback));
228 }
229
230 std::unique_ptr<ProfileResetter> SettingsResetterDelegate::GetProfileResetter(
231 Profile* profile) {
232 return base::MakeUnique<ProfileResetter>(profile);
233 }
234
235 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698