Blink roll 25b6bd3a7a131ffe68d809546ad1a20707915cdc:3a503f41ae42e5b79cfcd2ff10e65afde...
[chromium-blink-merge.git] / content / browser / webui / web_ui_data_source_impl.cc
blobcaa8f9ad858d3fa4fc594c3380c46548dca7a253
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
3 // found in the LICENSE file.
5 #include "content/browser/webui/web_ui_data_source_impl.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/strings/string_util.h"
12 #include "content/grit/content_resources.h"
13 #include "content/public/common/content_client.h"
14 #include "mojo/public/js/constants.h"
15 #include "ui/base/webui/jstemplate_builder.h"
16 #include "ui/base/webui/web_ui_util.h"
18 namespace content {
20 // static
21 WebUIDataSource* WebUIDataSource::Create(const std::string& source_name) {
22 return new WebUIDataSourceImpl(source_name);
25 // static
26 WebUIDataSource* WebUIDataSource::AddMojoDataSource(
27 BrowserContext* browser_context) {
28 WebUIDataSource* mojo_source = Create("mojo");
30 static const struct {
31 const char* path;
32 int id;
33 } resources[] = {
34 { mojo::kBufferModuleName, IDR_MOJO_BUFFER_JS },
35 { mojo::kCodecModuleName, IDR_MOJO_CODEC_JS },
36 { mojo::kConnectionModuleName, IDR_MOJO_CONNECTION_JS },
37 { mojo::kConnectorModuleName, IDR_MOJO_CONNECTOR_JS },
38 { mojo::kRouterModuleName, IDR_MOJO_ROUTER_JS },
39 { mojo::kUnicodeModuleName, IDR_MOJO_UNICODE_JS },
40 { mojo::kValidatorModuleName, IDR_MOJO_VALIDATOR_JS },
42 for (size_t i = 0; i < arraysize(resources); ++i)
43 mojo_source->AddResourcePath(resources[i].path, resources[i].id);
45 URLDataManager::AddWebUIDataSource(browser_context, mojo_source);
46 return mojo_source;
49 // static
50 void WebUIDataSource::Add(BrowserContext* browser_context,
51 WebUIDataSource* source) {
52 URLDataManager::AddWebUIDataSource(browser_context, source);
55 // Internal class to hide the fact that WebUIDataSourceImpl implements
56 // URLDataSource.
57 class WebUIDataSourceImpl::InternalDataSource : public URLDataSource {
58 public:
59 InternalDataSource(WebUIDataSourceImpl* parent) : parent_(parent) {
62 ~InternalDataSource() override {}
64 // URLDataSource implementation.
65 std::string GetSource() const override { return parent_->GetSource(); }
66 std::string GetMimeType(const std::string& path) const override {
67 return parent_->GetMimeType(path);
69 void StartDataRequest(
70 const std::string& path,
71 int render_process_id,
72 int render_frame_id,
73 const URLDataSource::GotDataCallback& callback) override {
74 return parent_->StartDataRequest(path, render_process_id, render_frame_id,
75 callback);
77 bool ShouldReplaceExistingSource() const override {
78 return parent_->replace_existing_source_;
80 bool AllowCaching() const override { return false; }
81 bool ShouldAddContentSecurityPolicy() const override {
82 return parent_->add_csp_;
84 std::string GetContentSecurityPolicyObjectSrc() const override {
85 if (parent_->object_src_set_)
86 return parent_->object_src_;
87 return URLDataSource::GetContentSecurityPolicyObjectSrc();
89 std::string GetContentSecurityPolicyFrameSrc() const override {
90 if (parent_->frame_src_set_)
91 return parent_->frame_src_;
92 return URLDataSource::GetContentSecurityPolicyFrameSrc();
94 bool ShouldDenyXFrameOptions() const override {
95 return parent_->deny_xframe_options_;
98 private:
99 WebUIDataSourceImpl* parent_;
102 WebUIDataSourceImpl::WebUIDataSourceImpl(const std::string& source_name)
103 : URLDataSourceImpl(
104 source_name,
105 new InternalDataSource(this)),
106 source_name_(source_name),
107 default_resource_(-1),
108 add_csp_(true),
109 object_src_set_(false),
110 frame_src_set_(false),
111 deny_xframe_options_(true),
112 disable_set_font_strings_(false),
113 replace_existing_source_(true) {
116 WebUIDataSourceImpl::~WebUIDataSourceImpl() {
119 void WebUIDataSourceImpl::AddString(const std::string& name,
120 const base::string16& value) {
121 localized_strings_.SetString(name, value);
124 void WebUIDataSourceImpl::AddString(const std::string& name,
125 const std::string& value) {
126 localized_strings_.SetString(name, value);
129 void WebUIDataSourceImpl::AddLocalizedString(const std::string& name,
130 int ids) {
131 localized_strings_.SetString(
132 name, GetContentClient()->GetLocalizedString(ids));
135 void WebUIDataSourceImpl::AddLocalizedStrings(
136 const base::DictionaryValue& localized_strings) {
137 localized_strings_.MergeDictionary(&localized_strings);
140 void WebUIDataSourceImpl::AddBoolean(const std::string& name, bool value) {
141 localized_strings_.SetBoolean(name, value);
144 void WebUIDataSourceImpl::SetJsonPath(const std::string& path) {
145 json_path_ = path;
148 void WebUIDataSourceImpl::AddResourcePath(const std::string &path,
149 int resource_id) {
150 path_to_idr_map_[path] = resource_id;
153 void WebUIDataSourceImpl::SetDefaultResource(int resource_id) {
154 default_resource_ = resource_id;
157 void WebUIDataSourceImpl::SetRequestFilter(
158 const WebUIDataSource::HandleRequestCallback& callback) {
159 filter_callback_ = callback;
162 void WebUIDataSourceImpl::DisableReplaceExistingSource() {
163 replace_existing_source_ = false;
166 void WebUIDataSourceImpl::DisableContentSecurityPolicy() {
167 add_csp_ = false;
170 void WebUIDataSourceImpl::OverrideContentSecurityPolicyObjectSrc(
171 const std::string& data) {
172 object_src_set_ = true;
173 object_src_ = data;
176 void WebUIDataSourceImpl::OverrideContentSecurityPolicyFrameSrc(
177 const std::string& data) {
178 frame_src_set_ = true;
179 frame_src_ = data;
182 void WebUIDataSourceImpl::DisableDenyXFrameOptions() {
183 deny_xframe_options_ = false;
186 std::string WebUIDataSourceImpl::GetSource() const {
187 return source_name_;
190 std::string WebUIDataSourceImpl::GetMimeType(const std::string& path) const {
191 if (EndsWith(path, ".js", false))
192 return "application/javascript";
194 if (EndsWith(path, ".json", false))
195 return "application/json";
197 if (EndsWith(path, ".pdf", false))
198 return "application/pdf";
200 if (EndsWith(path, ".svg", false))
201 return "image/svg+xml";
203 return "text/html";
206 void WebUIDataSourceImpl::StartDataRequest(
207 const std::string& path,
208 int render_process_id,
209 int render_frame_id,
210 const URLDataSource::GotDataCallback& callback) {
211 if (!filter_callback_.is_null() &&
212 filter_callback_.Run(path, callback)) {
213 return;
216 if (!json_path_.empty() && path == json_path_) {
217 SendLocalizedStringsAsJSON(callback);
218 return;
221 int resource_id = default_resource_;
222 std::map<std::string, int>::iterator result;
223 result = path_to_idr_map_.find(path);
224 if (result != path_to_idr_map_.end())
225 resource_id = result->second;
226 DCHECK_NE(resource_id, -1);
227 SendFromResourceBundle(callback, resource_id);
230 void WebUIDataSourceImpl::SendLocalizedStringsAsJSON(
231 const URLDataSource::GotDataCallback& callback) {
232 std::string template_data;
233 if (!disable_set_font_strings_)
234 webui::SetFontAndTextDirection(&localized_strings_);
236 webui::AppendJsonJS(&localized_strings_, &template_data);
237 callback.Run(base::RefCountedString::TakeString(&template_data));
240 void WebUIDataSourceImpl::SendFromResourceBundle(
241 const URLDataSource::GotDataCallback& callback, int idr) {
242 scoped_refptr<base::RefCountedStaticMemory> response(
243 GetContentClient()->GetDataResourceBytes(idr));
244 callback.Run(response.get());
247 } // namespace content