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

Side by Side Diff: chrome/browser/engagement/important_sites_util.cc

Issue 2752263003: Count site data size for important sites (Closed)
Patch Set: add localstorage to test case Created 3 years, 9 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 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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/engagement/important_sites_util.h" 5 #include "chrome/browser/engagement/important_sites_util.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <map> 8 #include <map>
9 #include <memory> 9 #include <memory>
10 #include <set> 10 #include <set>
11 #include <utility> 11 #include <utility>
12 12
13 #include "base/bind_helpers.h"
13 #include "base/containers/hash_tables.h" 14 #include "base/containers/hash_tables.h"
14 #include "base/memory/ptr_util.h" 15 #include "base/memory/ptr_util.h"
15 #include "base/metrics/histogram_macros.h" 16 #include "base/metrics/histogram_macros.h"
16 #include "base/stl_util.h" 17 #include "base/stl_util.h"
17 #include "base/time/time.h" 18 #include "base/time/time.h"
18 #include "base/values.h" 19 #include "base/values.h"
19 #include "chrome/browser/banners/app_banner_settings_helper.h" 20 #include "chrome/browser/banners/app_banner_settings_helper.h"
20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" 21 #include "chrome/browser/bookmarks/bookmark_model_factory.h"
21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" 22 #include "chrome/browser/content_settings/host_content_settings_map_factory.h"
22 #include "chrome/browser/engagement/site_engagement_score.h" 23 #include "chrome/browser/engagement/site_engagement_score.h"
23 #include "chrome/browser/engagement/site_engagement_service.h" 24 #include "chrome/browser/engagement/site_engagement_service.h"
24 #include "chrome/browser/profiles/profile.h" 25 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/common/pref_names.h" 26 #include "chrome/common/pref_names.h"
26 #include "components/bookmarks/browser/bookmark_model.h" 27 #include "components/bookmarks/browser/bookmark_model.h"
27 #include "components/content_settings/core/browser/host_content_settings_map.h" 28 #include "components/content_settings/core/browser/host_content_settings_map.h"
28 #include "components/content_settings/core/common/content_settings.h" 29 #include "components/content_settings/core/common/content_settings.h"
29 #include "components/pref_registry/pref_registry_syncable.h" 30 #include "components/pref_registry/pref_registry_syncable.h"
30 #include "components/prefs/pref_service.h" 31 #include "components/prefs/pref_service.h"
31 #include "components/prefs/scoped_user_pref_update.h" 32 #include "components/prefs/scoped_user_pref_update.h"
33 #include "content/public/browser/browser_thread.h"
34 #include "content/public/browser/dom_storage_context.h"
35 #include "content/public/browser/local_storage_usage_info.h"
32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" 36 #include "net/base/registry_controlled_domains/registry_controlled_domain.h"
37 #include "storage/browser/quota/quota_manager.h"
33 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" 38 #include "third_party/WebKit/public/platform/site_engagement.mojom.h"
34 #include "url/gurl.h" 39 #include "url/gurl.h"
40 #include "url/url_util.h"
35 41
36 namespace { 42 namespace {
37 using bookmarks::BookmarkModel; 43 using bookmarks::BookmarkModel;
44 using content::BrowserThread;
38 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; 45 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo;
39 using ImportantReason = ImportantSitesUtil::ImportantReason; 46 using ImportantReason = ImportantSitesUtil::ImportantReason;
40 47
41 // Note: These values are stored on both the per-site content settings 48 // Note: These values are stored on both the per-site content settings
42 // dictionary and the dialog preference dictionary. 49 // dictionary and the dialog preference dictionary.
43 50
44 static const char kTimeLastIgnored[] = "TimeLastIgnored"; 51 static const char kTimeLastIgnored[] = "TimeLastIgnored";
45 static const int kBlacklistExpirationTimeDays = 30 * 5; 52 static const int kBlacklistExpirationTimeDays = 30 * 5;
46 53
47 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; 54 static const char kNumTimesIgnoredName[] = "NumTimesIgnored";
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 141
135 std::string GetRegisterableDomainOrIP(const GURL& url) { 142 std::string GetRegisterableDomainOrIP(const GURL& url) {
136 std::string registerable_domain = 143 std::string registerable_domain =
137 net::registry_controlled_domains::GetDomainAndRegistry( 144 net::registry_controlled_domains::GetDomainAndRegistry(
138 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); 145 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
139 if (registerable_domain.empty() && url.HostIsIPAddress()) 146 if (registerable_domain.empty() && url.HostIsIPAddress())
140 registerable_domain = url.host(); 147 registerable_domain = url.host();
141 return registerable_domain; 148 return registerable_domain;
142 } 149 }
143 150
151 std::string GetRegisterableDomainOrIPFromHost(const std::string& host) {
152 std::string registerable_domain =
153 net::registry_controlled_domains::GetDomainAndRegistry(
154 host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
155 if (registerable_domain.empty() && url::HostIsIPAddress(host))
156 registerable_domain = host;
157 return registerable_domain;
158 }
159
144 void MaybePopulateImportantInfoForReason( 160 void MaybePopulateImportantInfoForReason(
145 const GURL& origin, 161 const GURL& origin,
146 std::set<GURL>* visited_origins, 162 std::set<GURL>* visited_origins,
147 ImportantReason reason, 163 ImportantReason reason,
148 base::hash_map<std::string, ImportantDomainInfo>* output) { 164 base::hash_map<std::string, ImportantDomainInfo>* output) {
149 if (!origin.is_valid() || !visited_origins->insert(origin).second) 165 if (!origin.is_valid() || !visited_origins->insert(origin).second)
150 return; 166 return;
151 std::string registerable_domain = GetRegisterableDomainOrIP(origin); 167 std::string registerable_domain = GetRegisterableDomainOrIP(origin);
152 ImportantDomainInfo& info = (*output)[registerable_domain]; 168 ImportantDomainInfo& info = (*output)[registerable_domain];
153 info.reason_bitfield |= 1 << reason; 169 info.reason_bitfield |= 1 << reason;
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
332 base::Time now = base::Time::Now(); 348 base::Time now = base::Time::Now();
333 for (const ContentSettingPatternSource& site : content_settings_list) { 349 for (const ContentSettingPatternSource& site : content_settings_list) {
334 GURL origin(site.primary_pattern.ToString()); 350 GURL origin(site.primary_pattern.ToString());
335 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) 351 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now))
336 continue; 352 continue;
337 MaybePopulateImportantInfoForReason(origin, &content_origins, 353 MaybePopulateImportantInfoForReason(origin, &content_origins,
338 ImportantReason::HOME_SCREEN, output); 354 ImportantReason::HOME_SCREEN, output);
339 } 355 }
340 } 356 }
341 357
358 class UsageReceiver {
359 public:
360 UsageReceiver(ImportantSitesUtil::UsageCallback done,
361 std::vector<ImportantDomainInfo> sites,
362 storage::QuotaManager* quota_manager,
363 content::DOMStorageContext* dom_storage_context)
364 : done_(done),
365 sites_(sites),
366 quota_manager_(quota_manager),
367 dom_storage_context_(dom_storage_context),
368 tasks_(-1) {
369 for (auto& site : sites_) {
370 site.usage = 0;
371 site_map[site.registerable_domain] = &site;
372 }
373 }
374
375 void RunAndDestroySelf() {
376 BrowserThread::PostTask(
377 BrowserThread::IO, FROM_HERE,
378 base::Bind(&UsageReceiver::RunOnIOThread, base::Unretained(this)));
379 }
380
381 private:
382 void RunOnIOThread() {
383 DCHECK_CURRENTLY_ON(BrowserThread::IO);
384 tasks_ = 1; // Task for this method
385 tasks_ += 1;
386 quota_manager_->GetUsageInfo(
387 base::Bind(&UsageReceiver::ReceiveQuotaUsage, base::Unretained(this)));
388
389 tasks_ += 1;
390 dom_storage_context_->GetLocalStorageUsage(base::Bind(
391 &UsageReceiver::ReceiveLocalStorageUsage, base::Unretained(this)));
392 Done();
393 }
394
395 void ReceiveQuotaUsage(const std::vector<storage::UsageInfo>& usage_infos) {
396 DCHECK_CURRENTLY_ON(BrowserThread::IO);
397 for (const auto& info : usage_infos) {
398 CountUsage(GetRegisterableDomainOrIPFromHost(info.host), info.usage);
399 }
400 Done();
401 }
402
403 void ReceiveLocalStorageUsage(
404 const std::vector<content::LocalStorageUsageInfo>& storage_infos) {
405 DCHECK_CURRENTLY_ON(BrowserThread::IO);
406 for (const auto& info : storage_infos) {
407 CountUsage(GetRegisterableDomainOrIP(info.origin), info.data_size);
408 }
409 Done();
410 }
411
412 // Look up the corresponding ImportantDomainInfo for |url| and increase its
413 // usage by |size|.
414 void CountUsage(const std::string& domain, int64_t size) {
415 auto it = site_map.find(domain);
416 if (it != site_map.end()) {
417 it->second->usage += size;
418 }
419 }
420
421 void Done() {
422 DCHECK_CURRENTLY_ON(BrowserThread::IO);
423 DCHECK(tasks_ > 0);
424 if (--tasks_ == 0) {
425 BrowserThread::PostTask(
426 BrowserThread::UI, FROM_HERE,
427 base::Bind(&UsageReceiver::Finish, base::Unretained(this)));
428 }
429 }
430
431 void Finish() {
432 DCHECK_CURRENTLY_ON(BrowserThread::UI);
433 done_.Run(std::move(sites_));
434 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
435 }
436
437 private:
438 ImportantSitesUtil::UsageCallback done_;
439 std::vector<ImportantDomainInfo> sites_;
440 std::map<std::string, ImportantDomainInfo*> site_map;
dmurph 2017/03/27 22:22:45 unordered_map should be more suitable here -- or e
dullweber 2017/03/28 14:06:45 I changed it to unordered_map. This map can have
dmurph 2017/03/28 21:51:13 Then SmallMap will definitely be faster. Iterating
441 storage::QuotaManager* quota_manager_;
442 content::DOMStorageContext* dom_storage_context_;
443 int tasks_;
444 };
445
342 } // namespace 446 } // namespace
343 447
344 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { 448 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) {
345 PrefService* service = profile->GetPrefs(); 449 PrefService* service = profile->GetPrefs();
346 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); 450 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory);
347 451
348 return ShouldSuppressItem(update.Get()); 452 return ShouldSuppressItem(update.Get());
349 } 453 }
350 454
351 void ImportantSitesUtil::RegisterProfilePrefs( 455 void ImportantSitesUtil::RegisterProfilePrefs(
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
393 final_list.push_back(domain_info.second); 497 final_list.push_back(domain_info.second);
394 RECORD_UMA_FOR_IMPORTANT_REASON( 498 RECORD_UMA_FOR_IMPORTANT_REASON(
395 "Storage.ImportantSites.GeneratedReason", 499 "Storage.ImportantSites.GeneratedReason",
396 "Storage.ImportantSites.GeneratedReasonCount", 500 "Storage.ImportantSites.GeneratedReasonCount",
397 domain_info.second.reason_bitfield); 501 domain_info.second.reason_bitfield);
398 } 502 }
399 503
400 return final_list; 504 return final_list;
401 } 505 }
402 506
507 void ImportantSitesUtil::PopulateUsage(storage::QuotaManager* quota_manager,
508 content::DOMStorageContext* dom_storage,
509 std::vector<ImportantDomainInfo> sites,
510 UsageCallback callback) {
511 UsageReceiver* usage_receiver =
512 new UsageReceiver(callback, std::move(sites), quota_manager, dom_storage);
513 usage_receiver->RunAndDestroySelf();
514 }
515
403 void ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( 516 void ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites(
404 Profile* profile, 517 Profile* profile,
405 const std::vector<std::string>& blacklisted_sites, 518 const std::vector<std::string>& blacklisted_sites,
406 const std::vector<int32_t>& blacklisted_sites_reason_bitfield, 519 const std::vector<int32_t>& blacklisted_sites_reason_bitfield,
407 const std::vector<std::string>& ignored_sites, 520 const std::vector<std::string>& ignored_sites,
408 const std::vector<int32_t>& ignored_sites_reason_bitfield) { 521 const std::vector<int32_t>& ignored_sites_reason_bitfield) {
409 // First, record the metrics for blacklisted and ignored sites. 522 // First, record the metrics for blacklisted and ignored sites.
410 for (int32_t reason_bitfield : blacklisted_sites_reason_bitfield) { 523 for (int32_t reason_bitfield : blacklisted_sites_reason_bitfield) {
411 RECORD_UMA_FOR_IMPORTANT_REASON( 524 RECORD_UMA_FOR_IMPORTANT_REASON(
412 "Storage.ImportantSites.CBDChosenReason", 525 "Storage.ImportantSites.CBDChosenReason",
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 const GURL& origin) { 585 const GURL& origin) {
473 SiteEngagementScore::SetParamValuesForTesting(); 586 SiteEngagementScore::SetParamValuesForTesting();
474 // First get data from site engagement. 587 // First get data from site engagement.
475 SiteEngagementService* site_engagement_service = 588 SiteEngagementService* site_engagement_service =
476 SiteEngagementService::Get(profile); 589 SiteEngagementService::Get(profile);
477 site_engagement_service->ResetBaseScoreForURL( 590 site_engagement_service->ResetBaseScoreForURL(
478 origin, SiteEngagementScore::GetMediumEngagementBoundary()); 591 origin, SiteEngagementScore::GetMediumEngagementBoundary());
479 DCHECK(site_engagement_service->IsEngagementAtLeast( 592 DCHECK(site_engagement_service->IsEngagementAtLeast(
480 origin, blink::mojom::EngagementLevel::MEDIUM)); 593 origin, blink::mojom::EngagementLevel::MEDIUM));
481 } 594 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698