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

Side by Side Diff: content/browser/webui/web_ui_data_source.cc

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

Powered by Google App Engine
This is Rietveld 408576698