OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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 "ppapi/shared_impl/ppb_url_request_info_shared.h" | |
6 | |
7 #include "base/string_util.h" | |
8 #include "ppapi/shared_impl/var.h" | |
9 #include "ppapi/thunk/enter.h" | |
10 #include "ppapi/thunk/ppb_file_ref_api.h" | |
11 | |
12 using ppapi::thunk::EnterResourceNoLock; | |
13 | |
14 namespace ppapi { | |
15 | |
16 namespace { | |
17 | |
18 const int32_t kDefaultPrefetchBufferUpperThreshold = 100 * 1000 * 1000; | |
19 const int32_t kDefaultPrefetchBufferLowerThreshold = 50 * 1000 * 1000; | |
20 | |
21 } // namespace | |
22 | |
23 PPB_URLRequestInfo_Data::BodyItem::BodyItem() | |
24 : is_file(false), | |
25 start_offset(0), | |
26 number_of_bytes(-1), | |
27 expected_last_modified_time(0.0) { | |
28 } | |
29 | |
30 PPB_URLRequestInfo_Data::BodyItem::BodyItem(const std::string& data) | |
31 : is_file(false), | |
32 data(data), | |
33 start_offset(0), | |
34 number_of_bytes(-1), | |
35 expected_last_modified_time(0.0) { | |
36 } | |
37 | |
38 PPB_URLRequestInfo_Data::BodyItem::BodyItem( | |
39 Resource* file_ref, | |
40 int64_t start_offset, | |
41 int64_t number_of_bytes, | |
42 PP_Time expected_last_modified_time) | |
43 : is_file(true), | |
44 file_ref(file_ref), | |
45 file_ref_host_resource(file_ref->host_resource()), | |
46 start_offset(start_offset), | |
47 number_of_bytes(number_of_bytes), | |
48 expected_last_modified_time(expected_last_modified_time) { | |
49 } | |
50 | |
51 PPB_URLRequestInfo_Data::PPB_URLRequestInfo_Data() | |
52 : url(), | |
53 method(), | |
54 headers(), | |
55 stream_to_file(false), | |
56 follow_redirects(true), | |
57 record_download_progress(false), | |
58 record_upload_progress(false), | |
59 has_custom_referrer_url(false), | |
60 custom_referrer_url(), | |
61 allow_cross_origin_requests(false), | |
62 allow_credentials(false), | |
63 has_custom_content_transfer_encoding(false), | |
64 custom_content_transfer_encoding(), | |
65 prefetch_buffer_upper_threshold(kDefaultPrefetchBufferUpperThreshold), | |
66 prefetch_buffer_lower_threshold(kDefaultPrefetchBufferLowerThreshold), | |
67 body() { | |
68 } | |
69 | |
70 PPB_URLRequestInfo_Data::~PPB_URLRequestInfo_Data() { | |
71 } | |
72 | |
73 PPB_URLRequestInfo_Shared::PPB_URLRequestInfo_Shared( | |
74 PP_Instance instance, | |
75 const PPB_URLRequestInfo_Data& data) | |
76 : Resource(instance), | |
77 data_(data) { | |
78 } | |
79 | |
80 PPB_URLRequestInfo_Shared::PPB_URLRequestInfo_Shared( | |
81 const HostResource& host_resource, | |
82 const PPB_URLRequestInfo_Data& data) | |
83 : Resource(host_resource), | |
84 data_(data) { | |
85 } | |
86 | |
87 PPB_URLRequestInfo_Shared::~PPB_URLRequestInfo_Shared() { | |
88 } | |
89 | |
90 thunk::PPB_URLRequestInfo_API* | |
91 PPB_URLRequestInfo_Shared::AsPPB_URLRequestInfo_API() { | |
92 return this; | |
93 } | |
94 | |
95 PP_Bool PPB_URLRequestInfo_Shared::SetProperty(PP_URLRequestProperty property, | |
96 PP_Var var) { | |
97 // IMPORTANT: Do not do security validation of parameters at this level | |
98 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. This | |
99 // code is used both in the plugin (which we don't trust) and in the renderer | |
100 // (which we trust more). When running out-of-process, the plugin calls this | |
101 // function to configure the PPB_URLRequestInfo_Data, which is then sent to | |
102 // the renderer and *not* run through SetProperty again. | |
103 // | |
104 // This means that anything in the PPB_URLRequestInfo_Data needs to be | |
105 // validated at the time the URL is requested (which is what ValidateData | |
106 // does). If your feature requires security checks, it should be in the | |
107 // implementation in the renderer when the WebKit request is actually | |
108 // constructed. | |
109 // | |
110 // It is legal to do some validation here if you want to report failure to | |
111 // the plugin as a convenience, as long as you also do it in the renderer | |
112 // later. | |
113 PP_Bool result = PP_FALSE; | |
114 switch (var.type) { | |
115 case PP_VARTYPE_UNDEFINED: | |
116 result = PP_FromBool(SetUndefinedProperty(property)); | |
117 break; | |
118 case PP_VARTYPE_BOOL: | |
119 result = PP_FromBool( | |
120 SetBooleanProperty(property, PP_ToBool(var.value.as_bool))); | |
121 break; | |
122 case PP_VARTYPE_INT32: | |
123 result = PP_FromBool( | |
124 SetIntegerProperty(property, var.value.as_int)); | |
125 break; | |
126 case PP_VARTYPE_STRING: { | |
127 StringVar* string = StringVar::FromPPVar(var); | |
128 if (string) | |
129 result = PP_FromBool(SetStringProperty(property, string->value())); | |
130 break; | |
131 } | |
132 default: | |
133 break; | |
134 } | |
135 return result; | |
136 } | |
137 | |
138 PP_Bool PPB_URLRequestInfo_Shared::AppendDataToBody(const void* data, | |
139 uint32_t len) { | |
140 if (len > 0) { | |
141 data_.body.push_back(PPB_URLRequestInfo_Data::BodyItem( | |
142 std::string(static_cast<const char*>(data), len))); | |
143 } | |
144 return PP_TRUE; | |
145 } | |
146 | |
147 PP_Bool PPB_URLRequestInfo_Shared::AppendFileToBody( | |
148 PP_Resource file_ref, | |
149 int64_t start_offset, | |
150 int64_t number_of_bytes, | |
151 PP_Time expected_last_modified_time) { | |
152 EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true); | |
153 if (enter.failed()) | |
154 return PP_FALSE; | |
155 | |
156 // Ignore a call to append nothing. | |
157 if (number_of_bytes == 0) | |
158 return PP_TRUE; | |
159 | |
160 // Check for bad values. (-1 means read until end of file.) | |
161 if (start_offset < 0 || number_of_bytes < -1) | |
162 return PP_FALSE; | |
163 | |
164 data_.body.push_back(PPB_URLRequestInfo_Data::BodyItem( | |
165 enter.resource(), | |
166 start_offset, | |
167 number_of_bytes, | |
168 expected_last_modified_time)); | |
169 return PP_TRUE; | |
170 } | |
171 | |
172 const PPB_URLRequestInfo_Data& PPB_URLRequestInfo_Shared::GetData() const { | |
173 return data_; | |
174 } | |
175 | |
176 bool PPB_URLRequestInfo_Shared::SetUndefinedProperty( | |
177 PP_URLRequestProperty property) { | |
178 // IMPORTANT: Do not do security validation of parameters at this level | |
179 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See | |
180 // SetProperty() above for why. | |
181 switch (property) { | |
182 case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL: | |
183 data_.has_custom_referrer_url = false; | |
184 data_.custom_referrer_url = std::string(); | |
185 return true; | |
186 case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING: | |
187 data_.has_custom_content_transfer_encoding = false; | |
188 data_.custom_content_transfer_encoding = std::string(); | |
189 return true; | |
190 default: | |
191 return false; | |
192 } | |
193 } | |
194 | |
195 bool PPB_URLRequestInfo_Shared::SetBooleanProperty( | |
196 PP_URLRequestProperty property, | |
197 bool value) { | |
198 // IMPORTANT: Do not do security validation of parameters at this level | |
199 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See | |
200 // SetProperty() above for why. | |
201 switch (property) { | |
202 case PP_URLREQUESTPROPERTY_STREAMTOFILE: | |
203 data_.stream_to_file = value; | |
204 return true; | |
205 case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS: | |
206 data_.follow_redirects = value; | |
207 return true; | |
208 case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS: | |
209 data_.record_download_progress = value; | |
210 return true; | |
211 case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS: | |
212 data_.record_upload_progress = value; | |
213 return true; | |
214 case PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS: | |
215 data_.allow_cross_origin_requests = value; | |
216 return true; | |
217 case PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS: | |
218 data_.allow_credentials = value; | |
219 return true; | |
220 default: | |
221 return false; | |
222 } | |
223 } | |
224 | |
225 bool PPB_URLRequestInfo_Shared::SetIntegerProperty( | |
226 PP_URLRequestProperty property, | |
227 int32_t value) { | |
228 // IMPORTANT: Do not do security validation of parameters at this level | |
229 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See | |
230 // SetProperty() above for why. | |
231 switch (property) { | |
232 case PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD: | |
233 data_.prefetch_buffer_upper_threshold = value; | |
234 return true; | |
235 case PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD: | |
236 data_.prefetch_buffer_lower_threshold = value; | |
237 return true; | |
238 default: | |
239 return false; | |
240 } | |
241 } | |
242 | |
243 bool PPB_URLRequestInfo_Shared::SetStringProperty( | |
244 PP_URLRequestProperty property, | |
245 const std::string& value) { | |
246 // IMPORTANT: Do not do security validation of parameters at this level | |
247 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See | |
248 // SetProperty() above for why. | |
249 switch (property) { | |
250 case PP_URLREQUESTPROPERTY_URL: | |
251 data_.url = value; // NOTE: This may be a relative URL. | |
252 return true; | |
253 case PP_URLREQUESTPROPERTY_METHOD: | |
254 data_.method = value; | |
255 return true; | |
256 case PP_URLREQUESTPROPERTY_HEADERS: | |
257 data_.headers = value; | |
258 return true; | |
259 case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL: | |
260 data_.has_custom_referrer_url = true; | |
261 data_.custom_referrer_url = value; | |
262 return true; | |
263 case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING: | |
264 data_.has_custom_content_transfer_encoding = true; | |
265 data_.custom_content_transfer_encoding = value; | |
266 return true; | |
267 default: | |
268 return false; | |
269 } | |
270 } | |
271 | |
272 } // namespace ppapi | |
OLD | NEW |