190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// found in the LICENSE file.
490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#ifndef CHROME_BROWSER_NET_TIMED_CACHE_H_
690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#define CHROME_BROWSER_NET_TIMED_CACHE_H_
790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/basictypes.h"
990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include "base/containers/mru_cache.h"
10eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
1190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class GURL;
1390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)namespace chrome_browser_net {
1590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
1690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// Define a LRU cache that recalls all navigations within the last N seconds.
1790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// When we learn about subresources to possibly preconnect to, it would be a
1890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// waste to preconnect when the original navigation was too long ago.  Any
1990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// connected, but unused TCP/IP connection, will generally be reset by the
2090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)// server if it is not used quickly (i.e., GET or POST is sent).
2190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)class TimedCache {
2290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) public:
2390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  explicit TimedCache(const base::TimeDelta& max_duration);
2490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  ~TimedCache();
2590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
2690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Evicts any entries that have been in the FIFO "too long," and then checks
2790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // to see if the given url is (still) in the FIFO cache.
2890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  bool WasRecentlySeen(const GURL& url);
2990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Adds the given url to the cache, where it will remain for max_duration_.
3190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  void SetRecentlySeen(const GURL& url);
3290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
3390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) private:
3490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // Our cache will be keyed on a URL (actually, just a scheme/host/port).
3590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // We will always track the time it was last added to the FIFO cache by
3690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // remembering a TimeTicks value.
3790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  typedef base::MRUCache<GURL, base::TimeTicks> UrlMruTimedCache;
3890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  UrlMruTimedCache mru_cache_;
3990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4090dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  // The longest time an entry can persist in the cache, and still be found.
4190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  const base::TimeDelta max_duration_;
4290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TimedCache);
4490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)};
4590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)}  // namespace chrome_browser_net
4790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)
4890dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#endif  // CHROME_BROWSER_NET_TIMED_CACHE_H_
49