summaryrefslogtreecommitdiffstats
path: root/net/ftp/ftp_auth_cache.cc
blob: a67c2e04d071e2f8427173de61953ecdb21c73f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/ftp/ftp_auth_cache.h"

#include "base/logging.h"
#include "googleurl/src/gurl.h"

namespace net {

// static
const size_t FtpAuthCache::kMaxEntries = 10;

FtpAuthCache::Entry::Entry(const GURL& origin,
                           const string16& username,
                           const string16& password)
    : origin(origin),
      username(username),
      password(password) {
}

FtpAuthCache::Entry::~Entry() {}

FtpAuthCache::FtpAuthCache() {}

FtpAuthCache::~FtpAuthCache() {}

FtpAuthCache::Entry* FtpAuthCache::Lookup(const GURL& origin) {
  for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
    if (it->origin == origin)
      return &(*it);
  }
  return NULL;
}

void FtpAuthCache::Add(const GURL& origin, const string16& username,
                       const string16& password) {
  DCHECK(origin.SchemeIs("ftp"));
  DCHECK_EQ(origin.GetOrigin(), origin);

  Entry* entry = Lookup(origin);
  if (entry) {
    entry->username = username;
    entry->password = password;
  } else {
    entries_.push_front(Entry(origin, username, password));

    // Prevent unbound memory growth of the cache.
    if (entries_.size() > kMaxEntries)
      entries_.pop_back();
  }
}

void FtpAuthCache::Remove(const GURL& origin, const string16& username,
                          const string16& password) {
  for (EntryList::iterator it = entries_.begin(); it != entries_.end(); ++it) {
    if (it->origin == origin && it->username == username &&
        it->password == password) {
      entries_.erase(it);
      DCHECK(!Lookup(origin));
      return;
    }
  }
}

}  // namespace net