| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/url_request/url_request_throttler_manager.h" | 5 #include "net/url_request/url_request_throttler_manager.h" |
| 6 | 6 |
| 7 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 8 | 8 |
| 9 namespace net { | 9 namespace net { |
| 10 | 10 |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 GarbageCollectEntriesIfNecessary(); | 24 GarbageCollectEntriesIfNecessary(); |
| 25 | 25 |
| 26 // Find the entry in the map or create it. | 26 // Find the entry in the map or create it. |
| 27 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; | 27 scoped_refptr<URLRequestThrottlerEntry>& entry = url_entries_[url_id]; |
| 28 if (entry == NULL) | 28 if (entry == NULL) |
| 29 entry = new URLRequestThrottlerEntry(); | 29 entry = new URLRequestThrottlerEntry(); |
| 30 | 30 |
| 31 return entry; | 31 return entry; |
| 32 } | 32 } |
| 33 | 33 |
| 34 void URLRequestThrottlerManager::OverrideEntryForTests( |
| 35 const GURL& url, |
| 36 URLRequestThrottlerEntry* entry) { |
| 37 if (entry == NULL) |
| 38 return; |
| 39 |
| 40 // Normalize the url. |
| 41 std::string url_id = GetIdFromUrl(url); |
| 42 |
| 43 // Periodically garbage collect old entries. |
| 44 GarbageCollectEntriesIfNecessary(); |
| 45 |
| 46 url_entries_[url_id] = entry; |
| 47 } |
| 48 |
| 49 void URLRequestThrottlerManager::EraseEntryForTests(const GURL& url) { |
| 50 // Normalize the url. |
| 51 std::string url_id = GetIdFromUrl(url); |
| 52 url_entries_.erase(url_id); |
| 53 } |
| 54 |
| 34 URLRequestThrottlerManager::URLRequestThrottlerManager() | 55 URLRequestThrottlerManager::URLRequestThrottlerManager() |
| 35 : requests_since_last_gc_(0), | 56 : requests_since_last_gc_(0), |
| 36 enforce_throttling_(true) { | 57 enforce_throttling_(true) { |
| 37 } | 58 } |
| 38 | 59 |
| 39 URLRequestThrottlerManager::~URLRequestThrottlerManager() { | 60 URLRequestThrottlerManager::~URLRequestThrottlerManager() { |
| 40 // Delete all entries. | 61 // Delete all entries. |
| 41 url_entries_.clear(); | 62 url_entries_.clear(); |
| 42 } | 63 } |
| 43 | 64 |
| 44 std::string URLRequestThrottlerManager::GetIdFromUrl(const GURL& url) const { | 65 std::string URLRequestThrottlerManager::GetIdFromUrl(const GURL& url) const { |
| 45 if (!url.is_valid()) | 66 if (!url.is_valid()) |
| 46 return url.possibly_invalid_spec(); | 67 return url.possibly_invalid_spec(); |
| 47 | 68 |
| 48 if (url_id_replacements_ == NULL) { | 69 if (url_id_replacements_ == NULL) { |
| 49 url_id_replacements_.reset(new GURL::Replacements()); | 70 url_id_replacements_.reset(new GURL::Replacements()); |
| 50 | 71 |
| 51 url_id_replacements_->ClearPassword(); | 72 url_id_replacements_->ClearPassword(); |
| 52 url_id_replacements_->ClearUsername(); | 73 url_id_replacements_->ClearUsername(); |
| 53 url_id_replacements_->ClearQuery(); | 74 url_id_replacements_->ClearQuery(); |
| 54 url_id_replacements_->ClearRef(); | 75 url_id_replacements_->ClearRef(); |
| 55 } | 76 } |
| 56 | 77 |
| 57 GURL id = url.ReplaceComponents(*url_id_replacements_); | 78 GURL id = url.ReplaceComponents(*url_id_replacements_); |
| 58 return StringToLowerASCII(id.spec()); | 79 return StringToLowerASCII(id.spec()); |
| 59 } | 80 } |
| 60 | 81 |
| 82 void URLRequestThrottlerManager::GarbageCollectEntriesIfNecessary() { |
| 83 requests_since_last_gc_++; |
| 84 if (requests_since_last_gc_ < kRequestsBetweenCollecting) |
| 85 return; |
| 86 |
| 87 requests_since_last_gc_ = 0; |
| 88 GarbageCollectEntries(); |
| 89 } |
| 90 |
| 61 void URLRequestThrottlerManager::GarbageCollectEntries() { | 91 void URLRequestThrottlerManager::GarbageCollectEntries() { |
| 62 UrlEntryMap::iterator i = url_entries_.begin(); | 92 UrlEntryMap::iterator i = url_entries_.begin(); |
| 63 | 93 |
| 64 while (i != url_entries_.end()) { | 94 while (i != url_entries_.end()) { |
| 65 if ((i->second)->IsEntryOutdated()) { | 95 if ((i->second)->IsEntryOutdated()) { |
| 66 url_entries_.erase(i++); | 96 url_entries_.erase(i++); |
| 67 } else { | 97 } else { |
| 68 ++i; | 98 ++i; |
| 69 } | 99 } |
| 70 } | 100 } |
| 71 | 101 |
| 72 // In case something broke we want to make sure not to grow indefinitely. | 102 // In case something broke we want to make sure not to grow indefinitely. |
| 73 while (url_entries_.size() > kMaximumNumberOfEntries) { | 103 while (url_entries_.size() > kMaximumNumberOfEntries) { |
| 74 url_entries_.erase(url_entries_.begin()); | 104 url_entries_.erase(url_entries_.begin()); |
| 75 } | 105 } |
| 76 } | 106 } |
| 77 | 107 |
| 78 void URLRequestThrottlerManager::GarbageCollectEntriesIfNecessary() { | |
| 79 requests_since_last_gc_++; | |
| 80 if (requests_since_last_gc_ < kRequestsBetweenCollecting) | |
| 81 return; | |
| 82 | |
| 83 requests_since_last_gc_ = 0; | |
| 84 GarbageCollectEntries(); | |
| 85 } | |
| 86 | |
| 87 void URLRequestThrottlerManager::OverrideEntryForTests( | |
| 88 const GURL& url, | |
| 89 URLRequestThrottlerEntry* entry) { | |
| 90 if (entry == NULL) | |
| 91 return; | |
| 92 | |
| 93 // Normalize the url. | |
| 94 std::string url_id = GetIdFromUrl(url); | |
| 95 | |
| 96 // Periodically garbage collect old entries. | |
| 97 GarbageCollectEntriesIfNecessary(); | |
| 98 | |
| 99 url_entries_[url_id] = entry; | |
| 100 } | |
| 101 | |
| 102 void URLRequestThrottlerManager::EraseEntryForTests(const GURL& url) { | |
| 103 // Normalize the url. | |
| 104 std::string url_id = GetIdFromUrl(url); | |
| 105 url_entries_.erase(url_id); | |
| 106 } | |
| 107 | |
| 108 } // namespace net | 108 } // namespace net |
| OLD | NEW |