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

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

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

Powered by Google App Engine
This is Rietveld 408576698