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

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

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

Powered by Google App Engine
This is Rietveld 408576698