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

Side by Side Diff: content/browser/renderer_host/duplicate_resource_handler.cc

Issue 10701151: DuplicateContentResourceHandler to monitor resources and track how many times th… (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 8 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/renderer_host/duplicate_resource_handler.h"
6
7 #include <string>
8
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "content/browser/renderer_host/resource_request_info_impl.h"
12 #include "net/base/io_buffer.h"
13 #include "third_party/smhasher/src/MurmurHash3.h"
14
15
16 namespace content {
17
18 namespace{
19
20 std::set<uint32>* GetSetOfHashes() {
21 static std::set<uint32> seen_resources;
22 return &seen_resources;
23 }
24
25 } // namespace
26
27 DuplicateResourceHandler::DuplicateResourceHandler(
28 scoped_ptr<ResourceHandler> next_handler,
29 net::URLRequest* request)
30 : LayeredResourceHandler(next_handler.Pass()),
31 bytes_read_(0),
32 bytes_hit_(0),
33 bytes_miss_(0),
34 read_buffer_size_(0), // keep track of bytes in read buffer
35 read_buffer_(new net::IOBuffer(kReadBufSize)),
36 request_(request) {
37 }
38
39 DuplicateResourceHandler::~DuplicateResourceHandler() {
40 }
41
42 /*bool DuplicateResourceHandler::OnWillStart(int request_id,
43 const GURL& url,
44 bool* defer) {
45 return true;
46 }*/
47
48 bool DuplicateResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf,
49 int* buf_size, int min_size) {
50 DCHECK_EQ(-1, min_size);
51
52 *buf = read_buffer_.get();
53 *buf_size = kReadBufSize;
54 return true;
gavinp 2012/07/13 21:16:04 I'm pretty sure you always want to pass through.
55
56 }
57
58 bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read,
59 bool* defer) {
60 /*if (!next_handler_->OnReadCompleted(request_id, bytes_read, defer))
61 return false;*/
62
63 //DVLOG(4) << "read_buffer_ is " << read_buffer_.get();
64 //DVLOG(4) << "read_buffer_->data() is " << read_buffer_->data();
65
66 // find hash of resource
67 uint32 buf_hash;
68 MurmurHash3_x86_32(read_buffer_->data(), bytes_read, 0x0, &buf_hash);
69
70
71 const bool did_we_find_it = GetSetOfHashes()->find(buf_hash) != GetSetOfHashes ()->end();
72 UMA_HISTOGRAM_BOOLEAN("Duplicate.HitRate", did_we_find_it);
73
74 // if element is in hash
75 if (did_we_find_it) {
76 bytes_hit_ += bytes_read;
77
78 } else {
79 bytes_miss_ += bytes_read;
80 GetSetOfHashes()->insert(buf_hash);
81 }
82
83 bytes_read_ += bytes_read;
84 return true;
85 }
86
87 bool DuplicateResourceHandler::OnResponseCompleted(
88 int request_id,
89 const net::URLRequestStatus& status,
90 const std::string& security_info) {
91
92 read_buffer_ = NULL;
93 return next_handler_->OnResponseCompleted(request_id, status, security_info);
94 }
95
96 } //namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698