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

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: Only forward-declare in the header file 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
« no previous file with comments | « chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 class ClearBrowsingDataHandler::TaskObserver
48 : public BrowsingDataRemover::Observer {
49 public:
50 TaskObserver(BrowsingDataRemover* remover, const base::Closure& callback);
51 ~TaskObserver() override;
52
53 void OnBrowsingDataRemoverDone() override;
54
55 private:
56 base::Closure callback_;
57 ScopedObserver<BrowsingDataRemover, BrowsingDataRemover::Observer>
58 remover_observer_;
59
60 DISALLOW_COPY_AND_ASSIGN(TaskObserver);
61 };
62
63 ClearBrowsingDataHandler::TaskObserver::TaskObserver(
64 BrowsingDataRemover* remover, const base::Closure& callback)
65 : callback_(callback),
66 remover_observer_(this) {
67 remover_observer_.Add(remover);
68 }
69
70 ClearBrowsingDataHandler::TaskObserver::~TaskObserver() {}
71
72 void ClearBrowsingDataHandler::TaskObserver::OnBrowsingDataRemoverDone() {
73 remover_observer_.RemoveAll();
74 callback_.Run();
75 }
Dan Beam 2016/09/01 15:49:32 fwiw: i usually find inline function bodies better
76
77 // ClearBrowsingDataHandler ----------------------------------------------------
78
45 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) 79 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui)
46 : profile_(Profile::FromWebUI(webui)), 80 : profile_(Profile::FromWebUI(webui)),
47 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)), 81 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)),
48 sync_service_observer_(this), 82 sync_service_observer_(this),
49 remover_(nullptr),
50 remover_observer_(this),
51 show_history_footer_(false), 83 show_history_footer_(false),
52 show_history_deletion_dialog_(false), 84 show_history_deletion_dialog_(false),
53 weak_ptr_factory_(this) {} 85 weak_ptr_factory_(this) {}
54 86
55 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { 87 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() {
56 } 88 }
57 89
58 void ClearBrowsingDataHandler::RegisterMessages() { 90 void ClearBrowsingDataHandler::RegisterMessages() {
59 web_ui()->RegisterMessageCallback( 91 web_ui()->RegisterMessageCallback(
60 "clearBrowsingData", 92 "clearBrowsingData",
61 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, 93 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData,
62 base::Unretained(this))); 94 base::Unretained(this)));
63 95
64 web_ui()->RegisterMessageCallback( 96 web_ui()->RegisterMessageCallback(
65 "initializeClearBrowsingData", 97 "initializeClearBrowsingData",
66 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, 98 base::Bind(&ClearBrowsingDataHandler::HandleInitialize,
67 base::Unretained(this))); 99 base::Unretained(this)));
68 } 100 }
69 101
70 void ClearBrowsingDataHandler::OnJavascriptAllowed() { 102 void ClearBrowsingDataHandler::OnJavascriptAllowed() {
71 PrefService* prefs = profile_->GetPrefs(); 103 PrefService* prefs = profile_->GetPrefs();
72 profile_pref_registrar_.Init(prefs); 104 profile_pref_registrar_.Init(prefs);
73 profile_pref_registrar_.Add( 105 profile_pref_registrar_.Add(
74 prefs::kAllowDeletingBrowserHistory, 106 prefs::kAllowDeletingBrowserHistory,
75 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged, 107 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged,
76 base::Unretained(this))); 108 base::Unretained(this)));
77 109
78 if (sync_service_) 110 if (sync_service_)
79 sync_service_observer_.Add(sync_service_); 111 sync_service_observer_.Add(sync_service_);
80
81 if (!remover_)
82 remover_ = BrowsingDataRemoverFactory::GetForBrowserContext(profile_);
83
84 remover_observer_.Add(remover_);
85 } 112 }
86 113
87 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { 114 void ClearBrowsingDataHandler::OnJavascriptDisallowed() {
88 remover_observer_.RemoveAll();
89 profile_pref_registrar_.RemoveAll(); 115 profile_pref_registrar_.RemoveAll();
90 sync_service_observer_.RemoveAll(); 116 sync_service_observer_.RemoveAll();
117 task_observer_.reset();
91 } 118 }
92 119
93 void ClearBrowsingDataHandler::HandleClearBrowsingData( 120 void ClearBrowsingDataHandler::HandleClearBrowsingData(
94 const base::ListValue* args) { 121 const base::ListValue* args) {
95 // We should never be called when the previous clearing has not yet finished. 122 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 123
101 PrefService* prefs = profile_->GetPrefs(); 124 PrefService* prefs = profile_->GetPrefs();
102 125
103 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA; 126 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA;
104 // Don't try to clear LSO data if it's not supported. 127 // Don't try to clear LSO data if it's not supported.
105 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) 128 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled))
106 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA; 129 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA;
107 130
108 int remove_mask = 0; 131 int remove_mask = 0;
109 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) { 132 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( 189 int checked_other_types = std::count_if(
167 other_types, other_types + num_other_types, 190 other_types, other_types + num_other_types,
168 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); 191 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); });
169 UMA_HISTOGRAM_SPARSE_SLOWLY( 192 UMA_HISTOGRAM_SPARSE_SLOWLY(
170 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", 193 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount",
171 checked_other_types); 194 checked_other_types);
172 } 195 }
173 196
174 int period_selected = 197 int period_selected =
175 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); 198 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod);
176 remover_->Remove(BrowsingDataRemover::Period( 199
177 static_cast<browsing_data::TimePeriod>(period_selected)), 200 std::string webui_callback_id;
178 remove_mask, origin_mask); 201 CHECK_EQ(1U, args->GetSize());
202 CHECK(args->GetString(0, &webui_callback_id));
203
204 BrowsingDataRemover* remover =
205 BrowsingDataRemoverFactory::GetForBrowserContext(profile_);
206 task_observer_ = base::MakeUnique<TaskObserver>(
207 remover,
208 base::Bind(&ClearBrowsingDataHandler::OnClearingTaskFinished,
209 base::Unretained(this), webui_callback_id));
210 remover->RemoveAndReply(
211 BrowsingDataRemover::Period(
212 static_cast<browsing_data::TimePeriod>(period_selected)),
213 remove_mask, origin_mask, task_observer_.get());
179 } 214 }
180 215
181 void ClearBrowsingDataHandler::OnBrowsingDataRemoving(bool is_removing) { 216 void ClearBrowsingDataHandler::OnClearingTaskFinished(
182 CallJavascriptFunction("cr.webUIListenerCallback", 217 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(); 218 PrefService* prefs = profile_->GetPrefs();
190 int notice_shown_times = 219 int notice_shown_times =
191 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes); 220 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes);
192 221
193 // When the deletion is complete, we might show an additional dialog with 222 // 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 223 // a notice about other forms of browsing history. This is the case if
195 const bool show_notice = 224 const bool show_notice =
196 // 1. The dialog is relevant for the user. 225 // 1. The dialog is relevant for the user.
197 show_history_deletion_dialog_ && 226 show_history_deletion_dialog_ &&
198 // 2. The notice has been shown less than |kMaxTimesHistoryNoticeShown|. 227 // 2. The notice has been shown less than |kMaxTimesHistoryNoticeShown|.
199 notice_shown_times < kMaxTimesHistoryNoticeShown && 228 notice_shown_times < kMaxTimesHistoryNoticeShown &&
200 // 3. The selected data types contained browsing history. 229 // 3. The selected data types contained browsing history.
201 prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory); 230 prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory);
202 231
203 if (show_notice) { 232 if (show_notice) {
204 // Increment the preference. 233 // Increment the preference.
205 prefs->SetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes, 234 prefs->SetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes,
206 notice_shown_times + 1); 235 notice_shown_times + 1);
207 } 236 }
208 237
209 UMA_HISTOGRAM_BOOLEAN( 238 UMA_HISTOGRAM_BOOLEAN(
210 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice); 239 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice);
211 240
212 ResolveJavascriptCallback( 241 ResolveJavascriptCallback(
213 base::StringValue(webui_callback_id_), 242 base::StringValue(webui_callback_id),
214 base::FundamentalValue(show_notice)); 243 base::FundamentalValue(show_notice));
215 webui_callback_id_.clear(); 244 task_observer_.reset();
216 } 245 }
217 246
218 void ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged() { 247 void ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged() {
219 CallJavascriptFunction( 248 CallJavascriptFunction(
220 "cr.webUIListenerCallback", 249 "cr.webUIListenerCallback",
221 base::StringValue("browsing-history-pref-changed"), 250 base::StringValue("browsing-history-pref-changed"),
222 base::FundamentalValue( 251 base::FundamentalValue(
223 profile_->GetPrefs()->GetBoolean( 252 profile_->GetPrefs()->GetBoolean(
224 prefs::kAllowDeletingBrowserHistory))); 253 prefs::kAllowDeletingBrowserHistory)));
225 } 254 }
226 255
227 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { 256 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) {
228 AllowJavascript(); 257 AllowJavascript();
229 const base::Value* callback_id; 258 const base::Value* callback_id;
230 CHECK(args->Get(0, &callback_id)); 259 CHECK(args->Get(0, &callback_id));
231 260
261 task_observer_.reset();
232 counters_.clear(); 262 counters_.clear();
233 263
234 for (const std::string& pref : kCounterPrefs) { 264 for (const std::string& pref : kCounterPrefs) {
235 AddCounter( 265 AddCounter(
236 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref)); 266 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref));
237 } 267 }
238 268
239 OnStateChanged(); 269 OnStateChanged();
240 RefreshHistoryNotice(); 270 RefreshHistoryNotice();
241 271
242 ResolveJavascriptCallback(*callback_id, 272 ResolveJavascriptCallback(
243 base::FundamentalValue(remover_->is_removing())); 273 *callback_id,
274 *base::Value::CreateNullValue() /* Promise<void> */);
244 } 275 }
245 276
246 void ClearBrowsingDataHandler::OnStateChanged() { 277 void ClearBrowsingDataHandler::OnStateChanged() {
247 CallJavascriptFunction( 278 CallJavascriptFunction(
248 "cr.webUIListenerCallback", 279 "cr.webUIListenerCallback",
249 base::StringValue("update-footer"), 280 base::StringValue("update-footer"),
250 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), 281 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()),
251 base::FundamentalValue(show_history_footer_)); 282 base::FundamentalValue(show_history_footer_));
252 } 283 }
253 284
(...skipping 23 matching lines...) Expand all
277 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) { 308 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) {
278 show_history_footer_ = show; 309 show_history_footer_ = show;
279 OnStateChanged(); 310 OnStateChanged();
280 311
281 UMA_HISTOGRAM_BOOLEAN( 312 UMA_HISTOGRAM_BOOLEAN(
282 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated", 313 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated",
283 show_history_footer_); 314 show_history_footer_);
284 } 315 }
285 316
286 void ClearBrowsingDataHandler::UpdateHistoryDeletionDialog(bool show) { 317 void ClearBrowsingDataHandler::UpdateHistoryDeletionDialog(bool show) {
287 // This is used by OnBrowsingDataRemoving (when the deletion finishes). 318 // This is used by OnClearingTaskFinished (when the deletion finishes).
288 show_history_deletion_dialog_ = show; 319 show_history_deletion_dialog_ = show;
289 } 320 }
290 321
291 void ClearBrowsingDataHandler::AddCounter( 322 void ClearBrowsingDataHandler::AddCounter(
292 std::unique_ptr<browsing_data::BrowsingDataCounter> counter) { 323 std::unique_ptr<browsing_data::BrowsingDataCounter> counter) {
293 counter->Init(profile_->GetPrefs(), 324 counter->Init(profile_->GetPrefs(),
294 base::Bind(&ClearBrowsingDataHandler::UpdateCounterText, 325 base::Bind(&ClearBrowsingDataHandler::UpdateCounterText,
295 base::Unretained(this))); 326 base::Unretained(this)));
296 counter->Restart(); 327 counter->Restart();
297 counters_.push_back(std::move(counter)); 328 counters_.push_back(std::move(counter));
298 } 329 }
299 330
300 void ClearBrowsingDataHandler::UpdateCounterText( 331 void ClearBrowsingDataHandler::UpdateCounterText(
301 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { 332 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
302 CallJavascriptFunction( 333 CallJavascriptFunction(
303 "cr.webUIListenerCallback", 334 "cr.webUIListenerCallback",
304 base::StringValue("update-counter-text"), 335 base::StringValue("update-counter-text"),
305 base::StringValue(result->source()->GetPrefName()), 336 base::StringValue(result->source()->GetPrefName()),
306 base::StringValue(GetChromeCounterTextFromResult(result.get()))); 337 base::StringValue(GetChromeCounterTextFromResult(result.get())));
307 } 338 }
308 339
309 } // namespace settings 340 } // namespace settings
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698