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

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

Powered by Google App Engine
This is Rietveld 408576698