Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1922)

Unified Diff: content/browser/webui/web_ui_data_source_impl.cc

Issue 1690773003: [Template expressions] expanding $i18n expressions on webui html (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: removed extra space in comment Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/browser/webui/web_ui_data_source_impl.cc
diff --git a/content/browser/webui/web_ui_data_source_impl.cc b/content/browser/webui/web_ui_data_source_impl.cc
index e8b5b9fdedbcfdf964678964695c33e40f04fea3..4e16b544d9f9e4e033fba55a96e7654b5c0eb4f6 100644
--- a/content/browser/webui/web_ui_data_source_impl.cc
+++ b/content/browser/webui/web_ui_data_source_impl.cc
@@ -12,9 +12,11 @@
#include "base/macros.h"
#include "base/memory/ref_counted_memory.h"
#include "base/strings/string_util.h"
+#include "base/strings/utf_string_conversions.h"
#include "content/grit/content_resources.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/common/content_client.h"
+#include "ui/base/template_expressions.h"
#include "ui/base/webui/jstemplate_builder.h"
#include "ui/base/webui/web_ui_util.h"
@@ -35,8 +37,7 @@ void WebUIDataSource::Add(BrowserContext* browser_context,
// URLDataSource.
class WebUIDataSourceImpl::InternalDataSource : public URLDataSource {
public:
- InternalDataSource(WebUIDataSourceImpl* parent) : parent_(parent) {
- }
+ explicit InternalDataSource(WebUIDataSourceImpl* parent) : parent_(parent) {}
~InternalDataSource() override {}
@@ -79,9 +80,7 @@ class WebUIDataSourceImpl::InternalDataSource : public URLDataSource {
};
WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name)
- : URLDataSourceImpl(
- source_name,
- new InternalDataSource(this)),
+ : URLDataSourceImpl(source_name, new InternalDataSource(this)),
source_name_(source_name),
default_resource_(-1),
add_csp_(true),
@@ -89,8 +88,7 @@ WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name)
frame_src_set_(false),
deny_xframe_options_(true),
disable_set_font_strings_(false),
- replace_existing_source_(true) {
-}
+ replace_existing_source_(true) {}
WebUIDataSourceImpl::~WebUIDataSourceImpl() {
}
@@ -98,17 +96,21 @@ WebUIDataSourceImpl::~WebUIDataSourceImpl() {
void WebUIDataSourceImpl::AddString(const std::string& name,
const base::string16& value) {
localized_strings_.SetString(name, value);
+ replacements_[name] = base::UTF16ToUTF8(value);
}
void WebUIDataSourceImpl::AddString(const std::string& name,
const std::string& value) {
localized_strings_.SetString(name, value);
+ replacements_[name] = value;
}
void WebUIDataSourceImpl::AddLocalizedString(const std::string& name,
int ids) {
localized_strings_.SetString(
name, GetContentClient()->GetLocalizedString(ids));
+ replacements_[name] =
+ base::UTF16ToUTF8(GetContentClient()->GetLocalizedString(ids));
}
void WebUIDataSourceImpl::AddLocalizedStrings(
@@ -118,6 +120,11 @@ void WebUIDataSourceImpl::AddLocalizedStrings(
void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) {
localized_strings_.SetBoolean(name, value);
+ // TODO(dschuyler) Change name of |localized_strings_| to |load_time_data_|
+ // or similar. These values haven't been found as strings for
+ // localization. The boolean values are not added to |localized_strings_map_|
+ // for the same reason, that they are used as flags, rather than string
+ // replacements.
}
void WebUIDataSourceImpl::SetJsonPath(const std::string& path) {
@@ -211,7 +218,19 @@ void WebUIDataSourceImpl::StartDataRequest(
if (result != path_to_idr_map_.end())
resource_id = result->second;
DCHECK_NE(resource_id, -1);
- SendFromResourceBundle(callback, resource_id);
+ scoped_refptr<base::RefCountedMemory> response(
+ GetContentClient()->GetDataResourceBytes(resource_id));
+
+ // TODO(dschuyler) improve filtering of which resource to run
+ // template expansion upon.
Dan Beam 2016/02/12 23:45:14 TODO(dschuyler): ^ also, don't wra
dschuyler 2016/02/13 00:23:24 Done.
Dan Beam 2016/02/13 00:28:57 this is what you have written 3 times in this CL:
Dan Beam 2016/02/13 00:29:17 what I think it should be
dschuyler 2016/02/13 00:38:58 Done.
dschuyler 2016/02/13 00:38:58 Acknowledged.
+ if (GetMimeType(path) == "text/html") {
+ std::string final_version = ui::ReplaceTemplateExpressions(
Dan Beam 2016/02/12 23:45:14 final_version -> replaced, translated
dschuyler 2016/02/13 00:23:24 Done.
+ base::StringPiece(response->front_as<char>(), response->size()),
+ replacements_);
+ response = base::RefCountedString::TakeString(&final_version);
+ }
+
+ callback.Run(response.get());
}
void WebUIDataSourceImpl::SendLocalizedStringsAsJSON(
@@ -226,11 +245,4 @@ void WebUIDataSourceImpl::SendLocalizedStringsAsJSON(
callback.Run(base::RefCountedString::TakeString(&template_data));
}
-void WebUIDataSourceImpl::SendFromResourceBundle(
- const URLDataSource::GotDataCallback& callback, int idr) {
- scoped_refptr<base::RefCountedStaticMemory> response(
- GetContentClient()->GetDataResourceBytes(idr));
- callback.Run(response.get());
-}
-
} // namespace content

Powered by Google App Engine
This is Rietveld 408576698