| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/ftp/ftp_auth_cache.h" | 5 #include "net/ftp/ftp_auth_cache.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "googleurl/src/gurl.h" | 8 #include "googleurl/src/gurl.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 // static | 12 // static |
| 13 const size_t FtpAuthCache::kMaxEntries = 10; | 13 const size_t FtpAuthCache::kMaxEntries = 10; |
| 14 | 14 |
| 15 AuthData* FtpAuthCache::Lookup(const GURL& origin) { | 15 FtpAuthCache::Entry* FtpAuthCache::Lookup(const GURL& origin) { |
| 16 Entry* entry = LookupEntry(origin); | 16 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 17 return (entry ? entry->auth_data : NULL); | 17 if (it->origin == origin) |
| 18 return &(*it); |
| 19 } |
| 20 return NULL; |
| 18 } | 21 } |
| 19 | 22 |
| 20 void FtpAuthCache::Add(const GURL& origin, AuthData* auth_data) { | 23 void FtpAuthCache::Add(const GURL& origin, const std::wstring& username, |
| 24 const std::wstring& password) { |
| 21 DCHECK(origin.SchemeIs("ftp")); | 25 DCHECK(origin.SchemeIs("ftp")); |
| 22 DCHECK_EQ(origin.GetOrigin(), origin); | 26 DCHECK_EQ(origin.GetOrigin(), origin); |
| 23 | 27 |
| 24 Entry* entry = LookupEntry(origin); | 28 Entry* entry = Lookup(origin); |
| 25 if (entry) { | 29 if (entry) { |
| 26 entry->auth_data = auth_data; | 30 entry->username = username; |
| 31 entry->password = password; |
| 27 } else { | 32 } else { |
| 28 entries_.push_front(Entry(origin, auth_data)); | 33 entries_.push_front(Entry(origin, username, password)); |
| 29 | 34 |
| 30 // Prevent unbound memory growth of the cache. | 35 // Prevent unbound memory growth of the cache. |
| 31 if (entries_.size() > kMaxEntries) | 36 if (entries_.size() > kMaxEntries) |
| 32 entries_.pop_back(); | 37 entries_.pop_back(); |
| 33 } | 38 } |
| 34 } | 39 } |
| 35 | 40 |
| 36 void FtpAuthCache::Remove(const GURL& origin) { | 41 void FtpAuthCache::Remove(const GURL& origin, const std::wstring& username, |
| 42 const std::wstring& password) { |
| 37 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | 43 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { |
| 38 if (it->origin == origin) { | 44 if (it->origin == origin && it->username == username && |
| 45 it->password == password) { |
| 39 entries_.erase(it); | 46 entries_.erase(it); |
| 40 DCHECK(!LookupEntry(origin)); | 47 DCHECK(!Lookup(origin)); |
| 41 return; | 48 return; |
| 42 } | 49 } |
| 43 } | 50 } |
| 44 } | 51 } |
| 45 | 52 |
| 46 FtpAuthCache::Entry* FtpAuthCache::LookupEntry(const GURL& origin) { | |
| 47 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) { | |
| 48 if (it->origin == origin) | |
| 49 return &(*it); | |
| 50 } | |
| 51 return NULL; | |
| 52 } | |
| 53 | |
| 54 } // namespace net | 53 } // namespace net |
| OLD | NEW |