| 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.h" |
| 25 #include "chrome/browser/history/top_sites_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 scoped_refptr<history::TopSites> ts = TopSitesFactory::GetForProfile(profile); |
| 87 if (ts) { | 88 if (ts) { |
| 88 // TopSites updates itself after a delay. This is especially noticable when | 89 // 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 | 90 // your profile is empty. Ask TopSites to update itself when we're about to |
| 90 // show the new tab page. | 91 // show the new tab page. |
| 91 ts->SyncWithHistory(); | 92 ts->SyncWithHistory(); |
| 92 | 93 |
| 93 // Register for notification when TopSites changes so that we can update | 94 // Register for notification when TopSites changes so that we can update |
| 94 // ourself. | 95 // ourself. |
| 95 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, | 96 registrar_.Add(this, chrome::NOTIFICATION_TOP_SITES_CHANGED, |
| 96 content::Source<history::TopSites>(ts)); | 97 content::Source<history::TopSites>(ts)); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 StartQueryForMostVisited(); | 132 StartQueryForMostVisited(); |
| 132 } | 133 } |
| 133 } | 134 } |
| 134 | 135 |
| 135 void MostVisitedHandler::SendPagesValue() { | 136 void MostVisitedHandler::SendPagesValue() { |
| 136 if (pages_value_) { | 137 if (pages_value_) { |
| 137 Profile* profile = Profile::FromWebUI(web_ui()); | 138 Profile* profile = Profile::FromWebUI(web_ui()); |
| 138 const base::DictionaryValue* url_blacklist = | 139 const base::DictionaryValue* url_blacklist = |
| 139 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); | 140 profile->GetPrefs()->GetDictionary(prefs::kNtpMostVisitedURLsBlacklist); |
| 140 bool has_blacklisted_urls = !url_blacklist->empty(); | 141 bool has_blacklisted_urls = !url_blacklist->empty(); |
| 141 history::TopSites* ts = profile->GetTopSites(); | 142 scoped_refptr<history::TopSites> ts = |
| 143 TopSitesFactory::GetForProfile(profile); |
| 142 if (ts) | 144 if (ts) |
| 143 has_blacklisted_urls = ts->HasBlacklistedItems(); | 145 has_blacklisted_urls = ts->HasBlacklistedItems(); |
| 144 | 146 |
| 145 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 147 base::FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
| 146 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", | 148 web_ui()->CallJavascriptFunction("ntp.setMostVisitedPages", |
| 147 *pages_value_, | 149 *pages_value_, |
| 148 has_blacklisted_urls_value); | 150 has_blacklisted_urls_value); |
| 149 pages_value_.reset(); | 151 pages_value_.reset(); |
| 150 } | 152 } |
| 151 } | 153 } |
| 152 | 154 |
| 153 void MostVisitedHandler::StartQueryForMostVisited() { | 155 void MostVisitedHandler::StartQueryForMostVisited() { |
| 154 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 156 scoped_refptr<history::TopSites> ts = |
| 157 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 155 if (ts) { | 158 if (ts) { |
| 156 ts->GetMostVisitedURLs( | 159 ts->GetMostVisitedURLs( |
| 157 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, | 160 base::Bind(&MostVisitedHandler::OnMostVisitedUrlsAvailable, |
| 158 weak_ptr_factory_.GetWeakPtr()), false); | 161 weak_ptr_factory_.GetWeakPtr()), false); |
| 159 } | 162 } |
| 160 } | 163 } |
| 161 | 164 |
| 162 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { | 165 void MostVisitedHandler::HandleBlacklistUrl(const base::ListValue* args) { |
| 163 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); | 166 std::string url = base::UTF16ToUTF8(ExtractStringValue(args)); |
| 164 BlacklistUrl(GURL(url)); | 167 BlacklistUrl(GURL(url)); |
| 165 } | 168 } |
| 166 | 169 |
| 167 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( | 170 void MostVisitedHandler::HandleRemoveUrlsFromBlacklist( |
| 168 const base::ListValue* args) { | 171 const base::ListValue* args) { |
| 169 DCHECK(args->GetSize() != 0); | 172 DCHECK(args->GetSize() != 0); |
| 170 | 173 |
| 171 for (base::ListValue::const_iterator iter = args->begin(); | 174 for (base::ListValue::const_iterator iter = args->begin(); |
| 172 iter != args->end(); ++iter) { | 175 iter != args->end(); ++iter) { |
| 173 std::string url; | 176 std::string url; |
| 174 bool r = (*iter)->GetAsString(&url); | 177 bool r = (*iter)->GetAsString(&url); |
| 175 if (!r) { | 178 if (!r) { |
| 176 NOTREACHED(); | 179 NOTREACHED(); |
| 177 return; | 180 return; |
| 178 } | 181 } |
| 179 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); | 182 content::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); |
| 180 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 183 scoped_refptr<history::TopSites> ts = |
| 184 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 181 if (ts) | 185 if (ts) |
| 182 ts->RemoveBlacklistedURL(GURL(url)); | 186 ts->RemoveBlacklistedURL(GURL(url)); |
| 183 } | 187 } |
| 184 } | 188 } |
| 185 | 189 |
| 186 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { | 190 void MostVisitedHandler::HandleClearBlacklist(const base::ListValue* args) { |
| 187 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 191 content::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); |
| 188 | 192 |
| 189 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 193 scoped_refptr<history::TopSites> ts = |
| 194 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 190 if (ts) | 195 if (ts) |
| 191 ts->ClearBlacklistedURLs(); | 196 ts->ClearBlacklistedURLs(); |
| 192 } | 197 } |
| 193 | 198 |
| 194 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { | 199 void MostVisitedHandler::HandleMostVisitedAction(const base::ListValue* args) { |
| 195 DCHECK(args); | 200 DCHECK(args); |
| 196 | 201 |
| 197 double action_id; | 202 double action_id; |
| 198 if (!args->GetDouble(0, &action_id)) | 203 if (!args->GetDouble(0, &action_id)) |
| 199 NOTREACHED(); | 204 NOTREACHED(); |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 void MostVisitedHandler::Observe(int type, | 251 void MostVisitedHandler::Observe(int type, |
| 247 const content::NotificationSource& source, | 252 const content::NotificationSource& source, |
| 248 const content::NotificationDetails& details) { | 253 const content::NotificationDetails& details) { |
| 249 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 254 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
| 250 | 255 |
| 251 // Most visited urls changed, query again. | 256 // Most visited urls changed, query again. |
| 252 StartQueryForMostVisited(); | 257 StartQueryForMostVisited(); |
| 253 } | 258 } |
| 254 | 259 |
| 255 void MostVisitedHandler::BlacklistUrl(const GURL& url) { | 260 void MostVisitedHandler::BlacklistUrl(const GURL& url) { |
| 256 history::TopSites* ts = Profile::FromWebUI(web_ui())->GetTopSites(); | 261 scoped_refptr<history::TopSites> ts = |
| 262 TopSitesFactory::GetForProfile(Profile::FromWebUI(web_ui())); |
| 257 if (ts) | 263 if (ts) |
| 258 ts->AddBlacklistedURL(url); | 264 ts->AddBlacklistedURL(url); |
| 259 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 265 content::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
| 260 } | 266 } |
| 261 | 267 |
| 262 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { | 268 std::string MostVisitedHandler::GetDictionaryKeyForUrl(const std::string& url) { |
| 263 return base::MD5String(url); | 269 return base::MD5String(url); |
| 264 } | 270 } |
| 265 | 271 |
| 266 // static | 272 // static |
| 267 void MostVisitedHandler::RegisterProfilePrefs( | 273 void MostVisitedHandler::RegisterProfilePrefs( |
| 268 user_prefs::PrefRegistrySyncable* registry) { | 274 user_prefs::PrefRegistrySyncable* registry) { |
| 269 registry->RegisterDictionaryPref( | 275 registry->RegisterDictionaryPref( |
| 270 prefs::kNtpMostVisitedURLsBlacklist, | 276 prefs::kNtpMostVisitedURLsBlacklist, |
| 271 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 277 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
| 272 } | 278 } |
| OLD | NEW |