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

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

Issue 2906103002: Post-cleanup settings reset. (Closed)
Patch Set: Addressed alito's comments 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 // Returns the post-cleanup reset pending prefs for |profile|.
38 bool ResetPending(Profile* profile) {
39 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
40 DCHECK(PostCleanupSettingsResetter::IsEnabled());
41 DCHECK(profile);
42
43 PrefService* prefs = profile->GetPrefs();
44 DCHECK(prefs);
alito 2017/06/09 01:52:07 No need for this DCHECK. Something would be deeply
ftirelo 2017/06/09 21:24:55 Done.
45 return prefs->GetBoolean(prefs::kChromeCleanerResetPending);
46 }
47
48 // Updates the post-cleanup reset pending prefs for |profile|.
49 void RecordResetPending(bool value, Profile* profile) {
50 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
51 DCHECK(PostCleanupSettingsResetter::IsEnabled());
52 DCHECK(profile);
53
54 PrefService* prefs = profile->GetPrefs();
55 DCHECK(prefs);
alito 2017/06/09 01:52:07 ditto.
ftirelo 2017/06/09 21:24:55 Done.
56 prefs->SetBoolean(prefs::kChromeCleanerResetPending, value);
57 }
58
59 // Manages post-cleanup settings reset for a list of profiles.
60 // An instance of this class is created by ResetPostCleanupSettingsIfTagged()
alito 2017/06/09 01:52:07 nit: ResetPostCleanupSettingsIfTagged -> ResetTagg
ftirelo 2017/06/09 21:24:55 Done.
61 // and will self-delete once all profiles in the list have been reset.
62 class SettingsResetter : public base::RefCounted<SettingsResetter> {
63 public:
64 SettingsResetter(
65 std::vector<Profile*> profiles_to_reset,
66 std::unique_ptr<PostCleanupSettingsResetter::Delegate> delegate,
67 base::OnceClosure done_callback);
68
69 // Resets settings for all profiles in |profiles_to_reset_| and invokes
70 // |done_callback_| when done.
71 void Run();
72
73 protected:
74 virtual ~SettingsResetter();
75
76 private:
77 friend class base::RefCounted<SettingsResetter>;
78
79 // Resets settings for |profile| according to default values given by
80 // |master_settings|. Used as a callback for
81 // DefaultSettingsFetcher::FetchDefaultSettings().
82 void OnFetchCompleted(
83 Profile* profile,
84 std::unique_ptr<BrandcodedDefaultSettings> master_settings);
85
86 // Removes the settings reset tag for |profile|. If there are no more
87 // profiles to reset, invokes |done_callback_| and deletes this object.
88 void OnResetCompleted(Profile* profile);
89
90 // The profiles to be reset.
91 std::vector<Profile*> profiles_to_reset_;
92
93 // The number of profiles that need to be reset.
94 int num_pending_resets_;
95
96 // The callback to be invoked once settings reset completes.
97 base::OnceClosure done_callback_;
98
99 std::unique_ptr<PostCleanupSettingsResetter::Delegate> delegate_;
100
101 DISALLOW_COPY_AND_ASSIGN(SettingsResetter);
102 };
103
104 SettingsResetter::SettingsResetter(
105 std::vector<Profile*> profiles_to_reset,
106 std::unique_ptr<PostCleanupSettingsResetter::Delegate> delegate,
107 base::OnceClosure done_callback)
108 : profiles_to_reset_(std::move(profiles_to_reset)),
109 num_pending_resets_(profiles_to_reset_.size()),
110 done_callback_(std::move(done_callback)),
111 delegate_(std::move(delegate)) {
112 DCHECK_LT(0, num_pending_resets_);
113 DCHECK(done_callback_);
114 DCHECK(delegate_);
115 }
116
117 SettingsResetter::~SettingsResetter() {
118 DCHECK(!done_callback_);
119 DCHECK(!num_pending_resets_);
120 }
121
122 void SettingsResetter::Run() {
123 for (Profile* profile : profiles_to_reset_) {
124 delegate_->FetchDefaultSettings(
125 base::Bind(&SettingsResetter::OnFetchCompleted, this, profile));
126 }
127 }
128
129 void SettingsResetter::OnFetchCompleted(
130 Profile* profile,
131 std::unique_ptr<BrandcodedDefaultSettings> master_settings) {
132 static const ProfileResetter::ResettableFlags kSettingsToReset =
133 ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE |
134 ProfileResetter::EXTENSIONS | ProfileResetter::STARTUP_PAGES |
135 ProfileResetter::SHORTCUTS;
136
137 delegate_->GetProfileResetter(profile)->Reset(
138 kSettingsToReset, std::move(master_settings),
139 base::Bind(&SettingsResetter::OnResetCompleted, this, profile));
140 }
141
142 void SettingsResetter::OnResetCompleted(Profile* profile) {
143 DCHECK_LT(0, num_pending_resets_);
144
145 RecordResetPending(false, profile);
146
147 --num_pending_resets_;
148 if (!num_pending_resets_)
149 std::move(done_callback_).Run();
150 }
151
152 // Returns true if there is information of a completed cleanup in the registry.
153 bool CleanupCompletedFromRegistry() {
154 base::string16 cleaner_key_path(
155 chrome_cleaner::kSoftwareRemovalToolRegistryKey);
156 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey);
157
158 base::win::RegKey srt_cleaner_key(HKEY_CURRENT_USER, cleaner_key_path.c_str(),
159 KEY_QUERY_VALUE);
160 int64_t ts_cleanup_started = 0L, ts_cleanup_completed = 0L;
161 return srt_cleaner_key.Valid() &&
162 srt_cleaner_key.ReadInt64(
163 chrome_cleaner::kCleanupStartedTimestampValueName,
164 &ts_cleanup_started) == ERROR_SUCCESS &&
165 srt_cleaner_key.ReadInt64(
166 chrome_cleaner::kCleanupConfirmedTimestampValueName,
167 &ts_cleanup_completed) == ERROR_SUCCESS &&
168 ts_cleanup_started < ts_cleanup_completed;
169 }
170
171 } // namespace
172
173 PostCleanupSettingsResetter::Delegate::Delegate() {}
174
175 PostCleanupSettingsResetter::Delegate::~Delegate() {}
176
177 void PostCleanupSettingsResetter::Delegate::FetchDefaultSettings(
178 DefaultSettingsFetcher::SettingsCallback callback) {
179 DefaultSettingsFetcher::FetchDefaultSettings(std::move(callback));
180 }
181
182 std::unique_ptr<ProfileResetter>
183 PostCleanupSettingsResetter::Delegate::GetProfileResetter(Profile* profile) {
184 return base::MakeUnique<ProfileResetter>(profile);
185 }
186
187 // static
188 void PostCleanupSettingsResetter::RegisterProfilePrefs(
189 user_prefs::PrefRegistrySyncable* registry) {
190 DCHECK(registry);
191 registry->RegisterBooleanPref(prefs::kChromeCleanerResetPending, false);
192 }
193
194 // static
195 PostCleanupSettingsResetter* PostCleanupSettingsResetter::Create() {
196 return IsEnabled() ? new PostCleanupSettingsResetter() : nullptr;
197 }
198
199 base::OnceClosure PostCleanupSettingsResetter::ReleaseWithContinuationClosure(
200 base::OnceClosure continuation) {
201 return base::BindOnce(
202 [](PostCleanupSettingsResetter* r, base::OnceClosure continuation) {
203 std::move(continuation).Run();
204 delete r;
205 },
206 this, std::move(continuation));
207 }
208
209 base::OnceClosure PostCleanupSettingsResetter::ReleaseClosure() {
210 return ReleaseWithContinuationClosure(base::BindOnce([] {}));
211 }
212
213 void PostCleanupSettingsResetter::TagForResetting(Profile* profile) {
214 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
215 DCHECK(IsEnabled());
216 DCHECK(profile);
217
218 RecordResetPending(true, profile);
219 }
220
221 void PostCleanupSettingsResetter::ResetTaggedProfiles(
222 std::vector<Profile*> profiles,
223 base::OnceClosure continuation,
224 std::unique_ptr<PostCleanupSettingsResetter::Delegate> delegate) {
225 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
226 DCHECK(IsEnabled());
227 DCHECK(continuation);
228
229 std::vector<Profile*> profiles_to_reset;
230 if (!CopyProfilesToReset(profiles, &profiles_to_reset) ||
231 !CleanupCompletedFromRegistry()) {
232 std::move(continuation).Run();
233 return;
234 }
235
236 // This object will self-delete once |continuation| is invoked.
237 make_scoped_refptr(
238 new SettingsResetter(
239 std::move(profiles_to_reset),
240 delegate ? std::move(delegate)
241 : base::MakeUnique<PostCleanupSettingsResetter::Delegate>(),
242 std::move(continuation)))
243 ->Run();
244 }
245
246 PostCleanupSettingsResetter::PostCleanupSettingsResetter() = default;
247
248 bool PostCleanupSettingsResetter::CopyProfilesToReset(
249 const std::vector<Profile*>& profiles,
250 std::vector<Profile*>* profiles_to_reset) {
251 std::copy_if(profiles.begin(), profiles.end(),
252 std::back_inserter(*profiles_to_reset),
253 [](Profile* profile) -> bool { return ResetPending(profile); });
254 return profiles_to_reset->size() > 0;
255 }
256
257 // static
258 bool PostCleanupSettingsResetter::IsEnabled() {
259 return base::FeatureList::IsEnabled(
260 safe_browsing::kInBrowserCleanerUIFeature);
261 }
262
263 } // namespace safe_browsing
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698