| 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/top_sites.h" | 24 #include "chrome/browser/history/top_sites_provider.h" |
| 25 #include "chrome/browser/history/top_sites_service_factory.h" |
| 25 #include "chrome/browser/profiles/profile.h" | 26 #include "chrome/browser/profiles/profile.h" |
| 26 #include "chrome/browser/thumbnails/thumbnail_list_source.h" | 27 #include "chrome/browser/thumbnails/thumbnail_list_source.h" |
| 27 #include "chrome/browser/ui/browser.h" | 28 #include "chrome/browser/ui/browser.h" |
| 28 #include "chrome/browser/ui/browser_finder.h" | 29 #include "chrome/browser/ui/browser_finder.h" |
| 29 #include "chrome/browser/ui/tabs/tab_strip_model.h" | 30 #include "chrome/browser/ui/tabs/tab_strip_model.h" |
| 30 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" | 31 #include "chrome/browser/ui/tabs/tab_strip_model_utils.h" |
| 31 #include "chrome/browser/ui/webui/favicon_source.h" | 32 #include "chrome/browser/ui/webui/favicon_source.h" |
| 32 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 33 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 33 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" | 34 #include "chrome/browser/ui/webui/ntp/ntp_stats.h" |
| 34 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 35 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 76 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); | 77 content::URLDataSource::Add(profile, new ThumbnailSource(profile, false)); |
| 77 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); | 78 content::URLDataSource::Add(profile, new ThumbnailSource(profile, true)); |
| 78 | 79 |
| 79 // Set up our sources for top-sites data. | 80 // Set up our sources for top-sites data. |
| 80 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); | 81 content::URLDataSource::Add(profile, new ThumbnailListSource(profile)); |
| 81 | 82 |
| 82 // Register chrome://favicon as a data source for favicons. | 83 // Register chrome://favicon as a data source for favicons. |
| 83 content::URLDataSource::Add( | 84 content::URLDataSource::Add( |
| 84 profile, new FaviconSource(profile, FaviconSource::FAVICON)); | 85 profile, new FaviconSource(profile, FaviconSource::FAVICON)); |
| 85 | 86 |
| 86 history::TopSites* ts = profile->GetTopSites(); | 87 history::TopSitesProvider* ts = |
| 88 TopSitesServiceFactory::GetForProfile(profile); |
| 87 if (ts) { | 89 if (ts) { |
| 88 // TopSites updates itself after a delay. This is especially noticable when | 90 // 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 | 91 // your profile is empty. Ask TopSites to update itself when we're about to |
| 90 // show the new tab page. | 92 // show the new tab page. |
| 91 ts->SyncWithHistory(); | 93 ts->SyncWithHistory(); |
| 92 | 94 |
| 93 // Register for notification when TopSites changes so that we can update | 95 // Register for notification when TopSites changes so that we can update |
| 94 // ourself. | 96 // ourself. |
| 95 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 97 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 96 content::Source<history::TopSites>(ts)); | 98 content::Source<history::TopSitesProvider>(ts)); |
| 97 } | 99 } |
| 98 | 100 |
| 99 // We pre-emptively make a fetch for the most visited pages so we have the | 101 // We pre-emptively make a fetch for the most visited pages so we have the |
| 100 // results sooner. | 102 // results sooner. |
| 101 StartQueryForMostVisited(); | 103 StartQueryForMostVisited(); |
| 102 | 104 |
| 103 web_ui()->RegisterMessageCallback("getMostVisited", | 105 web_ui()->RegisterMessageCallback("getMostVisited", |
| 104 base::Bind(&MostVisitedHandler::HandleGetMostVisited, | 106 base::Bind(&MostVisitedHandler::HandleGetMostVisited, |
| 105 base::Unretained(this))); | 107 base::Unretained(this))); |
| 106 | 108 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 131 StartQueryForMostVisited(); | 133 StartQueryForMostVisited(); |
| 132 } | 134 } |
| 133 } | 135 } |
| 134 | 136 |
| 135 void MostVisitedHandler::SendPagesValue() { | 137 void MostVisitedHandler::SendPagesValue() { |
| 136 if (pages_value_) { | 138 if (pages_value_) { |
| 137 Profile* profile = Profile::FromWebUI(web_ui()); | 139 Profile* profile = Profile::FromWebUI(web_ui()); |
| 138 const base::DictionaryValue* url_blacklist = | 140 const base::DictionaryValue* url_blacklist = |
| 139 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); | 141 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
| 140 bool has_blacklisted_urls = !url_blacklist->empty(); | 142 bool has_blacklisted_urls = !url_blacklist->empty(); |
| 141 history::TopSites* ts = profile->GetTopSites(); | 143 history::TopSitesProvider* ts = |
| 144 TopSitesServiceFactory::GetForProfile(profile); |
| 142 if (ts) | 145 if (ts) |
| 143 has_blacklisted_urls = ts->HasBlacklistedItems(); | 146 has_blacklisted_urls = ts->HasBlacklistedItems(); |
| 144 | 147 |
| 145 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 148 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
| 146 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", | 149 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", |
| 147 *pages_value_, | 150 *pages_value_, |
| 148 has_blacklisted_urls_value); | 151 has_blacklisted_urls_value); |
| 149 pages_value_.reset(); | 152 pages_value_.reset(); |
| 150 } | 153 } |
| 151 } | 154 } |
| 152 | 155 |
| 153 void MostVisitedHandler::StartQueryForMostVisited() { | 156 void MostVisitedHandler::StartQueryForMostVisited() { |
| 154 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 157 history::TopSitesProvider* ts = |
| 158 TopSitesServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 155 if (ts) { | 159 if (ts) { |
| 156 ts->GetMostVisitedURLs( | 160 ts->GetMostVisitedURLs( |
| 157 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, | 161 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, |
| 158 weak_ptr_factory_.GetWeakPtr()), false); | 162 weak_ptr_factory_.GetWeakPtr()), false); |
| 159 } | 163 } |
| 160 } | 164 } |
| 161 | 165 |
| 162 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { | 166 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { |
| 163 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); | 167 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); |
| 164 BlacklistUrl(GURL(url)); | 168 BlacklistUrl(GURL(url)); |
| 165 } | 169 } |
| 166 | 170 |
| 167 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( | 171 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( |
| 168 const base::ListValue* args) { | 172 const base::ListValue* args) { |
| 169 DCHECK(args->GetSize() != 0); | 173 DCHECK(args->GetSize() != 0); |
| 170 | 174 |
| 171 for (base::ListValue::const_iterator iter = args->begin(); | 175 for (base::ListValue::const_iterator iter = args->begin(); |
| 172 iter != args->end(); ++iter) { | 176 iter != args->end(); ++iter) { |
| 173 std::string url; | 177 std::string url; |
| 174 bool r = (*iter)->GetAsString(&url); | 178 bool r = (*iter)->GetAsString(&url); |
| 175 if (!r) { | 179 if (!r) { |
| 176 NOTREACHED(); | 180 NOTREACHED(); |
| 177 return; | 181 return; |
| 178 } | 182 } |
| 179 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); | 183 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); |
| 180 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 184 history::TopSitesProvider* ts = |
| 185 TopSitesServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 181 if (ts) | 186 if (ts) |
| 182 ts->RemoveBlacklistedURL(GURL(url)); | 187 ts->RemoveBlacklistedURL(GURL(url)); |
| 183 } | 188 } |
| 184 } | 189 } |
| 185 | 190 |
| 186 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { | 191 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { |
| 187 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 192 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); |
| 188 | 193 |
| 189 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 194 history::TopSitesProvider* ts = |
| 195 TopSitesServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 190 if (ts) | 196 if (ts) |
| 191 ts->ClearBlacklistedURLs(); | 197 ts->ClearBlacklistedURLs(); |
| 192 } | 198 } |
| 193 | 199 |
| 194 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { | 200 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { |
| 195 DCHECK(args); | 201 DCHECK(args); |
| 196 | 202 |
| 197 double action_id; | 203 double action_id; |
| 198 if (!args->GetDouble(0, &action_id)) | 204 if (!args->GetDouble(0, &action_id)) |
| 199 NOTREACHED(); | 205 NOTREACHED(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 void MostVisitedHandler::Observe(int type, | 252 void MostVisitedHandler::Observe(int type, |
| 247 const content::NotificationSource& source, | 253 const content::NotificationSource& source, |
| 248 const content::NotificationDetails& details) { | 254 const content::NotificationDetails& details) { |
| 249 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 255 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
| 250 | 256 |
| 251 // Most visited urls changed, query again. | 257 // Most visited urls changed, query again. |
| 252 StartQueryForMostVisited(); | 258 StartQueryForMostVisited(); |
| 253 } | 259 } |
| 254 | 260 |
| 255 void MostVisitedHandler::BlacklistUrl(const GURL& url) { | 261 void MostVisitedHandler::BlacklistUrl(const GURL& url) { |
| 256 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 262 history::TopSitesProvider* ts = |
| 263 TopSitesServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 257 if (ts) | 264 if (ts) |
| 258 ts->AddBlacklistedURL(url); | 265 ts->AddBlacklistedURL(url); |
| 259 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 266 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
| 260 } | 267 } |
| 261 | 268 |
| 262 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { | 269 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { |
| 263 return base::MD5String(url); | 270 return base::MD5String(url); |
| 264 } | 271 } |
| 265 | 272 |
| 266 // static | 273 // static |
| 267 void MostVisitedHandler::RegisterProfilePrefs( | 274 void MostVisitedHandler::RegisterProfilePrefs( |
| 268 user_prefs::PrefRegistrySyncable* registry) { | 275 user_prefs::PrefRegistrySyncable* registry) { |
| 269 registry->RegisterDictionaryPref( | 276 registry->RegisterDictionaryPref( |
| 270 prefs::kNtpMostVisitedURLsBlacklist, | 277 prefs::kNtpMostVisitedURLsBlacklist, |
| 271 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 278 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 272 } | 279 } |
| OLD | NEW |