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

Side by Side Diff: chrome/browser/ui/webui/chrome_web_ui_data_source.cc

Issue 12049052: Move core url data manager classes to content. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: review comments Created 7 years, 11 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 | Annotate | Revision Log
OLDNEW
(Empty)
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.
4
5 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h"
6
7 #include <string>
8
9 #include "base/bind.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/string_util.h"
12 #include "content/public/common/content_client.h"
13 #include "ui/base/l10n/l10n_util.h"
14 #include "ui/webui/jstemplate_builder.h"
15 #include "ui/webui/web_ui_util.h"
16
17 // Internal class to hide the fact that ChromeWebUIDataSource implements
18 // content::URLDataSource.
19 class ChromeWebUIDataSource::InternalDataSource
20 : public content::URLDataSource {
21 public:
22 InternalDataSource(ChromeWebUIDataSource* parent) : parent_(parent) {
23 }
24
25 ~InternalDataSource() {
26 }
27
28 // content::URLDataSource implementation.
29 virtual std::string GetSource() OVERRIDE {
30 return parent_->GetSource();
31 }
32 virtual std::string GetMimeType(const std::string& path) const OVERRIDE {
33 return parent_->GetMimeType(path);
34 }
35 virtual void StartDataRequest(
36 const std::string& path,
37 bool is_incognito,
38 const content::URLDataSource::GotDataCallback& callback) OVERRIDE {
39 return parent_->StartDataRequest(path, is_incognito, callback);
40 }
41 virtual bool ShouldAddContentSecurityPolicy() const OVERRIDE {
42 return parent_->add_csp_;
43 }
44 virtual std::string GetContentSecurityPolicyObjectSrc() const OVERRIDE {
45 if (parent_->object_src_set_)
46 return parent_->object_src_;
47 return content::URLDataSource::GetContentSecurityPolicyObjectSrc();
48 }
49 virtual std::string GetContentSecurityPolicyFrameSrc() const OVERRIDE {
50 if (parent_->frame_src_set_)
51 return parent_->frame_src_;
52 return content::URLDataSource::GetContentSecurityPolicyFrameSrc();
53 }
54 virtual bool ShouldDenyXFrameOptions() const OVERRIDE {
55 return parent_->deny_xframe_options_;
56 }
57
58 private:
59 ChromeWebUIDataSource* parent_;
60 };
61
62 content::WebUIDataSource* ChromeWebUIDataSource::Create(
63 const std::string& source_name) {
64 return new ChromeWebUIDataSource(source_name);
65 }
66
67 ChromeWebUIDataSource::ChromeWebUIDataSource(const std::string& source_name)
68 : URLDataSourceImpl(
69 source_name,
70 new InternalDataSource(ALLOW_THIS_IN_INITIALIZER_LIST(this))),
71 source_name_(source_name),
72 default_resource_(-1),
73 json_js_format_v2_(false),
74 add_csp_(true),
75 object_src_set_(false),
76 frame_src_set_(false),
77 deny_xframe_options_(true) {
78 }
79
80 ChromeWebUIDataSource::~ChromeWebUIDataSource() {
81 }
82
83 void ChromeWebUIDataSource::AddString(const std::string& name,
84 const string16& value) {
85 localized_strings_.SetString(name, value);
86 }
87
88 void ChromeWebUIDataSource::AddString(const std::string& name,
89 const std::string& value) {
90 localized_strings_.SetString(name, value);
91 }
92
93 void ChromeWebUIDataSource::AddLocalizedString(const std::string& name,
94 int ids) {
95 localized_strings_.SetString(name, l10n_util::GetStringUTF16(ids));
96 }
97
98 void ChromeWebUIDataSource::AddLocalizedStrings(
99 const DictionaryValue& localized_strings) {
100 localized_strings_.MergeDictionary(&localized_strings);
101 }
102
103 void ChromeWebUIDataSource::AddBoolean(const std::string& name, bool value) {
104 localized_strings_.SetBoolean(name, value);
105 }
106
107 void ChromeWebUIDataSource::SetJsonPath(const std::string& path) {
108 json_path_ = path;
109 }
110
111 void ChromeWebUIDataSource::SetUseJsonJSFormatV2() {
112 json_js_format_v2_ = true;
113 }
114
115 void ChromeWebUIDataSource::AddResourcePath(const std::string &path,
116 int resource_id) {
117 path_to_idr_map_[path] = resource_id;
118 }
119
120 void ChromeWebUIDataSource::SetDefaultResource(int resource_id) {
121 default_resource_ = resource_id;
122 }
123
124 void ChromeWebUIDataSource::SetRequestFilter(
125 const content::WebUIDataSource::HandleRequestCallback& callback) {
126 filter_callback_ = callback;
127 }
128
129 void ChromeWebUIDataSource::DisableContentSecurityPolicy() {
130 add_csp_ = false;
131 }
132
133 void ChromeWebUIDataSource::OverrideContentSecurityPolicyObjectSrc(
134 const std::string& data) {
135 object_src_set_ = true;
136 object_src_ = data;
137 }
138
139 void ChromeWebUIDataSource::OverrideContentSecurityPolicyFrameSrc(
140 const std::string& data) {
141 frame_src_set_ = true;
142 frame_src_ = data;
143 }
144
145 void ChromeWebUIDataSource::DisableDenyXFrameOptions() {
146 deny_xframe_options_ = false;
147 }
148
149 std::string ChromeWebUIDataSource::GetSource() {
150 return source_name_;
151 }
152
153 std::string ChromeWebUIDataSource::GetMimeType(const std::string& path) const {
154 if (EndsWith(path, ".js", false))
155 return "application/javascript";
156
157 if (EndsWith(path, ".json", false))
158 return "application/json";
159
160 if (EndsWith(path, ".pdf", false))
161 return "application/pdf";
162
163 return "text/html";
164 }
165
166 void ChromeWebUIDataSource::StartDataRequest(
167 const std::string& path,
168 bool is_incognito,
169 const content::URLDataSource::GotDataCallback& callback) {
170 if (!filter_callback_.is_null() &&
171 filter_callback_.Run(path, callback)) {
172 return;
173 }
174
175 if (!json_path_.empty() && path == json_path_) {
176 SendLocalizedStringsAsJSON(callback);
177 return;
178 }
179
180 int resource_id = default_resource_;
181 std::map<std::string, int>::iterator result;
182 result = path_to_idr_map_.find(path);
183 if (result != path_to_idr_map_.end())
184 resource_id = result->second;
185 DCHECK_NE(resource_id, -1);
186 SendFromResourceBundle(callback, resource_id);
187 }
188
189 void ChromeWebUIDataSource::SendLocalizedStringsAsJSON(
190 const content::URLDataSource::GotDataCallback& callback) {
191 std::string template_data;
192 webui::SetFontAndTextDirection(&localized_strings_);
193
194 scoped_ptr<webui::UseVersion2> version2;
195 if (json_js_format_v2_)
196 version2.reset(new webui::UseVersion2);
197
198 webui::AppendJsonJS(&localized_strings_, &template_data);
199 callback.Run(base::RefCountedString::TakeString(&template_data));
200 }
201
202 void ChromeWebUIDataSource::SendFromResourceBundle(
203 const content::URLDataSource::GotDataCallback& callback, int idr) {
204 scoped_refptr<base::RefCountedStaticMemory> response(
205 content::GetContentClient()->GetDataResourceBytes(idr));
206 callback.Run(response);
207 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/chrome_web_ui_data_source.h ('k') | chrome/browser/ui/webui/chrome_web_ui_data_source_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698