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

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

Issue 2752263003: Count site data size for important sites (Closed)
Patch Set: rebase and remove deb file 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
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/engagement/important_sites_usage_counter.h"
6
7 #include <utility>
8
9 #include "base/threading/thread_task_runner_handle.h"
10 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/dom_storage_context.h"
12
13 using ImportantDomainInfo = ImportantSitesUtil::ImportantDomainInfo;
14
15 // static
16 void ImportantSitesUsageCounter::GetUsage(
17 std::vector<ImportantSitesUtil::ImportantDomainInfo> sites,
18 storage::QuotaManager* quota_manager,
19 content::DOMStorageContext* dom_storage_context,
20 UsageCallback callback) {
21 (new ImportantSitesUsageCounter(std::move(sites), quota_manager,
22 dom_storage_context, std::move(callback)))
23 ->RunAndDestroySelfWhenFinished();
24 }
25
26 ImportantSitesUsageCounter::ImportantSitesUsageCounter(
27 std::vector<ImportantDomainInfo> sites,
28 storage::QuotaManager* quota_manager,
29 content::DOMStorageContext* dom_storage_context,
30 UsageCallback callback)
31 : callback_(std::move(callback)),
32 sites_(std::move(sites)),
33 quota_manager_(quota_manager),
34 dom_storage_context_(dom_storage_context),
35 tasks_(0) {
36 for (ImportantDomainInfo& site : sites_)
37 site.usage = 0;
38 }
39
40 ImportantSitesUsageCounter::~ImportantSitesUsageCounter() {}
41
42 void ImportantSitesUsageCounter::RunAndDestroySelfWhenFinished() {
43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
44 tasks_ += 1;
45 content::BrowserThread::PostTask(
46 content::BrowserThread::IO, FROM_HERE,
47 base::BindOnce(&ImportantSitesUsageCounter::GetQuotaUsageOnIOThread,
48 base::Unretained(this)));
49 tasks_ += 1;
50 dom_storage_context_->GetLocalStorageUsage(
51 base::Bind(&ImportantSitesUsageCounter::ReceiveLocalStorageUsage,
52 base::Unretained(this)));
53 }
54
55 void ImportantSitesUsageCounter::GetQuotaUsageOnIOThread() {
56 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
57 quota_manager_->GetUsageInfo(
58 base::Bind(&ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread,
59 base::Unretained(this)));
60 }
61
62 void ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread(
63 const std::vector<storage::UsageInfo>& usage_infos) {
64 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
65 content::BrowserThread::PostTask(
66 content::BrowserThread::UI, FROM_HERE,
67 base::BindOnce(&ImportantSitesUsageCounter::ReceiveQuotaUsage,
68 base::Unretained(this), usage_infos));
69 }
70
71 void ImportantSitesUsageCounter::ReceiveQuotaUsage(
72 const std::vector<storage::UsageInfo>& usage_infos) {
73 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
74 for (const storage::UsageInfo& info : usage_infos) {
75 IncrementUsage(
76 ImportantSitesUtil::GetRegisterableDomainOrIPFromHost(info.host),
77 info.usage);
78 }
79 Done();
80 }
81
82 void ImportantSitesUsageCounter::ReceiveLocalStorageUsage(
83 const std::vector<content::LocalStorageUsageInfo>& storage_infos) {
84 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
85 for (const content::LocalStorageUsageInfo& info : storage_infos) {
86 IncrementUsage(ImportantSitesUtil::GetRegisterableDomainOrIP(info.origin),
87 info.data_size);
88 }
89 Done();
90 }
91
92 // Look up the corresponding ImportantDomainInfo for |url| and increase its
93 // usage by |size|.
94 void ImportantSitesUsageCounter::IncrementUsage(const std::string& domain,
95 int64_t size) {
96 // Use a linear search over sites_ because it only has up to 10 entries.
97 auto it = std::find_if(sites_.begin(), sites_.end(),
98 [domain](ImportantDomainInfo& info) {
99 return info.registerable_domain == domain;
100 });
101 if (it != sites_.end())
102 it->usage += size;
103 }
104
105 void ImportantSitesUsageCounter::Done() {
106 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
107 DCHECK_GT(tasks_, 0);
108 if (--tasks_ == 0) {
109 std::move(callback_).Run(std::move(sites_));
110 delete this;
111 }
112 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698