OLD | NEW |
---|---|
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 Loading... | |
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) { |
100 localized_strings_.SetString(name, value); | 98 localized_strings_.SetString(name, value); |
99 replacements_[name] = base::UTF16ToUTF8(value); | |
101 } | 100 } |
102 | 101 |
103 void WebUIDataSourceImpl::AddString(const std::string& name, | 102 void WebUIDataSourceImpl::AddString(const std::string& name, |
104 const std::string& value) { | 103 const std::string& value) { |
105 localized_strings_.SetString(name, value); | 104 localized_strings_.SetString(name, value); |
105 replacements_[name] = value; | |
106 } | 106 } |
107 | 107 |
108 void WebUIDataSourceImpl::AddLocalizedString(const std::string& name, | 108 void WebUIDataSourceImpl::AddLocalizedString(const std::string& name, |
109 int ids) { | 109 int ids) { |
110 localized_strings_.SetString( | 110 localized_strings_.SetString( |
111 name, GetContentClient()->GetLocalizedString(ids)); | 111 name, GetContentClient()->GetLocalizedString(ids)); |
112 replacements_[name] = | |
113 base::UTF16ToUTF8(GetContentClient()->GetLocalizedString(ids)); | |
112 } | 114 } |
113 | 115 |
114 void WebUIDataSourceImpl::AddLocalizedStrings( | 116 void WebUIDataSourceImpl::AddLocalizedStrings( |
115 const base::DictionaryValue& localized_strings) { | 117 const base::DictionaryValue& localized_strings) { |
116 localized_strings_.MergeDictionary(&localized_strings); | 118 localized_strings_.MergeDictionary(&localized_strings); |
117 } | 119 } |
118 | 120 |
119 void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) { | 121 void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) { |
120 localized_strings_.SetBoolean(name, value); | 122 localized_strings_.SetBoolean(name, value); |
123 // TODO(dschuyler) Change name of |localized_strings_| to |load_time_data_| | |
124 // or similar. These values haven't been found as strings for | |
125 // localization. The boolean values are not added to |localized_strings_map_| | |
126 // for the same reason, that they are used as flags, rather than string | |
127 // replacements. | |
121 } | 128 } |
122 | 129 |
123 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) { | 130 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) { |
124 json_path_ = path; | 131 json_path_ = path; |
125 } | 132 } |
126 | 133 |
127 void WebUIDataSourceImpl::AddResourcePath(const std::string &path, | 134 void WebUIDataSourceImpl::AddResourcePath(const std::string &path, |
128 int resource_id) { | 135 int resource_id) { |
129 path_to_idr_map_[path] = resource_id; | 136 path_to_idr_map_[path] = resource_id; |
130 } | 137 } |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
204 } | 211 } |
205 | 212 |
206 int resource_id = default_resource_; | 213 int resource_id = default_resource_; |
207 std::map<std::string, int>::iterator result; | 214 std::map<std::string, int>::iterator result; |
208 // Remove the query string for named resource lookups. | 215 // Remove the query string for named resource lookups. |
209 std::string file_path = path.substr(0, path.find_first_of('?')); | 216 std::string file_path = path.substr(0, path.find_first_of('?')); |
210 result = path_to_idr_map_.find(file_path); | 217 result = path_to_idr_map_.find(file_path); |
211 if (result != path_to_idr_map_.end()) | 218 if (result != path_to_idr_map_.end()) |
212 resource_id = result->second; | 219 resource_id = result->second; |
213 DCHECK_NE(resource_id, -1); | 220 DCHECK_NE(resource_id, -1); |
214 SendFromResourceBundle(callback, resource_id); | 221 scoped_refptr<base::RefCountedMemory> response( |
222 GetContentClient()->GetDataResourceBytes(resource_id)); | |
223 | |
224 // TODO(dschuyler) improve filtering of which resource to run | |
225 // 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.
| |
226 if (GetMimeType(path) == "text/html") { | |
227 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.
| |
228 base::StringPiece(response->front_as<char>(), response->size()), | |
229 replacements_); | |
230 response = base::RefCountedString::TakeString(&final_version); | |
231 } | |
232 | |
233 callback.Run(response.get()); | |
215 } | 234 } |
216 | 235 |
217 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( | 236 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON( |
218 const URLDataSource::GotDataCallback& callback) { | 237 const URLDataSource::GotDataCallback& callback) { |
219 std::string template_data; | 238 std::string template_data; |
220 if (!disable_set_font_strings_) { | 239 if (!disable_set_font_strings_) { |
221 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); | 240 std::string locale = GetContentClient()->browser()->GetApplicationLocale(); |
222 webui::SetLoadTimeDataDefaults(locale, &localized_strings_); | 241 webui::SetLoadTimeDataDefaults(locale, &localized_strings_); |
223 } | 242 } |
224 | 243 |
225 webui::AppendJsonJS(&localized_strings_, &template_data); | 244 webui::AppendJsonJS(&localized_strings_, &template_data); |
226 callback.Run(base::RefCountedString::TakeString(&template_data)); | 245 callback.Run(base::RefCountedString::TakeString(&template_data)); |
227 } | 246 } |
228 | 247 |
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 | 248 } // namespace content |
OLD | NEW |