Commit d64a84c4 authored by Dan Beam's avatar Dan Beam Committed by Commit Bot

local_ntp: use $i18n{} template replacements for resource integrity

$i18n{} is used across many other web UI pages in Chrom{e,ium}
and is recommended by styleguide/web/web.md.

In this case, its also more efficient as it only need make 1 pass in
total over the contents of local_ntp.html (current code makes 8).

R=kristipark@chromium.org
BUG=none

Change-Id: Ie839d12322ab4baca8445eeba1a8a47c76d22e61
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1586695
Commit-Queue: Dan Beam <dbeam@chromium.org>
Commit-Queue: Kristi Park <kristipark@chromium.org>
Auto-Submit: Dan Beam <dbeam@chromium.org>
Reviewed-by: default avatarKristi Park <kristipark@chromium.org>
Cr-Commit-Position: refs/heads/master@{#654703}
parent bcff6cdc
...@@ -12,19 +12,19 @@ ...@@ -12,19 +12,19 @@
<link rel="stylesheet" href="chrome-search://local-ntp/theme.css"></link> <link rel="stylesheet" href="chrome-search://local-ntp/theme.css"></link>
<link rel="stylesheet" href="chrome-search://local-ntp/voice.css"></link> <link rel="stylesheet" href="chrome-search://local-ntp/voice.css"></link>
<meta http-equiv="Content-Security-Policy" <meta http-equiv="Content-Security-Policy"
content="{{CONTENT_SECURITY_POLICY}}"> content="$i18nRaw{contentSecurityPolicy}">
<script src="chrome-search://local-ntp/animations.js" <script src="chrome-search://local-ntp/animations.js"
{{ANIMATIONS_INTEGRITY}}></script> $i18nRaw{animationsIntegrity}></script>
<script src="chrome-search://local-ntp/config.js" <script src="chrome-search://local-ntp/config.js"
{{CONFIG_DATA_INTEGRITY}}></script> $i18nRaw{configDataIntegrity}></script>
<script src="chrome-search://local-ntp/custom-backgrounds.js" <script src="chrome-search://local-ntp/custom-backgrounds.js"
{{LOCAL_NTP_CUSTOM_BG_INTEGRITY}}></script> $i18nRaw{localNtpCustomBgIntegrity}></script>
<script src="chrome-search://local-ntp/doodles.js" <script src="chrome-search://local-ntp/doodles.js"
{{DOODLES_INTEGRITY}}></script> $i18nRaw{doodlesIntegrity}></script>
<script src="chrome-search://local-ntp/local-ntp.js" <script src="chrome-search://local-ntp/local-ntp.js"
{{LOCAL_NTP_INTEGRITY}}></script> $i18nRaw{localNtpIntegrity}></script>
<script src="chrome-search://local-ntp/utils.js" <script src="chrome-search://local-ntp/utils.js"
{{UTILS_INTEGRITY}}></script> $i18nRaw{utilsIntegrity}></script>
<meta charset="utf-8"> <meta charset="utf-8">
<meta name="google" value="notranslate"> <meta name="google" value="notranslate">
<meta name="referrer" content="strict-origin"> <meta name="referrer" content="strict-origin">
...@@ -207,6 +207,6 @@ ...@@ -207,6 +207,6 @@
<div id="one-google-end-of-body"></div> <div id="one-google-end-of-body"></div>
<script defer src="chrome-search://local-ntp/voice.js" <script defer src="chrome-search://local-ntp/voice.js"
{{LOCAL_NTP_VOICE_INTEGRITY}}></script> $i18nRaw{localNtpVoiceIntegrity}></script>
</body> </body>
</html> </html>
...@@ -18,7 +18,6 @@ ...@@ -18,7 +18,6 @@
#include "base/path_service.h" #include "base/path_service.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "base/strings/string_number_conversions.h" #include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h" #include "base/strings/stringprintf.h"
#include "base/task/post_task.h" #include "base/task/post_task.h"
#include "base/values.h" #include "base/values.h"
...@@ -74,6 +73,7 @@ ...@@ -74,6 +73,7 @@
#include "third_party/skia/include/core/SkColor.h" #include "third_party/skia/include/core/SkColor.h"
#include "ui/base/l10n/l10n_util.h" #include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/base/template_expressions.h"
#include "ui/base/webui/web_ui_util.h" #include "ui/base/webui/web_ui_util.h"
#include "ui/resources/grit/ui_resources.h" #include "ui/resources/grit/ui_resources.h"
#include "url/gurl.h" #include "url/gurl.h"
...@@ -905,62 +905,43 @@ void LocalNtpSource::StartDataRequest( ...@@ -905,62 +905,43 @@ void LocalNtpSource::StartDataRequest(
#endif // !defined(GOOGLE_CHROME_BUILD) #endif // !defined(GOOGLE_CHROME_BUILD)
if (stripped_path == kMainHtmlFilename) { if (stripped_path == kMainHtmlFilename) {
std::string html = ui::ResourceBundle::GetSharedInstance()
.GetRawDataResource(IDR_LOCAL_NTP_HTML)
.as_string();
std::string animations_integrity =
base::StringPrintf(kIntegrityFormat, ANIMATIONS_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{ANIMATIONS_INTEGRITY}}",
animations_integrity);
std::string config_data_integrity = base::StringPrintf(
kIntegrityFormat,
search_config_provider_->config_data_integrity().c_str());
base::ReplaceFirstSubstringAfterOffset(
&html, 0, "{{CONFIG_DATA_INTEGRITY}}", config_data_integrity);
std::string custom_bg_integrity =
base::StringPrintf(kIntegrityFormat, CUSTOM_BACKGROUNDS_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(
&html, 0, "{{LOCAL_NTP_CUSTOM_BG_INTEGRITY}}", custom_bg_integrity);
std::string doodles_integrity =
base::StringPrintf(kIntegrityFormat, DOODLES_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{DOODLES_INTEGRITY}}",
doodles_integrity);
std::string local_ntp_integrity =
base::StringPrintf(kIntegrityFormat, LOCAL_NTP_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{LOCAL_NTP_INTEGRITY}}",
local_ntp_integrity);
std::string utils_integrity =
base::StringPrintf(kIntegrityFormat, UTILS_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(&html, 0, "{{UTILS_INTEGRITY}}",
utils_integrity);
std::string voice_integrity =
base::StringPrintf(kIntegrityFormat, VOICE_JS_INTEGRITY);
base::ReplaceFirstSubstringAfterOffset(
&html, 0, "{{LOCAL_NTP_VOICE_INTEGRITY}}", voice_integrity);
base::ReplaceFirstSubstringAfterOffset(
&html, 0, "{{CONTENT_SECURITY_POLICY}}", GetContentSecurityPolicy());
std::string force_doodle_param; std::string force_doodle_param;
GURL path_url = GURL(chrome::kChromeSearchLocalNtpUrl).Resolve(path); GURL path_url = GURL(chrome::kChromeSearchLocalNtpUrl).Resolve(path);
if (net::GetValueForKeyInQuery(path_url, "force-doodle", if (net::GetValueForKeyInQuery(path_url, "force-doodle",
&force_doodle_param)) { &force_doodle_param)) {
base::CommandLine* command_line = base::CommandLine::ForCurrentProcess(); base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
command_line->AppendSwitchASCII( command_line->AppendSwitchASCII(
search_provider_logos::switches::kGoogleDoodleUrl, search_provider_logos::switches::kGoogleDoodleUrl,
"https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_desktop" + "https://www.gstatic.com/chrome/ntp/doodle_test/ddljson_desktop" +
force_doodle_param + ".json"); force_doodle_param + ".json");
} }
callback.Run(base::RefCountedString::TakeString(&html)); // TODO(dbeam): rewrite this class to WebUIDataSource instead of
// URLDataSource, and get magical $i18n{} replacement for free.
ui::TemplateReplacements replacements;
replacements["animationsIntegrity"] =
base::StringPrintf(kIntegrityFormat, ANIMATIONS_JS_INTEGRITY);
replacements["configDataIntegrity"] = base::StringPrintf(
kIntegrityFormat,
search_config_provider_->config_data_integrity().c_str());
replacements["localNtpCustomBgIntegrity"] =
base::StringPrintf(kIntegrityFormat, CUSTOM_BACKGROUNDS_JS_INTEGRITY);
replacements["doodlesIntegrity"] =
base::StringPrintf(kIntegrityFormat, DOODLES_JS_INTEGRITY);
replacements["localNtpIntegrity"] =
base::StringPrintf(kIntegrityFormat, LOCAL_NTP_JS_INTEGRITY);
replacements["utilsIntegrity"] =
base::StringPrintf(kIntegrityFormat, UTILS_JS_INTEGRITY);
replacements["localNtpVoiceIntegrity"] =
base::StringPrintf(kIntegrityFormat, VOICE_JS_INTEGRITY);
// TODO(dbeam): why is this needed? How does it interact with
// URLDataSource::GetContentSecurityPolicy*() methods?
replacements["contentSecurityPolicy"] = GetContentSecurityPolicy();
ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
base::StringPiece html = bundle.GetRawDataResource(IDR_LOCAL_NTP_HTML);
std::string replaced = ui::ReplaceTemplateExpressions(html, replacements);
callback.Run(base::RefCountedString::TakeString(&replaced));
return; return;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment