C++程序  |  67行  |  1.97 KB

// 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.

#ifndef NET_FTP_FTP_AUTH_CACHE_H_
#define NET_FTP_FTP_AUTH_CACHE_H_
#pragma once

#include <list>

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

namespace net {

// The FtpAuthCache class is a simple cache structure to store authentication
// information for ftp. Provides lookup, insertion, and deletion of entries.
// The parameter for doing lookups, insertions, and deletions is a GURL of the
// server's address (not a full URL with path, since FTP auth isn't per path).
// For example:
//   GURL("ftp://myserver") -- OK (implied port of 21)
//   GURL("ftp://myserver:21") -- OK
//   GURL("ftp://myserver/PATH") -- WRONG, paths not allowed
class FtpAuthCache {
 public:
  // Maximum number of entries we allow in the cache.
  static const size_t kMaxEntries;

  struct Entry {
    Entry(const GURL& origin, const string16& username,
          const string16& password);
    ~Entry();

    const GURL origin;
    string16 username;
    string16 password;
  };

  FtpAuthCache();
  ~FtpAuthCache();

  // Return Entry corresponding to given |origin| or NULL if not found.
  Entry* Lookup(const GURL& origin);

  // Add an entry for |origin| to the cache (consisting of |username| and
  // |password|). If there is already an entry for |origin|, it will be
  // overwritten.
  void Add(const GURL& origin, const string16& username,
           const string16& password);

  // Remove the entry for |origin| from the cache, if one exists and matches
  // |username| and |password|.
  void Remove(const GURL& origin, const string16& username,
              const string16& password);

 private:
  typedef std::list<Entry> EntryList;

  // Internal representation of cache, an STL list. This makes lookups O(n),
  // but we expect n to be very low.
  EntryList entries_;
};

}  // namespace net

#endif  // NET_FTP_FTP_AUTH_CACHE_H_