Chromium Code Reviews| OLD | NEW |
|---|---|
| (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, | |
|
alito
2017/06/02 05:32:41
Under what circumstances could we end up with mul
ftirelo
2017/06/02 21:20:57
Added a comment to ResetPostCleanupSettingsIfTagge
| |
| 43 std::unique_ptr<SettingsResetterDelegate> delegate, | |
| 44 base::OnceClosure done_callback); | |
| 45 virtual ~SettingsResetter(); | |
| 46 | |
| 47 // Resets settings of |profile_| and invokes |done_callback_| when done. | |
|
alito
2017/06/02 05:32:41
profile_ -> profiles_to_reset_
ftirelo
2017/06/02 21:20:57
Done.
| |
| 48 void Run(); | |
| 49 | |
| 50 private: | |
| 51 // Resets settings for |profile| according to default values given by | |
| 52 // |master_settings|. Used as a callback for | |
| 53 // DefaultSettingsFetcher::FetchDefaultSettings(). | |
| 54 void OnFetchCompleted( | |
| 55 Profile* profile, | |
| 56 std::unique_ptr<BrandcodedDefaultSettings> master_settings); | |
| 57 | |
| 58 // Removes the settings reset tag for |profile|. If there are no more | |
| 59 // profiles to reset, invokes |done_callback_| and deletes this object. | |
| 60 void OnResetCompleted(Profile* profile); | |
| 61 | |
| 62 // The profile to be reset. | |
|
alito
2017/06/02 05:32:41
nit: profile -> profiles
ftirelo
2017/06/02 21:20:57
Done.
| |
| 63 std::vector<Profile*> profiles_to_reset_; | |
| 64 | |
| 65 // The number of profiles that need to be reset. | |
| 66 int num_pending_resets_; | |
| 67 | |
| 68 // The callback to be invoked once settings reset completes. | |
| 69 base::OnceClosure done_callback_; | |
| 70 | |
| 71 std::unique_ptr<SettingsResetterDelegate> delegate_; | |
| 72 | |
| 73 DISALLOW_COPY_AND_ASSIGN(SettingsResetter); | |
| 74 }; | |
| 75 | |
| 76 SettingsResetter::SettingsResetter( | |
| 77 std::vector<Profile*> profiles_to_reset, | |
| 78 std::unique_ptr<SettingsResetterDelegate> delegate, | |
| 79 base::OnceClosure done_callback) | |
| 80 : done_callback_(std::move(done_callback)), | |
| 81 profiles_to_reset_(std::move(profiles_to_reset)), | |
| 82 num_pending_resets_(profiles_to_reset_.size()), | |
| 83 delegate_(std::move(delegate)) { | |
| 84 DCHECK_LT(0, num_pending_resets_); | |
| 85 DCHECK(done_callback_); | |
| 86 DCHECK(delegate_); | |
| 87 } | |
| 88 | |
| 89 SettingsResetter::~SettingsResetter() { | |
| 90 DCHECK(!done_callback_); | |
|
alito
2017/06/02 05:32:41
Worth adding a DCHECK also for num_pending_resets_
ftirelo
2017/06/02 21:20:57
Done.
| |
| 91 } | |
| 92 | |
| 93 void SettingsResetter::Run() { | |
| 94 for (Profile* profile : profiles_to_reset_) { | |
| 95 delegate_->FetchDefaultSettings(base::Bind( | |
| 96 &SettingsResetter::OnFetchCompleted, base::Unretained(this), profile)); | |
|
alito
2017/06/02 05:32:41
How does the SettingsResetter object keep itself a
ftirelo
2017/06/02 21:20:57
My previous implementation deleted the object once
| |
| 97 } | |
| 98 } | |
| 99 | |
| 100 void SettingsResetter::OnFetchCompleted( | |
| 101 Profile* profile, | |
| 102 std::unique_ptr<BrandcodedDefaultSettings> master_settings) { | |
| 103 static const ProfileResetter::ResettableFlags kSettingsToReset = | |
| 104 ProfileResetter::DEFAULT_SEARCH_ENGINE | ProfileResetter::HOMEPAGE | | |
| 105 ProfileResetter::EXTENSIONS | ProfileResetter::STARTUP_PAGES | | |
| 106 ProfileResetter::SHORTCUTS; | |
| 107 | |
| 108 delegate_->GetProfileResetter(profile)->Reset( | |
| 109 kSettingsToReset, std::move(master_settings), | |
| 110 base::Bind(&SettingsResetter::OnResetCompleted, base::Unretained(this), | |
| 111 profile)); | |
| 112 } | |
| 113 | |
| 114 void SettingsResetter::OnResetCompleted(Profile* profile) { | |
| 115 DCHECK_LT(0, num_pending_resets_); | |
| 116 | |
| 117 RecordPostCleanupSettingsResetPending(false, profile); | |
| 118 | |
| 119 --num_pending_resets_; | |
| 120 if (!num_pending_resets_) | |
| 121 std::move(done_callback_).Run(); | |
| 122 } | |
| 123 | |
| 124 // Returns true if there is information of a completed cleanup in the registry. | |
| 125 bool CleanupCompletedFromRegistry() { | |
| 126 base::string16 cleaner_key_path( | |
| 127 chrome_cleaner::kSoftwareRemovalToolRegistryKey); | |
| 128 cleaner_key_path.append(L"\\").append(chrome_cleaner::kCleanerSubKey); | |
| 129 | |
| 130 base::win::RegKey srt_cleaner_key(HKEY_CURRENT_USER, cleaner_key_path.c_str(), | |
| 131 KEY_QUERY_VALUE); | |
| 132 int64_t ts_cleanup_started = 0L, ts_cleanup_completed = 0L; | |
| 133 return srt_cleaner_key.Valid() && | |
| 134 srt_cleaner_key.ReadInt64( | |
| 135 chrome_cleaner::kCleanupStartedTimestampValueName, | |
| 136 &ts_cleanup_started) == ERROR_SUCCESS && | |
| 137 srt_cleaner_key.ReadInt64( | |
| 138 chrome_cleaner::kCleanupConfirmedTimestampValueName, | |
| 139 &ts_cleanup_completed) == ERROR_SUCCESS && | |
| 140 ts_cleanup_started < ts_cleanup_completed; | |
| 141 } | |
| 142 | |
| 143 Profile* GetLastActiveProfile() { | |
|
alito
2017/06/02 05:32:41
this doesn't seem to be used anywhere.
ftirelo
2017/06/02 21:20:57
Done.
| |
| 144 Browser* browser = chrome::FindLastActive(); | |
| 145 return browser ? browser->profile() : nullptr; | |
| 146 } | |
| 147 | |
| 148 } // namespace | |
| 149 | |
| 150 void RegisterChromeCleanerProfilePrefs( | |
| 151 user_prefs::PrefRegistrySyncable* registry) { | |
| 152 DCHECK(registry); | |
| 153 registry->RegisterBooleanPref(prefs::kChromeCleanerResetPending, false); | |
| 154 } | |
| 155 | |
| 156 bool PostCleanupSettingsResetPending(Profile* profile) { | |
| 157 DCHECK(profile); | |
| 158 PrefService* prefs = profile->GetPrefs(); | |
| 159 DCHECK(prefs); | |
| 160 return prefs->GetBoolean(prefs::kChromeCleanerResetPending); | |
| 161 } | |
| 162 | |
| 163 void RecordPostCleanupSettingsResetPending(bool value, Profile* profile) { | |
| 164 DCHECK(profile); | |
| 165 PrefService* prefs = profile->GetPrefs(); | |
| 166 DCHECK(prefs); | |
| 167 prefs->SetBoolean(prefs::kChromeCleanerResetPending, value); | |
| 168 } | |
| 169 | |
| 170 void TagProfileForResetting(Profile* profile) { | |
| 171 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 172 DCHECK(profile); | |
| 173 | |
| 174 if (base::FeatureList::IsEnabled(safe_browsing::kInBrowserCleanerUIFeature)) | |
|
alito
2017/06/02 05:32:41
why is this check performed here but not in Record
ftirelo
2017/06/02 21:20:57
Done.
| |
| 175 RecordPostCleanupSettingsResetPending(true, profile); | |
| 176 } | |
| 177 | |
| 178 bool CopyProfilesToReset(const std::vector<Profile*>& profiles, | |
| 179 std::vector<Profile*>* profiles_to_reset) { | |
| 180 std::copy_if(profiles.begin(), profiles.end(), | |
| 181 std::back_inserter(*profiles_to_reset), | |
| 182 [](Profile* profile) -> bool { | |
| 183 return PostCleanupSettingsResetPending(profile); | |
| 184 }); | |
| 185 return profiles_to_reset->size() > 0; | |
| 186 } | |
| 187 | |
| 188 void ResetPostCleanupSettingsIfTagged( | |
| 189 std::vector<Profile*> profiles, | |
| 190 base::OnceClosure continuation, | |
| 191 std::unique_ptr<SettingsResetterDelegate> delegate) { | |
| 192 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 193 DCHECK(continuation); | |
| 194 | |
| 195 std::vector<Profile*> profiles_to_reset; | |
| 196 if (!base::FeatureList::IsEnabled( | |
| 197 safe_browsing::kInBrowserCleanerUIFeature) || | |
| 198 !CopyProfilesToReset(profiles, &profiles_to_reset) || | |
| 199 !CleanupCompletedFromRegistry()) { | |
| 200 std::move(continuation).Run(); | |
| 201 return; | |
| 202 } | |
| 203 | |
| 204 // This object will self-delete once |continuation| is invoked. | |
| 205 make_scoped_refptr( | |
| 206 new SettingsResetter(std::move(profiles_to_reset), | |
| 207 delegate | |
| 208 ? std::move(delegate) | |
| 209 : base::MakeUnique<SettingsResetterDelegate>(), | |
| 210 std::move(continuation))) | |
| 211 ->Run(); | |
| 212 } | |
| 213 | |
| 214 SettingsResetterDelegate::SettingsResetterDelegate() {} | |
| 215 | |
| 216 SettingsResetterDelegate::~SettingsResetterDelegate() {} | |
| 217 | |
| 218 void SettingsResetterDelegate::FetchDefaultSettings( | |
| 219 DefaultSettingsFetcher::SettingsCallback callback) { | |
| 220 DefaultSettingsFetcher::FetchDefaultSettings(std::move(callback)); | |
| 221 } | |
| 222 | |
| 223 std::unique_ptr<ProfileResetter> SettingsResetterDelegate::GetProfileResetter( | |
| 224 Profile* profile) { | |
| 225 return base::MakeUnique<ProfileResetter>(profile); | |
| 226 } | |
| 227 | |
| 228 } // namespace safe_browsing | |
| OLD | NEW |