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

Side by Side 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: changed comments 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 unified diff | Download patch
« no previous file with comments | « content/browser/webui/web_ui_data_source_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 "content/browser/webui/web_ui_data_source_impl.h" 5 #include "content/browser/webui/web_ui_data_source_impl.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/macros.h" 12 #include "base/macros.h"
13 #include "base/memory/ref_counted_memory.h" 13 #include "base/memory/ref_counted_memory.h"
14 #include "base/strings/string_util.h" 14 #include "base/strings/string_util.h"
15 #include "base/strings/utf_string_conversions.h"
15 #include "content/grit/content_resources.h" 16 #include "content/grit/content_resources.h"
16 #include "content/public/browser/content_browser_client.h" 17 #include "content/public/browser/content_browser_client.h"
17 #include "content/public/common/content_client.h" 18 #include "content/public/common/content_client.h"
19 #include "ui/base/template_expressions.h"
18 #include "ui/base/webui/jstemplate_builder.h" 20 #include "ui/base/webui/jstemplate_builder.h"
19 #include "ui/base/webui/web_ui_util.h" 21 #include "ui/base/webui/web_ui_util.h"
20 22
21 namespace content { 23 namespace content {
22 24
23 // static 25 // static
24 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) { 26 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) {
25 return new WebUIDataSourceImpl(source_name); 27 return new WebUIDataSourceImpl(source_name);
26 } 28 }
27 29
28 // static 30 // static
29 void WebUIDataSource::Add(BrowserContext* browser_context, 31 void WebUIDataSource::Add(BrowserContext* browser_context,
30 WebUIDataSource* source) { 32 WebUIDataSource* source) {
31 URLDataManager::AddWebUIDataSource(browser_context, source); 33 URLDataManager::AddWebUIDataSource(browser_context, source);
32 } 34 }
33 35
34 // Internal class to hide the fact that WebUIDataSourceImpl implements 36 // Internal class to hide the fact that WebUIDataSourceImpl implements
35 // URLDataSource. 37 // URLDataSource.
36 class WebUIDataSourceImpl::InternalDataSource : public URLDataSource { 38 class WebUIDataSourceImpl::InternalDataSource : public URLDataSource {
37 public: 39 public:
38 InternalDataSource(WebUIDataSourceImpl* parent) : parent_(parent) { 40 explicit InternalDataSource(WebUIDataSourceImpl* parent) : parent_(parent) {}
39 }
40 41
41 ~InternalDataSource() override {} 42 ~InternalDataSource() override {}
42 43
43 // URLDataSource implementation. 44 // URLDataSource implementation.
44 std::string GetSource() const override { return parent_->GetSource(); } 45 std::string GetSource() const override { return parent_->GetSource(); }
45 std::string GetMimeType(const std::string& path) const override { 46 std::string GetMimeType(const std::string& path) const override {
46 return parent_->GetMimeType(path); 47 return parent_->GetMimeType(path);
47 } 48 }
48 void StartDataRequest( 49 void StartDataRequest(
49 const std::string& path, 50 const std::string& path,
(...skipping 22 matching lines...) Expand all
72 } 73 }
73 bool ShouldDenyXFrameOptions() const override { 74 bool ShouldDenyXFrameOptions() const override {
74 return parent_->deny_xframe_options_; 75 return parent_->deny_xframe_options_;
75 } 76 }
76 77
77 private: 78 private:
78 WebUIDataSourceImpl* parent_; 79 WebUIDataSourceImpl* parent_;
79 }; 80 };
80 81
81 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name) 82 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name)
82 : URLDataSourceImpl( 83 : URLDataSourceImpl(source_name, new InternalDataSource(this)),
83 source_name,
84 new InternalDataSource(this)),
85 source_name_(source_name), 84 source_name_(source_name),
86 default_resource_(-1), 85 default_resource_(-1),
87 add_csp_(true), 86 add_csp_(true),
88 object_src_set_(false), 87 object_src_set_(false),
89 frame_src_set_(false), 88 frame_src_set_(false),
90 deny_xframe_options_(true), 89 deny_xframe_options_(true),
91 disable_set_font_strings_(false), 90 disable_set_font_strings_(false),
92 replace_existing_source_(true) { 91 replace_existing_source_(true) {}
93 }
94 92
95 WebUIDataSourceImpl::~WebUIDataSourceImpl() { 93 WebUIDataSourceImpl::~WebUIDataSourceImpl() {
96 } 94 }
97 95
98 void WebUIDataSourceImpl::AddString(const std::string& name, 96 void WebUIDataSourceImpl::AddString(const std::string& name,
99 const base::string16& value) { 97 const base::string16& value) {
98 // TODO(dschuyler): Share only one copy of these strings.
100 localized_strings_.SetString(name, value); 99 localized_strings_.SetString(name, value);
100 replacements_[name] = base::UTF16ToUTF8(value);
101 } 101 }
102 102
103 void WebUIDataSourceImpl::AddString(const std::string& name, 103 void WebUIDataSourceImpl::AddString(const std::string& name,
104 const std::string& value) { 104 const std::string& value) {
105 localized_strings_.SetString(name, value); 105 localized_strings_.SetString(name, value);
106 replacements_[name] = value;
106 } 107 }
107 108
108 void WebUIDataSourceImpl::AddLocalizedString(const std::string& name, 109 void WebUIDataSourceImpl::AddLocalizedString(const std::string& name,
109 int ids) { 110 int ids) {
110 localized_strings_.SetString( 111 localized_strings_.SetString(
111 name, GetContentClient()->GetLocalizedString(ids)); 112 name, GetContentClient()->GetLocalizedString(ids));
113 replacements_[name] =
114 base::UTF16ToUTF8(GetContentClient()->GetLocalizedString(ids));
112 } 115 }
113 116
114 void WebUIDataSourceImpl::AddLocalizedStrings( 117 void WebUIDataSourceImpl::AddLocalizedStrings(
115 const base::DictionaryValue& localized_strings) { 118 const base::DictionaryValue& localized_strings) {
116 localized_strings_.MergeDictionary(&localized_strings); 119 localized_strings_.MergeDictionary(&localized_strings);
117 } 120 }
118 121
119 void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) { 122 void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) {
120 localized_strings_.SetBoolean(name, value); 123 localized_strings_.SetBoolean(name, value);
124 // TODO(dschuyler): Change name of |localized_strings_| to |load_time_data_|
125 // or similar. These values haven't been found as strings for
126 // localization. The boolean values are not added to |replacements_|
127 // for the same reason, that they are used as flags, rather than string
128 // replacements.
121 } 129 }
122 130
123 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) { 131 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) {
124 json_path_ = path; 132 json_path_ = path;
125 } 133 }
126 134
127 void WebUIDataSourceImpl::AddResourcePath(const std::string &path, 135 void WebUIDataSourceImpl::AddResourcePath(const std::string &path,
128 int resource_id) { 136 int resource_id) {
129 path_to_idr_map_[path] = resource_id; 137 path_to_idr_map_[path] = resource_id;
130 } 138 }
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 } 212 }
205 213
206 int resource_id = default_resource_; 214 int resource_id = default_resource_;
207 std::map<std::string, int>::iterator result; 215 std::map<std::string, int>::iterator result;
208 // Remove the query string for named resource lookups. 216 // Remove the query string for named resource lookups.
209 std::string file_path = path.substr(0, path.find_first_of('?')); 217 std::string file_path = path.substr(0, path.find_first_of('?'));
210 result = path_to_idr_map_.find(file_path); 218 result = path_to_idr_map_.find(file_path);
211 if (result != path_to_idr_map_.end()) 219 if (result != path_to_idr_map_.end())
212 resource_id = result->second; 220 resource_id = result->second;
213 DCHECK_NE(resource_id, -1); 221 DCHECK_NE(resource_id, -1);
214 SendFromResourceBundle(callback, resource_id); 222 scoped_refptr<base::RefCountedMemory> response(
223 GetContentClient()->GetDataResourceBytes(resource_id));
224
225 // TODO(dschuyler): improve filtering of which resource to run template
226 // expansion upon.
227 if (GetMimeType(path) == "text/html") {
228 std::string replaced = ui::ReplaceTemplateExpressions(
229 base::StringPiece(response->front_as<char>(), response->size()),
230 replacements_);
231 response = base::RefCountedString::TakeString(&replaced);
232 }
233
234 callback.Run(response.get());
215 } 235 }
216 236
217 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( 237 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON(
218 const URLDataSource::GotDataCallback& callback) { 238 const URLDataSource::GotDataCallback& callback) {
219 std::string template_data; 239 std::string template_data;
220 if (!disable_set_font_strings_) { 240 if (!disable_set_font_strings_) {
221 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); 241 std::string locale = GetContentClient()->browser()->GetApplicationLocale();
222 webui::SetLoadTimeDataDefaults(locale, &localized_strings_); 242 webui::SetLoadTimeDataDefaults(locale, &localized_strings_);
223 } 243 }
224 244
225 webui::AppendJsonJS(&localized_strings_, &template_data); 245 webui::AppendJsonJS(&localized_strings_, &template_data);
226 callback.Run(base::RefCountedString::TakeString(&template_data)); 246 callback.Run(base::RefCountedString::TakeString(&template_data));
227 } 247 }
228 248
229 void WebUIDataSourceImpl::SendFromResourceBundle(
230 const URLDataSource::GotDataCallback& callback, int idr) {
231 scoped_refptr<base::RefCountedStaticMemory> response(
232 GetContentClient()->GetDataResourceBytes(idr));
233 callback.Run(response.get());
234 }
235
236 } // namespace content 249 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/webui/web_ui_data_source_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698