1// Copyright 2013 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 "chrome/browser/net/timed_cache.h"
6
7#include "url/gurl.h"
8
9namespace chrome_browser_net {
10
11TimedCache::TimedCache(const base::TimeDelta& max_duration)
12    : mru_cache_(UrlMruTimedCache::NO_AUTO_EVICT),
13      max_duration_(max_duration) {
14}
15
16// Make Clang compilation happy with explicit destructor.
17TimedCache::~TimedCache() {}
18
19bool TimedCache::WasRecentlySeen(const GURL& url) {
20  DCHECK_EQ(url.GetWithEmptyPath(), url);
21  // Evict any overly old entries.
22  base::TimeTicks now = base::TimeTicks::Now();
23  UrlMruTimedCache::reverse_iterator eldest = mru_cache_.rbegin();
24  while (!mru_cache_.empty()) {
25    DCHECK(eldest == mru_cache_.rbegin());
26    if (now - eldest->second < max_duration_)
27      break;
28    eldest = mru_cache_.Erase(eldest);
29  }
30  return mru_cache_.end() != mru_cache_.Peek(url);
31}
32
33void TimedCache::SetRecentlySeen(const GURL& url) {
34  DCHECK_EQ(url.GetWithEmptyPath(), url);
35  mru_cache_.Put(url, base::TimeTicks::Now());
36}
37
38}  // namespace chrome_browser_net
39