| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/callback.h" | 9 #include "base/callback.h" |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 22 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
| 23 #include "chrome/browser/profiles/profile.h" | 23 #include "chrome/browser/profiles/profile.h" |
| 24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" | 24 #include "chrome/browser/ui/webui/chrome_url_data_manager.h" |
| 25 #include "chrome/browser/ui/webui/favicon_source.h" | 25 #include "chrome/browser/ui/webui/favicon_source.h" |
| 26 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" | 26 #include "chrome/browser/ui/webui/ntp/new_tab_ui.h" |
| 27 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" | 27 #include "chrome/browser/ui/webui/ntp/thumbnail_source.h" |
| 28 #include "chrome/common/chrome_notification_types.h" | 28 #include "chrome/common/chrome_notification_types.h" |
| 29 #include "chrome/common/pref_names.h" | 29 #include "chrome/common/pref_names.h" |
| 30 #include "chrome/common/url_constants.h" | 30 #include "chrome/common/url_constants.h" |
| 31 #include "content/browser/browser_thread.h" | 31 #include "content/browser/browser_thread.h" |
| 32 #include "content/browser/tab_contents/tab_contents.h" |
| 32 #include "content/browser/user_metrics.h" | 33 #include "content/browser/user_metrics.h" |
| 33 #include "content/common/notification_source.h" | 34 #include "content/common/notification_source.h" |
| 34 #include "googleurl/src/gurl.h" | 35 #include "googleurl/src/gurl.h" |
| 35 #include "grit/chromium_strings.h" | 36 #include "grit/chromium_strings.h" |
| 36 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
| 37 #include "grit/locale_settings.h" | 38 #include "grit/locale_settings.h" |
| 38 #include "ui/base/l10n/l10n_util.h" | 39 #include "ui/base/l10n/l10n_util.h" |
| 39 | 40 |
| 40 // This struct is used when getting the pre-populated pages in case the user | 41 // This struct is used when getting the pre-populated pages in case the user |
| 41 // hasn't filled up his most visited pages. | 42 // hasn't filled up his most visited pages. |
| 42 struct MostVisitedHandler::MostVisitedPage { | 43 struct MostVisitedHandler::MostVisitedPage { |
| 43 string16 title; | 44 string16 title; |
| 44 GURL url; | 45 GURL url; |
| 45 GURL thumbnail_url; | 46 GURL thumbnail_url; |
| 46 GURL favicon_url; | 47 GURL favicon_url; |
| 47 }; | 48 }; |
| 48 | 49 |
| 49 MostVisitedHandler::MostVisitedHandler() | 50 MostVisitedHandler::MostVisitedHandler() |
| 50 : got_first_most_visited_request_(false) { | 51 : got_first_most_visited_request_(false) { |
| 51 } | 52 } |
| 52 | 53 |
| 53 MostVisitedHandler::~MostVisitedHandler() { | 54 MostVisitedHandler::~MostVisitedHandler() { |
| 54 } | 55 } |
| 55 | 56 |
| 56 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { | 57 WebUIMessageHandler* MostVisitedHandler::Attach(WebUI* web_ui) { |
| 57 Profile* profile = web_ui->GetProfile(); | 58 Profile* profile = |
| 59 Profile::FromBrowserContext(web_ui->tab_contents()->browser_context()); |
| 58 // Set up our sources for thumbnail and favicon data. | 60 // Set up our sources for thumbnail and favicon data. |
| 59 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); | 61 ThumbnailSource* thumbnail_src = new ThumbnailSource(profile); |
| 60 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); | 62 profile->GetChromeURLDataManager()->AddDataSource(thumbnail_src); |
| 61 | 63 |
| 62 profile->GetChromeURLDataManager()->AddDataSource( | 64 profile->GetChromeURLDataManager()->AddDataSource( |
| 63 new FaviconSource(profile, FaviconSource::FAVICON)); | 65 new FaviconSource(profile, FaviconSource::FAVICON)); |
| 64 | 66 |
| 65 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); | 67 WebUIMessageHandler* result = WebUIMessageHandler::Attach(web_ui); |
| 66 | 68 |
| 67 history::TopSites* ts = profile->GetTopSites(); | 69 history::TopSites* ts = profile->GetTopSites(); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 // If our initial data is already here, return it. | 109 // If our initial data is already here, return it. |
| 108 SendPagesValue(); | 110 SendPagesValue(); |
| 109 got_first_most_visited_request_ = true; | 111 got_first_most_visited_request_ = true; |
| 110 } else { | 112 } else { |
| 111 StartQueryForMostVisited(); | 113 StartQueryForMostVisited(); |
| 112 } | 114 } |
| 113 } | 115 } |
| 114 | 116 |
| 115 void MostVisitedHandler::SendPagesValue() { | 117 void MostVisitedHandler::SendPagesValue() { |
| 116 if (pages_value_.get()) { | 118 if (pages_value_.get()) { |
| 117 Profile* profile = web_ui_->GetProfile(); | 119 Profile* profile = |
| 120 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 118 const DictionaryValue* url_blacklist = | 121 const DictionaryValue* url_blacklist = |
| 119 profile->GetPrefs()->GetDictionary(prefs::kNTPMostVisitedURLsBlacklist); | 122 profile->GetPrefs()->GetDictionary(prefs::kNTPMostVisitedURLsBlacklist); |
| 120 bool has_blacklisted_urls = !url_blacklist->empty(); | 123 bool has_blacklisted_urls = !url_blacklist->empty(); |
| 121 history::TopSites* ts = profile->GetTopSites(); | 124 history::TopSites* ts = profile->GetTopSites(); |
| 122 if (ts) | 125 if (ts) |
| 123 has_blacklisted_urls = ts->HasBlacklistedItems(); | 126 has_blacklisted_urls = ts->HasBlacklistedItems(); |
| 124 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); | 127 FundamentalValue has_blacklisted_urls_value(has_blacklisted_urls); |
| 125 web_ui_->CallJavascriptFunction("setMostVisitedPages", | 128 web_ui_->CallJavascriptFunction("setMostVisitedPages", |
| 126 *(pages_value_.get()), | 129 *(pages_value_.get()), |
| 127 has_blacklisted_urls_value); | 130 has_blacklisted_urls_value); |
| 128 pages_value_.reset(); | 131 pages_value_.reset(); |
| 129 } | 132 } |
| 130 } | 133 } |
| 131 | 134 |
| 132 void MostVisitedHandler::StartQueryForMostVisited() { | 135 void MostVisitedHandler::StartQueryForMostVisited() { |
| 133 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 136 Profile* profile = |
| 137 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 138 history::TopSites* ts = profile->GetTopSites(); |
| 134 if (ts) { | 139 if (ts) { |
| 135 ts->GetMostVisitedURLs( | 140 ts->GetMostVisitedURLs( |
| 136 &topsites_consumer_, | 141 &topsites_consumer_, |
| 137 NewCallback(this, &MostVisitedHandler::OnMostVisitedURLsAvailable)); | 142 NewCallback(this, &MostVisitedHandler::OnMostVisitedURLsAvailable)); |
| 138 } | 143 } |
| 139 } | 144 } |
| 140 | 145 |
| 141 void MostVisitedHandler::HandleBlacklistURL(const ListValue* args) { | 146 void MostVisitedHandler::HandleBlacklistURL(const ListValue* args) { |
| 142 std::string url = UTF16ToUTF8(ExtractStringValue(args)); | 147 std::string url = UTF16ToUTF8(ExtractStringValue(args)); |
| 143 BlacklistURL(GURL(url)); | 148 BlacklistURL(GURL(url)); |
| 144 } | 149 } |
| 145 | 150 |
| 146 void MostVisitedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { | 151 void MostVisitedHandler::HandleRemoveURLsFromBlacklist(const ListValue* args) { |
| 147 DCHECK(args->GetSize() != 0); | 152 DCHECK(args->GetSize() != 0); |
| 148 | 153 |
| 149 for (ListValue::const_iterator iter = args->begin(); | 154 for (ListValue::const_iterator iter = args->begin(); |
| 150 iter != args->end(); ++iter) { | 155 iter != args->end(); ++iter) { |
| 151 std::string url; | 156 std::string url; |
| 152 bool r = (*iter)->GetAsString(&url); | 157 bool r = (*iter)->GetAsString(&url); |
| 153 if (!r) { | 158 if (!r) { |
| 154 NOTREACHED(); | 159 NOTREACHED(); |
| 155 return; | 160 return; |
| 156 } | 161 } |
| 157 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); | 162 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlRemoved")); |
| 158 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 163 Profile* profile = |
| 164 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 165 history::TopSites* ts = profile->GetTopSites(); |
| 159 if (ts) | 166 if (ts) |
| 160 ts->RemoveBlacklistedURL(GURL(url)); | 167 ts->RemoveBlacklistedURL(GURL(url)); |
| 161 } | 168 } |
| 162 } | 169 } |
| 163 | 170 |
| 164 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { | 171 void MostVisitedHandler::HandleClearBlacklist(const ListValue* args) { |
| 165 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); | 172 UserMetrics::RecordAction(UserMetricsAction("MostVisited_BlacklistCleared")); |
| 166 | 173 |
| 167 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 174 Profile* profile = |
| 175 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 176 history::TopSites* ts = profile->GetTopSites(); |
| 168 if (ts) | 177 if (ts) |
| 169 ts->ClearBlacklistedURLs(); | 178 ts->ClearBlacklistedURLs(); |
| 170 } | 179 } |
| 171 | 180 |
| 172 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { | 181 void MostVisitedHandler::HandleAddPinnedURL(const ListValue* args) { |
| 173 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; | 182 DCHECK_EQ(5U, args->GetSize()) << "Wrong number of params to addPinnedURL"; |
| 174 MostVisitedPage mvp; | 183 MostVisitedPage mvp; |
| 175 std::string tmp_string; | 184 std::string tmp_string; |
| 176 string16 tmp_string16; | 185 string16 tmp_string16; |
| 177 int index; | 186 int index; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 197 mvp.thumbnail_url = GURL(tmp_string); | 206 mvp.thumbnail_url = GURL(tmp_string); |
| 198 | 207 |
| 199 r = args->GetString(4, &tmp_string); | 208 r = args->GetString(4, &tmp_string); |
| 200 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; | 209 DCHECK(r) << "Missing index in addPinnedURL from the NTP Most Visited."; |
| 201 base::StringToInt(tmp_string, &index); | 210 base::StringToInt(tmp_string, &index); |
| 202 | 211 |
| 203 AddPinnedURL(mvp, index); | 212 AddPinnedURL(mvp, index); |
| 204 } | 213 } |
| 205 | 214 |
| 206 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { | 215 void MostVisitedHandler::AddPinnedURL(const MostVisitedPage& page, int index) { |
| 207 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 216 Profile* profile = |
| 217 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 218 history::TopSites* ts = profile->GetTopSites(); |
| 208 if (ts) | 219 if (ts) |
| 209 ts->AddPinnedURL(page.url, index); | 220 ts->AddPinnedURL(page.url, index); |
| 210 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlPinned")); | 221 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlPinned")); |
| 211 } | 222 } |
| 212 | 223 |
| 213 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { | 224 void MostVisitedHandler::HandleRemovePinnedURL(const ListValue* args) { |
| 214 std::string url = UTF16ToUTF8(ExtractStringValue(args)); | 225 std::string url = UTF16ToUTF8(ExtractStringValue(args)); |
| 215 RemovePinnedURL(GURL(url)); | 226 RemovePinnedURL(GURL(url)); |
| 216 } | 227 } |
| 217 | 228 |
| 218 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { | 229 void MostVisitedHandler::RemovePinnedURL(const GURL& url) { |
| 219 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 230 Profile* profile = |
| 231 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 232 history::TopSites* ts = profile->GetTopSites(); |
| 220 if (ts) | 233 if (ts) |
| 221 ts->RemovePinnedURL(url); | 234 ts->RemovePinnedURL(url); |
| 222 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlUnpinned")); | 235 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlUnpinned")); |
| 223 } | 236 } |
| 224 | 237 |
| 225 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, | 238 bool MostVisitedHandler::GetPinnedURLAtIndex(int index, |
| 226 MostVisitedPage* page) { | 239 MostVisitedPage* page) { |
| 227 // This iterates over all the pinned URLs. It might seem like it is worth | 240 // This iterates over all the pinned URLs. It might seem like it is worth |
| 228 // having a map from the index to the item but the number of items is limited | 241 // having a map from the index to the item but the number of items is limited |
| 229 // to the number of items the most visited section is showing on the NTP so | 242 // to the number of items the most visited section is showing on the NTP so |
| 230 // this will be fast enough for now. | 243 // this will be fast enough for now. |
| 231 PrefService* prefs = web_ui_->GetProfile()->GetPrefs(); | 244 Profile* profile = |
| 245 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 246 PrefService* prefs = profile->GetPrefs(); |
| 232 const DictionaryValue* pinned_urls = | 247 const DictionaryValue* pinned_urls = |
| 233 prefs->GetDictionary(prefs::kNTPMostVisitedPinnedURLs); | 248 prefs->GetDictionary(prefs::kNTPMostVisitedPinnedURLs); |
| 234 for (DictionaryValue::key_iterator it = pinned_urls->begin_keys(); | 249 for (DictionaryValue::key_iterator it = pinned_urls->begin_keys(); |
| 235 it != pinned_urls->end_keys(); ++it) { | 250 it != pinned_urls->end_keys(); ++it) { |
| 236 Value* value; | 251 Value* value; |
| 237 if (pinned_urls->GetWithoutPathExpansion(*it, &value)) { | 252 if (pinned_urls->GetWithoutPathExpansion(*it, &value)) { |
| 238 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { | 253 if (!value->IsType(DictionaryValue::TYPE_DICTIONARY)) { |
| 239 // Moved on to TopSites and now going back. | 254 // Moved on to TopSites and now going back. |
| 240 DictionaryPrefUpdate update(prefs, prefs::kNTPMostVisitedPinnedURLs); | 255 DictionaryPrefUpdate update(prefs, prefs::kNTPMostVisitedPinnedURLs); |
| 241 update.Get()->Clear(); | 256 update.Get()->Clear(); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 290 page_value->SetString("thumbnailUrl", | 305 page_value->SetString("thumbnailUrl", |
| 291 "chrome://theme/IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL"); | 306 "chrome://theme/IDR_NEWTAB_CHROME_WELCOME_PAGE_THUMBNAIL"); |
| 292 page_value->SetString("faviconDominantColor", "rgb(0, 147, 60)"); | 307 page_value->SetString("faviconDominantColor", "rgb(0, 147, 60)"); |
| 293 } else if (url.url.spec() == | 308 } else if (url.url.spec() == |
| 294 l10n_util::GetStringUTF8(IDS_THEMES_GALLERY_URL)) { | 309 l10n_util::GetStringUTF8(IDS_THEMES_GALLERY_URL)) { |
| 295 page_value->SetString("thumbnailUrl", | 310 page_value->SetString("thumbnailUrl", |
| 296 "chrome://theme/IDR_NEWTAB_THEMES_GALLERY_THUMBNAIL"); | 311 "chrome://theme/IDR_NEWTAB_THEMES_GALLERY_THUMBNAIL"); |
| 297 page_value->SetString("faviconDominantColor", "rgb(63, 132, 197)"); | 312 page_value->SetString("faviconDominantColor", "rgb(63, 132, 197)"); |
| 298 } | 313 } |
| 299 | 314 |
| 300 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 315 Profile* profile = |
| 316 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 317 history::TopSites* ts = profile->GetTopSites(); |
| 301 if (ts && ts->IsURLPinned(url.url)) | 318 if (ts && ts->IsURLPinned(url.url)) |
| 302 page_value->SetBoolean("pinned", true); | 319 page_value->SetBoolean("pinned", true); |
| 303 pages_value_->Append(page_value); | 320 pages_value_->Append(page_value); |
| 304 } | 321 } |
| 305 } | 322 } |
| 306 | 323 |
| 307 void MostVisitedHandler::OnMostVisitedURLsAvailable( | 324 void MostVisitedHandler::OnMostVisitedURLsAvailable( |
| 308 const history::MostVisitedURLList& data) { | 325 const history::MostVisitedURLList& data) { |
| 309 SetPagesValueFromTopSites(data); | 326 SetPagesValueFromTopSites(data); |
| 310 if (got_first_most_visited_request_) { | 327 if (got_first_most_visited_request_) { |
| 311 SendPagesValue(); | 328 SendPagesValue(); |
| 312 } | 329 } |
| 313 } | 330 } |
| 314 | 331 |
| 315 void MostVisitedHandler::Observe(int type, | 332 void MostVisitedHandler::Observe(int type, |
| 316 const NotificationSource& source, | 333 const NotificationSource& source, |
| 317 const NotificationDetails& details) { | 334 const NotificationDetails& details) { |
| 318 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); | 335 DCHECK_EQ(type, chrome::NOTIFICATION_TOP_SITES_CHANGED); |
| 319 | 336 |
| 320 // Most visited urls changed, query again. | 337 // Most visited urls changed, query again. |
| 321 StartQueryForMostVisited(); | 338 StartQueryForMostVisited(); |
| 322 } | 339 } |
| 323 | 340 |
| 324 void MostVisitedHandler::BlacklistURL(const GURL& url) { | 341 void MostVisitedHandler::BlacklistURL(const GURL& url) { |
| 325 history::TopSites* ts = web_ui_->GetProfile()->GetTopSites(); | 342 Profile* profile = |
| 343 Profile::FromBrowserContext(web_ui_->tab_contents()->browser_context()); |
| 344 history::TopSites* ts = profile->GetTopSites(); |
| 326 if (ts) | 345 if (ts) |
| 327 ts->AddBlacklistedURL(url); | 346 ts->AddBlacklistedURL(url); |
| 328 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); | 347 UserMetrics::RecordAction(UserMetricsAction("MostVisited_UrlBlacklisted")); |
| 329 } | 348 } |
| 330 | 349 |
| 331 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { | 350 std::string MostVisitedHandler::GetDictionaryKeyForURL(const std::string& url) { |
| 332 return base::MD5String(url); | 351 return base::MD5String(url); |
| 333 } | 352 } |
| 334 | 353 |
| 335 // static | 354 // static |
| 336 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 355 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { |
| 337 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist, | 356 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist, |
| 338 PrefService::UNSYNCABLE_PREF); | 357 PrefService::UNSYNCABLE_PREF); |
| 339 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs, | 358 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs, |
| 340 PrefService::UNSYNCABLE_PREF); | 359 PrefService::UNSYNCABLE_PREF); |
| 341 } | 360 } |
| OLD | NEW |