| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/most_visited_handler.h" | 5 #include "chrome/browser/dom_ui/most_visited_handler.h" |
| 6 | 6 |
| 7 #include <set> | 7 #include <set> |
| 8 | 8 |
| 9 #include "app/l10n_util.h" | 9 #include "app/l10n_util.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.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/singleton.h" | 13 #include "base/singleton.h" |
| 14 #include "base/scoped_vector.h" | 14 #include "base/scoped_vector.h" |
| 15 #include "base/utf_string_conversions.h" | 15 #include "base/utf_string_conversions.h" |
| 16 #include "base/thread.h" | 16 #include "base/thread.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "chrome/browser/chrome_thread.h" | 18 #include "chrome/browser/chrome_thread.h" |
| 19 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" | 19 #include "chrome/browser/dom_ui/chrome_url_data_manager.h" |
| 20 #include "chrome/browser/dom_ui/dom_ui_favicon_source.h" | 20 #include "chrome/browser/dom_ui/dom_ui_favicon_source.h" |
| 21 #include "chrome/browser/dom_ui/dom_ui_thumbnail_source.h" | 21 #include "chrome/browser/dom_ui/dom_ui_thumbnail_source.h" |
| 22 #include "chrome/browser/dom_ui/new_tab_ui.h" | 22 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 23 #include "chrome/browser/extensions/extensions_service.h" |
| 24 #include "chrome/browser/google_util.h" |
| 23 #include "chrome/browser/history/page_usage_data.h" | 25 #include "chrome/browser/history/page_usage_data.h" |
| 24 #include "chrome/browser/history/history.h" | 26 #include "chrome/browser/history/history.h" |
| 25 #include "chrome/browser/history/top_sites.h" | 27 #include "chrome/browser/history/top_sites.h" |
| 26 #include "chrome/browser/metrics/user_metrics.h" | 28 #include "chrome/browser/metrics/user_metrics.h" |
| 27 #include "chrome/browser/pref_service.h" | 29 #include "chrome/browser/pref_service.h" |
| 28 #include "chrome/browser/profile.h" | 30 #include "chrome/browser/profile.h" |
| 29 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/common/extensions/extension.h" |
| 30 #include "chrome/common/notification_type.h" | 33 #include "chrome/common/notification_type.h" |
| 31 #include "chrome/common/notification_source.h" | 34 #include "chrome/common/notification_source.h" |
| 32 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 33 #include "grit/chromium_strings.h" | 36 #include "grit/chromium_strings.h" |
| 34 #include "grit/generated_resources.h" | 37 #include "grit/generated_resources.h" |
| 35 #include "grit/locale_settings.h" | 38 #include "grit/locale_settings.h" |
| 36 | 39 |
| 37 namespace { | 40 namespace { |
| 38 | 41 |
| 39 // The number of most visited pages we show. | 42 // The number of most visited pages we show. |
| (...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 void MostVisitedHandler::SetPagesValue(std::vector<PageUsageData*>* data) { | 357 void MostVisitedHandler::SetPagesValue(std::vector<PageUsageData*>* data) { |
| 355 most_visited_urls_.clear(); | 358 most_visited_urls_.clear(); |
| 356 pages_value_.reset(new ListValue); | 359 pages_value_.reset(new ListValue); |
| 357 std::set<GURL> seen_urls; | 360 std::set<GURL> seen_urls; |
| 358 | 361 |
| 359 size_t data_index = 0; | 362 size_t data_index = 0; |
| 360 size_t output_index = 0; | 363 size_t output_index = 0; |
| 361 size_t pre_populated_index = 0; | 364 size_t pre_populated_index = 0; |
| 362 const std::vector<MostVisitedPage> pre_populated_pages = | 365 const std::vector<MostVisitedPage> pre_populated_pages = |
| 363 MostVisitedHandler::GetPrePopulatedPages(); | 366 MostVisitedHandler::GetPrePopulatedPages(); |
| 367 bool add_chrome_store = !HasApps(); |
| 364 | 368 |
| 365 while (output_index < kMostVisitedPages) { | 369 while (output_index < kMostVisitedPages) { |
| 366 bool found = false; | 370 bool found = false; |
| 367 bool pinned = false; | 371 bool pinned = false; |
| 368 std::string pinned_url; | 372 std::string pinned_url; |
| 369 std::string pinned_title; | 373 std::string pinned_title; |
| 370 MostVisitedPage mvp; | 374 MostVisitedPage mvp; |
| 371 | 375 |
| 372 if (MostVisitedHandler::GetPinnedURLAtIndex(output_index, &mvp)) { | 376 if (MostVisitedHandler::GetPinnedURLAtIndex(output_index, &mvp)) { |
| 373 pinned = true; | 377 pinned = true; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 391 while (!found && pre_populated_index < pre_populated_pages.size()) { | 395 while (!found && pre_populated_index < pre_populated_pages.size()) { |
| 392 mvp = pre_populated_pages[pre_populated_index++]; | 396 mvp = pre_populated_pages[pre_populated_index++]; |
| 393 std::wstring key = GetDictionaryKeyForURL(mvp.url.spec()); | 397 std::wstring key = GetDictionaryKeyForURL(mvp.url.spec()); |
| 394 if (pinned_urls_->HasKey(key) || url_blacklist_->HasKey(key) || | 398 if (pinned_urls_->HasKey(key) || url_blacklist_->HasKey(key) || |
| 395 seen_urls.find(mvp.url) != seen_urls.end()) | 399 seen_urls.find(mvp.url) != seen_urls.end()) |
| 396 continue; | 400 continue; |
| 397 | 401 |
| 398 found = true; | 402 found = true; |
| 399 } | 403 } |
| 400 | 404 |
| 405 if (!found && add_chrome_store) { |
| 406 mvp = GetChromeStorePage(); |
| 407 std::wstring key = GetDictionaryKeyForURL(mvp.url.spec()); |
| 408 if (!pinned_urls_->HasKey(key) && !url_blacklist_->HasKey(key) && |
| 409 seen_urls.find(mvp.url) == seen_urls.end()) { |
| 410 found = true; |
| 411 } |
| 412 add_chrome_store = false; |
| 413 } |
| 414 |
| 401 if (found) { | 415 if (found) { |
| 402 // Add fillers as needed. | 416 // Add fillers as needed. |
| 403 while (pages_value_->GetSize() < output_index) { | 417 while (pages_value_->GetSize() < output_index) { |
| 404 DictionaryValue* filler_value = new DictionaryValue(); | 418 DictionaryValue* filler_value = new DictionaryValue(); |
| 405 filler_value->SetBoolean(L"filler", true); | 419 filler_value->SetBoolean(L"filler", true); |
| 406 pages_value_->Append(filler_value); | 420 pages_value_->Append(filler_value); |
| 407 } | 421 } |
| 408 | 422 |
| 409 DictionaryValue* page_value = new DictionaryValue(); | 423 DictionaryValue* page_value = new DictionaryValue(); |
| 410 SetMostVisistedPage(page_value, mvp); | 424 SetMostVisistedPage(page_value, mvp); |
| 411 page_value->SetBoolean(L"pinned", pinned); | 425 page_value->SetBoolean(L"pinned", pinned); |
| 412 pages_value_->Append(page_value); | 426 pages_value_->Append(page_value); |
| 413 most_visited_urls_.push_back(mvp.url); | 427 most_visited_urls_.push_back(mvp.url); |
| 414 seen_urls.insert(mvp.url); | 428 seen_urls.insert(mvp.url); |
| 415 } | 429 } |
| 416 output_index++; | 430 output_index++; |
| 417 } | 431 } |
| 432 |
| 433 // If we still need to show the Chrome Store go backwards until we find a non |
| 434 // pinned item we can replace. |
| 435 if (add_chrome_store) { |
| 436 MostVisitedPage chrome_store_page = GetChromeStorePage(); |
| 437 if (seen_urls.find(chrome_store_page.url) != seen_urls.end()) |
| 438 return; |
| 439 |
| 440 std::wstring key = GetDictionaryKeyForURL(chrome_store_page.url.spec()); |
| 441 if (url_blacklist_->HasKey(key)) |
| 442 return; |
| 443 |
| 444 for (int i = kMostVisitedPages - 1; i >= 0; --i) { |
| 445 GURL url = most_visited_urls_[i]; |
| 446 std::wstring key = GetDictionaryKeyForURL(url.spec()); |
| 447 if (!pinned_urls_->HasKey(key)) { |
| 448 // Not pinned, replace. |
| 449 DictionaryValue* page_value = new DictionaryValue(); |
| 450 SetMostVisistedPage(page_value, chrome_store_page); |
| 451 pages_value_->Set(i, page_value); |
| 452 return; |
| 453 } |
| 454 } |
| 455 } |
| 418 } | 456 } |
| 419 | 457 |
| 420 // Converts a MostVisitedURLList into a vector of PageUsageData to be | 458 // Converts a MostVisitedURLList into a vector of PageUsageData to be |
| 421 // sent to the Javascript side to the New Tab Page. | 459 // sent to the Javascript side to the New Tab Page. |
| 422 // Caller takes ownership of the PageUsageData objects in the vector. | 460 // Caller takes ownership of the PageUsageData objects in the vector. |
| 423 // NOTE: this doesn't set the thumbnail and favicon, only URL and title. | 461 // NOTE: this doesn't set the thumbnail and favicon, only URL and title. |
| 424 static void MakePageUsageDataVector(const history::MostVisitedURLList& data, | 462 static void MakePageUsageDataVector(const history::MostVisitedURLList& data, |
| 425 std::vector<PageUsageData*>* result) { | 463 std::vector<PageUsageData*>* result) { |
| 426 for (size_t i = 0; i < data.size(); i++) { | 464 for (size_t i = 0; i < data.size(); i++) { |
| 427 const history::MostVisitedURL& url = data[i]; | 465 const history::MostVisitedURL& url = data[i]; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 470 l10n_util::GetString(IDS_NEW_TAB_THEMES_GALLERY_PAGE_TITLE), | 508 l10n_util::GetString(IDS_NEW_TAB_THEMES_GALLERY_PAGE_TITLE), |
| 471 GURL(WideToUTF8(l10n_util::GetString(IDS_THEMES_GALLERY_URL))), | 509 GURL(WideToUTF8(l10n_util::GetString(IDS_THEMES_GALLERY_URL))), |
| 472 GURL("chrome://theme/IDR_NEWTAB_THEMES_GALLERY_THUMBNAIL"), | 510 GURL("chrome://theme/IDR_NEWTAB_THEMES_GALLERY_THUMBNAIL"), |
| 473 GURL("chrome://theme/IDR_NEWTAB_THEMES_GALLERY_FAVICON")}; | 511 GURL("chrome://theme/IDR_NEWTAB_THEMES_GALLERY_FAVICON")}; |
| 474 pages.push_back(gallery_page); | 512 pages.push_back(gallery_page); |
| 475 } | 513 } |
| 476 | 514 |
| 477 return pages; | 515 return pages; |
| 478 } | 516 } |
| 479 | 517 |
| 518 // static |
| 519 MostVisitedHandler::MostVisitedPage MostVisitedHandler::GetChromeStorePage() { |
| 520 MostVisitedHandler::MostVisitedPage page = { |
| 521 l10n_util::GetString(IDS_EXTENSION_WEB_STORE_TITLE), |
| 522 google_util::AppendGoogleLocaleParam(GURL(Extension::ChromeStoreURL())), |
| 523 GURL("chrome://theme/IDR_NEWTAB_CHROME_STORE_PAGE_THUMBNAIL"), |
| 524 GURL("chrome://theme/IDR_NEWTAB_CHROME_STORE_PAGE_FAVICON")}; |
| 525 return page; |
| 526 } |
| 527 |
| 480 void MostVisitedHandler::Observe(NotificationType type, | 528 void MostVisitedHandler::Observe(NotificationType type, |
| 481 const NotificationSource& source, | 529 const NotificationSource& source, |
| 482 const NotificationDetails& details) { | 530 const NotificationDetails& details) { |
| 483 if (type != NotificationType::HISTORY_URLS_DELETED) { | 531 if (type != NotificationType::HISTORY_URLS_DELETED) { |
| 484 NOTREACHED(); | 532 NOTREACHED(); |
| 485 return; | 533 return; |
| 486 } | 534 } |
| 487 | 535 |
| 488 // Some URLs were deleted from history. Reload the most visited list. | 536 // Some URLs were deleted from history. Reload the most visited list. |
| 489 HandleGetMostVisited(NULL); | 537 HandleGetMostVisited(NULL); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 501 std::wstring MostVisitedHandler::GetDictionaryKeyForURL( | 549 std::wstring MostVisitedHandler::GetDictionaryKeyForURL( |
| 502 const std::string& url) { | 550 const std::string& url) { |
| 503 return ASCIIToWide(MD5String(url)); | 551 return ASCIIToWide(MD5String(url)); |
| 504 } | 552 } |
| 505 | 553 |
| 506 // static | 554 // static |
| 507 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { | 555 void MostVisitedHandler::RegisterUserPrefs(PrefService* prefs) { |
| 508 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist); | 556 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedURLsBlacklist); |
| 509 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs); | 557 prefs->RegisterDictionaryPref(prefs::kNTPMostVisitedPinnedURLs); |
| 510 } | 558 } |
| 559 |
| 560 bool MostVisitedHandler::HasApps() const { |
| 561 ExtensionsService* service = dom_ui_->GetProfile()->GetExtensionsService(); |
| 562 if (!service) |
| 563 return false; |
| 564 |
| 565 return service->HasApps(); |
| 566 } |
| OLD | NEW |