[email protected] | 00d320a | 2012-02-14 00:27:04 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 5 | #include "ppapi/proxy/url_request_info_resource.h" |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 6 | |
dmichael | bdbeb65 | 2014-12-12 20:58:18 | [diff] [blame] | 7 | #include "base/strings/string_number_conversions.h" |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 8 | #include "ppapi/shared_impl/var.h" |
| 9 | #include "ppapi/thunk/enter.h" |
| 10 | #include "ppapi/thunk/ppb_file_ref_api.h" |
| 11 | |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 12 | namespace ppapi { |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 13 | namespace proxy { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 14 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 15 | URLRequestInfoResource::URLRequestInfoResource(Connection connection, |
| 16 | PP_Instance instance, |
| 17 | const URLRequestInfoData& data) |
| 18 | : PluginResource(connection, instance), |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 19 | data_(data) { |
| 20 | } |
| 21 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 22 | URLRequestInfoResource::~URLRequestInfoResource() { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 23 | } |
| 24 | |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 25 | thunk::PPB_URLRequestInfo_API* |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 26 | URLRequestInfoResource::AsPPB_URLRequestInfo_API() { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 27 | return this; |
| 28 | } |
| 29 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 30 | PP_Bool URLRequestInfoResource::SetProperty(PP_URLRequestProperty property, |
| 31 | PP_Var var) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 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 |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 36 | // function to configure the URLRequestInfoData, which is then sent to |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 37 | // 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 | } |
dmichael | bdbeb65 | 2014-12-12 20:58:18 | [diff] [blame] | 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); |
| 77 | } |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 78 | return result; |
| 79 | } |
| 80 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 81 | PP_Bool URLRequestInfoResource::AppendDataToBody(const void* data, |
| 82 | uint32_t len) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 83 | if (len > 0) { |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 84 | data_.body.push_back(URLRequestInfoData::BodyItem( |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 85 | std::string(static_cast<const char*>(data), len))); |
| 86 | } |
| 87 | return PP_TRUE; |
| 88 | } |
| 89 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 90 | PP_Bool URLRequestInfoResource::AppendFileToBody( |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 91 | PP_Resource file_ref, |
| 92 | int64_t start_offset, |
| 93 | int64_t number_of_bytes, |
| 94 | PP_Time expected_last_modified_time) { |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 95 | thunk::EnterResourceNoLock<thunk::PPB_FileRef_API> enter(file_ref, true); |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 96 | 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] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 107 | data_.body.push_back(URLRequestInfoData::BodyItem( |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 108 | enter.resource(), |
| 109 | start_offset, |
| 110 | number_of_bytes, |
| 111 | expected_last_modified_time)); |
| 112 | return PP_TRUE; |
| 113 | } |
| 114 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 115 | const URLRequestInfoData& URLRequestInfoResource::GetData() const { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 116 | return data_; |
| 117 | } |
| 118 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 119 | bool URLRequestInfoResource::SetUndefinedProperty( |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 120 | PP_URLRequestProperty property) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 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; |
[email protected] | c094eaf | 2012-07-10 16:09:01 | [diff] [blame] | 133 | case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT: |
| 134 | data_.has_custom_user_agent = false; |
| 135 | data_.custom_user_agent = std::string(); |
| 136 | return true; |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 137 | default: |
| 138 | return false; |
| 139 | } |
| 140 | } |
| 141 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 142 | bool URLRequestInfoResource::SetBooleanProperty( |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 143 | PP_URLRequestProperty property, |
| 144 | bool value) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 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) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 149 | case PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS: |
| 150 | data_.follow_redirects = value; |
| 151 | return true; |
| 152 | case PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS: |
| 153 | data_.record_download_progress = value; |
| 154 | return true; |
| 155 | case PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS: |
| 156 | data_.record_upload_progress = value; |
| 157 | return true; |
| 158 | case PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS: |
| 159 | data_.allow_cross_origin_requests = value; |
| 160 | return true; |
| 161 | case PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS: |
| 162 | data_.allow_credentials = value; |
| 163 | return true; |
| 164 | default: |
| 165 | return false; |
| 166 | } |
| 167 | } |
| 168 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 169 | bool URLRequestInfoResource::SetIntegerProperty( |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 170 | PP_URLRequestProperty property, |
| 171 | int32_t value) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 172 | // IMPORTANT: Do not do security validation of parameters at this level |
| 173 | // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See |
| 174 | // SetProperty() above for why. |
| 175 | switch (property) { |
| 176 | case PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD: |
| 177 | data_.prefetch_buffer_upper_threshold = value; |
| 178 | return true; |
| 179 | case PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD: |
| 180 | data_.prefetch_buffer_lower_threshold = value; |
| 181 | return true; |
| 182 | default: |
| 183 | return false; |
| 184 | } |
| 185 | } |
| 186 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 187 | bool URLRequestInfoResource::SetStringProperty( |
[email protected] | 9a57839 | 2011-12-07 18:59:27 | [diff] [blame] | 188 | PP_URLRequestProperty property, |
| 189 | const std::string& value) { |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 190 | // IMPORTANT: Do not do security validation of parameters at this level |
| 191 | // without also adding them to PPB_URLRequestInfo_Impl::ValidateData. See |
| 192 | // SetProperty() above for why. |
| 193 | switch (property) { |
| 194 | case PP_URLREQUESTPROPERTY_URL: |
| 195 | data_.url = value; // NOTE: This may be a relative URL. |
| 196 | return true; |
[email protected] | 65e88c7 | 2011-11-06 00:00:54 | [diff] [blame] | 197 | case PP_URLREQUESTPROPERTY_METHOD: |
| 198 | data_.method = value; |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 199 | return true; |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 200 | case PP_URLREQUESTPROPERTY_HEADERS: |
| 201 | data_.headers = value; |
| 202 | return true; |
| 203 | case PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL: |
| 204 | data_.has_custom_referrer_url = true; |
| 205 | data_.custom_referrer_url = value; |
| 206 | return true; |
| 207 | case PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING: |
| 208 | data_.has_custom_content_transfer_encoding = true; |
| 209 | data_.custom_content_transfer_encoding = value; |
| 210 | return true; |
[email protected] | c094eaf | 2012-07-10 16:09:01 | [diff] [blame] | 211 | case PP_URLREQUESTPROPERTY_CUSTOMUSERAGENT: |
| 212 | data_.has_custom_user_agent = true; |
| 213 | data_.custom_user_agent = value; |
| 214 | return true; |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 215 | default: |
| 216 | return false; |
| 217 | } |
| 218 | } |
| 219 | |
[email protected] | 7b2f729 | 2012-09-19 19:52:12 | [diff] [blame] | 220 | } // namespace proxy |
[email protected] | 6f75c95 | 2011-08-26 04:51:07 | [diff] [blame] | 221 | } // namespace ppapi |