Chromium Code Reviews| 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 "ppapi/c/pp_stdint.h" | 8 #include "ppapi/c/pp_stdint.h" |
| 9 #include "ppapi/cpp/resource.h" | 9 #include "ppapi/cpp/resource.h" |
| 10 | 10 |
| 11 /// @file | |
| 12 /// This file defines the API for loading URLs. | |
| 11 namespace pp { | 13 namespace pp { |
| 12 | 14 |
| 13 class CompletionCallback; | 15 class CompletionCallback; |
| 14 class Instance; | 16 class Instance; |
| 15 class URLRequestInfo; | 17 class URLRequestInfo; |
| 16 class URLResponseInfo; | 18 class URLResponseInfo; |
| 17 | 19 |
| 18 // URLLoader provides an API to download URLs. | 20 /// URLLoader provides an API for loading URLs. |
| 19 // | 21 /// |
| 20 // Please see the example in ppapi/examples/url_loader/url_loader.cc. | 22 /// <strong>Example:</strong> |
| 23 /// | |
| 24 /// @code | |
| 25 /// | |
| 26 /// class MyHandler { | |
|
brettw
2011/08/23 23:29:01
This code example should not have been added back.
jond
2011/08/24 15:33:59
I actually don't know how this example got in here
polina
2011/08/24 19:52:01
Jon, if you view the diff for patch sets 1 or 2, t
polina
2011/08/24 19:55:53
And I just verified that the file is there:
http:
polina
2011/08/24 20:06:14
Sorry, I rushed with my "helpful" comment. The dir
polina
2011/08/24 20:07:35
Confirmed with Brett. Please point to streaming.cc
| |
| 27 /// public: | |
| 28 /// MyHandler(Instance* instance) | |
| 29 /// : factory_(this), | |
| 30 /// loader_(instance), | |
| 31 /// did_open_(false) { | |
| 32 /// } | |
| 33 /// void ProcessURL(const char* url) { | |
| 34 /// CompletionCallback* cc = NewCallback(); | |
| 35 /// int32_t rv = loader_.Open(MakeRequest(url), cc); | |
| 36 /// if (rv != PP_Error_WouldBlock) | |
| 37 /// cc->Run(rv); | |
| 38 /// } | |
| 39 /// private: | |
| 40 /// CompletionCallback* NewCallback() { | |
| 41 /// return factory_.NewOptionalCallback(&MyHandler::DidCompleteIO); | |
| 42 /// } | |
| 43 /// URLRequestInfo MakeRequest(const char* url) { | |
| 44 /// URLRequestInfo request; | |
| 45 /// request.SetURL(url); | |
| 46 /// request.SetMethod("GET"); | |
| 47 /// request.SetFollowRedirects(true); | |
| 48 /// return request; | |
| 49 /// } | |
| 50 /// void DidCompleteIO(int32_t result) { | |
| 51 /// if (result > 0) { | |
| 52 /// // buf_ now contains 'result' number of bytes from the URL. | |
| 53 /// ProcessBytes(buf_, result); | |
| 54 /// ReadMore(); | |
| 55 /// } else if (result == PP_OK && !did_open_) { | |
| 56 /// // Headers are available, and we can start reading the body. | |
| 57 /// did_open_ = true; | |
| 58 /// ProcessResponseInfo(loader_.GetResponseInfo()); | |
| 59 /// ReadMore(); | |
| 60 /// } else { | |
| 61 /// // Done reading (possibly with an error given by 'result'). | |
| 62 /// } | |
| 63 /// } | |
| 64 /// void ReadMore() { | |
| 65 /// CompletionCallback* cc = NewCallback(); | |
| 66 /// int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_), cc); | |
| 67 /// if (rv != PP_Error_WouldBlock) | |
| 68 /// cc->Run(rv); | |
| 69 /// } | |
| 70 /// void ProcessResponseInfo(const URLResponseInfo& response_info) { | |
| 71 /// // Read response headers, etc. | |
| 72 /// } | |
| 73 /// void ProcessBytes(const char* bytes, int32_t length) { | |
| 74 /// // Do work ... | |
| 75 /// } | |
| 76 /// pp::CompletionCallbackFactory<MyHandler> factory_; | |
| 77 /// pp::URLLoader loader_; | |
| 78 /// char buf_[4096]; | |
| 79 /// bool did_open_; | |
| 80 /// }; | |
| 81 /// @endcode | |
| 21 class URLLoader : public Resource { | 82 class URLLoader : public Resource { |
| 22 public: | 83 public: |
| 23 // Creates an is_null() URLLoader object. | 84 /// Default constructor for creating an is_null() |
| 85 /// <code>URLLoader</code> object. | |
| 24 URLLoader() {} | 86 URLLoader() {} |
| 25 | 87 |
| 26 // TODO(brettw) remove this when NaCl is updated to use the new version | 88 // TODO(brettw) remove this when NaCl is updated to use the new version |
| 27 // that takes a pointer. | 89 // that takes a pointer. |
| 28 explicit URLLoader(const Instance& instance); | 90 explicit URLLoader(const Instance& instance); |
| 29 | 91 |
| 92 /// A constructor used when a <code>PP_Resource</code> is provided as a | |
| 93 /// return value whose reference count we need to increment. | |
| 94 /// | |
| 95 /// @param[in] resource A <code>PP_Resource</code>. | |
| 30 explicit URLLoader(PP_Resource resource); | 96 explicit URLLoader(PP_Resource resource); |
| 97 | |
| 98 /// A constructor used to allocate a new URLLoader in the browser. The | |
| 99 /// resulting object will be <code>is_null</code> if the allocation failed. | |
| 100 /// | |
| 101 /// @param[in] instance An <code>Instance</code>. | |
| 31 explicit URLLoader(Instance* instance); | 102 explicit URLLoader(Instance* instance); |
| 103 | |
| 104 /// The copy constructor for <code>URLLoader</code>. | |
| 105 /// | |
| 106 /// @param other A <code>URLLoader</code> to be copied. | |
| 32 URLLoader(const URLLoader& other); | 107 URLLoader(const URLLoader& other); |
| 33 | 108 |
| 34 // PPB_URLLoader methods: | 109 /// This function begins loading the <code>URLRequestInfo</code>. |
| 110 /// The operation completes when response headers are received or when an | |
| 111 /// error occurs. Use GetResponseInfo() to access the response | |
| 112 /// headers. | |
| 113 /// | |
| 114 /// @param[in] request_info A <code>URLRequestInfo</code> corresponding to a | |
| 115 /// URLRequestInfo. | |
| 116 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous | |
| 117 /// completion of Open(). This callback will run when response | |
| 118 /// headers for the url are received or error occured. This callback | |
| 119 /// will only run if Open() returns <code>PP_OK_COMPLETIONPENDING</code>. | |
| 120 /// | |
| 121 /// @return An int32_t containing an error code from | |
| 122 /// <code>pp_errors.h</code>. | |
| 35 int32_t Open(const URLRequestInfo& request_info, | 123 int32_t Open(const URLRequestInfo& request_info, |
| 36 const CompletionCallback& cc); | 124 const CompletionCallback& cc); |
| 125 | |
| 126 /// This function can be invoked to follow a | |
| 127 /// redirect after Open() completed on receiving redirect headers. | |
| 128 /// | |
| 129 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous | |
| 130 /// completion of FollowRedirect(). This callback will run when response | |
| 131 /// headers for the redirect url are received or error occured. This callback | |
| 132 /// will only run if FollowRedirect() returns | |
| 133 /// <code>PP_OK_COMPLETIONPENDING</code>. | |
| 134 /// @return An int32_t containing an error code from | |
| 135 /// <code>pp_errors.h</code>. | |
| 37 int32_t FollowRedirect(const CompletionCallback& cc); | 136 int32_t FollowRedirect(const CompletionCallback& cc); |
| 137 | |
| 138 /// This function returns the current upload progress (which is only | |
| 139 /// meaningful after Open() has been called). Progress only refers to the | |
| 140 /// request body and does not include the headers. | |
| 141 /// | |
| 142 /// This data is only available if the <code>URLRequestInfo</code> passed to | |
| 143 /// Open() had the | |
| 144 /// <code>PP_URLREQUESTPROPERTY_REPORTUPLOADPROGRESS</code> property set to | |
| 145 /// <code>PP_TRUE</code>. | |
| 146 /// | |
| 147 /// @param[in] bytes_sent The number of bytes sent thus far. | |
| 148 /// @param[in] total_bytes_to_be_sent The total number of bytes to be sent. | |
| 149 /// @return True if the upload progress is available, false if it is not | |
| 150 /// available. | |
| 38 bool GetUploadProgress(int64_t* bytes_sent, | 151 bool GetUploadProgress(int64_t* bytes_sent, |
| 39 int64_t* total_bytes_to_be_sent) const; | 152 int64_t* total_bytes_to_be_sent) const; |
| 153 | |
| 154 /// This function returns the current download progress, which is meaningful | |
| 155 /// after Open() has been called. Progress only refers to the response body | |
| 156 /// and does not include the headers. | |
| 157 /// | |
| 158 /// This data is only available if the <code>URLRequestInfo</code> passed to | |
| 159 /// Open() had the | |
| 160 /// <code>PP_URLREQUESTPROPERTY_REPORTDOWNLOADPROGRESS</code> property set to | |
| 161 /// PP_TRUE. | |
| 162 /// | |
| 163 /// @param[in] bytes_received The number of bytes received thus far. | |
| 164 /// @param[in] total_bytes_to_be_received The total number of bytes to be | |
| 165 /// received. The total bytes to be received may be unknown, in which case | |
| 166 /// <code>total_bytes_to_be_received</code> will be set to -1. | |
| 167 /// @return True if the download progress is available, false if it is | |
| 168 /// not available. | |
| 40 bool GetDownloadProgress(int64_t* bytes_received, | 169 bool GetDownloadProgress(int64_t* bytes_received, |
| 41 int64_t* total_bytes_to_be_received) const; | 170 int64_t* total_bytes_to_be_received) const; |
| 171 | |
| 172 /// This is a function that returns the current | |
| 173 /// <code>URLResponseInfo</code> object. | |
| 174 /// | |
| 175 /// @return A <code>URLResponseInfo</code> corresponding to the | |
| 176 /// <code>URLResponseInfo</code> if successful, an <code>is_null</code> | |
| 177 /// object if the loader is not a valid resource or if Open() has not been | |
| 178 /// called. | |
| 42 URLResponseInfo GetResponseInfo() const; | 179 URLResponseInfo GetResponseInfo() const; |
| 180 | |
| 181 /// This function is used to read the response body. The size of the buffer | |
| 182 /// must be large enough to hold the specified number of bytes to read. | |
| 183 /// This function might perform a partial read. | |
| 184 /// | |
| 185 /// @param[in,out] buffer A pointer to the buffer for the response body. | |
| 186 /// @param[in] bytes_to_read The number of bytes to read. | |
| 187 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous | |
| 188 /// completion. The callback will run if the bytes (full or partial) are | |
| 189 /// read or an error occurs asynchronously. This callback will run only if | |
| 190 /// this function returns <code>PP_OK_COMPLETIONPENDING</code>. | |
| 191 /// @return An int32_t containing the number of bytes read or an error code | |
| 192 /// from <code>pp_errors.h</code>. | |
| 43 int32_t ReadResponseBody(void* buffer, | 193 int32_t ReadResponseBody(void* buffer, |
| 44 int32_t bytes_to_read, | 194 int32_t bytes_to_read, |
| 45 const CompletionCallback& cc); | 195 const CompletionCallback& cc); |
| 196 | |
| 197 /// This function is used to wait for the response body to be completely | |
| 198 /// downloaded to the file provided by the GetBodyAsFileRef() in the current | |
| 199 /// <code>URLResponseInfo</code>. This function is only used if | |
| 200 /// <code>PP_URLREQUESTPROPERTY_STREAMTOFILE</code> was set on the | |
| 201 /// <code>URLRequestInfo</code> passed to Open(). | |
| 202 /// | |
| 203 /// @param[in] cc A <code>CompletionCallback</code> to run on asynchronous | |
| 204 /// completion. This callback will run when body is downloaded or an error | |
| 205 /// occurs after FinishStreamingToFile() returns | |
| 206 /// <code>PP_OK_COMPLETIONPENDING</code>. | |
| 207 /// @return An int32_t containing the number of bytes read or an error code | |
| 208 /// from <code>pp_errors.h</code>. | |
| 46 int32_t FinishStreamingToFile(const CompletionCallback& cc); | 209 int32_t FinishStreamingToFile(const CompletionCallback& cc); |
| 210 | |
| 211 /// This function is used to cancel any pending IO and close the URLLoader | |
| 212 /// object. Any pending callbacks will still run, reporting | |
| 213 /// <code>PP_ERROR_ABORTED</code> if pending IO was interrupted. It is NOT | |
| 214 /// valid to call Open() again after a call to this function. | |
| 215 /// | |
| 216 /// <strong>Note:</strong> If the <code>URLLoader</code> object is destroyed | |
| 217 /// while it is still open, then it will be implicitly closed so you are not | |
| 218 /// required to call Close(). | |
| 47 void Close(); | 219 void Close(); |
| 48 }; | 220 }; |
| 49 | 221 |
| 50 } // namespace pp | 222 } // namespace pp |
| 51 | 223 |
| 52 #endif // PPAPI_CPP_URL_LOADER_H_ | 224 #endif // PPAPI_CPP_URL_LOADER_H_ |
| OLD | NEW |