| 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_LOADER_H_ | 5 #ifndef PPAPI_CPP_URL_LOADER_H_ |
| 6 #define PPAPI_CPP_URL_LOADER_H_ | 6 #define PPAPI_CPP_URL_LOADER_H_ |
| 7 | 7 |
| 8 #include <vector> |
| 9 |
| 8 #include "ppapi/c/pp_stdint.h" | 10 #include "ppapi/c/pp_stdint.h" |
| 11 #include "ppapi/cpp/completion_callback.h" |
| 9 #include "ppapi/cpp/resource.h" | 12 #include "ppapi/cpp/resource.h" |
| 10 | 13 |
| 11 /// @file | 14 /// @file |
| 12 /// This file defines the API for loading URLs. | 15 /// This file defines the API for loading URLs. |
| 13 namespace pp { | 16 namespace pp { |
| 14 | 17 |
| 15 class CompletionCallback; | 18 class CompletionCallback; |
| 16 class InstanceHandle; | 19 class InstanceHandle; |
| 17 class URLRequestInfo; | 20 class URLRequestInfo; |
| 18 class URLResponseInfo; | 21 class URLResponseInfo; |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 /// @return A <code>URLResponseInfo</code> corresponding to the | 120 /// @return A <code>URLResponseInfo</code> corresponding to the |
| 118 /// <code>URLResponseInfo</code> if successful, an <code>is_null</code> | 121 /// <code>URLResponseInfo</code> if successful, an <code>is_null</code> |
| 119 /// object if the loader is not a valid resource or if Open() has not been | 122 /// object if the loader is not a valid resource or if Open() has not been |
| 120 /// called. | 123 /// called. |
| 121 URLResponseInfo GetResponseInfo() const; | 124 URLResponseInfo GetResponseInfo() const; |
| 122 | 125 |
| 123 /// This function is used to read the response body. The size of the buffer | 126 /// This function is used to read the response body. The size of the buffer |
| 124 /// must be large enough to hold the specified number of bytes to read. | 127 /// must be large enough to hold the specified number of bytes to read. |
| 125 /// This function might perform a partial read. | 128 /// This function might perform a partial read. |
| 126 /// | 129 /// |
| 130 /// <strong>Caveat:</strong> This ReadResponseBody() is potentially unsafe if |
| 131 /// you're using a CompletionCallbackFactory to scope callbacks to the |
| 132 /// lifetime of your class. When your class goes out of scope, the callback |
| 133 /// factory will not actually cancel the callback, but will rather just skip |
| 134 /// issuing the callback on your class. This means that if the UrlLoader |
| 135 /// object outlives your class (if you made a copy saved somewhere else, for |
| 136 /// example), then the browser will still try to write into your buffer when |
| 137 /// the asynchronous read completes, potentially causing a crash. |
| 138 /// |
| 139 /// See the other version of ReadResponseBody() which avoids this problem by |
| 140 /// writing into CompletionCallbackWithOutput, where the output buffer is |
| 141 /// automatically managed by the callback. |
| 142 /// |
| 127 /// @param[in,out] buffer A pointer to the buffer for the response body. | 143 /// @param[in,out] buffer A pointer to the buffer for the response body. |
| 128 /// @param[in] bytes_to_read The number of bytes to read. | 144 /// @param[in] bytes_to_read The number of bytes to read. |
| 129 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous | 145 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous |
| 130 /// completion. The callback will run if the bytes (full or partial) are | 146 /// completion. The callback will run if the bytes (full or partial) are |
| 131 /// read or an error occurs asynchronously. This callback will run only if | 147 /// read or an error occurs asynchronously. This callback will run only if |
| 132 /// this function returns <code>PP_OK_COMPLETIONPENDING</code>. | 148 /// this function returns <code>PP_OK_COMPLETIONPENDING</code>. |
| 133 /// | 149 /// |
| 134 /// @return An int32_t containing the number of bytes read or an error code | 150 /// @return An int32_t containing the number of bytes read or an error code |
| 135 /// from <code>pp_errors.h</code>. | 151 /// from <code>pp_errors.h</code>. |
| 136 int32_t ReadResponseBody(void* buffer, | 152 int32_t ReadResponseBody(void* buffer, |
| 137 int32_t bytes_to_read, | 153 int32_t bytes_to_read, |
| 138 const CompletionCallback& cc); | 154 const CompletionCallback& cc); |
| 139 | 155 |
| 156 /// This function is used to read the responsed body. A PP_ArrayOutput must be |
| 157 /// provided so that output will be stored in its allocated buffer. This |
| 158 /// function might perform a partial read. |
| 159 /// |
| 160 /// @param[in] max_read_length The number of bytes to read. |
| 161 /// @param[in] cc A <code>CompletionCallbackWithOutput</code> to hold a |
| 162 /// PP_ArrayOutput for output, and run on asynchronous CompletionCallback. |
| 163 /// ReadResponseBody(). The callback will run if the bytes (full or partial) |
| 164 /// are read or an error occurs asynchronously. This callback will run only if |
| 165 /// this function returns <code>PP_OK_COMPLETIONPENDING</code>. |
| 166 /// |
| 167 /// @return An int32_t containing the number of bytes read or an error code |
| 168 /// from <code>pp_errors.h</code>. |
| 169 int32_t ReadResponseBody( |
| 170 int32_t max_read_length, |
| 171 const CompletionCallbackWithOutput< std::vector<char> >& cc); |
| 172 |
| 140 /// This function is used to wait for the response body to be completely | 173 /// This function is used to wait for the response body to be completely |
| 141 /// downloaded to the file provided by the GetBodyAsFileRef() in the current | 174 /// downloaded to the file provided by the GetBodyAsFileRef() in the current |
| 142 /// <code>URLResponseInfo</code>. This function is only used if | 175 /// <code>URLResponseInfo</code>. This function is only used if |
| 143 /// <code>PP_URLREQUESTPROPERTY_STREAMTOFILE</code> was set on the | 176 /// <code>PP_URLREQUESTPROPERTY_STREAMTOFILE</code> was set on the |
| 144 /// <code>URLRequestInfo</code> passed to Open(). | 177 /// <code>URLRequestInfo</code> passed to Open(). |
| 145 /// | 178 /// |
| 146 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous | 179 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous |
| 147 /// completion. This callback will run when body is downloaded or an error | 180 /// completion. This callback will run when body is downloaded or an error |
| 148 /// occurs after FinishStreamingToFile() returns | 181 /// occurs after FinishStreamingToFile() returns |
| 149 /// <code>PP_OK_COMPLETIONPENDING</code>. | 182 /// <code>PP_OK_COMPLETIONPENDING</code>. |
| 150 /// | 183 /// |
| 151 /// @return An int32_t containing the number of bytes read or an error code | 184 /// @return An int32_t containing the number of bytes read or an error code |
| 152 /// from <code>pp_errors.h</code>. | 185 /// from <code>pp_errors.h</code>. |
| 153 int32_t FinishStreamingToFile(const CompletionCallback& cc); | 186 int32_t FinishStreamingToFile(const CompletionCallback& cc); |
| 154 | 187 |
| 155 /// This function is used to cancel any pending IO and close the URLLoader | 188 /// This function is used to cancel any pending IO and close the URLLoader |
| 156 /// object. Any pending callbacks will still run, reporting | 189 /// object. Any pending callbacks will still run, reporting |
| 157 /// <code>PP_ERROR_ABORTED</code> if pending IO was interrupted. It is NOT | 190 /// <code>PP_ERROR_ABORTED</code> if pending IO was interrupted. It is NOT |
| 158 /// valid to call Open() again after a call to this function. | 191 /// valid to call Open() again after a call to this function. |
| 159 /// | 192 /// |
| 160 /// <strong>Note:</strong> If the <code>URLLoader</code> object is destroyed | 193 /// <strong>Note:</strong> If the <code>URLLoader</code> object is destroyed |
| 161 /// while it is still open, then it will be implicitly closed so you are not | 194 /// while it is still open, then it will be implicitly closed so you are not |
| 162 /// required to call Close(). | 195 /// required to call Close(). |
| 163 void Close(); | 196 void Close(); |
| 197 |
| 198 private: |
| 199 struct CallbackData1_0 { |
| 200 PP_ArrayOutput output; |
| 201 char* temp_buffer; |
| 202 PP_CompletionCallback original_callback; |
| 203 }; |
| 204 |
| 205 // Provide backwards-compatability for older ReadResponseBody versions. |
| 206 // Converts the old-style "char*" output buffer of 1.0 to the new |
| 207 // "PP_ArrayOutput" interface in 1.1. |
| 208 // |
| 209 // This takes a heap-allocated CallbackData1_0 struct passed as the user data |
| 210 // and deletes it when the call completes. |
| 211 static void CallbackConverter(void* user_data, int32_t result); |
| 164 }; | 212 }; |
| 165 | 213 |
| 166 } // namespace pp | 214 } // namespace pp |
| 167 | 215 |
| 168 #endif // PPAPI_CPP_URL_LOADER_H_ | 216 #endif // PPAPI_CPP_URL_LOADER_H_ |
| OLD | NEW |