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

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: change element.$.someId to element.$$(#someId) 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 kIsCheckedField[] = "isChecked";
57 const char kStorageSizeField[] = "storageSize";
58 const char kHasNotificationsField[] = "hasNotifications";
59
41 } // namespace 60 } // namespace
42 61
43 namespace settings { 62 namespace settings {
44 63
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 ---------------------------------------------------- 64 // ClearBrowsingDataHandler ----------------------------------------------------
80 65
81 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) 66 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui)
82 : profile_(Profile::FromWebUI(webui)), 67 : profile_(Profile::FromWebUI(webui)),
83 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)), 68 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)),
84 sync_service_observer_(this), 69 sync_service_observer_(this),
85 show_history_footer_(false), 70 show_history_footer_(false),
86 show_history_deletion_dialog_(false), 71 show_history_deletion_dialog_(false),
87 weak_ptr_factory_(this) {} 72 weak_ptr_factory_(this) {}
88 73
89 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { 74 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() {
90 } 75 }
91 76
92 void ClearBrowsingDataHandler::RegisterMessages() { 77 void ClearBrowsingDataHandler::RegisterMessages() {
93 web_ui()->RegisterMessageCallback( 78 web_ui()->RegisterMessageCallback(
94 "clearBrowsingData", 79 "clearBrowsingData",
95 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, 80 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData,
96 base::Unretained(this))); 81 base::Unretained(this)));
97 82
98 web_ui()->RegisterMessageCallback( 83 web_ui()->RegisterMessageCallback(
84 "getImportantSites",
85 base::Bind(&ClearBrowsingDataHandler::HandleGetImportantSites,
86 base::Unretained(this)));
87
88 web_ui()->RegisterMessageCallback(
99 "initializeClearBrowsingData", 89 "initializeClearBrowsingData",
100 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, 90 base::Bind(&ClearBrowsingDataHandler::HandleInitialize,
101 base::Unretained(this))); 91 base::Unretained(this)));
102 } 92 }
103 93
104 void ClearBrowsingDataHandler::OnJavascriptAllowed() { 94 void ClearBrowsingDataHandler::OnJavascriptAllowed() {
105 if (sync_service_) 95 if (sync_service_)
106 sync_service_observer_.Add(sync_service_); 96 sync_service_observer_.Add(sync_service_);
107 97
108 DCHECK(counters_.empty()); 98 DCHECK(counters_.empty());
109 for (const std::string& pref : kCounterPrefs) { 99 for (const std::string& pref : kCounterPrefs) {
110 AddCounter( 100 AddCounter(
111 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref)); 101 BrowsingDataCounterFactory::GetForProfileAndPref(profile_, pref));
112 } 102 }
113 } 103 }
114 104
115 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { 105 void ClearBrowsingDataHandler::OnJavascriptDisallowed() {
116 sync_service_observer_.RemoveAll(); 106 sync_service_observer_.RemoveAll();
117 task_observer_.reset(); 107 weak_ptr_factory_.InvalidateWeakPtrs();
118 counters_.clear(); 108 counters_.clear();
119 } 109 }
120 110
121 void ClearBrowsingDataHandler::HandleClearBrowsingData( 111 void ClearBrowsingDataHandler::HandleClearBrowsingData(
122 const base::ListValue* args) { 112 const base::ListValue* args) {
123 DCHECK(!task_observer_);
124
125 PrefService* prefs = profile_->GetPrefs(); 113 PrefService* prefs = profile_->GetPrefs();
126 114
127 int site_data_mask = ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA; 115 int site_data_mask = ChromeBrowsingDataRemoverDelegate::DATA_TYPE_SITE_DATA;
128 // Don't try to clear LSO data if it's not supported. 116 // Don't try to clear LSO data if it's not supported.
129 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) 117 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled))
130 site_data_mask &= ~ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA; 118 site_data_mask &= ~ChromeBrowsingDataRemoverDelegate::DATA_TYPE_PLUGIN_DATA;
131 119
132 int remove_mask = 0; 120 int remove_mask = 0;
133 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) { 121 if (prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory)) {
134 if (prefs->GetBoolean(browsing_data::prefs::kDeleteBrowsingHistory)) 122 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); }); 180 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); });
193 UMA_HISTOGRAM_SPARSE_SLOWLY( 181 UMA_HISTOGRAM_SPARSE_SLOWLY(
194 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", 182 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount",
195 checked_other_types); 183 checked_other_types);
196 } 184 }
197 185
198 int period_selected = 186 int period_selected =
199 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); 187 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod);
200 188
201 std::string webui_callback_id; 189 std::string webui_callback_id;
202 CHECK_EQ(1U, args->GetSize()); 190 CHECK_EQ(2U, args->GetSize());
203 CHECK(args->GetString(0, &webui_callback_id)); 191 CHECK(args->GetString(0, &webui_callback_id));
204 192
193 const base::ListValue* important_sites = nullptr;
194 CHECK(args->GetList(1, &important_sites));
195 std::unique_ptr<content::BrowsingDataFilterBuilder> filter_builder =
196 ProcessImportantSites(important_sites);
197
205 content::BrowsingDataRemover* remover = 198 content::BrowsingDataRemover* remover =
206 content::BrowserContext::GetBrowsingDataRemover(profile_); 199 content::BrowserContext::GetBrowsingDataRemover(profile_);
207 task_observer_ = base::MakeUnique<TaskObserver>( 200
208 remover, 201 base::OnceClosure callback =
209 base::Bind(&ClearBrowsingDataHandler::OnClearingTaskFinished, 202 base::BindOnce(&ClearBrowsingDataHandler::OnClearingTaskFinished,
210 base::Unretained(this), webui_callback_id)); 203 weak_ptr_factory_.GetWeakPtr(), webui_callback_id);
211 browsing_data::TimePeriod time_period = 204 browsing_data::TimePeriod time_period =
212 static_cast<browsing_data::TimePeriod>(period_selected); 205 static_cast<browsing_data::TimePeriod>(period_selected);
213 browsing_data::RecordDeletionForPeriod(time_period); 206
214 remover->RemoveAndReply( 207 browsing_data_important_sites_util::Remove(
215 browsing_data::CalculateBeginDeleteTime(time_period), 208 remove_mask, origin_mask, time_period, std::move(filter_builder), remover,
216 browsing_data::CalculateEndDeleteTime(time_period), 209 std::move(callback));
217 remove_mask, origin_mask, task_observer_.get()); 210 }
211
212 std::unique_ptr<content::BrowsingDataFilterBuilder>
213 ClearBrowsingDataHandler::ProcessImportantSites(
214 const base::ListValue* important_sites) {
215 std::vector<std::string> excluding_domains;
216 std::vector<int32_t> excluding_domain_reasons;
217 std::vector<std::string> ignoring_domains;
218 std::vector<int32_t> ignoring_domain_reasons;
219 for (const auto& item : *important_sites) {
220 const base::DictionaryValue* site = nullptr;
221 CHECK(item.GetAsDictionary(&site));
222 bool is_checked = false;
223 CHECK(site->GetBoolean(kIsCheckedField, &is_checked));
224 std::string domain;
225 CHECK(site->GetString(kRegisterableDomainField, &domain));
226 int domain_reason = -1;
227 CHECK(site->GetInteger(kReasonBitField, &domain_reason));
228 if (is_checked) { // Selected important sites should be deleted.
229 ignoring_domains.push_back(domain);
230 ignoring_domain_reasons.push_back(domain_reason);
231 } else { // Unselected sites should be kept.
232 excluding_domains.push_back(domain);
233 excluding_domain_reasons.push_back(domain_reason);
234 }
235 }
236
237 if (!excluding_domains.empty() || !ignoring_domains.empty()) {
238 ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites(
239 profile_->GetOriginalProfile(), excluding_domains,
240 excluding_domain_reasons, ignoring_domains, ignoring_domain_reasons);
241 }
242
243 std::unique_ptr<content::BrowsingDataFilterBuilder> filter_builder(
244 content::BrowsingDataFilterBuilder::Create(
245 content::BrowsingDataFilterBuilder::BLACKLIST));
246 for (const std::string& domain : excluding_domains) {
247 filter_builder->AddRegisterableDomain(domain);
248 }
249 return filter_builder;
218 } 250 }
219 251
220 void ClearBrowsingDataHandler::OnClearingTaskFinished( 252 void ClearBrowsingDataHandler::OnClearingTaskFinished(
221 const std::string& webui_callback_id) { 253 const std::string& webui_callback_id) {
222 PrefService* prefs = profile_->GetPrefs(); 254 PrefService* prefs = profile_->GetPrefs();
223 int notice_shown_times = prefs->GetInteger( 255 int notice_shown_times = prefs->GetInteger(
224 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes); 256 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes);
225 257
226 // When the deletion is complete, we might show an additional dialog with 258 // 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 259 // a notice about other forms of browsing history. This is the case if
(...skipping 10 matching lines...) Expand all
238 prefs->SetInteger( 270 prefs->SetInteger(
239 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes, 271 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes,
240 notice_shown_times + 1); 272 notice_shown_times + 1);
241 } 273 }
242 274
243 UMA_HISTOGRAM_BOOLEAN( 275 UMA_HISTOGRAM_BOOLEAN(
244 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice); 276 "History.ClearBrowsingData.ShownHistoryNoticeAfterClearing", show_notice);
245 277
246 ResolveJavascriptCallback(base::Value(webui_callback_id), 278 ResolveJavascriptCallback(base::Value(webui_callback_id),
247 base::Value(show_notice)); 279 base::Value(show_notice));
248 task_observer_.reset(); 280 }
281
282 void ClearBrowsingDataHandler::HandleGetImportantSites(
283 const base::ListValue* args) {
284 AllowJavascript();
285 const base::Value* callback_id;
286 CHECK(args->Get(0, &callback_id));
287 DCHECK(base::FeatureList::IsEnabled(features::kImportantSitesInCbd));
288
289 Profile* profile = profile_->GetOriginalProfile();
290 bool important_sites_dialog_disabled =
291 ImportantSitesUtil::IsDialogDisabled(profile);
292 base::ListValue important_sites_list;
293
294 if (!important_sites_dialog_disabled) {
295 std::vector<ImportantSitesUtil::ImportantDomainInfo> important_sites =
296 ImportantSitesUtil::GetImportantRegisterableDomains(profile,
297 kMaxImportantSites);
298 for (const auto& info : important_sites) {
299 auto entry = base::MakeUnique<base::DictionaryValue>();
300 entry->SetString(kRegisterableDomainField, info.registerable_domain);
301 // The |reason_bitfield| is only passed to Javascript to be logged
302 // from |HandleClearBrowsingData|.
303 entry->SetInteger(kReasonBitField, info.reason_bitfield);
304 entry->SetString(kExampleOriginField, info.example_origin.spec());
305 // Initially all sites are selected for deletion.
306 entry->SetBoolean(kIsCheckedField, true);
307 // TODO(dullweber): Get size.
308 entry->SetString(kStorageSizeField, ui::FormatBytes(0));
309 bool has_notifications =
310 (info.reason_bitfield & (1 << ImportantReason::NOTIFICATIONS)) != 0;
311 entry->SetBoolean(kHasNotificationsField, has_notifications);
312 important_sites_list.Append(std::move(entry));
313 }
314 }
315
316 ResolveJavascriptCallback(*callback_id, important_sites_list);
249 } 317 }
250 318
251 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { 319 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) {
252 AllowJavascript(); 320 AllowJavascript();
253 const base::Value* callback_id; 321 const base::Value* callback_id;
254 CHECK(args->Get(0, &callback_id)); 322 CHECK(args->Get(0, &callback_id));
255 323
256 // Needed because WebUI doesn't handle renderer crashes. See crbug.com/610450. 324 // Needed because WebUI doesn't handle renderer crashes. See crbug.com/610450.
257 task_observer_.reset(); 325 weak_ptr_factory_.InvalidateWeakPtrs();
258 326
259 UpdateSyncState(); 327 UpdateSyncState();
260 RefreshHistoryNotice(); 328 RefreshHistoryNotice();
261 329
262 // Restart the counters each time the dialog is reopened. 330 // Restart the counters each time the dialog is reopened.
263 for (const auto& counter : counters_) 331 for (const auto& counter : counters_)
264 counter->Restart(); 332 counter->Restart();
265 333
266 ResolveJavascriptCallback(*callback_id, base::Value() /* Promise<void> */); 334 ResolveJavascriptCallback(*callback_id, base::Value() /* Promise<void> */);
267 } 335 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 393
326 void ClearBrowsingDataHandler::UpdateCounterText( 394 void ClearBrowsingDataHandler::UpdateCounterText(
327 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { 395 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) {
328 CallJavascriptFunction( 396 CallJavascriptFunction(
329 "cr.webUIListenerCallback", base::Value("update-counter-text"), 397 "cr.webUIListenerCallback", base::Value("update-counter-text"),
330 base::Value(result->source()->GetPrefName()), 398 base::Value(result->source()->GetPrefName()),
331 base::Value(GetChromeCounterTextFromResult(result.get()))); 399 base::Value(GetChromeCounterTextFromResult(result.get())));
332 } 400 }
333 401
334 } // namespace settings 402 } // namespace settings
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.h ('k') | chrome/common/chrome_features.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698