blob: a1ba17cc085b7884dad912fe01eb9d3bda3e59c1 [file] [log] [blame]
[email protected]00d320a2012-02-14 00:27:041// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]6f75c952011-08-26 04:51:072// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]7b2f7292012-09-19 19:52:125#include "ppapi/proxy/url_request_info_resource.h"
[email protected]6f75c952011-08-26 04:51:076
dmichaelbdbeb652014-12-12 20:58:187#include "base/strings/string_number_conversions.h"
[email protected]6f75c952011-08-26 04:51:078#include "ppapi/shared_impl/var.h"
9#include "ppapi/thunk/enter.h"
10#include "ppapi/thunk/ppb_file_ref_api.h"
11
[email protected]6f75c952011-08-26 04:51:0712namespace ppapi {
[email protected]7b2f7292012-09-19 19:52:1213namespace proxy {
[email protected]6f75c952011-08-26 04:51:0714
[email protected]7b2f7292012-09-19 19:52:1215URLRequestInfoResource::URLRequestInfoResource(Connection connection,
16 PP_Instance instance,
17 const URLRequestInfoData& data)
18 : PluginResource(connection, instance),
[email protected]6f75c952011-08-26 04:51:0719 data_(data) {
20}
21
[email protected]7b2f7292012-09-19 19:52:1222URLRequestInfoResource::~URLRequestInfoResource() {
[email protected]6f75c952011-08-26 04:51:0723}
24
[email protected]9a578392011-12-07 18:59:2725thunk::PPB_URLRequestInfo_API*
[email protected]7b2f7292012-09-19 19:52:1226URLRequestInfoResource::AsPPB_URLRequestInfo_API() {
[email protected]6f75c952011-08-26 04:51:0727 return this;
28}
29
[email protected]7b2f7292012-09-19 19:52:1230PP_Bool URLRequestInfoResource::SetProperty(PP_URLRequestProperty property,
31 PP_Var var) {
[email protected]6f75c952011-08-26 04:51:0732 // 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
[email protected]7b2f7292012-09-19 19:52:1236 // function to configure the URLRequestInfoData, which is then sent to
[email protected]6f75c952011-08-26 04:51:0737 // the renderer and *not* run through SetProperty again.
38 //
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.
44 //
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;
66 }
67 default:
68 break;
69 }
dmichaelbdbeb652014-12-12 20:58:1870 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);
77 }
[email protected]6f75c952011-08-26 04:51:0778 return result;
79}
80
[email protected]7b2f7292012-09-19 19:52:1281PP_Bool URLRequestInfoResource::AppendDataToBody(const void* data,
82 uint32_t len) {
[email protected]6f75c952011-08-26 04:51:0783 if (len > 0) {
[email protected]7b2f7292012-09-19 19:52:1284 data_.body.push_back(URLRequestInfoData::BodyItem(
[email protected]6f75c952011-08-26 04:51:0785 std::string(static_cast<const char*>(data), len)));
86 }
87 return PP_TRUE;
88}
89
[email protected]7b2f7292012-09-19 19:52:1290PP_Bool URLRequestInfoResource::AppendFileToBody(
[email protected]6f75c952011-08-26 04:51:0791 PP_Resource file_ref,
92 int64_t start_offset,
93 int64_t number_of_bytes,
94 PP_Time expected_last_modified_time) {
[email protected]7b2f7292012-09-19 19:52:1295 thunk::EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true);
[email protected]6f75c952011-08-26 04:51:0796 if (enter.failed())
97 return PP_FALSE;
98
99 // Ignore a call to append nothing.
100 if (number_of_bytes == 0)
101 return PP_TRUE;
102
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;
106
[email protected]7b2f7292012-09-19 19:52:12107 data_.body.push_back(URLRequestInfoData::BodyItem(
[email protected]6f75c952011-08-26 04:51:07108 enter.resource(),
109 start_offset,
110 number_of_bytes,
111 expected_last_modified_time));
112 return PP_TRUE;
113}
114
[email protected]7b2f7292012-09-19 19:52:12115const URLRequestInfoData& URLRequestInfoResource::GetData() const {
[email protected]6f75c952011-08-26 04:51:07116 return data_;
117}
118
[email protected]7b2f7292012-09-19 19:52:12119bool URLRequestInfoResource::SetUndefinedProperty(
[email protected]9a578392011-12-07 18:59:27120 PP_URLRequestProperty property) {
[email protected]6f75c952011-08-26 04:51:07121 // 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;
[email protected]c094eaf2012-07-10 16:09:01133 case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT:
134 data_.has_custom_user_agent = false;
135 data_.custom_user_agent = std::string();
136 return true;
[email protected]6f75c952011-08-26 04:51:07137 default:
138 return false;
139 }
140}
141
[email protected]7b2f7292012-09-19 19:52:12142bool URLRequestInfoResource::SetBooleanProperty(
[email protected]9a578392011-12-07 18:59:27143 PP_URLRequestProperty property,
144 bool value) {
[email protected]6f75c952011-08-26 04:51:07145 // 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;
169 }
170}
171
[email protected]7b2f7292012-09-19 19:52:12172bool URLRequestInfoResource::SetIntegerProperty(
[email protected]9a578392011-12-07 18:59:27173 PP_URLRequestProperty property,
174 int32_t value) {
[email protected]6f75c952011-08-26 04:51:07175 // 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;
187 }
188}
189
[email protected]7b2f7292012-09-19 19:52:12190bool URLRequestInfoResource::SetStringProperty(
[email protected]9a578392011-12-07 18:59:27191 PP_URLRequestProperty property,
192 const std::string& value) {
[email protected]6f75c952011-08-26 04:51:07193 // 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;
[email protected]65e88c72011-11-06 00:00:54200 case PP_URLREQUESTPROPERTY_METHOD:
201 data_.method = value;
[email protected]6f75c952011-08-26 04:51:07202 return true;
[email protected]6f75c952011-08-26 04:51:07203 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;
[email protected]c094eaf2012-07-10 16:09:01214 case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT:
215 data_.has_custom_user_agent = true;
216 data_.custom_user_agent = value;
217 return true;
[email protected]6f75c952011-08-26 04:51:07218 default:
219 return false;
220 }
221}
222
[email protected]7b2f7292012-09-19 19:52:12223} // namespace proxy
[email protected]6f75c952011-08-26 04:51:07224} // namespace ppapi