Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/precache/core/precache_fetcher.h" | 5 #include "components/precache/core/precache_fetcher.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <limits> | 8 #include <limits> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <utility> | 10 #include <utility> |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 21 #include "base/logging.h" | 21 #include "base/logging.h" |
| 22 #include "base/memory/ptr_util.h" | 22 #include "base/memory/ptr_util.h" |
| 23 #include "base/memory/ref_counted.h" | 23 #include "base/memory/ref_counted.h" |
| 24 #include "base/metrics/histogram_macros.h" | 24 #include "base/metrics/histogram_macros.h" |
| 25 #include "base/sha1.h" | 25 #include "base/sha1.h" |
| 26 #include "base/strings/string_piece.h" | 26 #include "base/strings/string_piece.h" |
| 27 #include "base/task_runner_util.h" | 27 #include "base/task_runner_util.h" |
| 28 #include "components/data_use_measurement/core/data_use_user_data.h" | 28 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 29 #include "components/precache/core/precache_database.h" | 29 #include "components/precache/core/precache_database.h" |
| 30 #include "components/precache/core/precache_switches.h" | 30 #include "components/precache/core/precache_switches.h" |
| 31 #include "components/precache/core/proto/precache.pb.h" | |
| 32 #include "components/precache/core/proto/quota.pb.h" | 31 #include "components/precache/core/proto/quota.pb.h" |
| 33 #include "components/precache/core/proto/unfinished_work.pb.h" | 32 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 34 #include "net/base/completion_callback.h" | 33 #include "net/base/completion_callback.h" |
| 35 #include "net/base/escape.h" | 34 #include "net/base/escape.h" |
| 36 #include "net/base/io_buffer.h" | 35 #include "net/base/io_buffer.h" |
| 37 #include "net/base/load_flags.h" | 36 #include "net/base/load_flags.h" |
| 38 #include "net/base/net_errors.h" | 37 #include "net/base/net_errors.h" |
| 39 #include "net/base/url_util.h" | 38 #include "net/base/url_util.h" |
| 40 #include "net/http/http_response_headers.h" | 39 #include "net/http/http_response_headers.h" |
| 41 #include "net/url_request/url_fetcher_response_writer.h" | 40 #include "net/url_request/url_fetcher_response_writer.h" |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 222 | 221 |
| 223 // Returns true if the |quota| time has expired. | 222 // Returns true if the |quota| time has expired. |
| 224 bool IsQuotaTimeExpired(const PrecacheQuota& quota, | 223 bool IsQuotaTimeExpired(const PrecacheQuota& quota, |
| 225 const base::Time& time_now) { | 224 const base::Time& time_now) { |
| 226 // Quota expires one day after the start time. | 225 // Quota expires one day after the start time. |
| 227 base::Time start_time = base::Time::FromInternalValue(quota.start_time()); | 226 base::Time start_time = base::Time::FromInternalValue(quota.start_time()); |
| 228 return start_time > time_now || | 227 return start_time > time_now || |
| 229 start_time + base::TimeDelta::FromDays(1) < time_now; | 228 start_time + base::TimeDelta::FromDays(1) < time_now; |
| 230 } | 229 } |
| 231 | 230 |
| 232 double ResourceWeight(const PrecacheResource& resource, int64_t host_visits) { | 231 // Models the expected number of requests for the resource. |
|
bengr
2017/01/12 18:48:01
Please describe the parameters here and at lines 2
twifkak
2017/01/12 20:53:39
Done.
| |
| 233 return resource.weight_ratio() * host_visits; | 232 double NaiveResourceWeight(double resource_weight_ratio, int64_t host_visits) { |
| 233 return resource_weight_ratio * host_visits; | |
| 234 } | |
| 235 | |
| 236 // Models the probability of at least one request for the resource. | |
| 237 double GeometricResourceWeight(double resource_weight_ratio, | |
| 238 int64_t host_visits) { | |
| 239 return 1 - pow(1 - resource_weight_ratio, host_visits); | |
| 234 } | 240 } |
| 235 | 241 |
| 236 } // namespace | 242 } // namespace |
| 237 | 243 |
| 244 double ResourceWeight( | |
| 245 PrecacheConfigurationSettings::ResourceWeightFunction function, | |
| 246 double resource_weight_ratio, | |
| 247 int64_t host_visits) { | |
| 248 switch (function) { | |
| 249 case PrecacheConfigurationSettings::FUNCTION_NAIVE: | |
| 250 return NaiveResourceWeight(resource_weight_ratio, host_visits); | |
| 251 case PrecacheConfigurationSettings::FUNCTION_GEOMETRIC: | |
| 252 return GeometricResourceWeight(resource_weight_ratio, host_visits); | |
| 253 } | |
| 254 } | |
| 255 | |
| 238 PrecacheFetcher::Fetcher::Fetcher( | 256 PrecacheFetcher::Fetcher::Fetcher( |
| 239 net::URLRequestContextGetter* request_context, | 257 net::URLRequestContextGetter* request_context, |
| 240 const GURL& url, | 258 const GURL& url, |
| 241 const std::string& referrer, | 259 const std::string& referrer, |
| 242 const base::Callback<void(const Fetcher&)>& callback, | 260 const base::Callback<void(const Fetcher&)>& callback, |
| 243 bool is_resource_request, | 261 bool is_resource_request, |
| 244 size_t max_bytes) | 262 size_t max_bytes) |
| 245 : request_context_(request_context), | 263 : request_context_(request_context), |
| 246 url_(url), | 264 url_(url), |
| 247 referrer_(referrer), | 265 referrer_(referrer), |
| (...skipping 462 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 710 const int32_t len = | 728 const int32_t len = |
| 711 std::min(manifest.resource_size(), | 729 std::min(manifest.resource_size(), |
| 712 unfinished_work_->config_settings().top_resources_count()); | 730 unfinished_work_->config_settings().top_resources_count()); |
| 713 const uint64_t resource_bitset = | 731 const uint64_t resource_bitset = |
| 714 GetResourceBitset(manifest, experiment_id_); | 732 GetResourceBitset(manifest, experiment_id_); |
| 715 for (int i = 0; i < len; ++i) { | 733 for (int i = 0; i < len; ++i) { |
| 716 if (((0x1ULL << i) & resource_bitset) && | 734 if (((0x1ULL << i) & resource_bitset) && |
| 717 manifest.resource(i).has_url()) { | 735 manifest.resource(i).has_url()) { |
| 718 GURL url(manifest.resource(i).url()); | 736 GURL url(manifest.resource(i).url()); |
| 719 if (url.is_valid()) { | 737 if (url.is_valid()) { |
| 720 double weight = ResourceWeight(manifest.resource(i), host_visits); | 738 double weight = ResourceWeight( |
| 739 unfinished_work_->config_settings().resource_weight_function(), | |
| 740 manifest.resource(i).weight_ratio(), host_visits); | |
| 721 if (weight >= unfinished_work_->config_settings().min_weight()) | 741 if (weight >= unfinished_work_->config_settings().min_weight()) |
| 722 resources_to_rank_.emplace_back(url, source.referrer(), weight); | 742 resources_to_rank_.emplace_back(url, source.referrer(), weight); |
| 723 } | 743 } |
| 724 } | 744 } |
| 725 } | 745 } |
| 726 db_task_runner_->PostTask( | 746 db_task_runner_->PostTask( |
| 727 FROM_HERE, base::Bind(&PrecacheDatabase::UpdatePrecacheReferrerHost, | 747 FROM_HERE, base::Bind(&PrecacheDatabase::UpdatePrecacheReferrerHost, |
| 728 precache_database_, source.referrer(), | 748 precache_database_, source.referrer(), |
| 729 manifest.id().id(), base::Time::Now())); | 749 manifest.id().id(), base::Time::Now())); |
| 730 } | 750 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 805 remaining = 0; | 825 remaining = 0; |
| 806 quota_.set_remaining( | 826 quota_.set_remaining( |
| 807 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); | 827 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); |
| 808 db_task_runner_->PostTask( | 828 db_task_runner_->PostTask( |
| 809 FROM_HERE, | 829 FROM_HERE, |
| 810 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); | 830 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); |
| 811 } | 831 } |
| 812 } | 832 } |
| 813 | 833 |
| 814 } // namespace precache | 834 } // namespace precache |
| OLD | NEW |