Partial componentization of //chrome/browser/history
[chromium-blink-merge.git] / chrome / browser / ui / webui / ntp / suggestions_source_top_sites.cc
blob49fb628fcdc457f39192d0bae1dda1423ff2d107
1 // Copyright (c) 2012 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.
5 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h"
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "base/command_line.h"
10 #include "base/stl_util.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/values.h"
13 #include "chrome/browser/history/history_service.h"
14 #include "chrome/browser/history/history_service_factory.h"
15 #include "chrome/browser/history/top_sites.h"
16 #include "chrome/browser/profiles/profile.h"
17 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
18 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h"
19 #include "chrome/common/chrome_switches.h"
20 #include "components/history/core/browser/visit_filter.h"
23 namespace {
25 // The weight used by the combiner to determine which ratio of suggestions
26 // should be obtained from this source.
27 const int kSuggestionsTopListWeight = 1;
29 } // namespace
31 SuggestionsSourceTopSites::SuggestionsSourceTopSites()
32 : combiner_(NULL),
33 debug_(false) {
36 SuggestionsSourceTopSites::~SuggestionsSourceTopSites() {
37 STLDeleteElements(&items_);
40 void SuggestionsSourceTopSites::SetDebug(bool enable) {
41 debug_ = enable;
44 inline int SuggestionsSourceTopSites::GetWeight() {
45 return kSuggestionsTopListWeight;
48 int SuggestionsSourceTopSites::GetItemCount() {
49 return items_.size();
52 base::DictionaryValue* SuggestionsSourceTopSites::PopItem() {
53 if (items_.empty())
54 return NULL;
56 base::DictionaryValue* item = items_.front();
57 items_.pop_front();
58 return item;
61 void SuggestionsSourceTopSites::FetchItems(Profile* profile) {
62 DCHECK(combiner_);
63 STLDeleteElements(&items_);
65 history_tracker_.TryCancelAll();
66 HistoryService* history = HistoryServiceFactory::GetForProfile(
67 profile, Profile::EXPLICIT_ACCESS);
68 // |history| may be null during unit tests.
69 if (history) {
70 history::VisitFilter time_filter;
71 time_filter.SetFilterTime(base::Time::Now());
72 time_filter.SetFilterWidth(GetFilterWidth());
73 time_filter.set_sorting_order(GetSortingOrder());
75 history->QueryFilteredURLs(
77 time_filter,
78 debug_,
79 base::Bind(&SuggestionsSourceTopSites::OnSuggestionsUrlsAvailable,
80 base::Unretained(this)),
81 &history_tracker_);
85 void SuggestionsSourceTopSites::SetCombiner(SuggestionsCombiner* combiner) {
86 DCHECK(!combiner_);
87 combiner_ = combiner;
90 void SuggestionsSourceTopSites::OnSuggestionsUrlsAvailable(
91 const history::FilteredURLList* data) {
92 DCHECK(data);
93 DCHECK(combiner_);
94 for (size_t i = 0; i < data->size(); i++) {
95 const history::FilteredURL& suggested_url = (*data)[i];
96 if (suggested_url.url.is_empty())
97 continue;
99 base::DictionaryValue* page_value = new base::DictionaryValue();
100 NewTabUI::SetUrlTitleAndDirection(page_value,
101 suggested_url.title,
102 suggested_url.url);
103 page_value->SetDouble("score", suggested_url.score);
104 if (debug_) {
105 if (suggested_url.extended_info.total_visits) {
106 page_value->SetInteger("extended_info.total visits",
107 suggested_url.extended_info.total_visits);
109 if (suggested_url.extended_info.visits) {
110 page_value->SetInteger("extended_info.visits",
111 suggested_url.extended_info.visits);
113 if (suggested_url.extended_info.duration_opened) {
114 page_value->SetInteger("extended_info.duration opened",
115 suggested_url.extended_info.duration_opened);
117 if (!suggested_url.extended_info.last_visit_time.is_null()) {
118 base::TimeDelta deltaTime =
119 base::Time::Now() - suggested_url.extended_info.last_visit_time;
120 page_value->SetInteger("extended_info.seconds since last visit",
121 deltaTime.InSeconds());
124 items_.push_back(page_value);
127 combiner_->OnItemsReady();
130 // static
131 base::TimeDelta SuggestionsSourceTopSites::GetFilterWidth() {
132 return base::TimeDelta::FromHours(1);
135 // static
136 history::VisitFilter::SortingOrder
137 SuggestionsSourceTopSites::GetSortingOrder() {
138 return history::VisitFilter::ORDER_BY_RECENCY;