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