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"
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_factory.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h"
16 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h"
17 #include "chrome/common/chrome_switches.h"
18 #include "components/history/core/browser/history_service.h"
19 #include "components/history/core/browser/top_sites.h"
20 #include "components/history/core/browser/visit_filter.h"
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;
31 SuggestionsSourceTopSites::SuggestionsSourceTopSites()
36 SuggestionsSourceTopSites::~SuggestionsSourceTopSites() {
37 STLDeleteElements(&items_
);
40 void SuggestionsSourceTopSites::SetDebug(bool enable
) {
44 inline int SuggestionsSourceTopSites::GetWeight() {
45 return kSuggestionsTopListWeight
;
48 int SuggestionsSourceTopSites::GetItemCount() {
52 base::DictionaryValue
* SuggestionsSourceTopSites::PopItem() {
56 base::DictionaryValue
* item
= items_
.front();
61 void SuggestionsSourceTopSites::FetchItems(Profile
* profile
) {
63 STLDeleteElements(&items_
);
65 history_tracker_
.TryCancelAll();
66 history::HistoryService
* history
= HistoryServiceFactory::GetForProfile(
67 profile
, ServiceAccessType::EXPLICIT_ACCESS
);
68 // |history| may be null during unit tests.
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(
79 base::Bind(&SuggestionsSourceTopSites::OnSuggestionsUrlsAvailable
,
80 base::Unretained(this)),
85 void SuggestionsSourceTopSites::SetCombiner(SuggestionsCombiner
* combiner
) {
90 void SuggestionsSourceTopSites::OnSuggestionsUrlsAvailable(
91 const history::FilteredURLList
* data
) {
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())
99 base::DictionaryValue
* page_value
= new base::DictionaryValue();
100 NewTabUI::SetUrlTitleAndDirection(page_value
,
103 page_value
->SetDouble("score", suggested_url
.score
);
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();
131 base::TimeDelta
SuggestionsSourceTopSites::GetFilterWidth() {
132 return base::TimeDelta::FromHours(1);
136 history::VisitFilter::SortingOrder
137 SuggestionsSourceTopSites::GetSortingOrder() {
138 return history::VisitFilter::ORDER_BY_RECENCY
;