| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #include "chrome/browser/dom_ui/new_tab_ui.h" | 7 #include "chrome/browser/dom_ui/new_tab_ui.h" |
| 8 | 8 |
| 9 #include <set> | 9 #include <set> |
| 10 | 10 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 #include "chrome/browser/dom_ui/shown_sections_handler.h" | 22 #include "chrome/browser/dom_ui/shown_sections_handler.h" |
| 23 #include "chrome/browser/dom_ui/tips_handler.h" | 23 #include "chrome/browser/dom_ui/tips_handler.h" |
| 24 #include "chrome/browser/metrics/user_metrics.h" | 24 #include "chrome/browser/metrics/user_metrics.h" |
| 25 #include "chrome/browser/profile.h" | 25 #include "chrome/browser/profile.h" |
| 26 #include "chrome/browser/renderer_host/render_view_host.h" | 26 #include "chrome/browser/renderer_host/render_view_host.h" |
| 27 #include "chrome/browser/sessions/session_types.h" | 27 #include "chrome/browser/sessions/session_types.h" |
| 28 #include "chrome/browser/tab_contents/tab_contents.h" | 28 #include "chrome/browser/tab_contents/tab_contents.h" |
| 29 #include "chrome/browser/sessions/tab_restore_service.h" | 29 #include "chrome/browser/sessions/tab_restore_service.h" |
| 30 #include "chrome/browser/sync/profile_sync_service.h" | 30 #include "chrome/browser/sync/profile_sync_service.h" |
| 31 #include "chrome/common/chrome_switches.h" | 31 #include "chrome/common/chrome_switches.h" |
| 32 #include "chrome/common/extensions/extension_constants.h" |
| 33 #include "chrome/common/jstemplate_builder.h" |
| 32 #include "chrome/common/notification_service.h" | 34 #include "chrome/common/notification_service.h" |
| 33 #include "chrome/common/pref_names.h" | 35 #include "chrome/common/pref_names.h" |
| 34 #include "chrome/common/pref_service.h" | 36 #include "chrome/common/pref_service.h" |
| 35 #include "chrome/common/url_constants.h" | 37 #include "chrome/common/url_constants.h" |
| 36 #include "grit/generated_resources.h" | 38 #include "grit/generated_resources.h" |
| 37 | 39 |
| 38 namespace { | 40 namespace { |
| 39 | 41 |
| 40 // The number of recent bookmarks we show. | 42 // The number of recent bookmarks we show. |
| 41 const int kRecentBookmarks = 9; | 43 const int kRecentBookmarks = 9; |
| (...skipping 637 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 679 // URL from the new tab page, but in any case it's an error. | 681 // URL from the new tab page, but in any case it's an error. |
| 680 NOTREACHED(); | 682 NOTREACHED(); |
| 681 return; | 683 return; |
| 682 } | 684 } |
| 683 | 685 |
| 684 scoped_refptr<RefCountedBytes> html_bytes = | 686 scoped_refptr<RefCountedBytes> html_bytes = |
| 685 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record); | 687 profile_->GetNTPResourceCache()->GetNewTabHTML(is_off_the_record); |
| 686 | 688 |
| 687 SendResponse(request_id, html_bytes); | 689 SendResponse(request_id, html_bytes); |
| 688 } | 690 } |
| 691 |
| 692 // static |
| 693 std::string NewTabUI::NewTabHTMLSource::GetCustomNewTabPageFromCommandLine() { |
| 694 const CommandLine* command_line = CommandLine::ForCurrentProcess(); |
| 695 const std::wstring file_path_wstring = command_line->GetSwitchValue( |
| 696 switches::kNewTabPage); |
| 697 |
| 698 #if defined(OS_WIN) |
| 699 const FilePath::StringType file_path = file_path_wstring; |
| 700 #else |
| 701 const FilePath::StringType file_path = WideToASCII(file_path_wstring); |
| 702 #endif |
| 703 |
| 704 if (!file_path.empty()) { |
| 705 // Read the file contents in, blocking the UI thread of the browser. |
| 706 // This is for testing purposes only! It is used to test new versions of |
| 707 // the new tab page using a special command line option. Never use this |
| 708 // in a way that will be used by default in product. |
| 709 std::string file_contents; |
| 710 if (file_util::ReadFileToString(FilePath(file_path), &file_contents)) |
| 711 return file_contents; |
| 712 } |
| 713 |
| 714 return std::string(); |
| 715 } |
| 716 |
| 717 void NewTabUI::NewTabHTMLSource::InitFullHTML(Profile* profile) { |
| 718 // Show the profile name in the title and most visited labels if the current |
| 719 // profile is not the default. |
| 720 std::wstring title; |
| 721 std::wstring most_visited; |
| 722 if (UserDataManager::Get()->is_current_profile_default()) { |
| 723 title = l10n_util::GetString(IDS_NEW_TAB_TITLE); |
| 724 most_visited = l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED); |
| 725 } else { |
| 726 // Get the current profile name. |
| 727 std::wstring profile_name = |
| 728 UserDataManager::Get()->current_profile_name(); |
| 729 title = l10n_util::GetStringF(IDS_NEW_TAB_TITLE_WITH_PROFILE_NAME, |
| 730 profile_name); |
| 731 most_visited = l10n_util::GetStringF( |
| 732 IDS_NEW_TAB_MOST_VISITED_WITH_PROFILE_NAME, |
| 733 profile_name); |
| 734 } |
| 735 DictionaryValue localized_strings; |
| 736 localized_strings.SetString(L"bookmarkbarattached", |
| 737 profile->GetPrefs()->GetBoolean(prefs::kShowBookmarkBar) ? |
| 738 "true" : "false"); |
| 739 localized_strings.SetString(L"hasattribution", |
| 740 profile->GetThemeProvider()->HasCustomImage(IDR_THEME_NTP_ATTRIBUTION) ? |
| 741 "true" : "false"); |
| 742 localized_strings.SetString(L"title", title); |
| 743 localized_strings.SetString(L"mostvisited", most_visited); |
| 744 localized_strings.SetString(L"searches", |
| 745 l10n_util::GetString(IDS_NEW_TAB_SEARCHES)); |
| 746 localized_strings.SetString(L"bookmarks", |
| 747 l10n_util::GetString(IDS_NEW_TAB_BOOKMARKS)); |
| 748 localized_strings.SetString(L"recent", |
| 749 l10n_util::GetString(IDS_NEW_TAB_RECENT)); |
| 750 localized_strings.SetString(L"showhistory", |
| 751 l10n_util::GetString(IDS_NEW_TAB_HISTORY_SHOW)); |
| 752 localized_strings.SetString(L"showhistoryurl", |
| 753 chrome::kChromeUIHistoryURL); |
| 754 localized_strings.SetString(L"editthumbnails", |
| 755 l10n_util::GetString(IDS_NEW_TAB_REMOVE_THUMBNAILS)); |
| 756 localized_strings.SetString(L"restorethumbnails", |
| 757 l10n_util::GetString(IDS_NEW_TAB_RESTORE_THUMBNAILS_LINK)); |
| 758 localized_strings.SetString(L"editmodeheading", |
| 759 l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED_EDIT_MODE_HEADING)); |
| 760 localized_strings.SetString(L"doneediting", |
| 761 l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED_DONE_REMOVING_BUTTON)); |
| 762 localized_strings.SetString(L"cancelediting", |
| 763 l10n_util::GetString(IDS_NEW_TAB_MOST_VISITED_CANCEL_REMOVING_BUTTON)); |
| 764 localized_strings.SetString(L"searchhistory", |
| 765 l10n_util::GetString(IDS_NEW_TAB_HISTORY_SEARCH)); |
| 766 localized_strings.SetString(L"recentlyclosed", |
| 767 l10n_util::GetString(IDS_NEW_TAB_RECENTLY_CLOSED)); |
| 768 localized_strings.SetString(L"mostvisitedintro", |
| 769 l10n_util::GetStringF(IDS_NEW_TAB_MOST_VISITED_INTRO, |
| 770 l10n_util::GetString(IDS_WELCOME_PAGE_URL))); |
| 771 localized_strings.SetString(L"closedwindowsingle", |
| 772 l10n_util::GetString(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_SINGLE)); |
| 773 localized_strings.SetString(L"closedwindowmultiple", |
| 774 l10n_util::GetString(IDS_NEW_TAB_RECENTLY_CLOSED_WINDOW_MULTIPLE)); |
| 775 localized_strings.SetString(L"attributionintro", |
| 776 l10n_util::GetString(IDS_NEW_TAB_ATTRIBUTION_INTRO)); |
| 777 localized_strings.SetString(L"viewfullhistory", |
| 778 l10n_util::GetString(IDS_NEW_TAB_VIEW_FULL_HISTORY)); |
| 779 localized_strings.SetString(L"showthumbnails", |
| 780 l10n_util::GetString(IDS_NEW_TAB_SHOW_THUMBNAILS)); |
| 781 localized_strings.SetString(L"hidethumbnails", |
| 782 l10n_util::GetString(IDS_NEW_TAB_HIDE_THUMBNAILS)); |
| 783 localized_strings.SetString(L"showlist", |
| 784 l10n_util::GetString(IDS_NEW_TAB_SHOW_LIST)); |
| 785 localized_strings.SetString(L"hidelist", |
| 786 l10n_util::GetString(IDS_NEW_TAB_HIDE_LIST)); |
| 787 localized_strings.SetString(L"showrecentlyclosedtabs", |
| 788 l10n_util::GetString(IDS_NEW_TAB_SHOW_RECENTLY_CLOSED_TABS)); |
| 789 localized_strings.SetString(L"hiderecentlyclosedtabs", |
| 790 l10n_util::GetString(IDS_NEW_TAB_HIDE_RECENTLY_CLOSED_TABS)); |
| 791 localized_strings.SetString(L"thumbnailremovednotification", |
| 792 l10n_util::GetString(IDS_NEW_TAB_THUMBNAIL_REMOVED_NOTIFICATION)); |
| 793 localized_strings.SetString(L"undothumbnailremove", |
| 794 l10n_util::GetString(IDS_NEW_TAB_UNDO_THUMBNAIL_REMOVE)); |
| 795 localized_strings.SetString(L"otrmessage", |
| 796 l10n_util::GetString(IDS_NEW_TAB_OTR_MESSAGE)); |
| 797 localized_strings.SetString(L"removethumbnailtooltip", |
| 798 l10n_util::GetString(IDS_NEW_TAB_REMOVE_THUMBNAIL_TOOLTIP)); |
| 799 localized_strings.SetString(L"pinthumbnailtooltip", |
| 800 l10n_util::GetString(IDS_NEW_TAB_PIN_THUMBNAIL_TOOLTIP)); |
| 801 localized_strings.SetString(L"unpinthumbnailtooltip", |
| 802 l10n_util::GetString(IDS_NEW_TAB_UNPIN_THUMBNAIL_TOOLTIP)); |
| 803 localized_strings.SetString(L"showhidethumbnailtooltip", |
| 804 l10n_util::GetString(IDS_NEW_TAB_SHOW_HIDE_THUMBNAIL_TOOLTIP)); |
| 805 localized_strings.SetString(L"showhidelisttooltip", |
| 806 l10n_util::GetString(IDS_NEW_TAB_SHOW_HIDE_LIST_TOOLTIP)); |
| 807 localized_strings.SetString(L"pagedisplaytooltip", |
| 808 l10n_util::GetString(IDS_NEW_TAB_PAGE_DISPLAY_TOOLTIP)); |
| 809 localized_strings.SetString(L"firstrunnotification", |
| 810 l10n_util::GetString(IDS_NEW_TAB_FIRST_RUN_NOTIFICATION)); |
| 811 localized_strings.SetString(L"closefirstrunnotification", |
| 812 l10n_util::GetString(IDS_NEW_TAB_CLOSE_FIRST_RUN_NOTIFICATION)); |
| 813 localized_strings.SetString(L"makethishomepage", |
| 814 l10n_util::GetString(IDS_NEW_TAB_MAKE_THIS_HOMEPAGE)); |
| 815 localized_strings.SetString(L"themelink", |
| 816 l10n_util::GetString(IDS_THEMES_GALLERY_URL)); |
| 817 localized_strings.SetString(L"tips", |
| 818 l10n_util::GetString(IDS_NEW_TAB_TIPS)); |
| 819 localized_strings.SetString(L"sync", |
| 820 l10n_util::GetString(IDS_NEW_TAB_SHOW_HIDE_BOOKMARK_SYNC)); |
| 821 localized_strings.SetString(L"promonew", |
| 822 l10n_util::GetString(IDS_NTP_PROMOTION_NEW)); |
| 823 localized_strings.SetString(L"promomessage", |
| 824 l10n_util::GetStringF(IDS_NTP_PROMOTION_MESSAGE, |
| 825 l10n_util::GetString(IDS_PRODUCT_NAME), |
| 826 ASCIIToWide(extension_urls::kGalleryBrowsePrefix), |
| 827 l10n_util::GetString(IDS_SYNC_SERVICE_HELP_URL))); |
| 828 localized_strings.SetString(L"extensionslink", |
| 829 ASCIIToWide(extension_urls::kGalleryBrowsePrefix)); |
| 830 |
| 831 // Don't initiate the sync related message passing with the page if the sync |
| 832 // code is not present. |
| 833 if (profile->GetProfileSyncService()) |
| 834 localized_strings.SetString(L"syncispresent", "true"); |
| 835 else |
| 836 localized_strings.SetString(L"syncispresent", "false"); |
| 837 |
| 838 if (!profile->GetPrefs()->GetBoolean(prefs::kHomePageIsNewTabPage)) |
| 839 localized_strings.SetString(L"showsetashomepage", "true"); |
| 840 |
| 841 SetFontAndTextDirection(&localized_strings); |
| 842 |
| 843 // Let the tab know whether it's the first tab being viewed. |
| 844 if (first_view_) { |
| 845 // Decrement ntp promo counters; the default values are specified in |
| 846 // Browser::RegisterUserPrefs. |
| 847 profile->GetPrefs()->SetInteger(prefs::kNTPPromoLineRemaining, |
| 848 profile->GetPrefs()->GetInteger(prefs::kNTPPromoLineRemaining) - 1); |
| 849 profile->GetPrefs()->SetInteger(prefs::kNTPPromoImageRemaining, |
| 850 profile->GetPrefs()->GetInteger(prefs::kNTPPromoImageRemaining) - 1); |
| 851 first_view_ = false; |
| 852 } |
| 853 |
| 854 // Control fade and resize animations. |
| 855 std::wstring anim = |
| 856 Animation::ShouldRenderRichAnimation() ? L"true" : L"false"; |
| 857 localized_strings.SetString(L"anim", anim); |
| 858 |
| 859 // Pass the shown_sections pref early so that we can prevent flicker. |
| 860 const int shown_sections = profile->GetPrefs()->GetInteger( |
| 861 prefs::kNTPShownSections); |
| 862 localized_strings.SetInteger(L"shown_sections", shown_sections); |
| 863 |
| 864 // In case we have the new new tab page enabled we first try to read the file |
| 865 // provided on the command line. If that fails we just get the resource from |
| 866 // the resource bundle. |
| 867 base::StringPiece new_tab_html; |
| 868 std::string new_tab_html_str; |
| 869 new_tab_html_str = GetCustomNewTabPageFromCommandLine(); |
| 870 |
| 871 if (!new_tab_html_str.empty()) { |
| 872 new_tab_html = base::StringPiece(new_tab_html_str); |
| 873 } |
| 874 |
| 875 if (new_tab_html.empty()) { |
| 876 new_tab_html = ResourceBundle::GetSharedInstance().GetRawDataResource( |
| 877 IDR_NEW_NEW_TAB_HTML); |
| 878 } |
| 879 |
| 880 // Inject the template data into the HTML so that it is available before any |
| 881 // layout is needed. |
| 882 std::string json_html; |
| 883 jstemplate_builder::AppendJsonHtml(&localized_strings, &json_html); |
| 884 |
| 885 static const base::StringPiece template_data_placeholder( |
| 886 "<!-- template data placeholder -->"); |
| 887 size_t pos = new_tab_html.find(template_data_placeholder); |
| 888 |
| 889 if (pos != base::StringPiece::npos) { |
| 890 full_html_.assign(new_tab_html.data(), pos); |
| 891 full_html_.append(json_html); |
| 892 size_t after_offset = pos + template_data_placeholder.size(); |
| 893 full_html_.append(new_tab_html.data() + after_offset, |
| 894 new_tab_html.size() - after_offset); |
| 895 } else { |
| 896 NOTREACHED(); |
| 897 full_html_.assign(new_tab_html.data(), new_tab_html.size()); |
| 898 } |
| 899 jstemplate_builder::AppendI18nTemplateProcessHtml(&full_html_); |
| 900 } |
| OLD | NEW |