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
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
10namespace net {
11
12// static
13const size_t FtpAuthCache::kMaxEntries = 10;
14
15FtpAuthCache::Entry* FtpAuthCache::Lookup(const GURL& origin) {
16  for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
17    if (it->origin == origin)
18      return &(*it);
19  }
20  return NULL;
21}
22
23void FtpAuthCache::Add(const GURL& origin, const std::wstring& username,
24                       const std::wstring& password) {
25  DCHECK(origin.SchemeIs("ftp"));
26  DCHECK_EQ(origin.GetOrigin(), origin);
27
28  Entry* entry = Lookup(origin);
29  if (entry) {
30    entry->username = username;
31    entry->password = password;
32  } else {
33    entries_.push_front(Entry(origin, username, password));
34
35    // Prevent unbound memory growth of the cache.
36    if (entries_.size() > kMaxEntries)
37      entries_.pop_back();
38  }
39}
40
41void FtpAuthCache::Remove(const GURL& origin, const std::wstring& username,
42                          const std::wstring& password) {
43  for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
44    if (it->origin == origin && it->username == username &&
45        it->password == password) {
46      entries_.erase(it);
47      DCHECK(!Lookup(origin));
48      return;
49    }
50  }
51}
52
53}  // namespace net
54