| 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, given that |
| 233 return resource.weight_ratio() * host_visits; | 232 // resource_weight_ratio is the probability of a request given a visit to the |
| 233 // host, and host_visits is the number of visits to the host in 30 days. |
| 234 double NaiveResourceWeight(double resource_weight_ratio, int64_t host_visits) { |
| 235 return resource_weight_ratio * host_visits; |
| 236 } |
| 237 |
| 238 // Models the probability of at least one request for the resource, given that |
| 239 // resource_weight_ratio is the probability of a request given a visit to the |
| 240 // host, and host_visits is the number of visits to the host in 30 days. |
| 241 double GeometricResourceWeight(double resource_weight_ratio, |
| 242 int64_t host_visits) { |
| 243 return 1 - pow(1 - resource_weight_ratio, host_visits); |
| 234 } | 244 } |
| 235 | 245 |
| 236 } // namespace | 246 } // namespace |
| 237 | 247 |
| 248 // Returns the weight of the resource. When global ranking is enabled, the |
| 249 // fetches are sorted by descending weight. Parameters: |
| 250 // function: Which combination function to use. |
| 251 // resource_weight_ratio: The weight_ratio of the resource. |
| 252 // host_visits: The count of visits to the given host in the past 30 days. |
| 253 double ResourceWeight( |
| 254 PrecacheConfigurationSettings::ResourceWeightFunction function, |
| 255 double resource_weight_ratio, |
| 256 int64_t host_visits) { |
| 257 switch (function) { |
| 258 case PrecacheConfigurationSettings::FUNCTION_NAIVE: |
| 259 return NaiveResourceWeight(resource_weight_ratio, host_visits); |
| 260 case PrecacheConfigurationSettings::FUNCTION_GEOMETRIC: |
| 261 return GeometricResourceWeight(resource_weight_ratio, host_visits); |
| 262 default: |
| 263 DLOG(FATAL) << "Unknown function " << function; |
| 264 return 0; |
| 265 } |
| 266 } |
| 267 |
| 238 PrecacheFetcher::Fetcher::Fetcher( | 268 PrecacheFetcher::Fetcher::Fetcher( |
| 239 net::URLRequestContextGetter* request_context, | 269 net::URLRequestContextGetter* request_context, |
| 240 const GURL& url, | 270 const GURL& url, |
| 241 const std::string& referrer, | 271 const std::string& referrer, |
| 242 const base::Callback<void(const Fetcher&)>& callback, | 272 const base::Callback<void(const Fetcher&)>& callback, |
| 243 bool is_resource_request, | 273 bool is_resource_request, |
| 244 size_t max_bytes, | 274 size_t max_bytes, |
| 245 bool revalidation_only) | 275 bool revalidation_only) |
| 246 : request_context_(request_context), | 276 : request_context_(request_context), |
| 247 url_(url), | 277 url_(url), |
| (...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 716 const int32_t len = | 746 const int32_t len = |
| 717 std::min(manifest.resource_size(), | 747 std::min(manifest.resource_size(), |
| 718 unfinished_work_->config_settings().top_resources_count()); | 748 unfinished_work_->config_settings().top_resources_count()); |
| 719 const uint64_t resource_bitset = | 749 const uint64_t resource_bitset = |
| 720 GetResourceBitset(manifest, experiment_id_); | 750 GetResourceBitset(manifest, experiment_id_); |
| 721 for (int i = 0; i < len; ++i) { | 751 for (int i = 0; i < len; ++i) { |
| 722 if (((0x1ULL << i) & resource_bitset) && | 752 if (((0x1ULL << i) & resource_bitset) && |
| 723 manifest.resource(i).has_url()) { | 753 manifest.resource(i).has_url()) { |
| 724 GURL url(manifest.resource(i).url()); | 754 GURL url(manifest.resource(i).url()); |
| 725 if (url.is_valid()) { | 755 if (url.is_valid()) { |
| 726 double weight = ResourceWeight(manifest.resource(i), host_visits); | 756 double weight = ResourceWeight( |
| 757 unfinished_work_->config_settings().resource_weight_function(), |
| 758 manifest.resource(i).weight_ratio(), host_visits); |
| 727 if (weight >= unfinished_work_->config_settings().min_weight()) | 759 if (weight >= unfinished_work_->config_settings().min_weight()) |
| 728 resources_to_rank_.emplace_back(url, source.referrer(), weight); | 760 resources_to_rank_.emplace_back(url, source.referrer(), weight); |
| 729 } | 761 } |
| 730 } | 762 } |
| 731 } | 763 } |
| 732 db_task_runner_->PostTask( | 764 db_task_runner_->PostTask( |
| 733 FROM_HERE, base::Bind(&PrecacheDatabase::UpdatePrecacheReferrerHost, | 765 FROM_HERE, base::Bind(&PrecacheDatabase::UpdatePrecacheReferrerHost, |
| 734 precache_database_, source.referrer(), | 766 precache_database_, source.referrer(), |
| 735 manifest.id().id(), base::Time::Now())); | 767 manifest.id().id(), base::Time::Now())); |
| 736 } | 768 } |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 811 remaining = 0; | 843 remaining = 0; |
| 812 quota_.set_remaining( | 844 quota_.set_remaining( |
| 813 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); | 845 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); |
| 814 db_task_runner_->PostTask( | 846 db_task_runner_->PostTask( |
| 815 FROM_HERE, | 847 FROM_HERE, |
| 816 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); | 848 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); |
| 817 } | 849 } |
| 818 } | 850 } |
| 819 | 851 |
| 820 } // namespace precache | 852 } // namespace precache |
| OLD | NEW |