| 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/url_request/url_request_throttler_manager.h" |
| 6 |
| 7 #include "base/string_util.h" |
| 8 |
| 9 namespace net { |
| 10 |
| 11 const unsigned int URLRequestThrottlerManager::kMaximumNumberOfEntries = 1500; |
| 12 const unsigned int URLRequestThrottlerManager::kRequestsBetweenCollecting = 200; |
| 13 |
| 14 URLRequestThrottlerManager* URLRequestThrottlerManager::GetInstance() { |
| 15 return Singleton<URLRequestThrottlerManager>::get(); |
| 16 } |
| 17 |
| 18 scoped_refptr<URLRequestThrottlerEntryInterface> |
| 19 URLRequestThrottlerManager::RegisterRequestUrl(const GURL &url) { |
| 20 // Normalize the url. |
| 21 std::string url_id = GetIdFromUrl(url); |
| 22 |
| 23 // Periodically garbage collect old entries. |
| 24 GarbageCollectEntriesIfNecessary(); |
| 25 |
| 26 // Find the entry in the map or create it. |
| 27 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; |
| 28 if (entry == NULL) |
| 29 entry = new URLRequestThrottlerEntry(); |
| 30 |
| 31 return entry; |
| 32 } |
| 33 |
| 34 URLRequestThrottlerManager::URLRequestThrottlerManager() |
| 35 : requests_since_last_gc_(0) { |
| 36 } |
| 37 |
| 38 URLRequestThrottlerManager::~URLRequestThrottlerManager() { |
| 39 // Delete all entries. |
| 40 url_entries_.clear(); |
| 41 } |
| 42 |
| 43 std::string URLRequestThrottlerManager::GetIdFromUrl(const GURL& url) const { |
| 44 if (!url.is_valid()) |
| 45 return url.possibly_invalid_spec(); |
| 46 |
| 47 if (url_id_replacements_ == NULL) { |
| 48 url_id_replacements_.reset(new GURL::Replacements()); |
| 49 |
| 50 url_id_replacements_->ClearPassword(); |
| 51 url_id_replacements_->ClearUsername(); |
| 52 url_id_replacements_->ClearQuery(); |
| 53 url_id_replacements_->ClearRef(); |
| 54 } |
| 55 |
| 56 GURL id = url.ReplaceComponents(*url_id_replacements_); |
| 57 return StringToLowerASCII(id.spec()); |
| 58 } |
| 59 |
| 60 void URLRequestThrottlerManager::GarbageCollectEntries() { |
| 61 UrlEntryMap::iterator i = url_entries_.begin(); |
| 62 |
| 63 while (i != url_entries_.end()) { |
| 64 if ((i->second)->IsEntryOutdated()) { |
| 65 url_entries_.erase(i++); |
| 66 } else { |
| 67 ++i; |
| 68 } |
| 69 } |
| 70 |
| 71 // In case something broke we want to make sure not to grow indefinitely. |
| 72 while (url_entries_.size() > kMaximumNumberOfEntries) { |
| 73 url_entries_.erase(url_entries_.begin()); |
| 74 } |
| 75 } |
| 76 |
| 77 void URLRequestThrottlerManager::GarbageCollectEntriesIfNecessary() { |
| 78 requests_since_last_gc_++; |
| 79 if (requests_since_last_gc_ < kRequestsBetweenCollecting) |
| 80 return; |
| 81 |
| 82 requests_since_last_gc_ = 0; |
| 83 GarbageCollectEntries(); |
| 84 } |
| 85 |
| 86 void URLRequestThrottlerManager::OverrideEntryForTests( |
| 87 const GURL& url, |
| 88 URLRequestThrottlerEntry* entry) { |
| 89 if (entry == NULL) |
| 90 return; |
| 91 |
| 92 // Normalize the url. |
| 93 std::string url_id = GetIdFromUrl(url); |
| 94 |
| 95 // Periodically garbage collect old entries. |
| 96 GarbageCollectEntriesIfNecessary(); |
| 97 |
| 98 url_entries_[url_id] = entry; |
| 99 } |
| 100 |
| 101 void URLRequestThrottlerManager::EraseEntryForTests(const GURL& url) { |
| 102 // Normalize the url. |
| 103 std::string url_id = GetIdFromUrl(url); |
| 104 url_entries_.erase(url_id); |
| 105 } |
| 106 |
| 107 } // namespace net |
| OLD | NEW |