OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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/ui/webui/ntp/suggestions_source_top_sites.h" | 5 #include "chrome/browser/ui/webui/ntp/suggestions_source_top_sites.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/command_line.h" |
9 #include "base/stl_util.h" | 10 #include "base/stl_util.h" |
10 #include "base/values.h" | 11 #include "base/values.h" |
| 12 #include "base/string_number_conversions.h" |
11 #include "chrome/browser/history/top_sites.h" | 13 #include "chrome/browser/history/top_sites.h" |
12 #include "chrome/browser/history/visit_filter.h" | 14 #include "chrome/browser/history/visit_filter.h" |
13 #include "chrome/browser/profiles/profile.h" | 15 #include "chrome/browser/profiles/profile.h" |
14 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 16 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
15 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h" | 17 #include "chrome/browser/ui/webui/ntp/suggestions_combiner.h" |
| 18 #include "chrome/common/chrome_switches.h" |
| 19 |
16 | 20 |
17 namespace { | 21 namespace { |
18 | 22 |
19 // The weight used by the combiner to determine which ratio of suggestions | 23 // The weight used by the combiner to determine which ratio of suggestions |
20 // should be obtained from this source. | 24 // should be obtained from this source. |
21 const int kSuggestionsTopListWeight = 1; | 25 const int kSuggestionsTopListWeight = 1; |
22 | 26 |
23 } // namespace | 27 } // namespace |
24 | 28 |
25 SuggestionsSourceTopSites::SuggestionsSourceTopSites() : combiner_(NULL) { | 29 SuggestionsSourceTopSites::SuggestionsSourceTopSites() : combiner_(NULL) { |
(...skipping 23 matching lines...) Expand all Loading... |
49 void SuggestionsSourceTopSites::FetchItems(Profile* profile) { | 53 void SuggestionsSourceTopSites::FetchItems(Profile* profile) { |
50 DCHECK(combiner_); | 54 DCHECK(combiner_); |
51 STLDeleteElements(&items_); | 55 STLDeleteElements(&items_); |
52 | 56 |
53 history_consumer_.CancelAllRequests(); | 57 history_consumer_.CancelAllRequests(); |
54 HistoryService* history = | 58 HistoryService* history = |
55 profile->GetHistoryService(Profile::EXPLICIT_ACCESS); | 59 profile->GetHistoryService(Profile::EXPLICIT_ACCESS); |
56 // |history| may be null during unit tests. | 60 // |history| may be null during unit tests. |
57 if (history) { | 61 if (history) { |
58 history::VisitFilter time_filter; | 62 history::VisitFilter time_filter; |
59 base::TimeDelta half_an_hour = | 63 time_filter.SetFilterTime(base::Time::Now()); |
60 base::TimeDelta::FromMicroseconds(base::Time::kMicrosecondsPerHour / 2); | 64 time_filter.SetFilterWidth(GetFilterWidth()); |
61 base::Time now = base::Time::Now(); | 65 time_filter.set_sorting_order(GetSortingOrder()); |
62 time_filter.SetTimeInRangeFilter(now - half_an_hour, now + half_an_hour); | 66 |
63 history->QueryFilteredURLs(0, time_filter, &history_consumer_, | 67 history->QueryFilteredURLs(0, time_filter, &history_consumer_, |
64 base::Bind(&SuggestionsSourceTopSites::OnSuggestionsURLsAvailable, | 68 base::Bind(&SuggestionsSourceTopSites::OnSuggestionsURLsAvailable, |
65 base::Unretained(this))); | 69 base::Unretained(this))); |
66 } | 70 } |
67 } | 71 } |
68 | 72 |
69 void SuggestionsSourceTopSites::SetCombiner(SuggestionsCombiner* combiner) { | 73 void SuggestionsSourceTopSites::SetCombiner(SuggestionsCombiner* combiner) { |
70 DCHECK(!combiner_); | 74 DCHECK(!combiner_); |
71 combiner_ = combiner; | 75 combiner_ = combiner; |
72 } | 76 } |
(...skipping 10 matching lines...) Expand all Loading... |
83 DictionaryValue* page_value = new DictionaryValue(); | 87 DictionaryValue* page_value = new DictionaryValue(); |
84 NewTabUI::SetURLTitleAndDirection(page_value, | 88 NewTabUI::SetURLTitleAndDirection(page_value, |
85 suggested_url.title, | 89 suggested_url.title, |
86 suggested_url.url); | 90 suggested_url.url); |
87 page_value->SetDouble("score", suggested_url.score); | 91 page_value->SetDouble("score", suggested_url.score); |
88 items_.push_back(page_value); | 92 items_.push_back(page_value); |
89 } | 93 } |
90 | 94 |
91 combiner_->OnItemsReady(); | 95 combiner_->OnItemsReady(); |
92 } | 96 } |
| 97 |
| 98 // static |
| 99 base::TimeDelta SuggestionsSourceTopSites::GetFilterWidth() { |
| 100 const CommandLine* cli = CommandLine::ForCurrentProcess(); |
| 101 const std::string filter_width_switch = |
| 102 cli->GetSwitchValueASCII(switches::kSuggestionNtpFilterWidth); |
| 103 unsigned int filter_width; |
| 104 if (base::StringToUint(filter_width_switch, &filter_width)) |
| 105 return base::TimeDelta::FromMinutes(filter_width); |
| 106 return base::TimeDelta::FromHours(1); |
| 107 } |
| 108 |
| 109 // static |
| 110 history::VisitFilter::SortingOrder |
| 111 SuggestionsSourceTopSites::GetSortingOrder() { |
| 112 const CommandLine* cli = CommandLine::ForCurrentProcess(); |
| 113 if (cli->HasSwitch(switches::kSuggestionNtpGaussianFilter)) |
| 114 return history::VisitFilter::ORDER_BY_TIME_GAUSSIAN; |
| 115 if (cli->HasSwitch(switches::kSuggestionNtpLinearFilter)) |
| 116 return history::VisitFilter::ORDER_BY_TIME_LINEAR; |
| 117 return history::VisitFilter::ORDER_BY_RECENCY; |
| 118 } |
OLD | NEW |