| OLD | NEW |
| (Empty) |
| 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. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h" | |
| 6 | |
| 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_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" | |
| 21 | |
| 22 | |
| 23 namespace { | |
| 24 | |
| 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; | |
| 28 | |
| 29 } // namespace | |
| 30 | |
| 31 SuggestionsSourceTopSites::SuggestionsSourceTopSites() | |
| 32 : combiner_(NULL), | |
| 33 debug_(false) { | |
| 34 } | |
| 35 | |
| 36 SuggestionsSourceTopSites::~SuggestionsSourceTopSites() { | |
| 37 STLDeleteElements(&items_); | |
| 38 } | |
| 39 | |
| 40 void SuggestionsSourceTopSites::SetDebug(bool enable) { | |
| 41 debug_ = enable; | |
| 42 } | |
| 43 | |
| 44 inline int SuggestionsSourceTopSites::GetWeight() { | |
| 45 return kSuggestionsTopListWeight; | |
| 46 } | |
| 47 | |
| 48 int SuggestionsSourceTopSites::GetItemCount() { | |
| 49 return items_.size(); | |
| 50 } | |
| 51 | |
| 52 base::DictionaryValue* SuggestionsSourceTopSites::PopItem() { | |
| 53 if (items_.empty()) | |
| 54 return NULL; | |
| 55 | |
| 56 base::DictionaryValue* item = items_.front(); | |
| 57 items_.pop_front(); | |
| 58 return item; | |
| 59 } | |
| 60 | |
| 61 void SuggestionsSourceTopSites::FetchItems(Profile* profile) { | |
| 62 DCHECK(combiner_); | |
| 63 STLDeleteElements(&items_); | |
| 64 | |
| 65 history_tracker_.TryCancelAll(); | |
| 66 history::HistoryService* history = HistoryServiceFactory::GetForProfile( | |
| 67 profile, ServiceAccessType::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()); | |
| 74 | |
| 75 history->QueryFilteredURLs( | |
| 76 0, | |
| 77 time_filter, | |
| 78 debug_, | |
| 79 base::Bind(&SuggestionsSourceTopSites::OnSuggestionsUrlsAvailable, | |
| 80 base::Unretained(this)), | |
| 81 &history_tracker_); | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 void SuggestionsSourceTopSites::SetCombiner(SuggestionsCombiner* combiner) { | |
| 86 DCHECK(!combiner_); | |
| 87 combiner_ = combiner; | |
| 88 } | |
| 89 | |
| 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; | |
| 98 | |
| 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); | |
| 108 } | |
| 109 if (suggested_url.extended_info.visits) { | |
| 110 page_value->SetInteger("extended_info.visits", | |
| 111 suggested_url.extended_info.visits); | |
| 112 } | |
| 113 if (suggested_url.extended_info.duration_opened) { | |
| 114 page_value->SetInteger("extended_info.duration opened", | |
| 115 suggested_url.extended_info.duration_opened); | |
| 116 } | |
| 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()); | |
| 122 } | |
| 123 } | |
| 124 items_.push_back(page_value); | |
| 125 } | |
| 126 | |
| 127 combiner_->OnItemsReady(); | |
| 128 } | |
| 129 | |
| 130 // static | |
| 131 base::TimeDelta SuggestionsSourceTopSites::GetFilterWidth() { | |
| 132 return base::TimeDelta::FromHours(1); | |
| 133 } | |
| 134 | |
| 135 // static | |
| 136 history::VisitFilter::SortingOrder | |
| 137 SuggestionsSourceTopSites::GetSortingOrder() { | |
| 138 return history::VisitFilter::ORDER_BY_RECENCY; | |
| 139 } | |
| OLD | NEW |