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