| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ui/webui/history_ui.h" | |
| 6 | |
| 7 #include <string> | |
| 8 #include <utility> | |
| 9 | |
| 10 #include "base/command_line.h" | |
| 11 #include "base/memory/ptr_util.h" | |
| 12 #include "base/memory/ref_counted_memory.h" | |
| 13 #include "base/strings/string16.h" | |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 15 #include "build/build_config.h" | |
| 16 #include "chrome/browser/prefs/incognito_mode_prefs.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/signin/signin_manager_factory.h" | |
| 19 #include "chrome/browser/ui/webui/browsing_history_handler.h" | |
| 20 #include "chrome/browser/ui/webui/metrics_handler.h" | |
| 21 #include "chrome/common/chrome_switches.h" | |
| 22 #include "chrome/common/pref_names.h" | |
| 23 #include "chrome/common/url_constants.h" | |
| 24 #include "chrome/grit/browser_resources.h" | |
| 25 #include "chrome/grit/generated_resources.h" | |
| 26 #include "chrome/grit/theme_resources.h" | |
| 27 #include "components/browsing_data/core/history_notice_utils.h" | |
| 28 #include "components/browsing_data/core/pref_names.h" | |
| 29 #include "components/grit/components_scaled_resources.h" | |
| 30 #include "components/prefs/pref_service.h" | |
| 31 #include "components/search/search.h" | |
| 32 #include "components/signin/core/browser/signin_manager.h" | |
| 33 #include "components/strings/grit/components_strings.h" | |
| 34 #include "content/public/browser/web_contents.h" | |
| 35 #include "content/public/browser/web_ui.h" | |
| 36 #include "content/public/browser/web_ui_data_source.h" | |
| 37 #include "ui/base/l10n/l10n_util.h" | |
| 38 #include "ui/base/resource/resource_bundle.h" | |
| 39 | |
| 40 #if !defined(OS_ANDROID) | |
| 41 #include "chrome/browser/ui/webui/foreign_session_handler.h" | |
| 42 #include "chrome/browser/ui/webui/history_login_handler.h" | |
| 43 #endif | |
| 44 | |
| 45 static const char kStringsJsFile[] = "strings.js"; | |
| 46 static const char kHistoryJsFile[] = "history.js"; | |
| 47 static const char kOtherDevicesJsFile[] = "other_devices.js"; | |
| 48 | |
| 49 namespace { | |
| 50 | |
| 51 static const char kMyActivityUrl[] = | |
| 52 "https://history.google.com/history/?utm_source=chrome_h"; | |
| 53 | |
| 54 #if defined(OS_MACOSX) | |
| 55 const char kIncognitoModeShortcut[] = "(" | |
| 56 "\xE2\x87\xA7" // Shift symbol (U+21E7 'UPWARDS WHITE ARROW'). | |
| 57 "\xE2\x8C\x98" // Command symbol (U+2318 'PLACE OF INTEREST SIGN'). | |
| 58 "N)"; | |
| 59 #elif defined(OS_WIN) | |
| 60 const char kIncognitoModeShortcut[] = "(Ctrl+Shift+N)"; | |
| 61 #else | |
| 62 const char kIncognitoModeShortcut[] = "(Shift+Ctrl+N)"; | |
| 63 #endif | |
| 64 | |
| 65 constexpr char kIsUserSignedInKey[] = "isUserSignedIn"; | |
| 66 | |
| 67 bool IsSignedIn(Profile* profile) { | |
| 68 // Check if the profile is authenticated. Guest profiles or incognito | |
| 69 // windows may not have a sign in manager, and are considered not | |
| 70 // authenticated. | |
| 71 SigninManagerBase* signin_manager = | |
| 72 SigninManagerFactory::GetForProfile(profile); | |
| 73 return signin_manager && signin_manager->IsAuthenticated(); | |
| 74 } | |
| 75 | |
| 76 content::WebUIDataSource* CreateHistoryUIHTMLSource(Profile* profile) { | |
| 77 PrefService* prefs = profile->GetPrefs(); | |
| 78 | |
| 79 content::WebUIDataSource* source = | |
| 80 content::WebUIDataSource::Create(chrome::kChromeUIHistoryFrameHost); | |
| 81 source->AddBoolean(kIsUserSignedInKey, IsSignedIn(profile)); | |
| 82 #if !defined(OS_ANDROID) | |
| 83 source->AddLocalizedString("collapseSessionMenuItemText", | |
| 84 IDS_HISTORY_OTHER_SESSIONS_COLLAPSE_SESSION); | |
| 85 source->AddLocalizedString("expandSessionMenuItemText", | |
| 86 IDS_HISTORY_OTHER_SESSIONS_EXPAND_SESSION); | |
| 87 source->AddLocalizedString("restoreSessionMenuItemText", | |
| 88 IDS_HISTORY_OTHER_SESSIONS_OPEN_ALL); | |
| 89 source->AddLocalizedString("deleteSessionMenuItemText", | |
| 90 IDS_HISTORY_OTHER_SESSIONS_HIDE_FOR_NOW); | |
| 91 #endif | |
| 92 source->AddLocalizedString("xMore", IDS_HISTORY_OTHER_DEVICES_X_MORE); | |
| 93 source->AddLocalizedString("loading", IDS_HISTORY_LOADING); | |
| 94 source->AddLocalizedString("title", IDS_HISTORY_TITLE); | |
| 95 source->AddLocalizedString("newest", IDS_HISTORY_NEWEST); | |
| 96 source->AddLocalizedString("newer", IDS_HISTORY_NEWER); | |
| 97 source->AddLocalizedString("older", IDS_HISTORY_OLDER); | |
| 98 source->AddLocalizedString("searchResultsFor", IDS_HISTORY_SEARCHRESULTSFOR); | |
| 99 source->AddLocalizedString("searchResult", IDS_HISTORY_SEARCH_RESULT); | |
| 100 source->AddLocalizedString("searchResults", IDS_HISTORY_SEARCH_RESULTS); | |
| 101 source->AddLocalizedString("foundSearchResults", | |
| 102 IDS_HISTORY_FOUND_SEARCH_RESULTS); | |
| 103 source->AddLocalizedString("history", IDS_HISTORY_BROWSERESULTS); | |
| 104 source->AddLocalizedString("cont", IDS_HISTORY_CONTINUED); | |
| 105 source->AddLocalizedString("searchButton", IDS_HISTORY_SEARCH_BUTTON); | |
| 106 source->AddLocalizedString("noSearchResults", IDS_HISTORY_NO_SEARCH_RESULTS); | |
| 107 source->AddLocalizedString("noResults", IDS_HISTORY_NO_RESULTS); | |
| 108 source->AddLocalizedString("removeSelected", | |
| 109 IDS_HISTORY_REMOVE_SELECTED_ITEMS); | |
| 110 source->AddLocalizedString("clearAllHistory", | |
| 111 IDS_HISTORY_OPEN_CLEAR_BROWSING_DATA_DIALOG); | |
| 112 | |
| 113 auto availability = IncognitoModePrefs::GetAvailability(profile->GetPrefs()); | |
| 114 base::string16 delete_string = availability == IncognitoModePrefs::ENABLED | |
| 115 ? l10n_util::GetStringFUTF16( | |
| 116 IDS_HISTORY_DELETE_PRIOR_VISITS_WARNING, | |
| 117 base::UTF8ToUTF16(kIncognitoModeShortcut)) | |
| 118 : l10n_util::GetStringUTF16( | |
| 119 IDS_HISTORY_DELETE_PRIOR_VISITS_WARNING_NO_INCOGNITO); | |
| 120 source->AddString("deleteWarning", delete_string); | |
| 121 | |
| 122 source->AddLocalizedString("removeBookmark", IDS_HISTORY_REMOVE_BOOKMARK); | |
| 123 source->AddLocalizedString("actionMenuDescription", | |
| 124 IDS_HISTORY_ACTION_MENU_DESCRIPTION); | |
| 125 source->AddLocalizedString("removeFromHistory", IDS_HISTORY_REMOVE_PAGE); | |
| 126 source->AddLocalizedString("moreFromSite", IDS_HISTORY_MORE_FROM_SITE); | |
| 127 source->AddLocalizedString("rangeLabel", IDS_HISTORY_RANGE_LABEL); | |
| 128 source->AddLocalizedString("rangeAllTime", IDS_HISTORY_RANGE_ALL_TIME); | |
| 129 source->AddLocalizedString("rangeWeek", IDS_HISTORY_RANGE_WEEK); | |
| 130 source->AddLocalizedString("rangeMonth", IDS_HISTORY_RANGE_MONTH); | |
| 131 source->AddLocalizedString("rangeToday", IDS_HISTORY_RANGE_TODAY); | |
| 132 source->AddLocalizedString("rangeNext", IDS_HISTORY_RANGE_NEXT); | |
| 133 source->AddLocalizedString("rangePrevious", IDS_HISTORY_RANGE_PREVIOUS); | |
| 134 source->AddLocalizedString("numberVisits", IDS_HISTORY_NUMBER_VISITS); | |
| 135 source->AddLocalizedString("filterBlocked", IDS_HISTORY_FILTER_BLOCKED); | |
| 136 source->AddLocalizedString("blockedVisitText", | |
| 137 IDS_HISTORY_BLOCKED_VISIT_TEXT); | |
| 138 source->AddLocalizedString("hasSyncedResults", | |
| 139 IDS_HISTORY_HAS_SYNCED_RESULTS); | |
| 140 source->AddLocalizedString("noSyncedResults", IDS_HISTORY_NO_SYNCED_RESULTS); | |
| 141 source->AddString("otherFormsOfBrowsingHistory", | |
| 142 l10n_util::GetStringFUTF16( | |
| 143 IDS_HISTORY_OTHER_FORMS_OF_HISTORY, | |
| 144 base::ASCIIToUTF16(kMyActivityUrl))); | |
| 145 source->AddLocalizedString("cancel", IDS_CANCEL); | |
| 146 source->AddLocalizedString("deleteConfirm", | |
| 147 IDS_HISTORY_DELETE_PRIOR_VISITS_CONFIRM_BUTTON); | |
| 148 source->AddLocalizedString("bookmarked", IDS_HISTORY_ENTRY_BOOKMARKED); | |
| 149 source->AddLocalizedString("entrySummary", IDS_HISTORY_ENTRY_SUMMARY); | |
| 150 bool group_by_domain = base::CommandLine::ForCurrentProcess()->HasSwitch( | |
| 151 switches::kHistoryEnableGroupByDomain); | |
| 152 // Supervised users get the "group by domain" version, but not on mobile, | |
| 153 // because that version isn't adjusted for small screens yet. crbug.com/452859 | |
| 154 #if !defined(OS_ANDROID) | |
| 155 group_by_domain = group_by_domain || profile->IsSupervised(); | |
| 156 #endif | |
| 157 source->AddBoolean("groupByDomain", group_by_domain); | |
| 158 bool allow_deleting_history = | |
| 159 prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory); | |
| 160 source->AddBoolean("allowDeletingHistory", allow_deleting_history); | |
| 161 source->AddBoolean("isInstantExtendedApiEnabled", | |
| 162 search::IsInstantExtendedAPIEnabled()); | |
| 163 source->AddBoolean("isSupervisedProfile", profile->IsSupervised()); | |
| 164 source->AddBoolean("hideDeleteVisitUI", | |
| 165 profile->IsSupervised() && !allow_deleting_history); | |
| 166 | |
| 167 source->SetJsonPath(kStringsJsFile); | |
| 168 source->AddResourcePath(kHistoryJsFile, IDR_HISTORY_JS); | |
| 169 source->AddResourcePath(kOtherDevicesJsFile, IDR_OTHER_DEVICES_JS); | |
| 170 source->SetDefaultResource(IDR_HISTORY_HTML); | |
| 171 source->DisableDenyXFrameOptions(); | |
| 172 source->UseGzip(std::unordered_set<std::string>()); | |
| 173 | |
| 174 return source; | |
| 175 } | |
| 176 | |
| 177 } // namespace | |
| 178 | |
| 179 HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { | |
| 180 // Set up the chrome://history-frame/ source. | |
| 181 Profile* profile = Profile::FromWebUI(web_ui); | |
| 182 content::WebUIDataSource::Add(profile, CreateHistoryUIHTMLSource(profile)); | |
| 183 | |
| 184 web_ui->AddMessageHandler(base::MakeUnique<BrowsingHistoryHandler>()); | |
| 185 web_ui->AddMessageHandler(base::MakeUnique<MetricsHandler>()); | |
| 186 | |
| 187 // On mobile we deal with foreign sessions differently. | |
| 188 #if !defined(OS_ANDROID) | |
| 189 if (search::IsInstantExtendedAPIEnabled()) { | |
| 190 web_ui->AddMessageHandler( | |
| 191 base::MakeUnique<browser_sync::ForeignSessionHandler>()); | |
| 192 web_ui->AddMessageHandler(base::MakeUnique<HistoryLoginHandler>( | |
| 193 base::Bind(&HistoryUI::UpdateDataSource, base::Unretained(this)))); | |
| 194 } | |
| 195 #endif | |
| 196 | |
| 197 // TODO(crbug.com/595332): Since the API to query other forms of browsing | |
| 198 // history is not ready yet, make it possible to test the history UI as if | |
| 199 // it were. If the user opens chrome://history/?reset_ofbh, we will assume | |
| 200 // that other forms of browsing history exist (for all accounts), and we will | |
| 201 // also reset the one-time notice shown in the Clear Browsing Data dialog. | |
| 202 // This code should be removed as soon as the API is ready. | |
| 203 GURL url = web_ui->GetWebContents()->GetVisibleURL(); | |
| 204 if (url.has_query() && url.query() == "reset_ofbh") { | |
| 205 profile->GetPrefs()->SetInteger( | |
| 206 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes, 0); | |
| 207 browsing_data::testing:: | |
| 208 g_override_other_forms_of_browsing_history_query = true; | |
| 209 } | |
| 210 } | |
| 211 | |
| 212 HistoryUI::~HistoryUI() {} | |
| 213 | |
| 214 // static | |
| 215 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes( | |
| 216 ui::ScaleFactor scale_factor) { | |
| 217 return ResourceBundle::GetSharedInstance(). | |
| 218 LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor); | |
| 219 } | |
| 220 | |
| 221 void HistoryUI::UpdateDataSource() { | |
| 222 CHECK(web_ui()); | |
| 223 Profile* profile = Profile::FromWebUI(web_ui()); | |
| 224 std::unique_ptr<base::DictionaryValue> update(new base::DictionaryValue); | |
| 225 update->SetBoolean(kIsUserSignedInKey, IsSignedIn(profile)); | |
| 226 content::WebUIDataSource::Update(profile, chrome::kChromeUIHistoryFrameHost, | |
| 227 std::move(update)); | |
| 228 } | |
| OLD | NEW |