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

Side by Side Diff: net/http/infinite_cache.cc

Issue 10986034: Track ga.js through the cache. (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Allocate a ResourceData Created 8 years, 2 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 unified diff | Download patch
« no previous file with comments | « net/http/infinite_cache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "net/http/infinite_cache.h" 5 #include "net/http/infinite_cache.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/bind.h" 10 #include "base/bind.h"
(...skipping 27 matching lines...) Expand all
38 38
39 // Flags to use with a particular resource. 39 // Flags to use with a particular resource.
40 enum Flags { 40 enum Flags {
41 NO_CACHE = 1 << 0, 41 NO_CACHE = 1 << 0,
42 NO_STORE = 1 << 1, 42 NO_STORE = 1 << 1,
43 EXPIRED = 1 << 2, 43 EXPIRED = 1 << 2,
44 TRUNCATED = 1 << 3, 44 TRUNCATED = 1 << 3,
45 RESUMABLE = 1 << 4, 45 RESUMABLE = 1 << 4,
46 REVALIDATEABLE = 1 << 5, 46 REVALIDATEABLE = 1 << 5,
47 DOOM_METHOD = 1 << 6, 47 DOOM_METHOD = 1 << 6,
48 CACHED = 1 << 7 48 CACHED = 1 << 7,
49 GA_JS_HTTP = 1 << 8,
50 GA_JS_HTTPS = 1 << 9,
49 }; 51 };
50 52
53 const char kGaJsHttpUrl[] = "http://www.google-analytics.com/ga.js";
54 const char kGaJsHttpsUrl[] = "https://ssl.google-analytics.com/ga.js";
55
51 const int kKeySizeBytes = 20; 56 const int kKeySizeBytes = 20;
52 COMPILE_ASSERT(base::kSHA1Length == static_cast<unsigned>(kKeySizeBytes), 57 COMPILE_ASSERT(base::kSHA1Length == static_cast<unsigned>(kKeySizeBytes),
53 invalid_key_length); 58 invalid_key_length);
54 struct Key { 59 struct Key {
55 char value[kKeySizeBytes]; 60 char value[kKeySizeBytes];
56 }; 61 };
57 62
58 // The actual data that we store for every resource. 63 // The actual data that we store for every resource.
59 struct Details { 64 struct Details {
60 int32 expiration; 65 int32 expiration;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
229 struct InfiniteCacheTransaction::ResourceData { 234 struct InfiniteCacheTransaction::ResourceData {
230 ResourceData() { 235 ResourceData() {
231 memset(this, 0, sizeof(*this)); 236 memset(this, 0, sizeof(*this));
232 } 237 }
233 238
234 Key key; 239 Key key;
235 Details details; 240 Details details;
236 }; 241 };
237 242
238 InfiniteCacheTransaction::InfiniteCacheTransaction(InfiniteCache* cache) 243 InfiniteCacheTransaction::InfiniteCacheTransaction(InfiniteCache* cache)
239 : cache_(cache->AsWeakPtr()), must_doom_entry_(false) { 244 : cache_(cache->AsWeakPtr()) {
240 } 245 }
241 246
242 InfiniteCacheTransaction::~InfiniteCacheTransaction() { 247 InfiniteCacheTransaction::~InfiniteCacheTransaction() {
243 Finish(); 248 Finish();
244 } 249 }
245 250
246 void InfiniteCacheTransaction::OnRequestStart(const HttpRequestInfo* request) { 251 void InfiniteCacheTransaction::OnRequestStart(const HttpRequestInfo* request) {
247 if (!cache_) 252 if (!cache_)
248 return; 253 return;
249 254
250 std::string method = request->method; 255 std::string method = request->method;
256 resource_data_.reset(new ResourceData);
251 if (method == "POST" || method == "DELETE" || method == "PUT") { 257 if (method == "POST" || method == "DELETE" || method == "PUT") {
252 must_doom_entry_ = true; 258 resource_data_->details.flags |= DOOM_METHOD;
253 } else if (method != "GET") { 259 } else if (method != "GET") {
254 cache_.reset(); 260 cache_.reset();
255 return; 261 return;
256 } 262 }
263 const std::string cache_key = cache_->GenerateKey(request);
264 if (cache_key == kGaJsHttpUrl) {
265 resource_data_->details.flags |= GA_JS_HTTP;
266 } else if (cache_key == kGaJsHttpsUrl) {
267 resource_data_->details.flags |= GA_JS_HTTPS;
268 }
257 269
258 resource_data_.reset(new ResourceData); 270 CryptoHash(cache_key, &resource_data_->key);
259 CryptoHash(cache_->GenerateKey(request), &resource_data_->key);
260 } 271 }
261 272
262 void InfiniteCacheTransaction::OnResponseReceived( 273 void InfiniteCacheTransaction::OnResponseReceived(
263 const HttpResponseInfo* response) { 274 const HttpResponseInfo* response) {
264 if (!cache_) 275 if (!cache_)
265 return; 276 return;
266 277
267 Details& details = resource_data_->details; 278 Details& details = resource_data_->details;
268 279
280 // Store the old flag values that we want to preserve.
281 const uint32 kPreserveFlagsBitMask = (GA_JS_HTTP | GA_JS_HTTPS | DOOM_METHOD);
282 uint32 old_flag_values = details.flags & kPreserveFlagsBitMask;
283
269 details.expiration = GetExpiration(response); 284 details.expiration = GetExpiration(response);
270 details.last_access = TimeToInt(response->request_time); 285 details.last_access = TimeToInt(response->request_time);
271 details.flags = GetCacheability(response); 286 details.flags = GetCacheability(response);
272 details.vary_hash = GetVaryHash(response); 287 details.vary_hash = GetVaryHash(response);
273 details.response_hash = adler32(0, Z_NULL, 0); // Init the hash. 288 details.response_hash = adler32(0, Z_NULL, 0); // Init the hash.
274 289
275 if (!details.flags && 290 if (!details.flags &&
276 TimeToInt(response->response_time) == details.expiration) { 291 TimeToInt(response->response_time) == details.expiration) {
277 details.flags = EXPIRED; 292 details.flags = EXPIRED;
278 } 293 }
294
295 // Restore the old flag values we wanted to preserve.
296 details.flags |= old_flag_values;
297
279 details.flags |= GetRevalidationFlags(response); 298 details.flags |= GetRevalidationFlags(response);
280 299
281 if (must_doom_entry_)
282 details.flags |= DOOM_METHOD;
283
284 Pickle pickle; 300 Pickle pickle;
285 response->Persist(&pickle, true, false); // Skip transient headers. 301 response->Persist(&pickle, true, false); // Skip transient headers.
286 details.headers_size = pickle.size(); 302 details.headers_size = pickle.size();
287 details.headers_hash = adler32(0, Z_NULL, 0); 303 details.headers_hash = adler32(0, Z_NULL, 0);
288 details.headers_hash = adler32(details.headers_hash, 304 details.headers_hash = adler32(details.headers_hash,
289 reinterpret_cast<const Bytef*>(pickle.data()), 305 reinterpret_cast<const Bytef*>(pickle.data()),
290 pickle.size()); 306 pickle.size());
291 } 307 }
292 308
293 void InfiniteCacheTransaction::OnDataRead(const char* data, int data_len) { 309 void InfiniteCacheTransaction::OnDataRead(const char* data, int data_len) {
(...skipping 471 matching lines...) Expand 10 before | Expand all | Expand 10 after
765 781
766 void InfiniteCache::Worker::UpdateSize(int old_size, int new_size) { 782 void InfiniteCache::Worker::UpdateSize(int old_size, int new_size) {
767 header_->total_size += new_size - old_size; 783 header_->total_size += new_size - old_size;
768 DCHECK_GE(header_->total_size, 0); 784 DCHECK_GE(header_->total_size, 0);
769 } 785 }
770 786
771 void InfiniteCache::Worker::RecordHit(const Details& old, Details* details) { 787 void InfiniteCache::Worker::RecordHit(const Details& old, Details* details) {
772 header_->num_hits++; 788 header_->num_hits++;
773 int access_delta = (IntToTime(details->last_access) - 789 int access_delta = (IntToTime(details->last_access) -
774 IntToTime(old.last_access)).InMinutes(); 790 IntToTime(old.last_access)).InMinutes();
775 if (old.use_count) 791 if (old.use_count) {
776 UMA_HISTOGRAM_COUNTS("InfiniteCache.ReuseAge", access_delta); 792 UMA_HISTOGRAM_COUNTS("InfiniteCache.ReuseAge", access_delta);
777 else 793 if (details->flags & GA_JS_HTTP) {
794 UMA_HISTOGRAM_COUNTS("InfiniteCache.GaJsHttpReuseAge", access_delta);
795 } else if (details->flags & GA_JS_HTTPS) {
796 UMA_HISTOGRAM_COUNTS("InfiniteCache.GaJsHttpsReuseAge", access_delta);
797 }
798 } else {
778 UMA_HISTOGRAM_COUNTS("InfiniteCache.FirstReuseAge", access_delta); 799 UMA_HISTOGRAM_COUNTS("InfiniteCache.FirstReuseAge", access_delta);
800 if (details->flags & GA_JS_HTTP) {
801 UMA_HISTOGRAM_COUNTS(
802 "InfiniteCache.GaJsHttpFirstReuseAge", access_delta);
803 } else if (details->flags & GA_JS_HTTPS) {
804 UMA_HISTOGRAM_COUNTS(
805 "InfiniteCache.GaJsHttpsFirstReuseAge", access_delta);
806 }
807 }
779 808
780 details->use_count = old.use_count; 809 details->use_count = old.use_count;
781 if (details->use_count < kuint8max) 810 if (details->use_count < kuint8max)
782 details->use_count++; 811 details->use_count++;
783 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.UseCount", details->use_count, 0, 812 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.UseCount", details->use_count, 0,
784 kuint8max, 25); 813 kuint8max, 25);
814 if (details->flags & GA_JS_HTTP) {
815 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.GaJsHttpUseCount",
816 details->use_count, 0, kuint8max, 25);
817 } else if (details->flags & GA_JS_HTTPS) {
818 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.GaJsHttpsUseCount",
819 details->use_count, 0, kuint8max, 25);
820 }
785 } 821 }
786 822
787 void InfiniteCache::Worker::RecordUpdate(const Details& old, Details* details) { 823 void InfiniteCache::Worker::RecordUpdate(const Details& old, Details* details) {
788 int access_delta = (IntToTime(details->last_access) - 824 int access_delta = (IntToTime(details->last_access) -
789 IntToTime(old.last_access)).InMinutes(); 825 IntToTime(old.last_access)).InMinutes();
790 if (old.update_count) 826 if (old.update_count) {
791 UMA_HISTOGRAM_COUNTS("InfiniteCache.UpdateAge", access_delta); 827 UMA_HISTOGRAM_COUNTS("InfiniteCache.UpdateAge", access_delta);
792 else 828 if (details->flags & GA_JS_HTTP) {
829 UMA_HISTOGRAM_COUNTS(
830 "InfiniteCache.GaJsHttpUpdateAge", access_delta);
831 } else if (details->flags & GA_JS_HTTPS) {
832 UMA_HISTOGRAM_COUNTS(
833 "InfiniteCache.GaJsHttpsUpdateAge", access_delta);
834 }
835 } else {
793 UMA_HISTOGRAM_COUNTS("InfiniteCache.FirstUpdateAge", access_delta); 836 UMA_HISTOGRAM_COUNTS("InfiniteCache.FirstUpdateAge", access_delta);
837 if (details->flags & GA_JS_HTTP) {
838 UMA_HISTOGRAM_COUNTS(
839 "InfiniteCache.GaJsHttpFirstUpdateAge", access_delta);
840 } else if (details->flags & GA_JS_HTTPS) {
841 UMA_HISTOGRAM_COUNTS(
842 "InfiniteCache.GaJsHttpsFirstUpdateAge", access_delta);
843 }
844 }
794 845
795 details->update_count = old.update_count; 846 details->update_count = old.update_count;
796 if (details->update_count < kuint8max) 847 if (details->update_count < kuint8max)
797 details->update_count++; 848 details->update_count++;
798 849
799 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.UpdateCount", 850 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.UpdateCount",
800 details->update_count, 0, kuint8max, 25); 851 details->update_count, 0, kuint8max, 25);
852 if (details->flags & GA_JS_HTTP) {
853 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.GaJsHttpUpdateCount",
854 details->update_count, 0, kuint8max, 25);
855 } else if (details->flags & GA_JS_HTTPS) {
856 UMA_HISTOGRAM_CUSTOM_COUNTS("InfiniteCache.GaJsHttpsUpdateCount",
857 details->update_count, 0, kuint8max, 25);
858 }
801 details->use_count = 0; 859 details->use_count = 0;
802 } 860 }
803 861
804 void InfiniteCache::Worker::GenerateHistograms() { 862 void InfiniteCache::Worker::GenerateHistograms() {
805 bool new_size_step = (header_->total_size / kReportSizeStep != 863 bool new_size_step = (header_->total_size / kReportSizeStep !=
806 header_->size_last_report / kReportSizeStep); 864 header_->size_last_report / kReportSizeStep);
807 header_->size_last_report = header_->total_size; 865 header_->size_last_report = header_->total_size;
808 if (!new_size_step && (header_->use_minutes % 60 != 0)) 866 if (!new_size_step && (header_->use_minutes % 60 != 0))
809 return; 867 return;
810 868
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
883 reason = REUSE_TRUNCATED; 941 reason = REUSE_TRUNCATED;
884 942
885 if (old.vary_hash != current.vary_hash) 943 if (old.vary_hash != current.vary_hash)
886 reason = REUSE_VARY; 944 reason = REUSE_VARY;
887 945
888 bool have_to_drop = (old.flags & TRUNCATED) && !(old.flags & RESUMABLE); 946 bool have_to_drop = (old.flags & TRUNCATED) && !(old.flags & RESUMABLE);
889 if (reason && (old.flags & REVALIDATEABLE) && !have_to_drop) 947 if (reason && (old.flags & REVALIDATEABLE) && !have_to_drop)
890 reason += REUSE_REVALIDATEABLE; 948 reason += REUSE_REVALIDATEABLE;
891 949
892 UMA_HISTOGRAM_ENUMERATION("InfiniteCache.ReuseFailure", reason, 15); 950 UMA_HISTOGRAM_ENUMERATION("InfiniteCache.ReuseFailure", reason, 15);
951 if (current.flags & GA_JS_HTTP) {
952 UMA_HISTOGRAM_ENUMERATION(
953 "InfiniteCache.GaJsHttpReuseFailure", reason, 15);
954 } else if (current.flags & GA_JS_HTTPS) {
955 UMA_HISTOGRAM_ENUMERATION(
956 "InfiniteCache.GaJsHttpsReuseFailure", reason, 15);
957 }
893 return !reason; 958 return !reason;
894 } 959 }
895 960
896 bool InfiniteCache::Worker::DataChanged(const Details& old, 961 bool InfiniteCache::Worker::DataChanged(const Details& old,
897 const Details& current) { 962 const Details& current) {
898 if (current.flags & CACHED) 963 if (current.flags & CACHED)
899 return false; 964 return false;
900 965
901 bool changed = false; 966 bool changed = false;
902 if (old.response_size != current.response_size) { 967 if (old.response_size != current.response_size) {
(...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after
1014 int InfiniteCache::FlushDataForTest(const CompletionCallback& callback) { 1079 int InfiniteCache::FlushDataForTest(const CompletionCallback& callback) {
1015 DCHECK(worker_); 1080 DCHECK(worker_);
1016 int* result = new int; 1081 int* result = new int;
1017 task_runner_->PostTaskAndReply( 1082 task_runner_->PostTaskAndReply(
1018 FROM_HERE, base::Bind(&InfiniteCache::Worker::Flush, worker_, result), 1083 FROM_HERE, base::Bind(&InfiniteCache::Worker::Flush, worker_, result),
1019 base::Bind(&OnComplete, callback, base::Owned(result))); 1084 base::Bind(&OnComplete, callback, base::Owned(result)));
1020 return net::ERR_IO_PENDING; 1085 return net::ERR_IO_PENDING;
1021 } 1086 }
1022 1087
1023 } // namespace net 1088 } // namespace net
OLDNEW
« no previous file with comments | « net/http/infinite_cache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698