OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/md_history_ui.h" | 5 #include "chrome/browser/ui/webui/md_history_ui.h" |
6 | 6 |
7 #include "build/build_config.h" | 7 #include "build/build_config.h" |
8 #include "chrome/browser/browser_process.h" | |
8 #include "chrome/browser/profiles/profile.h" | 9 #include "chrome/browser/profiles/profile.h" |
9 #include "chrome/browser/ui/webui/browsing_history_handler.h" | 10 #include "chrome/browser/ui/webui/browsing_history_handler.h" |
10 #include "chrome/browser/ui/webui/metrics_handler.h" | 11 #include "chrome/browser/ui/webui/metrics_handler.h" |
11 #include "chrome/common/url_constants.h" | 12 #include "chrome/common/url_constants.h" |
12 #include "components/search/search.h" | 13 #include "components/search/search.h" |
13 #include "content/public/browser/web_ui.h" | 14 #include "content/public/browser/web_ui.h" |
14 #include "content/public/browser/web_ui_data_source.h" | 15 #include "content/public/browser/web_ui_data_source.h" |
15 #include "grit/browser_resources.h" | 16 #include "grit/browser_resources.h" |
16 #include "grit/components_scaled_resources.h" | 17 #include "grit/components_scaled_resources.h" |
17 #include "grit/components_strings.h" | 18 #include "grit/components_strings.h" |
18 #include "grit/generated_resources.h" | 19 #include "grit/generated_resources.h" |
19 #include "grit/theme_resources.h" | 20 #include "grit/theme_resources.h" |
21 #include "ui/base/l10n/l10n_util.h" | |
20 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
23 #include "ui/base/template_expressions.h" | |
24 #include "ui/base/webui/web_ui_util.h" | |
21 | 25 |
22 #if !defined(OS_ANDROID) | 26 #if !defined(OS_ANDROID) |
23 #include "chrome/browser/ui/webui/foreign_session_handler.h" | 27 #include "chrome/browser/ui/webui/foreign_session_handler.h" |
24 #include "chrome/browser/ui/webui/history_login_handler.h" | 28 #include "chrome/browser/ui/webui/history_login_handler.h" |
25 #endif | 29 #endif |
26 | 30 |
27 namespace { | 31 namespace { |
28 | 32 |
33 bool HandleDataRequest( | |
34 const std::string& path, | |
35 const content::WebUIDataSource::GotDataCallback& callback) { | |
36 std::map<const std::string, std::string> substitutions; | |
37 // Localized strings (alphabetical order). | |
38 substitutions["clearSearch"] = | |
39 l10n_util::GetStringUTF8(IDS_MD_HISTORY_CLEAR_SEARCH); | |
40 substitutions["language"] = | |
41 l10n_util::GetLanguage(g_browser_process->GetApplicationLocale()); | |
42 substitutions["moreFromSite"] = | |
43 l10n_util::GetStringUTF8(IDS_HISTORY_MORE_FROM_SITE); | |
44 substitutions["removeFromHistory"] = | |
45 l10n_util::GetStringUTF8(IDS_HISTORY_REMOVE_PAGE); | |
46 substitutions["search"] = l10n_util::GetStringUTF8(IDS_MD_HISTORY_SEARCH); | |
47 substitutions["title"] = l10n_util::GetStringUTF8(IDS_HISTORY_TITLE); | |
48 substitutions["textdirection"] = webui::GetTextDirection(); | |
49 | |
50 std::map<std::string, int> router; | |
calamity
2016/02/01 23:45:33
Can we make this a const static local?
tsergeant
2016/02/02 01:33:10
Yes, we can.
| |
51 // Localized resources (alphabetical order). | |
52 router[""] = IDR_MD_HISTORY_HISTORY_HTML; | |
53 router["history_card.html"] = IDR_MD_HISTORY_HISTORY_CARD_HTML; | |
54 router["history_card_manager.html"] = | |
55 IDR_MD_HISTORY_HISTORY_CARD_MANAGER_HTML; | |
56 router["history_item.html"] = IDR_MD_HISTORY_HISTORY_ITEM_HTML; | |
57 router["history_toolbar.html"] = IDR_MD_HISTORY_HISTORY_TOOLBAR_HTML; | |
58 | |
59 auto path_resource = router.find(path); | |
60 if (path_resource != router.end()) { | |
61 base::StringPiece resource_full_text( | |
62 ResourceBundle::GetSharedInstance().GetRawDataResource( | |
63 (*path_resource).second)); | |
64 | |
65 std::string localized_text = | |
66 ui::ReplaceTemplateExpressions(resource_full_text, substitutions); | |
67 callback.Run(base::RefCountedString::TakeString(&localized_text)); | |
68 | |
69 return true; | |
70 } | |
71 | |
72 return false; | |
73 } | |
74 | |
29 content::WebUIDataSource* CreateMdHistoryUIHTMLSource() { | 75 content::WebUIDataSource* CreateMdHistoryUIHTMLSource() { |
30 content::WebUIDataSource* source = | 76 content::WebUIDataSource* source = |
31 content::WebUIDataSource::Create(chrome::kChromeUIHistoryHost); | 77 content::WebUIDataSource::Create(chrome::kChromeUIHistoryHost); |
32 | 78 |
33 // Localized strings (alphabetical order). | 79 // Resources which do not need localization. |
34 source->AddLocalizedString("clearSearch", IDS_MD_HISTORY_CLEAR_SEARCH); | |
35 source->AddLocalizedString("moreFromSite", IDS_HISTORY_MORE_FROM_SITE); | |
36 source->AddLocalizedString("removeFromHistory", IDS_HISTORY_REMOVE_PAGE); | |
37 source->AddLocalizedString("search", IDS_MD_HISTORY_SEARCH); | |
38 source->AddLocalizedString("title", IDS_HISTORY_TITLE); | |
39 | |
40 source->AddResourcePath("history_card.html", | |
41 IDR_MD_HISTORY_HISTORY_CARD_HTML); | |
42 source->AddResourcePath("history_card.js", IDR_MD_HISTORY_HISTORY_CARD_JS); | 80 source->AddResourcePath("history_card.js", IDR_MD_HISTORY_HISTORY_CARD_JS); |
43 source->AddResourcePath("history_card_manager.html", | |
44 IDR_MD_HISTORY_HISTORY_CARD_MANAGER_HTML); | |
45 source->AddResourcePath("history_card_manager.js", | 81 source->AddResourcePath("history_card_manager.js", |
46 IDR_MD_HISTORY_HISTORY_CARD_MANAGER_JS); | 82 IDR_MD_HISTORY_HISTORY_CARD_MANAGER_JS); |
47 source->AddResourcePath("history_item.html", | |
48 IDR_MD_HISTORY_HISTORY_ITEM_HTML); | |
49 source->AddResourcePath("history_item.js", IDR_MD_HISTORY_HISTORY_ITEM_JS); | 83 source->AddResourcePath("history_item.js", IDR_MD_HISTORY_HISTORY_ITEM_JS); |
50 source->AddResourcePath("history_toolbar.html", | |
51 IDR_MD_HISTORY_HISTORY_TOOLBAR_HTML); | |
52 source->AddResourcePath("history_toolbar.js", | 84 source->AddResourcePath("history_toolbar.js", |
53 IDR_MD_HISTORY_HISTORY_TOOLBAR_JS); | 85 IDR_MD_HISTORY_HISTORY_TOOLBAR_JS); |
54 source->AddResourcePath("history.js", IDR_MD_HISTORY_HISTORY_JS); | 86 source->AddResourcePath("history.js", IDR_MD_HISTORY_HISTORY_JS); |
55 | 87 |
56 source->SetDefaultResource(IDR_MD_HISTORY_HISTORY_HTML); | |
57 source->SetJsonPath("strings.js"); | 88 source->SetJsonPath("strings.js"); |
58 | 89 source->SetRequestFilter(base::Bind(&HandleDataRequest)); |
59 return source; | 90 return source; |
60 } | 91 } |
61 | 92 |
62 } // namespace | 93 } // namespace |
63 | 94 |
64 MdHistoryUI::MdHistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { | 95 MdHistoryUI::MdHistoryUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
65 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); | 96 web_ui->AddMessageHandler(new BrowsingHistoryHandler()); |
66 web_ui->AddMessageHandler(new MetricsHandler()); | 97 web_ui->AddMessageHandler(new MetricsHandler()); |
67 | 98 |
68 // On mobile we deal with foreign sessions differently. | 99 // On mobile we deal with foreign sessions differently. |
69 #if !defined(OS_ANDROID) | 100 #if !defined(OS_ANDROID) |
70 if (search::IsInstantExtendedAPIEnabled()) { | 101 if (search::IsInstantExtendedAPIEnabled()) { |
71 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); | 102 web_ui->AddMessageHandler(new browser_sync::ForeignSessionHandler()); |
72 web_ui->AddMessageHandler(new HistoryLoginHandler()); | 103 web_ui->AddMessageHandler(new HistoryLoginHandler()); |
73 } | 104 } |
74 #endif | 105 #endif |
75 | 106 |
76 Profile* profile = Profile::FromWebUI(web_ui); | 107 Profile* profile = Profile::FromWebUI(web_ui); |
77 content::WebUIDataSource::Add(profile, CreateMdHistoryUIHTMLSource()); | 108 content::WebUIDataSource::Add(profile, CreateMdHistoryUIHTMLSource()); |
78 } | 109 } |
79 | 110 |
80 MdHistoryUI::~MdHistoryUI() {} | 111 MdHistoryUI::~MdHistoryUI() {} |
81 | 112 |
82 base::RefCountedMemory* MdHistoryUI::GetFaviconResourceBytes( | 113 base::RefCountedMemory* MdHistoryUI::GetFaviconResourceBytes( |
83 ui::ScaleFactor scale_factor) { | 114 ui::ScaleFactor scale_factor) { |
84 return ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( | 115 return ResourceBundle::GetSharedInstance().LoadDataResourceBytesForScale( |
85 IDR_HISTORY_FAVICON, scale_factor); | 116 IDR_HISTORY_FAVICON, scale_factor); |
86 } | 117 } |
OLD | NEW |