| 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/history_ui.h" | 5 #include "chrome/browser/ui/webui/history_ui.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> |
| 8 | 9 |
| 9 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| 10 #include "base/memory/ref_counted_memory.h" | 11 #include "base/memory/ref_counted_memory.h" |
| 11 #include "base/strings/string16.h" | 12 #include "base/strings/string16.h" |
| 12 #include "base/strings/utf_string_conversions.h" | 13 #include "base/strings/utf_string_conversions.h" |
| 13 #include "build/build_config.h" | 14 #include "build/build_config.h" |
| 14 #include "chrome/browser/prefs/incognito_mode_prefs.h" | 15 #include "chrome/browser/prefs/incognito_mode_prefs.h" |
| 15 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 16 #include "chrome/browser/signin/signin_manager_factory.h" | 17 #include "chrome/browser/signin/signin_manager_factory.h" |
| 17 #include "chrome/browser/ui/webui/browsing_history_handler.h" | 18 #include "chrome/browser/ui/webui/browsing_history_handler.h" |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 const char kIncognitoModeShortcut[] = "(" | 54 const char kIncognitoModeShortcut[] = "(" |
| 54 "\xE2\x87\xA7" // Shift symbol (U+21E7 'UPWARDS WHITE ARROW'). | 55 "\xE2\x87\xA7" // Shift symbol (U+21E7 'UPWARDS WHITE ARROW'). |
| 55 "\xE2\x8C\x98" // Command symbol (U+2318 'PLACE OF INTEREST SIGN'). | 56 "\xE2\x8C\x98" // Command symbol (U+2318 'PLACE OF INTEREST SIGN'). |
| 56 "N)"; | 57 "N)"; |
| 57 #elif defined(OS_WIN) | 58 #elif defined(OS_WIN) |
| 58 const char kIncognitoModeShortcut[] = "(Ctrl+Shift+N)"; | 59 const char kIncognitoModeShortcut[] = "(Ctrl+Shift+N)"; |
| 59 #else | 60 #else |
| 60 const char kIncognitoModeShortcut[] = "(Shift+Ctrl+N)"; | 61 const char kIncognitoModeShortcut[] = "(Shift+Ctrl+N)"; |
| 61 #endif | 62 #endif |
| 62 | 63 |
| 63 content::WebUIDataSource* CreateHistoryUIHTMLSource(Profile* profile) { | 64 constexpr char kIsUserSignedInKey[] = "isUserSignedIn"; |
| 64 PrefService* prefs = profile->GetPrefs(); | |
| 65 | 65 |
| 66 bool IsSignedIn(Profile* profile) { |
| 66 // Check if the profile is authenticated. Guest profiles or incognito | 67 // Check if the profile is authenticated. Guest profiles or incognito |
| 67 // windows may not have a sign in manager, and are considered not | 68 // windows may not have a sign in manager, and are considered not |
| 68 // authenticated. | 69 // authenticated. |
| 69 SigninManagerBase* signin_manager = | 70 SigninManagerBase* signin_manager = |
| 70 SigninManagerFactory::GetForProfile(profile); | 71 SigninManagerFactory::GetForProfile(profile); |
| 71 bool is_authenticated = signin_manager != nullptr && | 72 return signin_manager && signin_manager->IsAuthenticated(); |
| 72 signin_manager->IsAuthenticated(); | 73 } |
| 74 |
| 75 content::WebUIDataSource* CreateHistoryUIHTMLSource(Profile* profile) { |
| 76 PrefService* prefs = profile->GetPrefs(); |
| 73 | 77 |
| 74 content::WebUIDataSource* source = | 78 content::WebUIDataSource* source = |
| 75 content::WebUIDataSource::Create(chrome::kChromeUIHistoryFrameHost); | 79 content::WebUIDataSource::Create(chrome::kChromeUIHistoryFrameHost); |
| 76 source->AddBoolean("isUserSignedIn", is_authenticated); | 80 source->AddBoolean(kIsUserSignedInKey, IsSignedIn(profile)); |
| 77 #if !defined(OS_ANDROID) | 81 #if !defined(OS_ANDROID) |
| 78 source->AddLocalizedString("collapseSessionMenuItemText", | 82 source->AddLocalizedString("collapseSessionMenuItemText", |
| 79 IDS_HISTORY_OTHER_SESSIONS_COLLAPSE_SESSION); | 83 IDS_HISTORY_OTHER_SESSIONS_COLLAPSE_SESSION); |
| 80 source->AddLocalizedString("expandSessionMenuItemText", | 84 source->AddLocalizedString("expandSessionMenuItemText", |
| 81 IDS_HISTORY_OTHER_SESSIONS_EXPAND_SESSION); | 85 IDS_HISTORY_OTHER_SESSIONS_EXPAND_SESSION); |
| 82 source->AddLocalizedString("restoreSessionMenuItemText", | 86 source->AddLocalizedString("restoreSessionMenuItemText", |
| 83 IDS_HISTORY_OTHER_SESSIONS_OPEN_ALL); | 87 IDS_HISTORY_OTHER_SESSIONS_OPEN_ALL); |
| 84 source->AddLocalizedString("deleteSessionMenuItemText", | 88 source->AddLocalizedString("deleteSessionMenuItemText", |
| 85 IDS_HISTORY_OTHER_SESSIONS_HIDE_FOR_NOW); | 89 IDS_HISTORY_OTHER_SESSIONS_HIDE_FOR_NOW); |
| 86 #endif | 90 #endif |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 source->SetDefaultResource(IDR_HISTORY_HTML); | 170 source->SetDefaultResource(IDR_HISTORY_HTML); |
| 167 source->DisableDenyXFrameOptions(); | 171 source->DisableDenyXFrameOptions(); |
| 168 source->DisableI18nAndUseGzipForAllPaths(); | 172 source->DisableI18nAndUseGzipForAllPaths(); |
| 169 | 173 |
| 170 return source; | 174 return source; |
| 171 } | 175 } |
| 172 | 176 |
| 173 } // namespace | 177 } // namespace |
| 174 | 178 |
| 175 HistoryUI::HistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 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 |
| 176 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); | 184 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); |
| 177 web_ui->AddMessageHandler(new MetricsHandler()); | 185 web_ui->AddMessageHandler(new MetricsHandler()); |
| 178 | 186 |
| 179 // On mobile we deal with foreign sessions differently. | 187 // On mobile we deal with foreign sessions differently. |
| 180 #if !defined(OS_ANDROID) | 188 #if !defined(OS_ANDROID) |
| 181 if (search::IsInstantExtendedAPIEnabled()) { | 189 if (search::IsInstantExtendedAPIEnabled()) { |
| 182 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 190 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
| 183 web_ui->AddMessageHandler(new HistoryLoginHandler( | 191 web_ui->AddMessageHandler(new HistoryLoginHandler( |
| 184 base::Bind(&HistoryUI::CreateDataSource, base::Unretained(this)))); | 192 base::Bind(&HistoryUI::UpdateDataSource, base::Unretained(this)))); |
| 185 } | 193 } |
| 186 #endif | 194 #endif |
| 187 | 195 |
| 188 // TODO(crbug.com/595332): Since the API to query other forms of browsing | 196 // TODO(crbug.com/595332): Since the API to query other forms of browsing |
| 189 // history is not ready yet, make it possible to test the history UI as if | 197 // history is not ready yet, make it possible to test the history UI as if |
| 190 // it were. If the user opens chrome://history/?reset_ofbh, we will assume | 198 // it were. If the user opens chrome://history/?reset_ofbh, we will assume |
| 191 // that other forms of browsing history exist (for all accounts), and we will | 199 // that other forms of browsing history exist (for all accounts), and we will |
| 192 // also reset the one-time notice shown in the Clear Browsing Data dialog. | 200 // also reset the one-time notice shown in the Clear Browsing Data dialog. |
| 193 // This code should be removed as soon as the API is ready. | 201 // This code should be removed as soon as the API is ready. |
| 194 GURL url = web_ui->GetWebContents()->GetVisibleURL(); | 202 GURL url = web_ui->GetWebContents()->GetVisibleURL(); |
| 195 if (url.has_query() && url.query() == "reset_ofbh") { | 203 if (url.has_query() && url.query() == "reset_ofbh") { |
| 196 Profile::FromWebUI(web_ui)->GetPrefs()->SetInteger( | 204 profile->GetPrefs()->SetInteger( |
| 197 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes, 0); | 205 browsing_data::prefs::kClearBrowsingDataHistoryNoticeShownTimes, 0); |
| 198 browsing_data::testing:: | 206 browsing_data::testing:: |
| 199 g_override_other_forms_of_browsing_history_query = true; | 207 g_override_other_forms_of_browsing_history_query = true; |
| 200 } | 208 } |
| 201 | |
| 202 // Set up the chrome://history-frame/ source. | |
| 203 CreateDataSource(); | |
| 204 } | 209 } |
| 205 | 210 |
| 206 HistoryUI::~HistoryUI() {} | 211 HistoryUI::~HistoryUI() {} |
| 207 | 212 |
| 208 // static | 213 // static |
| 209 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes( | 214 base::RefCountedMemory* HistoryUI::GetFaviconResourceBytes( |
| 210 ui::ScaleFactor scale_factor) { | 215 ui::ScaleFactor scale_factor) { |
| 211 return ResourceBundle::GetSharedInstance(). | 216 return ResourceBundle::GetSharedInstance(). |
| 212 LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor); | 217 LoadDataResourceBytesForScale(IDR_HISTORY_FAVICON, scale_factor); |
| 213 } | 218 } |
| 214 | 219 |
| 215 // TODO(lshang): Change to not re-create data source every time after we use | 220 void HistoryUI::UpdateDataSource() { |
| 216 // unique_ptr instead of raw pointers for data source. | 221 CHECK(web_ui()); |
| 217 void HistoryUI::CreateDataSource() { | |
| 218 Profile* profile = Profile::FromWebUI(web_ui()); | 222 Profile* profile = Profile::FromWebUI(web_ui()); |
| 219 content::WebUIDataSource* data_source = CreateHistoryUIHTMLSource(profile); | 223 std::unique_ptr<base::DictionaryValue> update(new base::DictionaryValue); |
| 220 content::WebUIDataSource::Add(profile, data_source); | 224 update->SetBoolean(kIsUserSignedInKey, IsSignedIn(profile)); |
| 225 content::WebUIDataSource::Update(profile, chrome::kChromeUIHistoryFrameHost, |
| 226 std::move(update)); |
| 221 } | 227 } |
| OLD | NEW |