| 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/optional.h" | 25 #include "base/optional.h" |
| 26 #include "base/sha1.h" | 26 #include "base/sha1.h" |
| 27 #include "base/strings/string_piece.h" | 27 #include "base/strings/string_piece.h" |
| 28 #include "base/task_runner_util.h" | 28 #include "base/task_runner_util.h" |
| 29 #include "components/data_use_measurement/core/data_use_user_data.h" | 29 #include "components/data_use_measurement/core/data_use_user_data.h" |
| 30 #include "components/precache/core/precache_database.h" | 30 #include "components/precache/core/precache_database.h" |
| 31 #include "components/precache/core/precache_manifest_util.h" |
| 31 #include "components/precache/core/precache_switches.h" | 32 #include "components/precache/core/precache_switches.h" |
| 32 #include "components/precache/core/proto/quota.pb.h" | 33 #include "components/precache/core/proto/quota.pb.h" |
| 33 #include "components/precache/core/proto/unfinished_work.pb.h" | 34 #include "components/precache/core/proto/unfinished_work.pb.h" |
| 34 #include "net/base/completion_callback.h" | 35 #include "net/base/completion_callback.h" |
| 35 #include "net/base/escape.h" | 36 #include "net/base/escape.h" |
| 36 #include "net/base/io_buffer.h" | 37 #include "net/base/io_buffer.h" |
| 37 #include "net/base/load_flags.h" | 38 #include "net/base/load_flags.h" |
| 38 #include "net/base/net_errors.h" | 39 #include "net/base/net_errors.h" |
| 39 #include "net/base/url_util.h" | 40 #include "net/base/url_util.h" |
| 40 #include "net/http/http_response_headers.h" | 41 #include "net/http/http_response_headers.h" |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 118 return false; | 119 return false; |
| 119 } | 120 } |
| 120 if (!message->ParseFromString(response_string)) { | 121 if (!message->ParseFromString(response_string)) { |
| 121 DLOG(WARNING) << "Unable to parse proto served from " | 122 DLOG(WARNING) << "Unable to parse proto served from " |
| 122 << source.GetOriginalURL().spec(); | 123 << source.GetOriginalURL().spec(); |
| 123 return false; | 124 return false; |
| 124 } | 125 } |
| 125 return true; | 126 return true; |
| 126 } | 127 } |
| 127 | 128 |
| 128 // Returns the resource selection bitset from the |manifest| for the given | |
| 129 // |experiment_id|. If the experiment group is not found, then this returns | |
| 130 // nullopt, in which case all resources should be selected. | |
| 131 base::Optional<std::vector<bool>> GetResourceBitset( | |
| 132 const PrecacheManifest& manifest, | |
| 133 uint32_t experiment_id) { | |
| 134 base::Optional<std::vector<bool>> ret; | |
| 135 if (manifest.has_experiments()) { | |
| 136 const auto& resource_bitset_map = | |
| 137 manifest.experiments().resources_by_experiment_group(); | |
| 138 const auto& it = resource_bitset_map.find(experiment_id); | |
| 139 if (it != resource_bitset_map.end()) { | |
| 140 if (it->second.has_bitset()) { | |
| 141 const std::string& bitset = it->second.bitset(); | |
| 142 ret.emplace(bitset.size() * 8); | |
| 143 for (size_t i = 0; i < bitset.size(); ++i) { | |
| 144 for (size_t j = 0; j < 8; ++j) { | |
| 145 if ((1 << j) & bitset[i]) | |
| 146 ret.value()[i * 8 + j] = true; | |
| 147 } | |
| 148 } | |
| 149 } else if (it->second.has_deprecated_bitset()) { | |
| 150 uint64_t bitset = it->second.deprecated_bitset(); | |
| 151 ret.emplace(64); | |
| 152 for (int i = 0; i < 64; ++i) { | |
| 153 if ((0x1ULL << i) & bitset) | |
| 154 ret.value()[i] = true; | |
| 155 } | |
| 156 } | |
| 157 } | |
| 158 } | |
| 159 // Only return one variable to ensure RVO triggers. | |
| 160 return ret; | |
| 161 } | |
| 162 | |
| 163 // URLFetcherResponseWriter that ignores the response body, in order to avoid | 129 // URLFetcherResponseWriter that ignores the response body, in order to avoid |
| 164 // the unnecessary memory usage. Use it rather than the default if you don't | 130 // the unnecessary memory usage. Use it rather than the default if you don't |
| 165 // care about parsing the response body. We use it below as a means to populate | 131 // care about parsing the response body. We use it below as a means to populate |
| 166 // the cache with requested resource URLs. | 132 // the cache with requested resource URLs. |
| 167 class URLFetcherNullWriter : public net::URLFetcherResponseWriter { | 133 class URLFetcherNullWriter : public net::URLFetcherResponseWriter { |
| 168 public: | 134 public: |
| 169 int Initialize(const net::CompletionCallback& callback) override { | 135 int Initialize(const net::CompletionCallback& callback) override { |
| 170 return net::OK; | 136 return net::OK; |
| 171 } | 137 } |
| 172 | 138 |
| (...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 997 remaining = 0; | 963 remaining = 0; |
| 998 quota_.set_remaining( | 964 quota_.set_remaining( |
| 999 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); | 965 used_bytes > quota_.remaining() ? 0U : quota_.remaining() - used_bytes); |
| 1000 db_task_runner_->PostTask( | 966 db_task_runner_->PostTask( |
| 1001 FROM_HERE, | 967 FROM_HERE, |
| 1002 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); | 968 base::Bind(&PrecacheDatabase::SaveQuota, precache_database_, quota_)); |
| 1003 } | 969 } |
| 1004 } | 970 } |
| 1005 | 971 |
| 1006 } // namespace precache | 972 } // namespace precache |
| OLD | NEW |