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

Unified Diff: components/precache/core/precache_fetcher.cc

Issue 2335913002: Add daily quota for precache (Closed)
Patch Set: Addressed sclittle comments Created 4 years, 3 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 side-by-side diff with in-line comments
Download patch
Index: components/precache/core/precache_fetcher.cc
diff --git a/components/precache/core/precache_fetcher.cc b/components/precache/core/precache_fetcher.cc
index 90d05bb776b6eb6b841c69369f29a3acc5846c87..e8129d47c39ff15c8c48e3eb84f93ac7ff379f31 100644
--- a/components/precache/core/precache_fetcher.cc
+++ b/components/precache/core/precache_fetcher.cc
@@ -27,6 +27,7 @@
#include "components/precache/core/precache_database.h"
#include "components/precache/core/precache_switches.h"
#include "components/precache/core/proto/precache.pb.h"
+#include "components/precache/core/proto/quota.pb.h"
#include "components/precache/core/proto/unfinished_work.pb.h"
#include "net/base/completion_callback.h"
#include "net/base/escape.h"
@@ -204,6 +205,15 @@ std::deque<ManifestHostInfo> RetrieveManifestInfo(
return hosts_info;
}
+PrecacheQuota RetrieveQuotaInfo(
+ const base::WeakPtr<PrecacheDatabase>& precache_database) {
+ PrecacheQuota quota;
+ if (precache_database) {
+ quota = precache_database->GetQuota();
+ }
+ return quota;
+}
+
} // namespace
PrecacheFetcher::Fetcher::Fetcher(
@@ -466,10 +476,11 @@ void PrecacheFetcher::StartNextResourceFetch() {
DCHECK(unfinished_work_->has_config_settings());
while (!resources_to_fetch_.empty() && pool_.IsAvailable()) {
const auto& resource = resources_to_fetch_.front();
- const size_t max_bytes =
+ const size_t max_bytes = std::min(
+ quota_.remaining(),
std::min(unfinished_work_->config_settings().max_bytes_per_resource(),
unfinished_work_->config_settings().max_bytes_total() -
- unfinished_work_->total_bytes());
+ unfinished_work_->total_bytes()));
VLOG(3) << "Fetching " << resource.first << " " << resource.second;
pool_.Add(base::MakeUnique<Fetcher>(
request_context_.get(), resource.first, resource.second,
@@ -506,9 +517,13 @@ void PrecacheFetcher::NotifyDone(
void PrecacheFetcher::StartNextFetch() {
DCHECK(unfinished_work_->has_config_settings());
- // If over the precache total size cap, then stop prefetching.
- if (unfinished_work_->total_bytes() >
- unfinished_work_->config_settings().max_bytes_total()) {
+ DCHECK(base::Time::FromInternalValue(quota_.expiry_time()) >
sclittle 2016/09/21 20:07:24 This DCHECK will not hold if the user changes thei
Raj 2016/09/22 18:34:33 Done.
+ base::Time::Now());
+
+ // If over the precache total size cap or daily quota, then stop prefetching.
+ if ((unfinished_work_->total_bytes() >
+ unfinished_work_->config_settings().max_bytes_total()) ||
+ quota_.remaining() == 0) {
size_t pending_manifests_in_pool = 0;
size_t pending_resources_in_pool = 0;
for (const auto& element_pair : pool_.elements()) {
@@ -622,6 +637,27 @@ void PrecacheFetcher::OnManifestInfoRetrieved(
}
}
unfinished_work_->set_num_manifest_urls(top_hosts_to_fetch_.size());
+
+ PostTaskAndReplyWithResult(
+ db_task_runner_.get(), FROM_HERE,
+ base::Bind(&RetrieveQuotaInfo, precache_database_),
+ base::Bind(&PrecacheFetcher::OnQuotaInfoRetrieved, AsWeakPtr()));
+}
+
+void PrecacheFetcher::OnQuotaInfoRetrieved(const PrecacheQuota& quota) {
+ quota_ = quota;
+ base::Time time_now = base::Time::Now();
+ if (base::Time::FromInternalValue(quota_.expiry_time()) <= time_now) {
sclittle 2016/09/21 20:07:24 What happens if the user changes their clock? If a
Raj 2016/09/22 18:34:33 Done. I have added start_time and expiry_hours.
+ // This is a new day. Update daily quota, that expires by end of today.
+ quota_.set_expiry_time(
+ (time_now.LocalMidnight() + base::TimeDelta::FromDays(1))
+ .ToInternalValue());
+ quota_.set_remaining(
+ unfinished_work_->config_settings().daily_quota_total());
+ db_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_));
+ }
StartNextFetch();
}
@@ -696,6 +732,17 @@ void PrecacheFetcher::UpdateStats(int64_t response_bytes,
unfinished_work_->total_bytes() + response_bytes);
unfinished_work_->set_network_bytes(
unfinished_work_->network_bytes() + network_response_bytes);
+
+ if (base::Time::FromInternalValue(quota_.expiry_time()) > base::Time::Now()) {
+ int64_t remaining =
+ static_cast<int64_t>(quota_.remaining()) - network_response_bytes;
+ if (remaining < 0)
+ remaining = 0;
+ quota_.set_remaining(static_cast<uint64_t>(remaining));
+ db_task_runner_->PostTask(
+ FROM_HERE,
+ base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_));
+ }
}
} // namespace precache

Powered by Google App Engine
This is Rietveld 408576698