1// Copyright (c) 2011 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/prerender/prerender_history.h"
6
7#include "base/logging.h"
8#include "base/strings/string_number_conversions.h"
9#include "base/values.h"
10
11namespace prerender {
12
13PrerenderHistory::PrerenderHistory(size_t max_items)
14    : max_items_(max_items) {
15  DCHECK(max_items > 0);
16}
17
18PrerenderHistory::~PrerenderHistory() {
19}
20
21void PrerenderHistory::AddEntry(const Entry& entry) {
22  DCHECK(CalledOnValidThread());
23  while (entries_.size() >= max_items_)
24    entries_.pop_front();
25  entries_.push_back(entry);
26}
27
28void PrerenderHistory::Clear() {
29  entries_.clear();
30}
31
32base::Value* PrerenderHistory::GetEntriesAsValue() const {
33  base::ListValue* return_list = new base::ListValue();
34  // Javascript needs times in terms of milliseconds since Jan 1, 1970.
35  base::Time epoch_start = base::Time::UnixEpoch();
36  for (std::list<Entry>::const_reverse_iterator it = entries_.rbegin();
37       it != entries_.rend();
38       ++it) {
39    const Entry& entry = *it;
40    base::DictionaryValue* entry_dict = new base::DictionaryValue();
41    entry_dict->SetString("url", entry.url.spec());
42    entry_dict->SetString("final_status",
43                          NameFromFinalStatus(entry.final_status));
44    entry_dict->SetString("origin", NameFromOrigin(entry.origin));
45    // Use a string to prevent overflow, as Values don't support 64-bit
46    // integers.
47    entry_dict->SetString(
48        "end_time",
49        base::Int64ToString((entry.end_time - epoch_start).InMilliseconds()));
50    return_list->Append(entry_dict);
51  }
52  return return_list;
53}
54
55}  // namespace prerender
56