Index: content/browser/renderer_host/duplicate_resource_handler.cc |
=================================================================== |
--- content/browser/renderer_host/duplicate_resource_handler.cc (revision 0) |
+++ content/browser/renderer_host/duplicate_resource_handler.cc (revision 0) |
@@ -0,0 +1,78 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <string> |
+ |
+#include "content/browser/renderer_host/duplicate_resource_handler.h" |
gavinp
2012/07/11 23:39:46
Move this above #include <string>
frankwang
2012/07/12 01:50:36
Done.
|
+ |
+#include "base/logging.h" |
+#include "base/metrics/histogram.h" |
+#include "content/browser/renderer_host/resource_request_info_impl.h" |
+#include "net/base/io_buffer.h" |
+#include "third_party/smhasher/src/MurmurHash3.h" |
+ |
+ |
+namespace content{ |
+ |
+namespace{ |
+ |
+ std::set<uint32>* GetSetOfHashes(){ |
gavinp
2012/07/11 23:39:46
Nit: don't indent this.
frankwang
2012/07/12 01:50:36
Done.
|
+ static std::set<uint32> seen_resources; |
+ return &seen_resources; |
+ } |
+ |
+} // namespace |
+ |
+DuplicateResourceHandler::DuplicateResourceHandler( |
+ scoped_ptr<ResourceHandler> next_handler, |
+ net::URLRequest* request) |
+ : LayeredResourceHandler(next_handler.Pass()), |
+ bytes_hit_(0), |
+ bytes_miss_(0), |
+ read_buffer_size_(0), // keep track of bytes in read buffer |
+ request_(request) { |
+} |
+ |
+DuplicateResourceHandler::~DuplicateResourceHandler(){ |
+} |
+ |
+bool DuplicateResourceHandler::OnWillRead(int request_id, net::IOBuffer** buf, |
+ int* buf_size, int min_size){ |
+ DCHECK_EQ(-1, min_size); |
+ |
+ read_buffer_ = *buf; |
+ read_buffer_size_ = *buf_size; |
gavinp
2012/07/11 23:39:46
This is wrong. The buffer is provided by your down
frankwang
2012/07/12 01:50:36
Done.
|
+ |
+ return true; |
+} |
+ |
+bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read, |
+ bool* defer) { |
+ |
gavinp
2012/07/11 23:39:46
if (!next_handler_->OnReadCompleted(...))
return
frankwang
2012/07/12 01:50:36
Done.
|
+ // find hash of resource |
+ uint32 buf_hash; |
+ MurmurHash3_x86_32(read_buffer_, bytes_read, 0x0, &buf_hash); |
gavinp
2012/07/11 23:39:46
This is conceptually broken. The reads come to you
gavinp
2012/07/11 23:39:46
Also:
MurmurHash3_x86_32(read_buffer_->data(), by
frankwang
2012/07/12 01:50:36
Done.
|
+ |
+ static base::Histogram* num_hits(NULL); |
gavinp
2012/07/11 23:39:46
This isn't a common pattern for creating histogram
frankwang
2012/07/12 01:50:36
I changed it just for one histogram. I am still di
|
+ if (!num_hits) |
+ num_hits = base::BooleanHistogram::FactoryGet( |
+ "hash.hit", base::Histogram::kUmaTargetedHistogramFlag); |
+ |
+ // if element is in hash |
+ if (GetSetOfHashes()->find(buf_hash) != GetSetOfHashes()->end()){ |
+ bytes_hit_ += bytes_read; |
+ num_hits->AddBoolean(true); |
+ |
+ } else{ |
+ bytes_miss_ += bytes_read; |
+ num_hits->AddBoolean(false); |
+ GetSetOfHashes()->insert(buf_hash); |
+ } |
+ |
gavinp
2012/07/11 23:41:31
I'm not sure if it's kosher to keep read_buffer_ a
frankwang
2012/07/12 01:50:36
Yes, I called release() on it because it was a sco
|
+ read_buffer_size_ = 0; |
+ |
+ return true; |
+} |
+ |
+} //namespace content |