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

Side by Side Diff: chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.cc

Issue 2240883002: Make ClearBrowsingDataHandler only observe its own removal task (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Observer as a subclass. Created 4 years, 3 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
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler. h" 5 #include "chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler. h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include "base/macros.h" 9 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 10 #include "base/memory/ptr_util.h"
(...skipping 24 matching lines...) Expand all
35 browsing_data::prefs::kDeleteFormData, 35 browsing_data::prefs::kDeleteFormData,
36 browsing_data::prefs::kDeleteHostedAppsData, 36 browsing_data::prefs::kDeleteHostedAppsData,
37 browsing_data::prefs::kDeleteMediaLicenses, 37 browsing_data::prefs::kDeleteMediaLicenses,
38 browsing_data::prefs::kDeletePasswords, 38 browsing_data::prefs::kDeletePasswords,
39 }; 39 };
40 40
41 } // namespace 41 } // namespace
42 42
43 namespace settings { 43 namespace settings {
44 44
45 // TaskObserver ----------------------------------------------------------------
46
47 ClearBrowsingDataHandler::TaskObserver::TaskObserver(
48 BrowsingDataRemover* remover, const base::Closure& callback)
49 : callback_(callback),
50 remover_observer_(this) {
51 remover_observer_.Add(remover);
52 }
53
54 ClearBrowsingDataHandler::TaskObserver::~TaskObserver() {}
55
56 void ClearBrowsingDataHandler::TaskObserver::OnBrowsingDataRemoverDone() {
57 remover_observer_.RemoveAll();
58 callback_.Run();
59 }
60
61 // ClearBrowsingDataHandler ----------------------------------------------------
62
45 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) 63 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui)
46 : profile_(Profile::FromWebUI(webui)), 64 : profile_(Profile::FromWebUI(webui)),
47 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)), 65 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)),
48 sync_service_observer_(this), 66 sync_service_observer_(this),
49 remover_(nullptr),
50 remover_observer_(this),
51 show_history_footer_(false), 67 show_history_footer_(false),
52 show_history_deletion_dialog_(false), 68 show_history_deletion_dialog_(false),
53 weak_ptr_factory_(this) {} 69 weak_ptr_factory_(this) {}
54 70
55 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { 71 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() {
56 } 72 }
57 73
58 void ClearBrowsingDataHandler::RegisterMessages() { 74 void ClearBrowsingDataHandler::RegisterMessages() {
59 web_ui()->RegisterMessageCallback( 75 web_ui()->RegisterMessageCallback(
60 "clearBrowsingData", 76 "clearBrowsingData",
61 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, 77 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData,
62 base::Unretained(this))); 78 base::Unretained(this)));
63 79
64 web_ui()->RegisterMessageCallback( 80 web_ui()->RegisterMessageCallback(
65 "initializeClearBrowsingData", 81 "initializeClearBrowsingData",
66 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, 82 base::Bind(&ClearBrowsingDataHandler::HandleInitialize,
67 base::Unretained(this))); 83 base::Unretained(this)));
68 } 84 }
69 85
70 void ClearBrowsingDataHandler::OnJavascriptAllowed() { 86 void ClearBrowsingDataHandler::OnJavascriptAllowed() {
71 PrefService* prefs = profile_->GetPrefs(); 87 PrefService* prefs = profile_->GetPrefs();
72 profile_pref_registrar_.Init(prefs); 88 profile_pref_registrar_.Init(prefs);
73 profile_pref_registrar_.Add( 89 profile_pref_registrar_.Add(
74 prefs::kAllowDeletingBrowserHistory, 90 prefs::kAllowDeletingBrowserHistory,
75 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged, 91 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged,
76 base::Unretained(this))); 92 base::Unretained(this)));
77 93
78 if (sync_service_) 94 if (sync_service_)
79 sync_service_observer_.Add(sync_service_); 95 sync_service_observer_.Add(sync_service_);
80
81 if (!remover_)
82 remover_ = BrowsingDataRemoverFactory::GetForBrowserContext(profile_);
83
84 remover_observer_.Add(remover_);
85 } 96 }
86 97
87 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { 98 void ClearBrowsingDataHandler::OnJavascriptDisallowed() {
88 remover_observer_.RemoveAll();
89 profile_pref_registrar_.RemoveAll(); 99 profile_pref_registrar_.RemoveAll();
90 sync_service_observer_.RemoveAll(); 100 sync_service_observer_.RemoveAll();
101 task_observer_.reset();
91 } 102 }
92 103
93 void ClearBrowsingDataHandler::HandleClearBrowsingData( 104 void ClearBrowsingDataHandler::HandleClearBrowsingData(
94 const base::ListValue* args) { 105 const base::ListValue* args) {
95 // We should never be called when the previous clearing has not yet finished. 106 DCHECK(!task_observer_);
96 CHECK(!remover_->is_removing());
97 CHECK_EQ(1U, args->GetSize());
98 CHECK(webui_callback_id_.empty());
99 CHECK(args->GetString(0, &webui_callback_id_));
100 107
101 PrefService* prefs = profile_->GetPrefs(); 108 PrefService* prefs = profile_->GetPrefs();
102 109
103 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA; 110 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA;
104 // Don't try to clear LSO data if it's not supported. 111 // Don't try to clear LSO data if it's not supported.
105 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) 112 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled))
106 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA; 113 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA;
107 114
108 int remove_mask = 0; 115 int remove_mask = 0;
109 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) { 116 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) {
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
166 int checked_other_types = std::count_if( 173 int checked_other_types = std::count_if(
167 other_types, other_types + num_other_types, 174 other_types, other_types + num_other_types,
168 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); 175 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); });
169 UMA_HISTOGRAM_SPARSE_SLOWLY( 176 UMA_HISTOGRAM_SPARSE_SLOWLY(
170 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", 177 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount",
171 checked_other_types); 178 checked_other_types);
172 } 179 }
173 180
174 int period_selected = 181 int period_selected =
175 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); 182 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod);
176 remover_->Remove(BrowsingDataRemover::Period( 183
177 static_cast<browsing_data::TimePeriod>(period_selected)), 184 std::string webui_callback_id;
178 remove_mask, origin_mask); 185 CHECK_EQ(1U, args->GetSize());
186 CHECK(args->GetString(0, &webui_callback_id));
187
188 BrowsingDataRemover* remover =
189 BrowsingDataRemoverFactory::GetForBrowserContext(profile_);
190 task_observer_ = base::MakeUnique<TaskObserver>(
191 remover,
192 base::Bind(&ClearBrowsingDataHandler::OnClearingTaskFinished,
193 base::Unretained(this), webui_callback_id));
194 remover->RemoveAndReply(
195 BrowsingDataRemover::Period(
196 static_cast<browsing_data::TimePeriod>(period_selected)),
197 remove_mask, origin_mask, task_observer_.get());
179 } 198 }
180 199
181 void ClearBrowsingDataHandler::OnBrowsingDataRemoving(bool is_removing) { 200 void ClearBrowsingDataHandler::OnClearingTaskFinished(
182 CallJavascriptFunction("cr.webUIListenerCallback", 201 const std::string& webui_callback_id) {
183 base::StringValue("browsing-data-removing"),
184 base::FundamentalValue(is_removing));
185
186 if (is_removing || webui_callback_id_.empty())
187 return;
188
189 PrefService* prefs = profile_->GetPrefs(); 202 PrefService* prefs = profile_->GetPrefs();
190 int notice_shown_times = 203 int notice_shown_times =
191 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes); 204 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes);
192 205
193 // When the deletion is complete, we might show an additional dialog with 206 // When the deletion is complete, we might show an additional dialog with
194 // a notice about other forms of browsing history. This is the case if 207 // a notice about other forms of browsing history. This is the case if
195 const bool show_notice = 208 const bool show_notice =
196 // 1. The dialog is relevant for the user. 209 // 1. The dialog is relevant for the user.
197 show_history_deletion_dialog_ && 210 show_history_deletion_dialog_ &&
198 // 2. The notice has been shown less than |kMaxTimesHistoryNoticeShown|. 211 // 2. The notice has been shown less than |kMaxTimesHistoryNoticeShown|.
199 notice_shown_times < kMaxTimesHistoryNoticeShown && 212 notice_shown_times < kMaxTimesHistoryNoticeShown &&
200 // 3. The selected data types contained browsing history. 213 // 3. The selected data types contained browsing history.
201 prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory); 214 prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory);
202 215
203 if (show_notice) { 216 if (show_notice) {
204 // Increment the preference. 217 // Increment the preference.
205 prefs->SetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes, 218 prefs->SetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes,
206 notice_shown_times + 1); 219 notice_shown_times + 1);
207 } 220 }
208 221
209 UMA_HISTOGRAM_BOOLEAN( 222 UMA_HISTOGRAM_BOOLEAN(
210 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice); 223 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice);
211 224
212 ResolveJavascriptCallback( 225 ResolveJavascriptCallback(
213 base::StringValue(webui_callback_id_), 226 base::StringValue(webui_callback_id),
214 base::FundamentalValue(show_notice)); 227 base::FundamentalValue(show_notice));
215 webui_callback_id_.clear(); 228 task_observer_.reset();
216 } 229 }
217 230
218 void ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged() { 231 void ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged() {
219 CallJavascriptFunction( 232 CallJavascriptFunction(
220 "cr.webUIListenerCallback", 233 "cr.webUIListenerCallback",
221 base::StringValue("browsing-history-pref-changed"), 234 base::StringValue("browsing-history-pref-changed"),
222 base::FundamentalValue( 235 base::FundamentalValue(
223 profile_->GetPrefs()->GetBoolean( 236 profile_->GetPrefs()->GetBoolean(
224 prefs::kAllowDeletingBrowserHistory))); 237 prefs::kAllowDeletingBrowserHistory)));
225 } 238 }
226 239
227 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { 240 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) {
228 AllowJavascript(); 241 AllowJavascript();
229 const base::Value* callback_id; 242 const base::Value* callback_id;
230 CHECK(args->Get(0, &callback_id)); 243 CHECK(args->Get(0, &callback_id));
231 244
245 task_observer_.reset();
232 counters_.clear(); 246 counters_.clear();
233 247
234 for (const std::string& pref : kCounterPrefs) { 248 for (const std::string& pref : kCounterPrefs) {
235 AddCounter( 249 AddCounter(
236 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref)); 250 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref));
237 } 251 }
238 252
239 OnStateChanged(); 253 OnStateChanged();
240 RefreshHistoryNotice(); 254 RefreshHistoryNotice();
241 255
242 ResolveJavascriptCallback(*callback_id, 256 ResolveJavascriptCallback(
243 base::FundamentalValue(remover_->is_removing())); 257 *callback_id,
258 *base::Value::CreateNullValue() /* Promise<void> */);
244 } 259 }
245 260
246 void ClearBrowsingDataHandler::OnStateChanged() { 261 void ClearBrowsingDataHandler::OnStateChanged() {
247 CallJavascriptFunction( 262 CallJavascriptFunction(
248 "cr.webUIListenerCallback", 263 "cr.webUIListenerCallback",
249 base::StringValue("update-footer"), 264 base::StringValue("update-footer"),
250 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), 265 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()),
251 base::FundamentalValue(show_history_footer_)); 266 base::FundamentalValue(show_history_footer_));
252 } 267 }
253 268
(...skipping 23 matching lines...) Expand all
277 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) { 292 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) {
278 show_history_footer_ = show; 293 show_history_footer_ = show;
279 OnStateChanged(); 294 OnStateChanged();
280 295
281 UMA_HISTOGRAM_BOOLEAN( 296 UMA_HISTOGRAM_BOOLEAN(
282 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated", 297 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated",
283 show_history_footer_); 298 show_history_footer_);
284 } 299 }
285 300
286 void ClearBrowsingDataHandler::UpdateHistoryDeletionDialog(bool show) { 301 void ClearBrowsingDataHandler::UpdateHistoryDeletionDialog(bool show) {
287 // This is used by OnBrowsingDataRemoving (when the deletion finishes). 302 // This is used by OnClearingTaskFinished (when the deletion finishes).
288 show_history_deletion_dialog_ = show; 303 show_history_deletion_dialog_ = show;
289 } 304 }
290 305
291 void ClearBrowsingDataHandler::AddCounter( 306 void ClearBrowsingDataHandler::AddCounter(
292 std::unique_ptr<browsing_data::BrowsingDataCounter> counter) { 307 std::unique_ptr<browsing_data::BrowsingDataCounter> counter) {
293 counter->Init(profile_->GetPrefs(), 308 counter->Init(profile_->GetPrefs(),
294 base::Bind(&ClearBrowsingDataHandler::UpdateCounterText, 309 base::Bind(&ClearBrowsingDataHandler::UpdateCounterText,
295 base::Unretained(this))); 310 base::Unretained(this)));
296 counter->Restart(); 311 counter->Restart();
297 counters_.push_back(std::move(counter)); 312 counters_.push_back(std::move(counter));
298 } 313 }
299 314
300 void ClearBrowsingDataHandler::UpdateCounterText( 315 void ClearBrowsingDataHandler::UpdateCounterText(
301 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { 316 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
302 CallJavascriptFunction( 317 CallJavascriptFunction(
303 "cr.webUIListenerCallback", 318 "cr.webUIListenerCallback",
304 base::StringValue("update-counter-text"), 319 base::StringValue("update-counter-text"),
305 base::StringValue(result->source()->GetPrefName()), 320 base::StringValue(result->source()->GetPrefName()),
306 base::StringValue(GetChromeCounterTextFromResult(result.get()))); 321 base::StringValue(GetChromeCounterTextFromResult(result.get())));
307 } 322 }
308 323
309 } // namespace settings 324 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698