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

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

Issue 2716333002: Implement important sites dialog for desktop. (Closed)
Patch Set: replace task observer in -Bridge and -Handler with a callback Created 3 years, 7 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 #include <vector>
8 9
10 #include "base/feature_list.h"
9 #include "base/macros.h" 11 #include "base/macros.h"
10 #include "base/memory/ptr_util.h" 12 #include "base/memory/ptr_util.h"
11 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
12 #include "base/metrics/sparse_histogram.h" 14 #include "base/metrics/sparse_histogram.h"
15 #include "base/values.h"
13 #include "chrome/browser/browsing_data/browsing_data_counter_factory.h" 16 #include "chrome/browser/browsing_data/browsing_data_counter_factory.h"
14 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h" 17 #include "chrome/browser/browsing_data/browsing_data_counter_utils.h"
15 #include "chrome/browser/browsing_data/browsing_data_helper.h" 18 #include "chrome/browser/browsing_data/browsing_data_helper.h"
19 #include "chrome/browser/browsing_data/browsing_data_important_sites_util.h"
16 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h" 20 #include "chrome/browser/browsing_data/chrome_browsing_data_remover_delegate.h"
21 #include "chrome/browser/engagement/important_sites_util.h"
17 #include "chrome/browser/history/web_history_service_factory.h" 22 #include "chrome/browser/history/web_history_service_factory.h"
18 #include "chrome/browser/sync/profile_sync_service_factory.h" 23 #include "chrome/browser/sync/profile_sync_service_factory.h"
19 #include "chrome/common/channel_info.h" 24 #include "chrome/common/channel_info.h"
25 #include "chrome/common/chrome_features.h"
20 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
21 #include "components/browsing_data/core/history_notice_utils.h" 27 #include "components/browsing_data/core/history_notice_utils.h"
22 #include "components/browsing_data/core/pref_names.h" 28 #include "components/browsing_data/core/pref_names.h"
23 #include "components/prefs/pref_service.h" 29 #include "components/prefs/pref_service.h"
30 #include "content/public/browser/browsing_data_filter_builder.h"
24 #include "content/public/browser/web_ui.h" 31 #include "content/public/browser/web_ui.h"
32 #include "ui/base/text/bytes_formatting.h"
33
34 using ImportantReason = ImportantSitesUtil::ImportantReason;
25 35
26 namespace { 36 namespace {
27 37
28 const int kMaxTimesHistoryNoticeShown = 1; 38 const int kMaxTimesHistoryNoticeShown = 1;
29 39
40 const int kMaxImportantSites = 10;
41
30 // TODO(msramek): Get the list of deletion preferences from the JS side. 42 // TODO(msramek): Get the list of deletion preferences from the JS side.
31 const char* kCounterPrefs[] = { 43 const char* kCounterPrefs[] = {
32 browsing_data::prefs::kDeleteBrowsingHistory, 44 browsing_data::prefs::kDeleteBrowsingHistory,
33 browsing_data::prefs::kDeleteCache, 45 browsing_data::prefs::kDeleteCache,
34 browsing_data::prefs::kDeleteDownloadHistory, 46 browsing_data::prefs::kDeleteDownloadHistory,
35 browsing_data::prefs::kDeleteFormData, 47 browsing_data::prefs::kDeleteFormData,
36 browsing_data::prefs::kDeleteHostedAppsData, 48 browsing_data::prefs::kDeleteHostedAppsData,
37 browsing_data::prefs::kDeleteMediaLicenses, 49 browsing_data::prefs::kDeleteMediaLicenses,
38 browsing_data::prefs::kDeletePasswords, 50 browsing_data::prefs::kDeletePasswords,
39 }; 51 };
40 52
53 const char kRegisterableDomainField[] = "registerableDomain";
54 const char kReasonBitField[] = "reasonBitfield";
55 const char kExampleOriginField[] = "exampleOrigin";
56 const char kFlagEnabledField[] = "flagEnabled";
57 const char kImportantSitesField[] = "importantSites";
58 const char kIsCheckedField[] = "isChecked";
59 const char kStorageSizeField[] = "storageSize";
60 const char kHasNotificationsField[] = "hasNotifications";
61
41 } // namespace 62 } // namespace
42 63
43 namespace settings { 64 namespace settings {
44 65
45 // TaskObserver ----------------------------------------------------------------
46
47 class ClearBrowsingDataHandler::TaskObserver
48 : public content::BrowsingDataRemover::Observer {
49 public:
50 TaskObserver(content::BrowsingDataRemover* remover,
51 const base::Closure& callback);
52 ~TaskObserver() override;
53
54 void OnBrowsingDataRemoverDone() override;
55
56 private:
57 base::Closure callback_;
58 ScopedObserver<content::BrowsingDataRemover,
59 content::BrowsingDataRemover::Observer>
60 remover_observer_;
61
62 DISALLOW_COPY_AND_ASSIGN(TaskObserver);
63 };
64
65 ClearBrowsingDataHandler::TaskObserver::TaskObserver(
66 content::BrowsingDataRemover* remover,
67 const base::Closure& callback)
68 : callback_(callback), remover_observer_(this) {
69 remover_observer_.Add(remover);
70 }
71
72 ClearBrowsingDataHandler::TaskObserver::~TaskObserver() {}
73
74 void ClearBrowsingDataHandler::TaskObserver::OnBrowsingDataRemoverDone() {
75 remover_observer_.RemoveAll();
76 callback_.Run();
77 }
78
79 // ClearBrowsingDataHandler ---------------------------------------------------- 66 // ClearBrowsingDataHandler ----------------------------------------------------
80 67
81 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) 68 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui)
82 : profile_(Profile::FromWebUI(webui)), 69 : profile_(Profile::FromWebUI(webui)),
83 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)), 70 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)),
84 sync_service_observer_(this), 71 sync_service_observer_(this),
85 show_history_footer_(false), 72 show_history_footer_(false),
86 show_history_deletion_dialog_(false), 73 show_history_deletion_dialog_(false),
87 weak_ptr_factory_(this) {} 74 weak_ptr_factory_(this) {}
88 75
89 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { 76 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() {
90 } 77 }
91 78
92 void ClearBrowsingDataHandler::RegisterMessages() { 79 void ClearBrowsingDataHandler::RegisterMessages() {
93 web_ui()->RegisterMessageCallback( 80 web_ui()->RegisterMessageCallback(
94 "clearBrowsingData", 81 "clearBrowsingData",
95 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, 82 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData,
96 base::Unretained(this))); 83 base::Unretained(this)));
97 84
98 web_ui()->RegisterMessageCallback( 85 web_ui()->RegisterMessageCallback(
86 "fetchImportantSites",
87 base::Bind(&ClearBrowsingDataHandler::HandleFetchImportantSites,
88 base::Unretained(this)));
89
90 web_ui()->RegisterMessageCallback(
99 "initializeClearBrowsingData", 91 "initializeClearBrowsingData",
100 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, 92 base::Bind(&ClearBrowsingDataHandler::HandleInitialize,
101 base::Unretained(this))); 93 base::Unretained(this)));
102 } 94 }
103 95
104 void ClearBrowsingDataHandler::OnJavascriptAllowed() { 96 void ClearBrowsingDataHandler::OnJavascriptAllowed() {
105 if (sync_service_) 97 if (sync_service_)
106 sync_service_observer_.Add(sync_service_); 98 sync_service_observer_.Add(sync_service_);
107 99
108 DCHECK(counters_.empty()); 100 DCHECK(counters_.empty());
109 for (const std::string& pref : kCounterPrefs) { 101 for (const std::string& pref : kCounterPrefs) {
110 AddCounter( 102 AddCounter(
111 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref)); 103 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref));
112 } 104 }
113 } 105 }
114 106
115 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { 107 void ClearBrowsingDataHandler::OnJavascriptDisallowed() {
116 sync_service_observer_.RemoveAll(); 108 sync_service_observer_.RemoveAll();
117 task_observer_.reset(); 109 weak_ptr_factory_.InvalidateWeakPtrs();
118 counters_.clear(); 110 counters_.clear();
119 } 111 }
120 112
121 void ClearBrowsingDataHandler::HandleClearBrowsingData( 113 void ClearBrowsingDataHandler::HandleClearBrowsingData(
122 const base::ListValue* args) { 114 const base::ListValue* args) {
123 DCHECK(!task_observer_);
124
125 PrefService* prefs = profile_->GetPrefs(); 115 PrefService* prefs = profile_->GetPrefs();
126 116
127 int site_data_mask = ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA; 117 int site_data_mask = ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA;
128 // Don't try to clear LSO data if it's not supported. 118 // Don't try to clear LSO data if it's not supported.
129 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) 119 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled))
130 site_data_mask &= ~ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA; 120 site_data_mask &= ~ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA;
131 121
132 int remove_mask = 0; 122 int remove_mask = 0;
133 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) { 123 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) {
134 if (prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory)) 124 if (prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory))
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); 182 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); });
193 UMA_HISTOGRAM_SPARSE_SLOWLY( 183 UMA_HISTOGRAM_SPARSE_SLOWLY(
194 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", 184 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount",
195 checked_other_types); 185 checked_other_types);
196 } 186 }
197 187
198 int period_selected = 188 int period_selected =
199 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); 189 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod);
200 190
201 std::string webui_callback_id; 191 std::string webui_callback_id;
202 CHECK_EQ(1U, args->GetSize()); 192 CHECK_EQ(2U, args->GetSize());
203 CHECK(args->GetString(0, &webui_callback_id)); 193 CHECK(args->GetString(0, &webui_callback_id));
204 194
195 const base::ListValue* important_sites = nullptr;
196 CHECK(args->GetList(1, &important_sites));
197 std::unique_ptr<content::BrowsingDataFilterBuilder> filter_builder =
198 ProcessImportantSites(important_sites);
199
205 content::BrowsingDataRemover* remover = 200 content::BrowsingDataRemover* remover =
206 content::BrowserContext::GetBrowsingDataRemover(profile_); 201 content::BrowserContext::GetBrowsingDataRemover(profile_);
207 task_observer_ = base::MakeUnique<TaskObserver>( 202
208 remover, 203 base::OnceClosure callback =
209 base::Bind(&ClearBrowsingDataHandler::OnClearingTaskFinished, 204 base::BindOnce(&ClearBrowsingDataHandler::OnClearingTaskFinished,
210 base::Unretained(this), webui_callback_id)); 205 weak_ptr_factory_.GetWeakPtr(), webui_callback_id);
211 browsing_data::TimePeriod time_period = 206 browsing_data::TimePeriod time_period =
212 static_cast<browsing_data::TimePeriod>(period_selected); 207 static_cast<browsing_data::TimePeriod>(period_selected);
213 browsing_data::RecordDeletionForPeriod(time_period); 208
214 remover->RemoveAndReply( 209 browsing_data_important_sites_util::Remove(
215 browsing_data::CalculateBeginDeleteTime(time_period), 210 remove_mask, origin_mask, time_period, std::move(filter_builder), remover,
216 browsing_data::CalculateEndDeleteTime(time_period), 211 std::move(callback));
217 remove_mask, origin_mask, task_observer_.get()); 212 }
213
214 std::unique_ptr<content::BrowsingDataFilterBuilder>
215 ClearBrowsingDataHandler::ProcessImportantSites(
216 const base::ListValue* important_sites) {
217 std::vector<std::string> excluding_domains;
218 std::vector<int32_t> excluding_domain_reasons;
219 std::vector<std::string> ignoring_domains;
220 std::vector<int32_t> ignoring_domain_reasons;
221 for (const auto& item : *important_sites) {
222 const base::DictionaryValue* site = nullptr;
223 CHECK(item.GetAsDictionary(&site));
224 bool is_checked = false;
225 CHECK(site->GetBoolean(kIsCheckedField, &is_checked));
226 std::string domain;
227 CHECK(site->GetString(kRegisterableDomainField, &domain));
228 int domain_reason = -1;
229 CHECK(site->GetInteger(kReasonBitField, &domain_reason));
230 if (is_checked) { // Selected important sites should be deleted.
231 ignoring_domains.push_back(domain);
232 ignoring_domain_reasons.push_back(domain_reason);
233 } else { // Unselected sites should be kept.
234 excluding_domains.push_back(domain);
235 excluding_domain_reasons.push_back(domain_reason);
236 }
237 }
238
239 if (!excluding_domains.empty() || !ignoring_domains.empty()) {
240 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites(
241 profile_->GetOriginalProfile(), excluding_domains,
242 excluding_domain_reasons, ignoring_domains, ignoring_domain_reasons);
243 }
244
245 std::unique_ptr<content::BrowsingDataFilterBuilder> filter_builder(
246 content::BrowsingDataFilterBuilder::Create(
247 content::BrowsingDataFilterBuilder::BLACKLIST));
248 for (const std::string& domain : excluding_domains) {
249 filter_builder->AddRegisterableDomain(domain);
250 }
251 return filter_builder;
218 } 252 }
219 253
220 void ClearBrowsingDataHandler::OnClearingTaskFinished( 254 void ClearBrowsingDataHandler::OnClearingTaskFinished(
221 const std::string& webui_callback_id) { 255 const std::string& webui_callback_id) {
222 PrefService* prefs = profile_->GetPrefs(); 256 PrefService* prefs = profile_->GetPrefs();
223 int notice_shown_times = prefs->GetInteger( 257 int notice_shown_times = prefs->GetInteger(
224 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes); 258 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes);
225 259
226 // When the deletion is complete, we might show an additional dialog with 260 // When the deletion is complete, we might show an additional dialog with
227 // a notice about other forms of browsing history. This is the case if 261 // a notice about other forms of browsing history. This is the case if
(...skipping 10 matching lines...) Expand all
238 prefs->SetInteger( 272 prefs->SetInteger(
239 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes, 273 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes,
240 notice_shown_times + 1); 274 notice_shown_times + 1);
241 } 275 }
242 276
243 UMA_HISTOGRAM_BOOLEAN( 277 UMA_HISTOGRAM_BOOLEAN(
244 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice); 278 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice);
245 279
246 ResolveJavascriptCallback(base::Value(webui_callback_id), 280 ResolveJavascriptCallback(base::Value(webui_callback_id),
247 base::Value(show_notice)); 281 base::Value(show_notice));
248 task_observer_.reset(); 282 }
283
284 void ClearBrowsingDataHandler::HandleFetchImportantSites(
285 const base::ListValue* args) {
286 AllowJavascript();
287 const base::Value* callback_id;
288 CHECK(args->Get(0, &callback_id));
289
290 bool important_sites_flag_enabled =
291 base::FeatureList::IsEnabled(features::kImportantSitesInCbd);
Dan Beam 2017/05/05 17:52:51 can we just send this separately? and only ask fo
dullweber 2017/05/09 08:56:39 I could send this separately but I already skip fe
Dan Beam 2017/05/09 19:25:03 i'd just like to do as little as possible when thi
dullweber 2017/05/11 13:13:43 Is there another way to access flags from Javascri
Dan Beam 2017/05/16 03:25:24 yes, here's an example of passing a boolean of whe
292 Profile* profile = profile_->GetOriginalProfile();
293 bool important_sites_dialog_disabled =
294 ImportantSitesUtil::IsDialogDisabled(profile);
295 auto important_sites_list = base::MakeUnique<base::ListValue>();
296
297 if (important_sites_flag_enabled && !important_sites_dialog_disabled) {
298 std::vector<ImportantSitesUtil::ImportantDomainInfo> important_sites =
299 ImportantSitesUtil::GetImportantRegisterableDomains(profile,
300 kMaxImportantSites);
301 for (const auto& info : important_sites) {
302 auto entry = base::MakeUnique<base::DictionaryValue>();
303 entry->SetString(kRegisterableDomainField, info.registerable_domain);
304 // The |reason_bitfield| is only passed to Javascript to be logged
305 // from |HandleClearBrowsingData|.
306 entry->SetInteger(kReasonBitField, info.reason_bitfield);
307 entry->SetString(kExampleOriginField, info.example_origin.spec());
308 // Initially all sites are selected for deletion.
309 entry->SetBoolean(kIsCheckedField, true);
310 // TODO(dullweber): Get size.
311 entry->SetString(kStorageSizeField, ui::FormatBytes(0));
312 bool has_notifications =
313 (info.reason_bitfield & (1 << ImportantReason::NOTIFICATIONS)) != 0;
314 entry->SetBoolean(kHasNotificationsField, has_notifications);
315 important_sites_list->Append(std::move(entry));
316 }
317 }
318
319 std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
320 result->SetBoolean(kFlagEnabledField, important_sites_flag_enabled);
321 result->Set(kImportantSitesField, std::move(important_sites_list));
322
323 ResolveJavascriptCallback(*callback_id, *result);
249 } 324 }
250 325
251 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { 326 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) {
252 AllowJavascript(); 327 AllowJavascript();
253 const base::Value* callback_id; 328 const base::Value* callback_id;
254 CHECK(args->Get(0, &callback_id)); 329 CHECK(args->Get(0, &callback_id));
255 330
256 // Needed because WebUI doesn't handle renderer crashes. See crbug.com/610450. 331 // Needed because WebUI doesn't handle renderer crashes. See crbug.com/610450.
257 task_observer_.reset(); 332 weak_ptr_factory_.InvalidateWeakPtrs();
258 333
259 UpdateSyncState(); 334 UpdateSyncState();
260 RefreshHistoryNotice(); 335 RefreshHistoryNotice();
261 336
262 // Restart the counters each time the dialog is reopened. 337 // Restart the counters each time the dialog is reopened.
263 for (const auto& counter : counters_) 338 for (const auto& counter : counters_)
264 counter->Restart(); 339 counter->Restart();
265 340
266 ResolveJavascriptCallback(*callback_id, base::Value() /* Promise<void> */); 341 ResolveJavascriptCallback(*callback_id, base::Value() /* Promise<void> */);
267 } 342 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 400
326 void ClearBrowsingDataHandler::UpdateCounterText( 401 void ClearBrowsingDataHandler::UpdateCounterText(
327 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { 402 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
328 CallJavascriptFunction( 403 CallJavascriptFunction(
329 "cr.webUIListenerCallback", base::Value("update-counter-text"), 404 "cr.webUIListenerCallback", base::Value("update-counter-text"),
330 base::Value(result->source()->GetPrefName()), 405 base::Value(result->source()->GetPrefName()),
331 base::Value(GetChromeCounterTextFromResult(result.get()))); 406 base::Value(GetChromeCounterTextFromResult(result.get())));
332 } 407 }
333 408
334 } // namespace settings 409 } // namespace settings
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698