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