OLD | NEW |
---|---|
(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_hit_(0), | |
32 bytes_miss_(0), | |
33 read_buffer_size_(0), // keep track of bytes in read buffer | |
34 read_buffer_(new net::IOBuffer(kReadBufSize)), | |
35 request_(request) { | |
36 } | |
37 | |
38 DuplicateResourceHandler::~DuplicateResourceHandler() { | |
39 } | |
40 | |
41 bool DuplicateResourceHandler::OnWillStart(int request_id, | |
42 const GURL& url, | |
43 bool* defer) { | |
44 return true; | |
45 } | |
frankwang
2012/07/12 21:50:36
I added this function to prevent it from asking th
| |
46 | |
47 bool DuplicateResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | |
48 int* buf_size, int min_size) { | |
49 DCHECK_EQ(-1, min_size); | |
50 | |
51 if (next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) { | |
52 *buf = read_buffer_.get(); | |
53 *buf_size = kReadBufSize; | |
54 return true; | |
55 } | |
56 | |
frankwang
2012/07/12 21:50:36
Instead of storing the pointer, *buf is now equal
| |
57 return false; | |
58 } | |
59 | |
60 bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read, | |
61 bool* defer) { | |
62 if (!next_handler_->OnReadCompleted(request_id, bytes_read, defer)) | |
63 return false; | |
64 | |
65 //DVLOG(4) << "read_buffer_ is " << read_buffer_.get(); | |
66 //DVLOG(4) << "read_buffer_->data() is " << read_buffer_->data(); | |
67 | |
68 // find hash of resource | |
69 uint32 buf_hash; | |
70 MurmurHash3_x86_32(read_buffer_->data(), bytes_read, 0x0, &buf_hash); | |
71 | |
72 const bool did_we_find_it = GetSetOfHashes()->find(buf_hash) != GetSetOfHashes ()->end(); | |
73 UMA_HISTOGRAM_BOOLEAN("Duplicate.HitRate", did_we_find_it); | |
74 | |
75 // if element is in hash | |
76 if (did_we_find_it) { | |
77 bytes_hit_ += bytes_read; | |
78 | |
79 } else { | |
80 bytes_miss_ += bytes_read; | |
81 GetSetOfHashes()->insert(buf_hash); | |
82 } | |
83 | |
84 read_buffer_size_ = 0; | |
85 | |
86 return true; | |
87 } | |
88 | |
89 } //namespace content | |
OLD | NEW |