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 <set> | |
8 | |
9 #include "base/logging.h" | |
10 #include "base/memory/singleton.h" | |
11 #include "base/metrics/histogram.h" | |
12 #include "content/browser/renderer_host/resource_request_info_impl.h" | |
13 #include "net/base/io_buffer.h" | |
14 #include "net/url_request/url_request.h" | |
15 | |
16 namespace content { | |
17 | |
18 namespace { | |
19 | |
20 class GlobalDuplicateRecords { | |
21 public: | |
22 static GlobalDuplicateRecords* GetInstance() { | |
23 return Singleton<GlobalDuplicateRecords>::get(); | |
24 } | |
25 | |
26 std::set<MH_UINT32>* content_matches() { | |
27 return &content_matches_; | |
28 } | |
29 | |
30 std::set<MH_UINT32>* content_and_url_matches() { | |
31 return &content_and_url_matches_; | |
32 } | |
33 | |
34 private: | |
35 friend class Singleton<GlobalDuplicateRecords>; | |
36 friend struct DefaultSingletonTraits<GlobalDuplicateRecords>; | |
37 | |
38 GlobalDuplicateRecords() {} | |
39 ~GlobalDuplicateRecords() {} | |
40 | |
41 std::set<MH_UINT32> content_matches_; | |
42 std::set<MH_UINT32> content_and_url_matches_; | |
43 }; | |
44 | |
45 } // namespace | |
46 | |
47 DuplicateResourceHandler::DuplicateResourceHandler( | |
48 scoped_ptr<ResourceHandler> next_handler, | |
49 ResourceType::Type resource_type, | |
50 net::URLRequest* request) | |
51 : LayeredResourceHandler(next_handler.Pass()), | |
52 resource_type_(resource_type), | |
53 bytes_read_(0), | |
54 request_(request), | |
55 pmurhash_ph1_(0), | |
56 pmurhash_pcarry_(0) { | |
57 } | |
58 | |
59 DuplicateResourceHandler::~DuplicateResourceHandler() { | |
60 } | |
61 | |
62 bool DuplicateResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, | |
63 int* buf_size, int min_size) { | |
64 DCHECK_EQ(-1, min_size); | |
65 | |
66 if (!next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) | |
67 return false; | |
68 read_buffer_ = *buf; | |
69 return true; | |
70 } | |
71 | |
72 bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read, | |
73 bool* defer) { | |
74 PMurHash32_Process(&pmurhash_ph1_, &pmurhash_pcarry_, | |
75 read_buffer_->data(), bytes_read); | |
76 bytes_read_ += bytes_read; | |
77 return next_handler_->OnReadCompleted(request_id, bytes_read, defer); | |
78 } | |
79 | |
80 bool DuplicateResourceHandler::OnResponseCompleted( | |
81 int request_id, | |
82 const net::URLRequestStatus& status, | |
83 const std::string& security_info) { | |
84 | |
85 if (status.status() != net::URLRequestStatus::SUCCESS) | |
86 return next_handler_->OnResponseCompleted(request_id, | |
87 status, security_info); | |
88 | |
89 MH_UINT32 contents_hash = PMurHash32_Result(pmurhash_ph1_, | |
darin (slow to review)
2012/07/23 23:30:14
nit: move the code between the two OnResponseCompl
frankwang
2012/07/24 02:59:57
Done.
| |
90 pmurhash_pcarry_, bytes_read_); | |
91 | |
92 // Combine the contents_hash with the url, so we can test if future content | |
93 // identical resources have the same original url or not. | |
94 MH_UINT32 hashed_with_url; | |
95 const std::string url_spec = request_->url().spec(); | |
96 PMurHash32_Process(&pmurhash_ph1_, &pmurhash_pcarry_, | |
97 url_spec.data(), url_spec.length()); | |
98 hashed_with_url = PMurHash32_Result(pmurhash_ph1_, pmurhash_pcarry_, | |
99 url_spec.length() + bytes_read_); | |
100 | |
101 DVLOG(4) << "url: " << url_spec; | |
102 DVLOG(4) << "contents hash: " << contents_hash; | |
103 DVLOG(4) << "hash with url: " << hashed_with_url; | |
104 | |
105 std::set<MH_UINT32>* content_matches = | |
106 GlobalDuplicateRecords::GetInstance()->content_matches(); | |
107 std::set<MH_UINT32>* content_and_url_matches = | |
108 GlobalDuplicateRecords::GetInstance()->content_and_url_matches(); | |
109 | |
110 const bool did_match_contents = content_matches->count(contents_hash) > 0; | |
111 const bool did_match_contents_and_url = | |
112 content_and_url_matches->count(hashed_with_url) > 0; | |
113 | |
114 UMA_HISTOGRAM_BOOLEAN("Duplicate.Hits", did_match_contents); | |
115 UMA_HISTOGRAM_BOOLEAN("Duplicate.HitsSameUrl", did_match_contents && | |
116 did_match_contents_and_url); | |
117 if (did_match_contents && !did_match_contents_and_url) { | |
118 content_and_url_matches->insert(hashed_with_url); | |
119 UMA_HISTOGRAM_CUSTOM_COUNTS("Duplicate.Size.HashHitUrlMiss", bytes_read_, | |
120 1, 0x7FFFFFFF, 50); | |
121 UMA_HISTOGRAM_ENUMERATION("Duplicate.ResourceType.HashHitUrlMiss", | |
122 resource_type_, ResourceType::LAST_TYPE); | |
123 } | |
124 content_matches->insert(contents_hash); | |
125 content_and_url_matches->insert(hashed_with_url); | |
126 | |
127 bytes_read_ = 0; | |
128 read_buffer_ = NULL; | |
129 return next_handler_->OnResponseCompleted(request_id, status, security_info); | |
130 } | |
131 | |
132 } // namespace content | |
133 | |
OLD | NEW |