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" | |
24 #include "chrome/browser/history/top_sites.h" | 23 #include "chrome/browser/history/top_sites.h" |
25 #include "chrome/browser/profiles/profile.h" | 24 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 25 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
27 #include "chrome/browser/ui/browser.h" | 26 #include "chrome/browser/ui/browser.h" |
28 #include "chrome/browser/ui/browser_finder.h" | 27 #include "chrome/browser/ui/browser_finder.h" |
29 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 28 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
30 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" | 29 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" |
31 #include "chrome/browser/ui/webui/favicon_source.h" | 30 #include "chrome/browser/ui/webui/favicon_source.h" |
32 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 31 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
33 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | 32 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" |
34 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 33 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
35 #include "chrome/common/pref_names.h" | 34 #include "chrome/common/pref_names.h" |
36 #include "chrome/common/url_constants.h" | 35 #include "chrome/common/url_constants.h" |
37 #include "components/history/core/browser/page_usage_data.h" | 36 #include "components/history/core/browser/page_usage_data.h" |
38 #include "components/pref_registry/pref_registry_syncable.h" | 37 #include "components/pref_registry/pref_registry_syncable.h" |
39 #include "content/public/browser/navigation_controller.h" | 38 #include "content/public/browser/navigation_controller.h" |
40 #include "content/public/browser/navigation_entry.h" | 39 #include "content/public/browser/navigation_entry.h" |
41 #include "content/public/browser/notification_source.h" | |
42 #include "content/public/browser/url_data_source.h" | 40 #include "content/public/browser/url_data_source.h" |
43 #include "content/public/browser/user_metrics.h" | 41 #include "content/public/browser/user_metrics.h" |
44 #include "content/public/browser/web_contents.h" | 42 #include "content/public/browser/web_contents.h" |
45 #include "content/public/browser/web_ui.h" | 43 #include "content/public/browser/web_ui.h" |
46 #include "url/gurl.h" | 44 #include "url/gurl.h" |
47 | 45 |
48 using base::UserMetricsAction; | 46 using base::UserMetricsAction; |
49 | 47 |
50 MostVisitedHandler::MostVisitedHandler() | 48 MostVisitedHandler::MostVisitedHandler() |
51 : got_first_most_visited_request_(false), | 49 : scoped_observer_(this), |
| 50 got_first_most_visited_request_(false), |
52 most_visited_viewed_(false), | 51 most_visited_viewed_(false), |
53 user_action_logged_(false), | 52 user_action_logged_(false), |
54 weak_ptr_factory_(this) { | 53 weak_ptr_factory_(this) { |
55 } | 54 } |
56 | 55 |
57 MostVisitedHandler::~MostVisitedHandler() { | 56 MostVisitedHandler::~MostVisitedHandler() { |
58 if (!user_action_logged_ && most_visited_viewed_) { | 57 if (!user_action_logged_ && most_visited_viewed_) { |
59 const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); | 58 const GURL ntp_url = GURL(chrome::kChromeUINewTabURL); |
60 int action_id = NTP_FOLLOW_ACTION_OTHER; | 59 int action_id = NTP_FOLLOW_ACTION_OTHER; |
61 content::NavigationEntry* entry = | 60 content::NavigationEntry* entry = |
(...skipping 14 matching lines...) Expand all Loading... |
76 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); | 75 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); |
77 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); | 76 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); |
78 | 77 |
79 // Set up our sources for top-sites data. | 78 // Set up our sources for top-sites data. |
80 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); | 79 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); |
81 | 80 |
82 // Register chrome://favicon as a data source for favicons. | 81 // Register chrome://favicon as a data source for favicons. |
83 content::URLDataSource::Add( | 82 content::URLDataSource::Add( |
84 profile, new FaviconSource(profile, FaviconSource::FAVICON)); | 83 profile, new FaviconSource(profile, FaviconSource::FAVICON)); |
85 | 84 |
86 history::TopSites* ts = profile->GetTopSites(); | 85 history::TopSites* top_sites = profile->GetTopSites(); |
87 if (ts) { | 86 if (top_sites) { |
88 // TopSites updates itself after a delay. This is especially noticable when | 87 // TopSites updates itself after a delay. This is especially noticable when |
89 // your profile is empty. Ask TopSites to update itself when we're about to | 88 // your profile is empty. Ask TopSites to update itself when we're about to |
90 // show the new tab page. | 89 // show the new tab page. |
91 ts->SyncWithHistory(); | 90 top_sites->SyncWithHistory(); |
92 | 91 |
93 // Register for notification when TopSites changes so that we can update | 92 // Register as TopSitesObserver so that we can update ourselves when the |
94 // ourself. | 93 // TopSites changes. |
95 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 94 scoped_observer_.Add(top_sites); |
96 content::Source<history::TopSites>(ts)); | |
97 } | 95 } |
98 | 96 |
99 // We pre-emptively make a fetch for the most visited pages so we have the | 97 // We pre-emptively make a fetch for the most visited pages so we have the |
100 // results sooner. | 98 // results sooner. |
101 StartQueryForMostVisited(); | 99 StartQueryForMostVisited(); |
102 | 100 |
103 web_ui()->RegisterMessageCallback("getMostVisited", | 101 web_ui()->RegisterMessageCallback("getMostVisited", |
104 base::Bind(&MostVisitedHandler::HandleGetMostVisited, | 102 base::Bind(&MostVisitedHandler::HandleGetMostVisited, |
105 base::Unretained(this))); | 103 base::Unretained(this))); |
106 | 104 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
236 } | 234 } |
237 | 235 |
238 void MostVisitedHandler::OnMostVisitedUrlsAvailable( | 236 void MostVisitedHandler::OnMostVisitedUrlsAvailable( |
239 const history::MostVisitedURLList& data) { | 237 const history::MostVisitedURLList& data) { |
240 SetPagesValueFromTopSites(data); | 238 SetPagesValueFromTopSites(data); |
241 if (got_first_most_visited_request_) { | 239 if (got_first_most_visited_request_) { |
242 SendPagesValue(); | 240 SendPagesValue(); |
243 } | 241 } |
244 } | 242 } |
245 | 243 |
246 void MostVisitedHandler::Observe(int type, | 244 void MostVisitedHandler::TopSitesLoaded(history::TopSites* top_sites) { |
247 const content::NotificationSource& source, | 245 } |
248 const content::NotificationDetails& details) { | |
249 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | |
250 | 246 |
| 247 void MostVisitedHandler::TopSitesChanged(history::TopSites* top_sites) { |
251 // Most visited urls changed, query again. | 248 // Most visited urls changed, query again. |
252 StartQueryForMostVisited(); | 249 StartQueryForMostVisited(); |
253 } | 250 } |
254 | 251 |
255 void MostVisitedHandler::BlacklistUrl(const GURL& url) { | 252 void MostVisitedHandler::BlacklistUrl(const GURL& url) { |
256 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 253 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); |
257 if (ts) | 254 if (ts) |
258 ts->AddBlacklistedURL(url); | 255 ts->AddBlacklistedURL(url); |
259 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 256 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
260 } | 257 } |
261 | 258 |
262 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { | 259 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { |
263 return base::MD5String(url); | 260 return base::MD5String(url); |
264 } | 261 } |
265 | 262 |
266 // static | 263 // static |
267 void MostVisitedHandler::RegisterProfilePrefs( | 264 void MostVisitedHandler::RegisterProfilePrefs( |
268 user_prefs::PrefRegistrySyncable* registry) { | 265 user_prefs::PrefRegistrySyncable* registry) { |
269 registry->RegisterDictionaryPref( | 266 registry->RegisterDictionaryPref( |
270 prefs::kNtpMostVisitedURLsBlacklist, | 267 prefs::kNtpMostVisitedURLsBlacklist, |
271 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 268 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
272 } | 269 } |
OLD | NEW |