| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/ssl/ssl_client_session_cache.h" | 5 #include "net/ssl/ssl_client_session_cache.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/memory/memory_coordinator_client_registry.h" | 9 #include "base/memory/memory_coordinator_client_registry.h" |
| 10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 SSLClientSessionCache::~SSLClientSessionCache() { | 30 SSLClientSessionCache::~SSLClientSessionCache() { |
| 31 Flush(); | 31 Flush(); |
| 32 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); | 32 base::MemoryCoordinatorClientRegistry::GetInstance()->Unregister(this); |
| 33 } | 33 } |
| 34 | 34 |
| 35 size_t SSLClientSessionCache::size() const { | 35 size_t SSLClientSessionCache::size() const { |
| 36 return cache_.size(); | 36 return cache_.size(); |
| 37 } | 37 } |
| 38 | 38 |
| 39 bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Lookup( | 39 bssl::UniquePtr<SSL_SESSION> SSLClientSessionCache::Lookup( |
| 40 const std::string& cache_key) { | 40 const std::string& cache_key, |
| 41 int* count) { |
| 41 base::AutoLock lock(lock_); | 42 base::AutoLock lock(lock_); |
| 42 | 43 |
| 43 // Expire stale sessions. | 44 // Expire stale sessions. |
| 44 lookups_since_flush_++; | 45 lookups_since_flush_++; |
| 45 if (lookups_since_flush_ >= config_.expiration_check_count) { | 46 if (lookups_since_flush_ >= config_.expiration_check_count) { |
| 46 lookups_since_flush_ = 0; | 47 lookups_since_flush_ = 0; |
| 47 FlushExpiredSessions(); | 48 FlushExpiredSessions(); |
| 48 } | 49 } |
| 49 | 50 |
| 51 // Set count to 0 if there's no session in the cache. |
| 52 if (count != nullptr) |
| 53 *count = 0; |
| 54 |
| 50 auto iter = cache_.Get(cache_key); | 55 auto iter = cache_.Get(cache_key); |
| 51 if (iter == cache_.end()) | 56 if (iter == cache_.end()) |
| 52 return nullptr; | 57 return nullptr; |
| 53 | 58 |
| 54 SSL_SESSION* session = iter->second.get(); | 59 SSL_SESSION* session = iter->second.session.get(); |
| 55 if (IsExpired(session, clock_->Now().ToTimeT())) { | 60 if (IsExpired(session, clock_->Now().ToTimeT())) { |
| 56 cache_.Erase(iter); | 61 cache_.Erase(iter); |
| 57 return nullptr; | 62 return nullptr; |
| 58 } | 63 } |
| 59 | 64 |
| 65 iter->second.lookups++; |
| 66 if (count != nullptr) { |
| 67 *count = iter->second.lookups; |
| 68 } |
| 69 |
| 60 SSL_SESSION_up_ref(session); | 70 SSL_SESSION_up_ref(session); |
| 61 return bssl::UniquePtr<SSL_SESSION>(session); | 71 return bssl::UniquePtr<SSL_SESSION>(session); |
| 62 } | 72 } |
| 63 | 73 |
| 74 void SSLClientSessionCache::ResetLookupCount(const std::string& cache_key) { |
| 75 base::AutoLock lock(lock_); |
| 76 |
| 77 // It's possible that the cached session for this key was deleted after the |
| 78 // Lookup. If that's the case, don't do anything. |
| 79 auto iter = cache_.Get(cache_key); |
| 80 if (iter == cache_.end()) |
| 81 return; |
| 82 |
| 83 iter->second.lookups = 0; |
| 84 } |
| 85 |
| 64 void SSLClientSessionCache::Insert(const std::string& cache_key, | 86 void SSLClientSessionCache::Insert(const std::string& cache_key, |
| 65 SSL_SESSION* session) { | 87 SSL_SESSION* session) { |
| 66 base::AutoLock lock(lock_); | 88 base::AutoLock lock(lock_); |
| 67 | 89 |
| 68 SSL_SESSION_up_ref(session); | 90 SSL_SESSION_up_ref(session); |
| 69 cache_.Put(cache_key, bssl::UniquePtr<SSL_SESSION>(session)); | 91 Entry entry; |
| 92 entry.session = bssl::UniquePtr<SSL_SESSION>(session); |
| 93 cache_.Put(cache_key, std::move(entry)); |
| 70 } | 94 } |
| 71 | 95 |
| 72 void SSLClientSessionCache::Flush() { | 96 void SSLClientSessionCache::Flush() { |
| 73 base::AutoLock lock(lock_); | 97 base::AutoLock lock(lock_); |
| 74 | 98 |
| 75 cache_.Clear(); | 99 cache_.Clear(); |
| 76 } | 100 } |
| 77 | 101 |
| 78 void SSLClientSessionCache::SetClockForTesting( | 102 void SSLClientSessionCache::SetClockForTesting( |
| 79 std::unique_ptr<base::Clock> clock) { | 103 std::unique_ptr<base::Clock> clock) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 94 // This method can be reached from different URLRequestContexts. Since this is | 118 // This method can be reached from different URLRequestContexts. Since this is |
| 95 // a singleton, only log memory stats once. | 119 // a singleton, only log memory stats once. |
| 96 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. | 120 // TODO(xunjieli): Change this once crbug.com/458365 is fixed. |
| 97 if (cache_dump) | 121 if (cache_dump) |
| 98 return; | 122 return; |
| 99 cache_dump = pmd->CreateAllocatorDump(absolute_name); | 123 cache_dump = pmd->CreateAllocatorDump(absolute_name); |
| 100 base::AutoLock lock(lock_); | 124 base::AutoLock lock(lock_); |
| 101 size_t total_serialized_cert_size = 0; | 125 size_t total_serialized_cert_size = 0; |
| 102 size_t total_cert_count = 0; | 126 size_t total_cert_count = 0; |
| 103 for (const auto& pair : cache_) { | 127 for (const auto& pair : cache_) { |
| 104 const SSL_SESSION* session = pair.second.get(); | 128 const SSL_SESSION* session = pair.second.session.get(); |
| 105 size_t cert_count = sk_CRYPTO_BUFFER_num(session->certs); | 129 size_t cert_count = sk_CRYPTO_BUFFER_num(session->certs); |
| 106 total_cert_count += cert_count; | 130 total_cert_count += cert_count; |
| 107 for (size_t i = 0; i < cert_count; ++i) { | 131 for (size_t i = 0; i < cert_count; ++i) { |
| 108 const CRYPTO_BUFFER* cert = sk_CRYPTO_BUFFER_value(session->certs, i); | 132 const CRYPTO_BUFFER* cert = sk_CRYPTO_BUFFER_value(session->certs, i); |
| 109 total_serialized_cert_size += CRYPTO_BUFFER_len(cert); | 133 total_serialized_cert_size += CRYPTO_BUFFER_len(cert); |
| 110 } | 134 } |
| 111 } | 135 } |
| 112 // This measures the lower bound of the serialized certificate. It doesn't | 136 // This measures the lower bound of the serialized certificate. It doesn't |
| 113 // measure the actual memory used, which is 4x this amount (see | 137 // measure the actual memory used, which is 4x this amount (see |
| 114 // crbug.com/671420 for more details). | 138 // crbug.com/671420 for more details). |
| 115 cache_dump->AddScalar("serialized_cert_size", | 139 cache_dump->AddScalar("serialized_cert_size", |
| 116 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 140 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 117 total_serialized_cert_size); | 141 total_serialized_cert_size); |
| 118 cache_dump->AddScalar("cert_count", | 142 cache_dump->AddScalar("cert_count", |
| 119 base::trace_event::MemoryAllocatorDump::kUnitsObjects, | 143 base::trace_event::MemoryAllocatorDump::kUnitsObjects, |
| 120 total_cert_count); | 144 total_cert_count); |
| 121 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, | 145 cache_dump->AddScalar(base::trace_event::MemoryAllocatorDump::kNameSize, |
| 122 base::trace_event::MemoryAllocatorDump::kUnitsBytes, | 146 base::trace_event::MemoryAllocatorDump::kUnitsBytes, |
| 123 total_serialized_cert_size); | 147 total_serialized_cert_size); |
| 124 } | 148 } |
| 125 | 149 |
| 150 SSLClientSessionCache::Entry::Entry() : lookups(0) {} |
| 151 SSLClientSessionCache::Entry::Entry(Entry&&) = default; |
| 152 SSLClientSessionCache::Entry::~Entry() = default; |
| 153 |
| 126 void SSLClientSessionCache::FlushExpiredSessions() { | 154 void SSLClientSessionCache::FlushExpiredSessions() { |
| 127 time_t now = clock_->Now().ToTimeT(); | 155 time_t now = clock_->Now().ToTimeT(); |
| 128 auto iter = cache_.begin(); | 156 auto iter = cache_.begin(); |
| 129 while (iter != cache_.end()) { | 157 while (iter != cache_.end()) { |
| 130 if (IsExpired(iter->second.get(), now)) { | 158 if (IsExpired(iter->second.session.get(), now)) { |
| 131 iter = cache_.Erase(iter); | 159 iter = cache_.Erase(iter); |
| 132 } else { | 160 } else { |
| 133 ++iter; | 161 ++iter; |
| 134 } | 162 } |
| 135 } | 163 } |
| 136 } | 164 } |
| 137 | 165 |
| 138 void SSLClientSessionCache::OnMemoryPressure( | 166 void SSLClientSessionCache::OnMemoryPressure( |
| 139 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { | 167 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) { |
| 140 switch (memory_pressure_level) { | 168 switch (memory_pressure_level) { |
| (...skipping 20 matching lines...) Expand all Loading... |
| 161 break; | 189 break; |
| 162 case base::MemoryState::SUSPENDED: | 190 case base::MemoryState::SUSPENDED: |
| 163 // Note: Not supported at present. Fall through. | 191 // Note: Not supported at present. Fall through. |
| 164 case base::MemoryState::UNKNOWN: | 192 case base::MemoryState::UNKNOWN: |
| 165 NOTREACHED(); | 193 NOTREACHED(); |
| 166 break; | 194 break; |
| 167 } | 195 } |
| 168 } | 196 } |
| 169 | 197 |
| 170 } // namespace net | 198 } // namespace net |
| OLD | NEW |