 Chromium Code Reviews
 Chromium Code Reviews Issue 2752263003:
  Count site data size for important sites  (Closed)
    
  
    Issue 2752263003:
  Count site data size for important sites  (Closed) 
  | 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 | |
| 
dominickn
2017/04/11 00:18:20
Nit: #include <utility> for std::move
 
dullweber
2017/04/11 09:01:34
Done.
 | |
| 7 #include "base/bind_helpers.h" | |
| 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 ImportantSitesUsageCounter::ImportantSitesUsageCounter( | |
| 15 std::vector<ImportantDomainInfo> sites, | |
| 16 storage::QuotaManager* quota_manager, | |
| 17 content::DOMStorageContext* dom_storage_context, | |
| 18 UsageCallback callback) | |
| 19 : callback_(callback), | |
| 20 sites_(std::move(sites)), | |
| 21 quota_manager_(quota_manager), | |
| 22 dom_storage_context_(dom_storage_context), | |
| 23 tasks_(-1) { | |
| 24 for (ImportantDomainInfo& site : sites_) { | |
| 
dominickn
2017/04/11 00:18:20
Nit: no braces for 1 line conditional
 
dullweber
2017/04/11 09:01:34
Done.
 | |
| 25 site.usage = 0; | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 ImportantSitesUsageCounter::~ImportantSitesUsageCounter() {} | |
| 30 | |
| 31 void ImportantSitesUsageCounter::RunAndDestroySelf() { | |
| 32 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 33 tasks_ = 1; // Task for this method | |
| 34 tasks_ += 1; | |
| 35 content::BrowserThread::PostTask( | |
| 36 content::BrowserThread::IO, FROM_HERE, | |
| 37 base::Bind(&ImportantSitesUsageCounter::GetQuotaUsageOnIOThread, | |
| 38 base::Unretained(this))); | |
| 39 tasks_ += 1; | |
| 40 dom_storage_context_->GetLocalStorageUsage( | |
| 41 base::Bind(&ImportantSitesUsageCounter::ReceiveLocalStorageUsage, | |
| 42 base::Unretained(this))); | |
| 43 Done(); | |
| 44 } | |
| 45 | |
| 46 void ImportantSitesUsageCounter::GetQuotaUsageOnIOThread() { | |
| 47 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 48 quota_manager_->GetUsageInfo( | |
| 49 base::Bind(&ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread, | |
| 50 base::Unretained(this))); | |
| 51 } | |
| 52 | |
| 53 void ImportantSitesUsageCounter::ReceiveQuotaUsageOnIOThread( | |
| 54 const std::vector<storage::UsageInfo>& usage_infos) { | |
| 55 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 56 content::BrowserThread::PostTask( | |
| 
dominickn
2017/04/11 00:18:20
I'm a bit concerned about the thread lifetime here
 
dullweber
2017/04/11 09:01:34
Thanks for pointing that out. I just read through
 
dominickn
2017/04/11 10:03:52
I don't think that doc applies here because the us
 
dullweber
2017/04/11 11:08:03
I agree that the usage_infos parameter in ReceiveQ
 | |
| 57 content::BrowserThread::UI, FROM_HERE, | |
| 58 base::Bind(&ImportantSitesUsageCounter::ReceiveQuotaUsage, | |
| 59 base::Unretained(this), usage_infos)); | |
| 60 } | |
| 61 | |
| 62 void ImportantSitesUsageCounter::ReceiveQuotaUsage( | |
| 63 const std::vector<storage::UsageInfo>& usage_infos) { | |
| 64 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 65 for (const storage::UsageInfo& info : usage_infos) | |
| 
dominickn
2017/04/11 00:18:20
Nit: this loop needs braces since the body is more
 
dullweber
2017/04/11 09:01:34
Done.
 | |
| 66 IncrementUsage( | |
| 67 ImportantSitesUtil::GetRegisterableDomainOrIPFromHost(info.host), | |
| 68 info.usage); | |
| 69 Done(); | |
| 70 } | |
| 71 | |
| 72 void ImportantSitesUsageCounter::ReceiveLocalStorageUsage( | |
| 73 const std::vector<content::LocalStorageUsageInfo>& storage_infos) { | |
| 74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 75 for (const content::LocalStorageUsageInfo& info : storage_infos) | |
| 
dominickn
2017/04/11 00:18:20
Nit: this one needs braces because the body is 2 l
 
dullweber
2017/04/11 09:01:34
Done.
 | |
| 76 IncrementUsage(ImportantSitesUtil::GetRegisterableDomainOrIP(info.origin), | |
| 77 info.data_size); | |
| 78 Done(); | |
| 79 } | |
| 80 | |
| 81 // Look up the corresponding ImportantDomainInfo for |url| and increase its | |
| 82 // usage by |size|. | |
| 83 void ImportantSitesUsageCounter::IncrementUsage(const std::string& domain, | |
| 84 int64_t size) { | |
| 85 // Use a linear search over sites_ because it only has up to 10 entries. | |
| 86 auto it = std::find_if(sites_.begin(), sites_.end(), | |
| 87 [domain](ImportantDomainInfo& info) { | |
| 88 return info.registerable_domain == domain; | |
| 89 }); | |
| 90 if (it != sites_.end()) | |
| 91 it->usage += size; | |
| 92 } | |
| 93 | |
| 94 void ImportantSitesUsageCounter::Done() { | |
| 95 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 96 DCHECK_GT(tasks_, 0); | |
| 97 if (--tasks_ == 0) { | |
| 98 callback_.Run(std::move(sites_)); | |
| 99 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this); | |
| 100 } | |
| 101 } | |
| OLD | NEW |