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