Chromium Code Reviews| 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..0002dd12064e165bcdcba5a6c5f2fd986a1e3b48 100644 |
| --- a/content/browser/webui/web_ui_data_source_impl.cc |
| +++ b/content/browser/webui/web_ui_data_source_impl.cc |
| @@ -1,4 +1,4 @@ |
| -// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Copyright 2012 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| @@ -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,22 @@ 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)); |
| + base::UTF16ToUTF8(GetContentClient()->GetLocalizedString(ids)); |
|
Dan Beam
2016/02/11 02:12:32
wat
dschuyler
2016/02/11 02:22:29
Done.
|
| + replacements_[name] = |
| + base::UTF16ToUTF8(GetContentClient()->GetLocalizedString(ids)); |
| } |
| void WebUIDataSourceImpl::AddLocalizedStrings( |
| @@ -118,6 +121,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_flags_| |
|
Dan Beam
2016/02/11 02:12:32
nit: load_time_data_
dschuyler
2016/02/11 02:22:29
Done.
|
| + // or similar. These values haven't been found as strings for |
|
Dan Beam
2016/02/11 02:12:32
one space between sentences in comments
dschuyler
2016/02/11 02:22:29
Done.
|
| + // 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 +219,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. |
| + if (GetMimeType(path) == "text/html") { |
| + std::string final_version = ui::ReplaceTemplateExpressions( |
| + base::StringPiece(response->front_as<char>(), response->size()), |
| + replacements_); |
| + response = base::RefCountedString::TakeString(&final_version); |
| + } |
| + |
| + callback.Run(response.get()); |
| } |
| void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
| @@ -226,11 +246,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 |