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/most_visited_handler.h" | 5 #include "chrome/browser/ui/webui/ntp/most_visited_handler.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/md5.h" | 12 #include "base/md5.h" |
13 #include "base/memory/scoped_vector.h" | 13 #include "base/memory/scoped_vector.h" |
14 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/metrics/histogram.h" | 15 #include "base/metrics/histogram.h" |
16 #include "base/prefs/pref_service.h" | 16 #include "base/prefs/pref_service.h" |
17 #include "base/prefs/scoped_user_pref_update.h" | 17 #include "base/prefs/scoped_user_pref_update.h" |
18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/threading/thread.h" | 21 #include "base/threading/thread.h" |
22 #include "base/values.h" | 22 #include "base/values.h" |
23 #include "chrome/browser/chrome_notification_types.h" | 23 #include "chrome/browser/chrome_notification_types.h" |
24 #include "chrome/browser/history/most_visited_tiles_experiment.h" | |
25 #include "chrome/browser/history/page_usage_data.h" | |
26 #include "chrome/browser/history/top_sites.h" | 24 #include "chrome/browser/history/top_sites.h" |
27 #include "chrome/browser/profiles/profile.h" | 25 #include "chrome/browser/profiles/profile.h" |
28 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
29 #include "chrome/browser/ui/browser.h" | 27 #include "chrome/browser/ui/browser.h" |
30 #include "chrome/browser/ui/browser_finder.h" | 28 #include "chrome/browser/ui/browser_finder.h" |
31 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
32 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" | 30 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" |
33 #include "chrome/browser/ui/webui/favicon_source.h" | 31 #include "chrome/browser/ui/webui/favicon_source.h" |
34 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 32 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
35 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | 33 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 } | 135 } |
138 } | 136 } |
139 | 137 |
140 void MostVisitedHandler::SendPagesValue() { | 138 void MostVisitedHandler::SendPagesValue() { |
141 if (pages_value_) { | 139 if (pages_value_) { |
142 Profile* profile = Profile::FromWebUI(web_ui()); | 140 Profile* profile = Profile::FromWebUI(web_ui()); |
143 const base::DictionaryValue* url_blacklist = | 141 const base::DictionaryValue* url_blacklist = |
144 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); | 142 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
145 bool has_blacklisted_urls = !url_blacklist->empty(); | 143 bool has_blacklisted_urls = !url_blacklist->empty(); |
146 history::TopSites* ts = profile->GetTopSites(); | 144 history::TopSites* ts = profile->GetTopSites(); |
147 if (ts) { | 145 if (ts) |
148 has_blacklisted_urls = ts->HasBlacklistedItems(); | 146 has_blacklisted_urls = ts->HasBlacklistedItems(); |
149 | 147 |
150 MaybeRemovePageValues(); | |
151 } | |
152 | |
153 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 148 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
154 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", | 149 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", |
155 *pages_value_, | 150 *pages_value_, |
156 has_blacklisted_urls_value); | 151 has_blacklisted_urls_value); |
157 pages_value_.reset(); | 152 pages_value_.reset(); |
158 } | 153 } |
159 } | 154 } |
160 | 155 |
161 void MostVisitedHandler::StartQueryForMostVisited() { | 156 void MostVisitedHandler::StartQueryForMostVisited() { |
162 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 157 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 void MostVisitedHandler::HandleMostVisitedSelected( | 211 void MostVisitedHandler::HandleMostVisitedSelected( |
217 const base::ListValue* args) { | 212 const base::ListValue* args) { |
218 most_visited_viewed_ = true; | 213 most_visited_viewed_ = true; |
219 } | 214 } |
220 | 215 |
221 void MostVisitedHandler::SetPagesValueFromTopSites( | 216 void MostVisitedHandler::SetPagesValueFromTopSites( |
222 const history::MostVisitedURLList& data) { | 217 const history::MostVisitedURLList& data) { |
223 pages_value_.reset(new base::ListValue); | 218 pages_value_.reset(new base::ListValue); |
224 | 219 |
225 history::MostVisitedURLList top_sites(data); | 220 history::MostVisitedURLList top_sites(data); |
226 history::MostVisitedTilesExperiment::MaybeShuffle(&top_sites); | |
227 | |
228 for (size_t i = 0; i < top_sites.size(); i++) { | 221 for (size_t i = 0; i < top_sites.size(); i++) { |
229 const history::MostVisitedURL& url = top_sites[i]; | 222 const history::MostVisitedURL& url = top_sites[i]; |
230 base::DictionaryValue* page_value = new base::DictionaryValue(); | 223 base::DictionaryValue* page_value = new base::DictionaryValue(); |
231 if (url.url.is_empty()) { | 224 if (url.url.is_empty()) { |
232 page_value->SetBoolean("filler", true); | 225 page_value->SetBoolean("filler", true); |
233 pages_value_->Append(page_value); | 226 pages_value_->Append(page_value); |
234 continue; | 227 continue; |
235 } | 228 } |
236 | 229 |
237 NewTabUI::SetUrlTitleAndDirection(page_value, | 230 NewTabUI::SetUrlTitleAndDirection(page_value, |
(...skipping 24 matching lines...) Expand all Loading... |
262 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 255 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); |
263 if (ts) | 256 if (ts) |
264 ts->AddBlacklistedURL(url); | 257 ts->AddBlacklistedURL(url); |
265 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 258 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
266 } | 259 } |
267 | 260 |
268 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { | 261 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { |
269 return base::MD5String(url); | 262 return base::MD5String(url); |
270 } | 263 } |
271 | 264 |
272 void MostVisitedHandler::MaybeRemovePageValues() { | |
273 if (!history::MostVisitedTilesExperiment::IsDontShowOpenURLsEnabled()) | |
274 return; | |
275 | |
276 TabStripModel* tab_strip_model = chrome::FindBrowserWithWebContents( | |
277 web_ui()->GetWebContents())->tab_strip_model(); | |
278 history::TopSites* top_sites = Profile::FromWebUI(web_ui())->GetTopSites(); | |
279 if (!tab_strip_model || !top_sites) { | |
280 NOTREACHED(); | |
281 return; | |
282 } | |
283 | |
284 std::set<std::string> open_urls; | |
285 chrome::GetOpenUrls(*tab_strip_model, *top_sites, &open_urls); | |
286 history::MostVisitedTilesExperiment::RemovePageValuesMatchingOpenTabs( | |
287 open_urls, | |
288 pages_value_.get()); | |
289 } | |
290 | |
291 // static | 265 // static |
292 void MostVisitedHandler::RegisterProfilePrefs( | 266 void MostVisitedHandler::RegisterProfilePrefs( |
293 user_prefs::PrefRegistrySyncable* registry) { | 267 user_prefs::PrefRegistrySyncable* registry) { |
294 registry->RegisterDictionaryPref( | 268 registry->RegisterDictionaryPref( |
295 prefs::kNtpMostVisitedURLsBlacklist, | 269 prefs::kNtpMostVisitedURLsBlacklist, |
296 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 270 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
297 } | 271 } |
OLD | NEW |