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