OLD | NEW |
---|---|
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 <unordered_map> | |
11 #include <utility> | 12 #include <utility> |
12 | 13 |
14 #include "base/bind_helpers.h" | |
13 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.h" |
16 #include "base/containers/small_map.h" | |
14 #include "base/memory/ptr_util.h" | 17 #include "base/memory/ptr_util.h" |
15 #include "base/metrics/histogram_macros.h" | 18 #include "base/metrics/histogram_macros.h" |
16 #include "base/stl_util.h" | 19 #include "base/stl_util.h" |
17 #include "base/time/time.h" | 20 #include "base/time/time.h" |
18 #include "base/values.h" | 21 #include "base/values.h" |
19 #include "chrome/browser/banners/app_banner_settings_helper.h" | 22 #include "chrome/browser/banners/app_banner_settings_helper.h" |
20 #include "chrome/browser/bookmarks/bookmark_model_factory.h" | 23 #include "chrome/browser/bookmarks/bookmark_model_factory.h" |
21 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" | 24 #include "chrome/browser/content_settings/host_content_settings_map_factory.h" |
22 #include "chrome/browser/engagement/site_engagement_score.h" | 25 #include "chrome/browser/engagement/site_engagement_score.h" |
23 #include "chrome/browser/engagement/site_engagement_service.h" | 26 #include "chrome/browser/engagement/site_engagement_service.h" |
24 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
25 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
26 #include "components/bookmarks/browser/bookmark_model.h" | 29 #include "components/bookmarks/browser/bookmark_model.h" |
27 #include "components/content_settings/core/browser/host_content_settings_map.h" | 30 #include "components/content_settings/core/browser/host_content_settings_map.h" |
28 #include "components/content_settings/core/common/content_settings.h" | 31 #include "components/content_settings/core/common/content_settings.h" |
29 #include "components/pref_registry/pref_registry_syncable.h" | 32 #include "components/pref_registry/pref_registry_syncable.h" |
30 #include "components/prefs/pref_service.h" | 33 #include "components/prefs/pref_service.h" |
31 #include "components/prefs/scoped_user_pref_update.h" | 34 #include "components/prefs/scoped_user_pref_update.h" |
35 #include "content/public/browser/browser_thread.h" | |
36 #include "content/public/browser/dom_storage_context.h" | |
37 #include "content/public/browser/local_storage_usage_info.h" | |
32 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 38 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
39 #include "storage/browser/quota/quota_manager.h" | |
33 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" | 40 #include "third_party/WebKit/public/platform/site_engagement.mojom.h" |
34 #include "url/gurl.h" | 41 #include "url/gurl.h" |
42 #include "url/url_util.h" | |
35 | 43 |
36 namespace { | 44 namespace { |
37 using bookmarks::BookmarkModel; | 45 using bookmarks::BookmarkModel; |
46 using content::BrowserThread; | |
dominickn
2017/04/10 05:06:12
Nit: the other files in engagement/ explicitly spe
dullweber
2017/04/10 13:29:38
Done.
| |
38 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; | 47 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo; |
39 using ImportantReason = ImportantSitesUtil::ImportantReason; | 48 using ImportantReason = ImportantSitesUtil::ImportantReason; |
49 using ImportantSiteMap = | |
50 base::SmallMap<std::unordered_map<std::string, ImportantDomainInfo*>, | |
51 ImportantSitesUtil::kMaxImportantSites>; | |
40 | 52 |
41 // Note: These values are stored on both the per-site content settings | 53 // Note: These values are stored on both the per-site content settings |
42 // dictionary and the dialog preference dictionary. | 54 // dictionary and the dialog preference dictionary. |
43 | 55 |
44 static const char kTimeLastIgnored[] = "TimeLastIgnored"; | 56 static const char kTimeLastIgnored[] = "TimeLastIgnored"; |
45 static const int kBlacklistExpirationTimeDays = 30 * 5; | 57 static const int kBlacklistExpirationTimeDays = 30 * 5; |
46 | 58 |
47 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; | 59 static const char kNumTimesIgnoredName[] = "NumTimesIgnored"; |
48 static const int kTimesIgnoredForBlacklist = 3; | 60 static const int kTimesIgnoredForBlacklist = 3; |
49 | 61 |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
134 | 146 |
135 std::string GetRegisterableDomainOrIP(const GURL& url) { | 147 std::string GetRegisterableDomainOrIP(const GURL& url) { |
136 std::string registerable_domain = | 148 std::string registerable_domain = |
137 net::registry_controlled_domains::GetDomainAndRegistry( | 149 net::registry_controlled_domains::GetDomainAndRegistry( |
138 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 150 url, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
139 if (registerable_domain.empty() && url.HostIsIPAddress()) | 151 if (registerable_domain.empty() && url.HostIsIPAddress()) |
140 registerable_domain = url.host(); | 152 registerable_domain = url.host(); |
141 return registerable_domain; | 153 return registerable_domain; |
142 } | 154 } |
143 | 155 |
156 std::string GetRegisterableDomainOrIPFromHost(const std::string& host) { | |
dominickn
2017/04/10 05:06:12
Would it be cheaper / more clear to just call GetR
dullweber
2017/04/10 13:29:38
The issue is that I can't create a GURL from hosts
| |
157 std::string registerable_domain = | |
158 net::registry_controlled_domains::GetDomainAndRegistry( | |
159 host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | |
160 if (registerable_domain.empty() && url::HostIsIPAddress(host)) | |
161 registerable_domain = host; | |
162 return registerable_domain; | |
163 } | |
164 | |
144 void MaybePopulateImportantInfoForReason( | 165 void MaybePopulateImportantInfoForReason( |
145 const GURL& origin, | 166 const GURL& origin, |
146 std::set<GURL>* visited_origins, | 167 std::set<GURL>* visited_origins, |
147 ImportantReason reason, | 168 ImportantReason reason, |
148 base::hash_map<std::string, ImportantDomainInfo>* output) { | 169 base::hash_map<std::string, ImportantDomainInfo>* output) { |
149 if (!origin.is_valid() || !visited_origins->insert(origin).second) | 170 if (!origin.is_valid() || !visited_origins->insert(origin).second) |
150 return; | 171 return; |
151 std::string registerable_domain = GetRegisterableDomainOrIP(origin); | 172 std::string registerable_domain = GetRegisterableDomainOrIP(origin); |
152 ImportantDomainInfo& info = (*output)[registerable_domain]; | 173 ImportantDomainInfo& info = (*output)[registerable_domain]; |
153 info.reason_bitfield |= 1 << reason; | 174 info.reason_bitfield |= 1 << reason; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
332 base::Time now = base::Time::Now(); | 353 base::Time now = base::Time::Now(); |
333 for (const ContentSettingPatternSource& site : content_settings_list) { | 354 for (const ContentSettingPatternSource& site : content_settings_list) { |
334 GURL origin(site.primary_pattern.ToString()); | 355 GURL origin(site.primary_pattern.ToString()); |
335 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) | 356 if (!AppBannerSettingsHelper::WasLaunchedRecently(profile, origin, now)) |
336 continue; | 357 continue; |
337 MaybePopulateImportantInfoForReason(origin, &content_origins, | 358 MaybePopulateImportantInfoForReason(origin, &content_origins, |
338 ImportantReason::HOME_SCREEN, output); | 359 ImportantReason::HOME_SCREEN, output); |
339 } | 360 } |
340 } | 361 } |
341 | 362 |
363 // A helper class that retrieves the localstorage and quota usage for each | |
364 // domain in |ImportantDomainInfo| and populates |ImportantDomainInfo::usage|. | |
dominickn
2017/04/10 05:06:12
This is a pretty big class (100 lines). It might b
dullweber
2017/04/10 13:29:38
I moved it a separate file. That cleaned up this f
| |
365 class UsageReceiver { | |
366 public: | |
dominickn
2017/04/10 05:06:12
You should probably document that the receiver wil
dullweber
2017/04/10 13:29:39
The |sites| parameter is a std::vector<ImportantDo
dominickn
2017/04/11 00:18:20
D'oh, I misread and thought that param was an rval
| |
367 UsageReceiver(ImportantSitesUtil::UsageCallback done, | |
dominickn
2017/04/10 05:06:12
Nit: call this "callback" or "finish_callback". "d
dullweber
2017/04/10 13:29:38
Done.
| |
368 std::vector<ImportantDomainInfo> sites, | |
369 storage::QuotaManager* quota_manager, | |
370 content::DOMStorageContext* dom_storage_context) | |
371 : done_(done), | |
372 sites_(std::move(sites)), | |
373 quota_manager_(quota_manager), | |
374 dom_storage_context_(dom_storage_context), | |
375 tasks_(-1) { | |
376 for (auto& site : sites_) { | |
dominickn
2017/04/10 05:06:12
It's not clear what the type is here, so I'd prefe
dullweber
2017/04/10 13:29:38
Done.
| |
377 site.usage = 0; | |
378 site_map_[site.registerable_domain] = &site; | |
379 } | |
380 } | |
381 | |
382 void RunAndDestroySelf() { | |
383 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
384 BrowserThread::PostTask( | |
385 BrowserThread::IO, FROM_HERE, | |
386 base::Bind(&UsageReceiver::RunOnIOThread, base::Unretained(this))); | |
387 } | |
388 | |
389 private: | |
390 void RunOnIOThread() { | |
391 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
392 tasks_ = 1; // Task for this method | |
393 tasks_ += 1; | |
394 quota_manager_->GetUsageInfo( | |
395 base::Bind(&UsageReceiver::ReceiveQuotaUsage, base::Unretained(this))); | |
396 | |
397 tasks_ += 1; | |
398 dom_storage_context_->GetLocalStorageUsage(base::Bind( | |
dominickn
2017/04/10 05:06:12
I can't find any other place in the codebase which
dullweber
2017/04/10 13:29:39
Yes it looks like localstorage should be queried f
| |
399 &UsageReceiver::ReceiveLocalStorageUsage, base::Unretained(this))); | |
400 Done(); | |
401 } | |
402 | |
403 void ReceiveQuotaUsage(const std::vector<storage::UsageInfo>& usage_infos) { | |
404 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
405 for (const auto& info : usage_infos) { | |
dominickn
2017/04/10 05:06:12
Nit: no braces for 1 line conditional.
dullweber
2017/04/10 13:29:38
Done.
| |
406 CountUsage(GetRegisterableDomainOrIPFromHost(info.host), info.usage); | |
407 } | |
408 Done(); | |
409 } | |
410 | |
411 void ReceiveLocalStorageUsage( | |
412 const std::vector<content::LocalStorageUsageInfo>& storage_infos) { | |
413 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
414 for (const auto& info : storage_infos) { | |
dominickn
2017/04/10 05:06:12
Nit: no braces for 1 line conditional.
dullweber
2017/04/10 13:29:38
Done.
| |
415 CountUsage(GetRegisterableDomainOrIP(info.origin), info.data_size); | |
416 } | |
417 Done(); | |
418 } | |
419 | |
420 // Look up the corresponding ImportantDomainInfo for |url| and increase its | |
421 // usage by |size|. | |
422 void CountUsage(const std::string& domain, int64_t size) { | |
dominickn
2017/04/10 05:06:12
Nit: This is more accurately named "IncrementUsage
dullweber
2017/04/10 13:29:39
Done.
| |
423 auto it = site_map_.find(domain); | |
dominickn
2017/04/10 05:06:11
It looks to me like the only reason for site_map_
dullweber
2017/04/10 13:29:38
That sounds like a good idea, thanks!
| |
424 if (it != site_map_.end()) { | |
dominickn
2017/04/10 05:06:12
Nit: no braces for a 1 line conditional.
dullweber
2017/04/10 13:29:39
Done.
| |
425 it->second->usage += size; | |
426 } | |
427 } | |
428 | |
429 void Done() { | |
430 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
431 DCHECK(tasks_ > 0); | |
dominickn
2017/04/10 05:06:12
DCHECK_GE(tasks_, 0);
dullweber
2017/04/10 13:29:39
Done.
| |
432 if (--tasks_ == 0) { | |
433 BrowserThread::PostTask( | |
434 BrowserThread::UI, FROM_HERE, | |
435 base::Bind(&UsageReceiver::Finish, base::Unretained(this))); | |
436 } | |
437 } | |
438 | |
439 void Finish() { | |
440 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
441 done_.Run(std::move(sites_)); | |
442 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
443 } | |
444 | |
445 ImportantSitesUtil::UsageCallback done_; | |
446 std::vector<ImportantDomainInfo> sites_; | |
447 ImportantSiteMap site_map_; | |
448 storage::QuotaManager* quota_manager_; | |
449 content::DOMStorageContext* dom_storage_context_; | |
450 int tasks_; | |
451 }; | |
452 | |
342 } // namespace | 453 } // namespace |
343 | 454 |
344 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { | 455 bool ImportantSitesUtil::IsDialogDisabled(Profile* profile) { |
345 PrefService* service = profile->GetPrefs(); | 456 PrefService* service = profile->GetPrefs(); |
346 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); | 457 DictionaryPrefUpdate update(service, prefs::kImportantSitesDialogHistory); |
347 | 458 |
348 return ShouldSuppressItem(update.Get()); | 459 return ShouldSuppressItem(update.Get()); |
349 } | 460 } |
350 | 461 |
351 void ImportantSitesUtil::RegisterProfilePrefs( | 462 void ImportantSitesUtil::RegisterProfilePrefs( |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
393 final_list.push_back(domain_info.second); | 504 final_list.push_back(domain_info.second); |
394 RECORD_UMA_FOR_IMPORTANT_REASON( | 505 RECORD_UMA_FOR_IMPORTANT_REASON( |
395 "Storage.ImportantSites.GeneratedReason", | 506 "Storage.ImportantSites.GeneratedReason", |
396 "Storage.ImportantSites.GeneratedReasonCount", | 507 "Storage.ImportantSites.GeneratedReasonCount", |
397 domain_info.second.reason_bitfield); | 508 domain_info.second.reason_bitfield); |
398 } | 509 } |
399 | 510 |
400 return final_list; | 511 return final_list; |
401 } | 512 } |
402 | 513 |
514 void ImportantSitesUtil::PopulateUsage(storage::QuotaManager* quota_manager, | |
515 content::DOMStorageContext* dom_storage, | |
516 std::vector<ImportantDomainInfo> sites, | |
517 UsageCallback callback) { | |
518 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
519 UsageReceiver* usage_receiver = | |
520 new UsageReceiver(callback, std::move(sites), quota_manager, dom_storage); | |
521 usage_receiver->RunAndDestroySelf(); | |
522 } | |
523 | |
403 void ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( | 524 void ImportantSitesUtil::RecordBlacklistedAndIgnoredImportantSites( |
404 Profile* profile, | 525 Profile* profile, |
405 const std::vector<std::string>& blacklisted_sites, | 526 const std::vector<std::string>& blacklisted_sites, |
406 const std::vector<int32_t>& blacklisted_sites_reason_bitfield, | 527 const std::vector<int32_t>& blacklisted_sites_reason_bitfield, |
407 const std::vector<std::string>& ignored_sites, | 528 const std::vector<std::string>& ignored_sites, |
408 const std::vector<int32_t>& ignored_sites_reason_bitfield) { | 529 const std::vector<int32_t>& ignored_sites_reason_bitfield) { |
409 // First, record the metrics for blacklisted and ignored sites. | 530 // First, record the metrics for blacklisted and ignored sites. |
410 for (int32_t reason_bitfield : blacklisted_sites_reason_bitfield) { | 531 for (int32_t reason_bitfield : blacklisted_sites_reason_bitfield) { |
411 RECORD_UMA_FOR_IMPORTANT_REASON( | 532 RECORD_UMA_FOR_IMPORTANT_REASON( |
412 "Storage.ImportantSites.CBDChosenReason", | 533 "Storage.ImportantSites.CBDChosenReason", |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 const GURL& origin) { | 593 const GURL& origin) { |
473 SiteEngagementScore::SetParamValuesForTesting(); | 594 SiteEngagementScore::SetParamValuesForTesting(); |
474 // First get data from site engagement. | 595 // First get data from site engagement. |
475 SiteEngagementService* site_engagement_service = | 596 SiteEngagementService* site_engagement_service = |
476 SiteEngagementService::Get(profile); | 597 SiteEngagementService::Get(profile); |
477 site_engagement_service->ResetBaseScoreForURL( | 598 site_engagement_service->ResetBaseScoreForURL( |
478 origin, SiteEngagementScore::GetMediumEngagementBoundary()); | 599 origin, SiteEngagementScore::GetMediumEngagementBoundary()); |
479 DCHECK(site_engagement_service->IsEngagementAtLeast( | 600 DCHECK(site_engagement_service->IsEngagementAtLeast( |
480 origin, blink::mojom::EngagementLevel::MEDIUM)); | 601 origin, blink::mojom::EngagementLevel::MEDIUM)); |
481 } | 602 } |
OLD | NEW |