Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / ppapi / proxy / url_request_info_resource.cc
bloba1ba17cc085b7884dad912fe01eb9d3bda3e59c1
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"
12 namespace ppapi {
13 namespace proxy {
15 URLRequestInfoResource::URLRequestInfoResource(Connection connection,
16 PP_Instance instance,
17 const URLRequestInfoData& data)
18 : PluginResource(connection, instance),
19 data_(data) {
22 URLRequestInfoResource::~URLRequestInfoResource() {
25 thunk::PPB_URLRequestInfo_API*
26 URLRequestInfoResource::AsPPB_URLRequestInfo_API() {
27 return this;
30 PP_Bool URLRequestInfoResource::SetProperty(PP_URLRequestProperty property,
31 PP_Var var) {
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
43 // constructed.
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
47 // later.
48 PP_Bool result = PP_FALSE;
49 switch (var.type) {
50 case PP_VARTYPE_UNDEFINED:
51 result = PP_FromBool(SetUndefinedProperty(property));
52 break;
53 case PP_VARTYPE_BOOL:
54 result = PP_FromBool(
55 SetBooleanProperty(property, PP_ToBool(var.value.as_bool)));
56 break;
57 case PP_VARTYPE_INT32:
58 result = PP_FromBool(
59 SetIntegerProperty(property, var.value.as_int));
60 break;
61 case PP_VARTYPE_STRING: {
62 StringVar* string = StringVar::FromPPVar(var);
63 if (string)
64 result = PP_FromBool(SetStringProperty(property, string->value()));
65 break;
67 default:
68 break;
70 if (!result) {
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);
78 return result;
81 PP_Bool URLRequestInfoResource::AppendDataToBody(const void* data,
82 uint32_t len) {
83 if (len > 0) {
84 data_.body.push_back(URLRequestInfoData::BodyItem(
85 std::string(static_cast<const char*>(data), len)));
87 return PP_TRUE;
90 PP_Bool URLRequestInfoResource::AppendFileToBody(
91 PP_Resource file_ref,
92 int64_t start_offset,
93 int64_t number_of_bytes,
94 PP_Time expected_last_modified_time) {
95 thunk::EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true);
96 if (enter.failed())
97 return PP_FALSE;
99 // Ignore a call to append nothing.
100 if (number_of_bytes == 0)
101 return PP_TRUE;
103 // Check for bad values. (-1 means read until end of file.)
104 if (start_offset < 0 || number_of_bytes < -1)
105 return PP_FALSE;
107 data_.body.push_back(URLRequestInfoData::BodyItem(
108 enter.resource(),
109 start_offset,
110 number_of_bytes,
111 expected_last_modified_time));
112 return PP_TRUE;
115 const URLRequestInfoData& URLRequestInfoResource::GetData() const {
116 return data_;
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.
124 switch (property) {
125 case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL:
126 data_.has_custom_referrer_url = false;
127 data_.custom_referrer_url = std::string();
128 return true;
129 case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING:
130 data_.has_custom_content_transfer_encoding = false;
131 data_.custom_content_transfer_encoding = std::string();
132 return true;
133 case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT:
134 data_.has_custom_user_agent = false;
135 data_.custom_user_agent = std::string();
136 return true;
137 default:
138 return false;
142 bool URLRequestInfoResource::SetBooleanProperty(
143 PP_URLRequestProperty property,
144 bool value) {
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.
148 switch (property) {
149 case PP_URLREQUESTPROPERTY_STREAMTOFILE:
150 data_.stream_to_file = value;
151 return true;
152 case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS:
153 data_.follow_redirects = value;
154 return true;
155 case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS:
156 data_.record_download_progress = value;
157 return true;
158 case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS:
159 data_.record_upload_progress = value;
160 return true;
161 case PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS:
162 data_.allow_cross_origin_requests = value;
163 return true;
164 case PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS:
165 data_.allow_credentials = value;
166 return true;
167 default:
168 return false;
172 bool URLRequestInfoResource::SetIntegerProperty(
173 PP_URLRequestProperty property,
174 int32_t value) {
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.
178 switch (property) {
179 case PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD:
180 data_.prefetch_buffer_upper_threshold = value;
181 return true;
182 case PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD:
183 data_.prefetch_buffer_lower_threshold = value;
184 return true;
185 default:
186 return false;
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.
196 switch (property) {
197 case PP_URLREQUESTPROPERTY_URL:
198 data_.url = value; // NOTE: This may be a relative URL.
199 return true;
200 case PP_URLREQUESTPROPERTY_METHOD:
201 data_.method = value;
202 return true;
203 case PP_URLREQUESTPROPERTY_HEADERS:
204 data_.headers = value;
205 return true;
206 case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL:
207 data_.has_custom_referrer_url = true;
208 data_.custom_referrer_url = value;
209 return true;
210 case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING:
211 data_.has_custom_content_transfer_encoding = true;
212 data_.custom_content_transfer_encoding = value;
213 return true;
214 case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT:
215 data_.has_custom_user_agent = true;
216 data_.custom_user_agent = value;
217 return true;
218 default:
219 return false;
223 } // namespace proxy
224 } // namespace ppapi