OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef PPAPI_CPP_URL_REQUEST_INFO_H_ | 5 #ifndef PPAPI_CPP_URL_REQUEST_INFO_H_ |
6 #define PPAPI_CPP_URL_REQUEST_INFO_H_ | 6 #define PPAPI_CPP_URL_REQUEST_INFO_H_ |
7 | 7 |
8 #include "ppapi/c/ppb_url_request_info.h" | 8 #include "ppapi/c/ppb_url_request_info.h" |
9 #include "ppapi/cpp/resource.h" | 9 #include "ppapi/cpp/resource.h" |
10 #include "ppapi/cpp/var.h" | 10 #include "ppapi/cpp/var.h" |
11 | 11 |
| 12 /// @file |
| 13 /// This file defines the API for creating and manipulating URL requests. |
12 namespace pp { | 14 namespace pp { |
13 | 15 |
14 class FileRef; | 16 class FileRef; |
15 class Instance; | 17 class Instance; |
16 | 18 |
| 19 /// URLRequestInfo provides an API for creating and manipulating URL requests. |
17 class URLRequestInfo : public Resource { | 20 class URLRequestInfo : public Resource { |
18 public: | 21 public: |
19 // Creates an is_null resource. | 22 /// Default constructor. This constructor creates an |
| 23 /// <code>is_null</code> resource. |
20 URLRequestInfo() {} | 24 URLRequestInfo() {} |
21 | 25 |
| 26 /// A constructor used to allocate a new <code>URLLoader</code> in the |
| 27 /// browser. The resulting object will be <code>is_null</code> if the |
| 28 /// allocation failed. |
| 29 /// |
| 30 /// @param[in] instance An <code>Instance</code>. |
22 explicit URLRequestInfo(Instance* instance); | 31 explicit URLRequestInfo(Instance* instance); |
| 32 |
| 33 /// The copy constructor for <code>URLRequestInfo</code>. |
| 34 /// |
| 35 /// @param[in] other A <code>URLRequestInfo</code> to be copied. |
23 URLRequestInfo(const URLRequestInfo& other); | 36 URLRequestInfo(const URLRequestInfo& other); |
24 | 37 |
25 // PPB_URLRequestInfo methods: | 38 /// SetProperty() sets a request property. The value of the property must be |
| 39 /// the correct type according to the property being set. |
| 40 /// |
| 41 /// @param[in] property A <code>PP_URLRequestProperty</code> identifying the |
| 42 /// property to set. |
| 43 /// @param[in] value A <code>Var</code> containing the property value. |
| 44 /// |
| 45 /// @return True if successful, false if any of the |
| 46 /// parameters are invalid. |
26 bool SetProperty(PP_URLRequestProperty property, const Var& value); | 47 bool SetProperty(PP_URLRequestProperty property, const Var& value); |
| 48 |
| 49 /// AppendDataToBody() appends data to the request body. A content-length |
| 50 /// request header will be automatically generated. |
| 51 /// |
| 52 /// @param[in] data A pointer to a buffer holding the data. |
| 53 /// @param[in] len The length, in bytes, of the data. |
| 54 /// |
| 55 /// @return True if successful, false if any of the |
| 56 /// parameters are invalid. |
27 bool AppendDataToBody(const void* data, uint32_t len); | 57 bool AppendDataToBody(const void* data, uint32_t len); |
| 58 |
| 59 /// AppendFileToBody() is used to append an entire file, to be uploaded, to |
| 60 /// the request body. A content-length request header will be automatically |
| 61 /// generated. |
| 62 /// |
| 63 /// @param[in] file_ref A <code>FileRef</code> containing the file |
| 64 /// reference. |
| 65 |
| 66 /// @param[in] expected_last_modified_time An optional (non-zero) last |
| 67 /// modified time stamp used to validate that the file was not modified since |
| 68 /// the given time before it was uploaded. The upload will fail with an error |
| 69 /// code of <code>PP_ERROR_FILECHANGED</code> if the file has been modified |
| 70 /// since the given time. If expected_last_modified_time is 0, then no |
| 71 /// validation is performed. |
| 72 /// |
| 73 /// @return True if successful, false if any of the |
| 74 /// parameters are invalid. |
28 bool AppendFileToBody(const FileRef& file_ref, | 75 bool AppendFileToBody(const FileRef& file_ref, |
29 PP_Time expected_last_modified_time = 0); | 76 PP_Time expected_last_modified_time = 0); |
| 77 |
| 78 /// AppendFileRangeToBody() is a pointer to a function used to append part or |
| 79 /// all of a file, to be uploaded, to the request body. A content-length |
| 80 /// request header will be automatically generated. |
| 81 /// |
| 82 /// @param[in] file_ref A <code>FileRef</code> containing the file |
| 83 /// reference. |
| 84 /// @param[in] start_offset An optional starting point offset within the |
| 85 /// file. |
| 86 /// @param[in] length An optional number of bytes of the file to |
| 87 /// be included. If the value is -1, then the sub-range to upload extends |
| 88 /// to the end of the file. |
| 89 /// @param[in] expected_last_modified_time An optional (non-zero) last |
| 90 /// modified time stamp used to validate that the file was not modified since |
| 91 /// the given time before it was uploaded. The upload will fail with an error |
| 92 /// code of <code>PP_ERROR_FILECHANGED</code> if the file has been modified |
| 93 /// since the given time. If expected_last_modified_time is 0, then no |
| 94 /// validation is performed. |
| 95 /// |
| 96 /// @return True if successful, false if any of the |
| 97 /// parameters are invalid. |
30 bool AppendFileRangeToBody(const FileRef& file_ref, | 98 bool AppendFileRangeToBody(const FileRef& file_ref, |
31 int64_t start_offset, | 99 int64_t start_offset, |
32 int64_t length, | 100 int64_t length, |
33 PP_Time expected_last_modified_time = 0); | 101 PP_Time expected_last_modified_time = 0); |
34 | 102 |
35 // Convenient helpers for setting properties: | 103 /// SetURL() sets the <code>PP_URLREQUESTPROPERTY_URL</code> |
| 104 /// property for the request. |
| 105 /// |
| 106 /// @param[in] url_string A <code>Var</code> containing the property value. |
| 107 /// |
| 108 /// @return True if successful, false if the parameter is invalid. |
36 bool SetURL(const Var& url_string) { | 109 bool SetURL(const Var& url_string) { |
37 return SetProperty(PP_URLREQUESTPROPERTY_URL, url_string); | 110 return SetProperty(PP_URLREQUESTPROPERTY_URL, url_string); |
38 } | 111 } |
| 112 |
| 113 /// SetMethod() sets the <code>PP_URLREQUESTPROPERTY_METHOD</code> |
| 114 /// (corresponding to a string of type <code>PP_VARTYPE_STRING</code>) |
| 115 /// property for the request. This string is either a POST or GET. Refer to |
| 116 /// the <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html">HTTP |
| 117 /// Methods</a> documentation for further information. |
| 118 /// |
| 119 /// @param[in] method_string A <code>Var</code> containing the property |
| 120 /// value. |
| 121 /// |
| 122 /// @return True if successful, false if the parameter is invalid. |
39 bool SetMethod(const Var& method_string) { | 123 bool SetMethod(const Var& method_string) { |
40 return SetProperty(PP_URLREQUESTPROPERTY_METHOD, method_string); | 124 return SetProperty(PP_URLREQUESTPROPERTY_METHOD, method_string); |
41 } | 125 } |
| 126 |
| 127 /// SetHeaders() sets the <code>PP_URLREQUESTPROPERTY_HEADERS</code> |
| 128 /// (corresponding to a <code>\n</code> delimited string of type |
| 129 /// <code>PP_VARTYPE_STRING</code>) property for the request. |
| 130 /// Refer to the |
| 131 /// <a href="http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html"Header |
| 132 /// Field Definitions</a> documentaiton for further information. |
| 133 /// |
| 134 /// @param[in] headers_string A <code>Var</code> containing the property |
| 135 /// value. |
| 136 /// |
| 137 /// @return True if successful, false if the parameter is invalid. |
42 bool SetHeaders(const Var& headers_string) { | 138 bool SetHeaders(const Var& headers_string) { |
43 return SetProperty(PP_URLREQUESTPROPERTY_HEADERS, headers_string); | 139 return SetProperty(PP_URLREQUESTPROPERTY_HEADERS, headers_string); |
44 } | 140 } |
| 141 |
| 142 /// SetStreamToFile() sets the |
| 143 /// <code>PP_URLREQUESTPROPERTY_STREAMTOFILE</code> (corresponding |
| 144 /// to a bool of type <code>PP_VARTYPE_BOOL</code>). The default of the |
| 145 /// property is false. Set this value to true if you want to download the |
| 146 /// data to a file. Use URL_Loader::FinishStreamingToFile() to complete |
| 147 /// the download. |
| 148 /// |
| 149 /// @param[in] enable A <code>bool</code> containing the property value. |
| 150 /// |
| 151 /// @return True if successful, false if the parameter is invalid. |
45 bool SetStreamToFile(bool enable) { | 152 bool SetStreamToFile(bool enable) { |
46 return SetProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE, enable); | 153 return SetProperty(PP_URLREQUESTPROPERTY_STREAMTOFILE, enable); |
47 } | 154 } |
| 155 |
| 156 /// SetFollowRedirects() sets the |
| 157 /// <code>PP_URLREQUESTPROPERTY_FOLLOWREDIRECT</code> (corresponding |
| 158 /// to a bool of type <code>PP_VARTYPE_BOOL</code>). The default of the |
| 159 /// property is true. Set this value to false if you want to use |
| 160 /// URLLoader::FollowRedirects() to follow the redirects only after examining |
| 161 /// redirect headers. |
| 162 /// |
| 163 /// @param[in] enable A <code>bool</code> containing the property value. |
| 164 /// |
| 165 /// @return True if successful, false if the parameter is invalid. |
48 bool SetFollowRedirects(bool enable) { | 166 bool SetFollowRedirects(bool enable) { |
49 return SetProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, enable); | 167 return SetProperty(PP_URLREQUESTPROPERTY_FOLLOWREDIRECTS, enable); |
50 } | 168 } |
| 169 |
| 170 /// SetRecordDownloadProgress() sets the |
| 171 /// <code>PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGESS</code> |
| 172 /// (corresponding to a bool of type <code>PP_VARTYPE_BOOL</code>). The |
| 173 /// default of the property is false. Set this value to true if you want to |
| 174 /// be able to poll the download progress using |
| 175 /// URLLoader::GetDownloadProgress(). |
| 176 /// |
| 177 /// @param[in] enable A <code>bool</code> containing the property value. |
| 178 //// |
| 179 /// @return True if successful, false if the parameter is invalid. |
51 bool SetRecordDownloadProgress(bool enable) { | 180 bool SetRecordDownloadProgress(bool enable) { |
52 return SetProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, enable); | 181 return SetProperty(PP_URLREQUESTPROPERTY_RECORDDOWNLOADPROGRESS, enable); |
53 } | 182 } |
| 183 |
| 184 /// SetRecordUploadProgress() sets the |
| 185 /// <code>PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS</code> |
| 186 /// (corresponding to a bool of type <code>PP_VARTYPE_BOOL</code>). The |
| 187 /// default of the property is false. Set this value to true if you want to |
| 188 /// be able to poll the upload progress using URLLoader::GetUploadProgress(). |
| 189 /// |
| 190 /// @param[in] enable A <code>bool</code> containing the property value. |
| 191 /// |
| 192 /// @return True if successful, false if the parameter is invalid. |
54 bool SetRecordUploadProgress(bool enable) { | 193 bool SetRecordUploadProgress(bool enable) { |
55 return SetProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, enable); | 194 return SetProperty(PP_URLREQUESTPROPERTY_RECORDUPLOADPROGRESS, enable); |
56 } | 195 } |
57 // To use the default referrer, set url to an Undefined Var. | 196 |
| 197 /// SetCustomReferrerURL() sets the |
| 198 /// <code>PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL</code> |
| 199 /// (corresponding to a string of type <code>PP_VARTYPE_STRING</code> or |
| 200 /// might be set to undefined as <code>PP_VARTYPE_UNDEFINED</code>). Set it |
| 201 /// to a string to set a custom referrer (if empty, the referrer header will |
| 202 /// be omitted), or to undefined to use the default referrer. Only loaders |
| 203 /// with universal access (only available on trusted implementations) will |
| 204 /// accept <code>URLRequestInfo</code> objects that try to set a custom |
| 205 /// referrer; if given to a loader without universal access, |
| 206 /// <code>PP_ERROR_BADARGUMENT</code> will result. |
| 207 /// |
| 208 /// @param[in] url A <code>Var</code> containing the property value. |
| 209 /// |
| 210 /// @return True if successful, false if the parameter is invalid. |
58 bool SetCustomReferrerURL(const Var& url) { | 211 bool SetCustomReferrerURL(const Var& url) { |
59 return SetProperty(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL, url); | 212 return SetProperty(PP_URLREQUESTPROPERTY_CUSTOMREFERRERURL, url); |
60 } | 213 } |
| 214 |
| 215 /// SetAllowCrossOriginRequests() sets the |
| 216 /// <code>PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS</code> |
| 217 /// (corresponding to a bool of type <code>PP_VARTYPE_BOOL</code>). The |
| 218 /// default of the property is false. Whether cross-origin requests are |
| 219 /// allowed. Cross-origin requests are made using the CORS (Cross-Origin |
| 220 /// Resource Sharing) algorithm to check whether the request should be |
| 221 /// allowed. For the complete CORS algorithm, refer to the |
| 222 /// <a href="http://www.w3.org/TR/access-control">Cross-Origin Resource |
| 223 /// Sharing</a> documentation. |
| 224 /// |
| 225 /// @param[in] enable A <code>bool</code> containing the property value. |
| 226 /// |
| 227 /// @return True if successful, false if the parameter is invalid. |
61 bool SetAllowCrossOriginRequests(bool enable) { | 228 bool SetAllowCrossOriginRequests(bool enable) { |
62 return SetProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, enable); | 229 return SetProperty(PP_URLREQUESTPROPERTY_ALLOWCROSSORIGINREQUESTS, enable); |
63 } | 230 } |
| 231 |
| 232 /// SetAllowCredentials() sets the |
| 233 /// <code>PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS</code> |
| 234 /// (corresponding to a bool of type <code>PP_VARTYPE_BOOL</code>). The |
| 235 /// default of the property is false. Whether HTTP credentials are sent with |
| 236 /// cross-origin requests. If false, no credentials are sent with the request |
| 237 /// and cookies are ignored in the response. If the request is not |
| 238 /// cross-origin, this property is ignored. |
| 239 /// |
| 240 /// @param[in] enable A <code>bool</code> containing the property value. |
| 241 /// |
| 242 /// @return True if successful, false if the parameter is invalid. |
64 bool SetAllowCredentials(bool enable) { | 243 bool SetAllowCredentials(bool enable) { |
65 return SetProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, enable); | 244 return SetProperty(PP_URLREQUESTPROPERTY_ALLOWCREDENTIALS, enable); |
66 } | 245 } |
67 // To use the default content transfer encoding, set content_transfer_encoding | 246 |
68 // to an Undefined Var. | 247 |
| 248 /// SetCustomContentTransferEncoding() sets the |
| 249 /// <code>PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING</code> |
| 250 /// (corresponding to a string of type <code>PP_VARTYPE_STRING</code> or |
| 251 /// might be set to undefined as <code>PP_VARTYPE_UNDEFINED</code>). Set it |
| 252 /// to a string to set a custom content-transfer-encoding header (if empty, |
| 253 /// that header will be omitted), or to undefined to use the default (if |
| 254 /// any). Only loaders with universal access (only available on trusted |
| 255 /// implementations) will accept <code>URLRequestInfo</code> objects that try |
| 256 /// to set a custom content transfer encoding; if given to a loader without |
| 257 /// universal access, <code>PP_ERROR_BADARGUMENT</code> will result. |
| 258 /// |
| 259 /// @param[in] content_transfer_encoding A <code>Var</code> containing the |
| 260 /// property value. To use the default content transfer encoding, set |
| 261 /// <code>content_transfer_encoding</code> to an undefined <code>Var</code>. |
| 262 /// |
| 263 /// @return True if successful, false if the parameter is invalid. |
69 bool SetCustomContentTransferEncoding(const Var& content_transfer_encoding) { | 264 bool SetCustomContentTransferEncoding(const Var& content_transfer_encoding) { |
70 return SetProperty(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING, | 265 return SetProperty(PP_URLREQUESTPROPERTY_CUSTOMCONTENTTRANSFERENCODING, |
71 content_transfer_encoding); | 266 content_transfer_encoding); |
72 } | 267 } |
| 268 |
| 269 /// SetPrefetchBufferUpperThreshold() sets the |
| 270 /// <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD</code> |
| 271 /// (corresponding to a integer of type <code>PP_VARTYPE_INT32</code>). The |
| 272 /// default is not defined and is set by the browser possibly depending on |
| 273 /// system capabilities. Set it to an integer to set an upper threshold for |
| 274 /// the prefetched buffer of an asynchronous load. When exceeded, the browser |
| 275 /// will defer loading until |
| 276 /// <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERERTHRESHOLD</code> is hit, |
| 277 /// at which time it will begin prefetching again. When setting this |
| 278 /// property, |
| 279 /// <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERERTHRESHOLD</code> must |
| 280 /// also be set. Behavior is undefined if the former is <= the latter. |
| 281 /// |
| 282 /// @param[in] size An int32_t containing the property value. |
| 283 /// |
| 284 /// @return True if successful, false if the parameter is invalid. |
73 bool SetPrefetchBufferUpperThreshold(int32_t size) { | 285 bool SetPrefetchBufferUpperThreshold(int32_t size) { |
74 return SetProperty(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD, | 286 return SetProperty(PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD, |
75 size); | 287 size); |
76 } | 288 } |
| 289 |
| 290 /// SetPrefetchBufferLowerThreshold() sets the |
| 291 /// <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD</code> |
| 292 /// (corresponding to a integer of type <code>PP_VARTYPE_INT32</code>). The |
| 293 /// default is not defined and is set by the browser to a value appropriate |
| 294 /// for the default |
| 295 /// <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD</code>. |
| 296 /// Set it to an integer to set a lower threshold for the prefetched buffer |
| 297 /// of an asynchronous load. When reached, the browser will resume loading if |
| 298 /// If <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERERTHRESHOLD</code> had |
| 299 /// previously been reached. |
| 300 /// When setting this property, |
| 301 /// <code>PP_URLREQUESTPROPERTY_PREFETCHBUFFERUPPERTHRESHOLD</code> must also |
| 302 /// be set. Behavior is undefined if the former is >= the latter. |
| 303 /// |
| 304 /// @param[in] size An int32_t containing the property value. |
| 305 /// |
| 306 /// @return True if successful, false if the parameter is invalid. |
77 bool SetPrefetchBufferLowerThreshold(int32_t size) { | 307 bool SetPrefetchBufferLowerThreshold(int32_t size) { |
78 return SetProperty(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD, | 308 return SetProperty(PP_URLREQUESTPROPERTY_PREFETCHBUFFERLOWERTHRESHOLD, |
79 size); | 309 size); |
80 } | 310 } |
81 }; | 311 }; |
82 | 312 |
83 } // namespace pp | 313 } // namespace pp |
84 | 314 |
85 #endif // PPAPI_CPP_URL_REQUEST_INFO_H_ | 315 #endif // PPAPI_CPP_URL_REQUEST_INFO_H_ |
OLD | NEW |