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

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

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

Powered by Google App Engine
This is Rietveld 408576698