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.
5 #include "ppapi/proxy/url_request_info_resource.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "ppapi/shared_impl/var.h"
9 #include "ppapi/thunk/enter.h"
10 #include "ppapi/thunk/ppb_file_ref_api.h"
15 URLRequestInfoResource::URLRequestInfoResource(Connection connection
,
17 const URLRequestInfoData
& data
)
18 : PluginResource(connection
, instance
),
22 URLRequestInfoResource::~URLRequestInfoResource() {
25 thunk::PPB_URLRequestInfo_API
*
26 URLRequestInfoResource::AsPPB_URLRequestInfo_API() {
30 PP_Bool
URLRequestInfoResource::SetProperty(PP_URLRequestProperty property
,
32 // IMPORTANT: Do not do security validation of parameters at this level
33 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. This
34 // code is used both in the plugin (which we don't trust) and in the renderer
35 // (which we trust more). When running out-of-process, the plugin calls this
36 // function to configure the URLRequestInfoData, which is then sent to
37 // the renderer and *not* run through SetProperty again.
39 // This means that anything in the PPB_URLRequestInfo_Data needs to be
40 // validated at the time the URL is requested (which is what ValidateData
41 // does). If your feature requires security checks, it should be in the
42 // implementation in the renderer when the WebKit request is actually
45 // It is legal to do some validation here if you want to report failure to
46 // the plugin as a convenience, as long as you also do it in the renderer
48 PP_Bool result
= PP_FALSE
;
50 case PP_VARTYPE_UNDEFINED
:
51 result
= PP_FromBool(SetUndefinedProperty(property
));
55 SetBooleanProperty(property
, PP_ToBool(var
.value
.as_bool
)));
57 case PP_VARTYPE_INT32
:
59 SetIntegerProperty(property
, var
.value
.as_int
));
61 case PP_VARTYPE_STRING
: {
62 StringVar
* string
= StringVar::FromPPVar(var
);
64 result
= PP_FromBool(SetStringProperty(property
, string
->value()));
71 std::string
error_msg("PPB_URLRequestInfo.SetProperty: Attempted to set a "
72 "value for PP_URLRequestProperty ");
73 error_msg
+= base::IntToString(property
);
74 error_msg
+= ", but either this property type is invalid or its parameter "
75 "was inappropriate (e.g., the wrong type of PP_Var).";
76 Log(PP_LOGLEVEL_ERROR
, error_msg
);
81 PP_Bool
URLRequestInfoResource::AppendDataToBody(const void* data
,
84 data_
.body
.push_back(URLRequestInfoData::BodyItem(
85 std::string(static_cast<const char*>(data
), len
)));
90 PP_Bool
URLRequestInfoResource::AppendFileToBody(
93 int64_t number_of_bytes
,
94 PP_Time expected_last_modified_time
) {
95 thunk::EnterResourceNoLock
<thunk::PPB_FileRef_API
> enter(file_ref
, true);
99 // Ignore a call to append nothing.
100 if (number_of_bytes
== 0)
103 // Check for bad values. (-1 means read until end of file.)
104 if (start_offset
< 0 || number_of_bytes
< -1)
107 data_
.body
.push_back(URLRequestInfoData::BodyItem(
111 expected_last_modified_time
));
115 const URLRequestInfoData
& URLRequestInfoResource::GetData() const {
119 bool URLRequestInfoResource::SetUndefinedProperty(
120 PP_URLRequestProperty property
) {
121 // IMPORTANT: Do not do security validation of parameters at this level
122 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See
123 // SetProperty() above for why.
125 case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL
:
126 data_
.has_custom_referrer_url
= false;
127 data_
.custom_referrer_url
= std::string();
129 case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING
:
130 data_
.has_custom_content_transfer_encoding
= false;
131 data_
.custom_content_transfer_encoding
= std::string();
133 case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT
:
134 data_
.has_custom_user_agent
= false;
135 data_
.custom_user_agent
= std::string();
142 bool URLRequestInfoResource::SetBooleanProperty(
143 PP_URLRequestProperty property
,
145 // IMPORTANT: Do not do security validation of parameters at this level
146 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See
147 // SetProperty() above for why.
149 case PP_URLREQUESTPROPERTY_STREAMTOFILE
:
150 data_
.stream_to_file
= value
;
152 case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS
:
153 data_
.follow_redirects
= value
;
155 case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS
:
156 data_
.record_download_progress
= value
;
158 case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS
:
159 data_
.record_upload_progress
= value
;
161 case PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS
:
162 data_
.allow_cross_origin_requests
= value
;
164 case PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS
:
165 data_
.allow_credentials
= value
;
172 bool URLRequestInfoResource::SetIntegerProperty(
173 PP_URLRequestProperty property
,
175 // IMPORTANT: Do not do security validation of parameters at this level
176 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See
177 // SetProperty() above for why.
179 case PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD
:
180 data_
.prefetch_buffer_upper_threshold
= value
;
182 case PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD
:
183 data_
.prefetch_buffer_lower_threshold
= value
;
190 bool URLRequestInfoResource::SetStringProperty(
191 PP_URLRequestProperty property
,
192 const std::string
& value
) {
193 // IMPORTANT: Do not do security validation of parameters at this level
194 // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See
195 // SetProperty() above for why.
197 case PP_URLREQUESTPROPERTY_URL
:
198 data_
.url
= value
; // NOTE: This may be a relative URL.
200 case PP_URLREQUESTPROPERTY_METHOD
:
201 data_
.method
= value
;
203 case PP_URLREQUESTPROPERTY_HEADERS
:
204 data_
.headers
= value
;
206 case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL
:
207 data_
.has_custom_referrer_url
= true;
208 data_
.custom_referrer_url
= value
;
210 case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING
:
211 data_
.has_custom_content_transfer_encoding
= true;
212 data_
.custom_content_transfer_encoding
= value
;
214 case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT
:
215 data_
.has_custom_user_agent
= true;
216 data_
.custom_user_agent
= value
;