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

Unified 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 side-by-side diff with in-line comments
Download patch
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,83 @@
+// 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 "content/browser/renderer_host/duplicate_resource_handler.h"
+
+#include <string>
+
+#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() {
+ 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);
+
+ if (next_handler_->OnWillRead(request_id, buf, buf_size, min_size)) {
+ read_buffer_ = *buf;
+ read_buffer_size_ = *buf_size;
+ return true;
+ }
+
+ return false;
+}
+
+bool DuplicateResourceHandler::OnReadCompleted(int request_id, int bytes_read,
+ bool* defer) {
+ if (!next_handler_->OnReadCompleted(request_id, bytes_read, defer))
+ return false;
+
+ DVLOG(4) << "read_buffer_ is " << read_buffer_.get();
+ DVLOG(4) << "read_buffer_->data() is " << read_buffer_->data();
+
+ // find hash of resource
+ uint32 buf_hash;
+ MurmurHash3_x86_32(read_buffer_->data(), bytes_read, 0x0, &buf_hash);
gavinp 2012/07/13 21:16:04 0, but in hexadecimal? Just use 0. If it helps, y
+
+ const bool did_we_find_it = GetSetOfHashes()->find(buf_hash) != GetSetOfHashes()->end();
+ UMA_HISTOGRAM_BOOLEAN("Duplicate.HitRate", did_we_find_it);
+
+ // if element is in hash
gavinp 2012/07/13 21:16:04 Your comment not full sentence with punctuation
+ if (did_we_find_it) {
+ bytes_hit_ += bytes_read;
+
+ } else {
+ bytes_miss_ += bytes_read;
+ GetSetOfHashes()->insert(buf_hash);
+ }
+
+ read_buffer_.release();
+ read_buffer_size_ = 0;
+
+ return true;
+}
+
+} //namespace content

Powered by Google App Engine
This is Rietveld 408576698