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

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

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

Powered by Google App Engine
This is Rietveld 408576698