Chromium Code Reviews| OLD | NEW |
|---|---|
| (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, callback)) | |
| 23 ->RunAndDestroySelf(); | |
| 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_(callback), | |
| 32 sites_(std::move(sites)), | |
| 33 quota_manager_(quota_manager), | |
| 34 dom_storage_context_(dom_storage_context), | |
| 35 tasks_(-1) { | |
| 36 for (ImportantDomainInfo& site : sites_) | |
| 37 site.usage = 0; | |
| 38 } | |
| 39 | |
| 40 ImportantSitesUsageCounter::~ImportantSitesUsageCounter() {} | |
| 41 | |
| 42 void ImportantSitesUsageCounter::RunAndDestroySelf() { | |
|
Bernhard Bauer
2017/05/04 14:16:44
This method doesn't immediately destroy the object
dullweber
2017/05/05 08:35:27
I changed it to RunAndDestroySelfWhenFinished()
Bernhard Bauer
2017/05/05 09:36:04
FWIW, I would be fine with just Run() too (and a c
| |
| 43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 44 tasks_ = 1; // Task for this method | |
| 45 tasks_ += 1; | |
| 46 content::BrowserThread::PostTask( | |
| 47 content::BrowserThread::IO, FROM_HERE, | |
| 48 base::Bind(&ImportantSitesUsageCounter::GetQuotaUsageOnIOThread, | |
|
Bernhard Bauer
2017/05/04 14:16:44
Nit: base::BindOnce() where possible.
dullweber
2017/05/05 08:35:27
I changed to BindOnce where it was accepted
| |
| 49 base::Unretained(this))); | |
| 50 tasks_ += 1; | |
| 51 dom_storage_context_->GetLocalStorageUsage( | |
| 52 base::Bind(&ImportantSitesUsageCounter::ReceiveLocalStorageUsage, | |
| 53 base::Unretained(this))); | |
| 54 Done(); | |
|
Bernhard Bauer
2017/05/04 14:16:44
You can set |tasks_| to 0 and leave out this call.
dullweber
2017/05/05 08:35:27
Ok, I removed it. Just to confirm: I can only do t
Bernhard Bauer
2017/05/05 09:36:04
Yes, otherwise it would depend on whether or not G
| |
| 55 } | |
| 56 | |
| 57 void ImportantSitesUsageCounter::GetQuotaUsageOnIOThread() { | |
| 58 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 59 quota_manager_->GetUsageInfo( | |
| 60 base::Bind(&ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread, | |
| 61 base::Unretained(this))); | |
| 62 } | |
| 63 | |
| 64 void ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread( | |
| 65 const std::vector<storage::UsageInfo>& usage_infos) { | |
| 66 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 67 content::BrowserThread::PostTask( | |
| 68 content::BrowserThread::UI, FROM_HERE, | |
| 69 base::Bind(&ImportantSitesUsageCounter::ReceiveQuotaUsage, | |
| 70 base::Unretained(this), usage_infos)); | |
| 71 } | |
| 72 | |
| 73 void ImportantSitesUsageCounter::ReceiveQuotaUsage( | |
| 74 const std::vector<storage::UsageInfo>& usage_infos) { | |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 76 for (const storage::UsageInfo& info : usage_infos) { | |
| 77 IncrementUsage( | |
| 78 ImportantSitesUtil::GetRegisterableDomainOrIPFromHost(info.host), | |
| 79 info.usage); | |
| 80 } | |
| 81 Done(); | |
| 82 } | |
| 83 | |
| 84 void ImportantSitesUsageCounter::ReceiveLocalStorageUsage( | |
| 85 const std::vector<content::LocalStorageUsageInfo>& storage_infos) { | |
| 86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 87 for (const content::LocalStorageUsageInfo& info : storage_infos) { | |
| 88 IncrementUsage(ImportantSitesUtil::GetRegisterableDomainOrIP(info.origin), | |
| 89 info.data_size); | |
| 90 } | |
| 91 Done(); | |
| 92 } | |
| 93 | |
| 94 // Look up the corresponding ImportantDomainInfo for |url| and increase its | |
| 95 // usage by |size|. | |
| 96 void ImportantSitesUsageCounter::IncrementUsage(const std::string& domain, | |
| 97 int64_t size) { | |
| 98 // Use a linear search over sites_ because it only has up to 10 entries. | |
| 99 auto it = std::find_if(sites_.begin(), sites_.end(), | |
| 100 [domain](ImportantDomainInfo& info) { | |
| 101 return info.registerable_domain == domain; | |
| 102 }); | |
| 103 if (it != sites_.end()) | |
| 104 it->usage += size; | |
| 105 } | |
| 106 | |
| 107 void ImportantSitesUsageCounter::Done() { | |
| 108 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 109 DCHECK_GT(tasks_, 0); | |
| 110 if (--tasks_ == 0) { | |
| 111 callback_.Run(std::move(sites_)); | |
| 112 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
|
Bernhard Bauer
2017/05/04 14:16:44
I would just directly delete |this| here. Given th
dullweber
2017/05/05 08:35:27
Done.
| |
| 113 } | |
| 114 } | |
| OLD | NEW |