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/ftp/ftp_auth_cache.h"
6
7 #include "base/logging.h"
8 #include "googleurl/src/gurl.h"
9
10 namespace net {
11
12 // static
13 const size_t FtpAuthCache::kMaxEntries = 10;
14
Entry(const GURL & origin,const string16 & username,const string16 & password)15 FtpAuthCache::Entry::Entry(const GURL& origin,
16 const string16& username,
17 const string16& password)
18 : origin(origin),
19 username(username),
20 password(password) {
21 }
22
~Entry()23 FtpAuthCache::Entry::~Entry() {}
24
FtpAuthCache()25 FtpAuthCache::FtpAuthCache() {}
26
~FtpAuthCache()27 FtpAuthCache::~FtpAuthCache() {}
28
Lookup(const GURL & origin)29 FtpAuthCache::Entry* FtpAuthCache::Lookup(const GURL& origin) {
30 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
31 if (it->origin == origin)
32 return &(*it);
33 }
34 return NULL;
35 }
36
Add(const GURL & origin,const string16 & username,const string16 & password)37 void FtpAuthCache::Add(const GURL& origin, const string16& username,
38 const string16& password) {
39 DCHECK(origin.SchemeIs("ftp"));
40 DCHECK_EQ(origin.GetOrigin(), origin);
41
42 Entry* entry = Lookup(origin);
43 if (entry) {
44 entry->username = username;
45 entry->password = password;
46 } else {
47 entries_.push_front(Entry(origin, username, password));
48
49 // Prevent unbound memory growth of the cache.
50 if (entries_.size() > kMaxEntries)
51 entries_.pop_back();
52 }
53 }
54
Remove(const GURL & origin,const string16 & username,const string16 & password)55 void FtpAuthCache::Remove(const GURL& origin, const string16& username,
56 const string16& password) {
57 for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
58 if (it->origin == origin && it->username == username &&
59 it->password == password) {
60 entries_.erase(it);
61 DCHECK(!Lookup(origin));
62 return;
63 }
64 }
65 }
66
67 } // namespace net
68