| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2010 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 "net/request_throttler/request_throttler_manager.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/message_loop.h" |
| 9 #include "base/rand_util.h" |
| 10 #include "base/string_util.h" |
| 11 |
| 12 const unsigned int RequestThrottlerManager::kMaximumNumberOfEntries = 1500; |
| 13 const unsigned int RequestThrottlerManager::kRequestsBetweenCollecting = 200; |
| 14 |
| 15 scoped_refptr<RequestThrottlerEntryInterface> |
| 16 RequestThrottlerManager::RegisterRequestUrl( |
| 17 const GURL &url) { |
| 18 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO); |
| 19 |
| 20 // Periodically garbage collect old entries. |
| 21 requests_since_last_gc_++; |
| 22 if (requests_since_last_gc_ >= kRequestsBetweenCollecting) { |
| 23 GarbageCollectEntries(); |
| 24 requests_since_last_gc_ = 0; |
| 25 } |
| 26 |
| 27 // Normalize the url |
| 28 std::string url_id = GetIdFromUrl(url); |
| 29 |
| 30 // Finds the entry in the map or creates it. |
| 31 scoped_refptr<RequestThrottlerEntry>& entry = url_entries_[url_id]; |
| 32 if (entry == NULL) |
| 33 entry = new RequestThrottlerEntry(); |
| 34 |
| 35 return entry; |
| 36 } |
| 37 |
| 38 RequestThrottlerManager::RequestThrottlerManager() |
| 39 : current_loop_(MessageLoop::current()), |
| 40 requests_since_last_gc_(0) { |
| 41 } |
| 42 |
| 43 RequestThrottlerManager::~RequestThrottlerManager() { |
| 44 // Deletes all entries |
| 45 url_entries_.clear(); |
| 46 } |
| 47 |
| 48 std::string RequestThrottlerManager::GetIdFromUrl(const GURL& url) { |
| 49 std::string url_id; |
| 50 url_id += url.scheme(); |
| 51 url_id += "://"; |
| 52 url_id += url.host(); |
| 53 url_id += url.path(); |
| 54 |
| 55 return StringToLowerASCII(url_id); |
| 56 } |
| 57 |
| 58 void RequestThrottlerManager::GarbageCollectEntries() { |
| 59 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO); |
| 60 UrlEntryMap::iterator i = url_entries_.begin(); |
| 61 |
| 62 while (i != url_entries_.end()) { |
| 63 if ((i->second)->IsEntryOutdated()) { |
| 64 url_entries_.erase(i++); |
| 65 } else { |
| 66 ++i; |
| 67 } |
| 68 } |
| 69 |
| 70 // In case something broke we want to make sure not to grow indefinitely. |
| 71 while (url_entries_.size() > kMaximumNumberOfEntries) { |
| 72 url_entries_.erase(url_entries_.begin()); |
| 73 } |
| 74 } |
| 75 |
| 76 void RequestThrottlerManager::NotifyRequestBodyWasMalformed(const GURL& url) { |
| 77 DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO); |
| 78 // Normalize the url |
| 79 std::string url_id = GetIdFromUrl(url); |
| 80 UrlEntryMap::iterator i = url_entries_.find(url_id); |
| 81 |
| 82 if (i != url_entries_.end()) { |
| 83 i->second->ReceivedContentWasMalformed(); |
| 84 } |
| 85 } |
| OLD | NEW |