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