OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef PPAPI_CPP_DEV_URL_LOADER_DEV_H_ | |
6 #define PPAPI_CPP_DEV_URL_LOADER_DEV_H_ | |
7 | |
8 #include "ppapi/c/pp_stdint.h" | |
9 #include "ppapi/cpp/resource.h" | |
10 | |
11 namespace pp { | |
12 | |
13 class CompletionCallback; | |
14 class Instance; | |
15 class URLRequestInfo_Dev; | |
16 class URLResponseInfo_Dev; | |
17 | |
18 // URLLoader provides an API to download URLs. | |
19 // | |
20 // EXAMPLE USAGE: | |
21 // | |
22 // class MyHandler { | |
23 // public: | |
24 // MyHandler(const Instance& instance) | |
25 // : factory_(this), | |
26 // loader_(instance), | |
27 // did_open_(false) { | |
28 // } | |
29 // void ProcessURL(const char* url) { | |
30 // CompletionCallback* cc = NewCallback(); | |
31 // int32_t rv = loader_.Open(MakeRequest(url), cc); | |
32 // if (rv != PP_Error_WouldBlock) | |
33 // cc->Run(rv); | |
34 // } | |
35 // private: | |
36 // CompletionCallback* NewCallback() { | |
37 // return factory_.NewCallback(&MyHandler::DidCompleteIO); | |
38 // } | |
39 // URLRequestInfo MakeRequest(const char* url) { | |
40 // URLRequestInfo request; | |
41 // request.SetURL(url); | |
42 // request.SetMethod("GET"); | |
43 // request.SetFollowRedirects(true); | |
44 // return request; | |
45 // } | |
46 // void DidCompleteIO(int32_t result) { | |
47 // if (result > 0) { | |
48 // // buf_ now contains 'result' number of bytes from the URL. | |
49 // ProcessBytes(buf_, result); | |
50 // ReadMore(); | |
51 // } else if (result == PP_OK && !did_open_) { | |
52 // // Headers are available, and we can start reading the body. | |
53 // did_open_ = true; | |
54 // ProcessResponseInfo(loader_.GetResponseInfo()); | |
55 // ReadMore(); | |
56 // } else { | |
57 // // Done reading (possibly with an error given by 'result'). | |
58 // } | |
59 // } | |
60 // void ReadMore() { | |
61 // CompletionCallback* cc = NewCallback(); | |
62 // int32_t rv = fio_.Read(offset_, buf_, sizeof(buf_), cc); | |
63 // if (rv != PP_Error_WouldBlock) | |
64 // cc->Run(rv); | |
65 // } | |
66 // void ProcessResponseInfo(const URLResponseInfo& response_info) { | |
67 // // Read response headers, etc. | |
68 // } | |
69 // void ProcessBytes(const char* bytes, int32_t length) { | |
70 // // Do work ... | |
71 // } | |
72 // pp::CompletionCallbackFactory<MyHandler> factory_; | |
73 // pp::URLLoader loader_; | |
74 // char buf_[4096]; | |
75 // bool did_open_; | |
76 // }; | |
77 // | |
78 class URLLoader_Dev : public Resource { | |
79 public: | |
80 // Creates an is_null() URLLoader object. | |
81 URLLoader_Dev() {} | |
82 | |
83 explicit URLLoader_Dev(PP_Resource resource); | |
84 explicit URLLoader_Dev(const Instance& instance); | |
85 URLLoader_Dev(const URLLoader_Dev& other); | |
86 | |
87 URLLoader_Dev& operator=(const URLLoader_Dev& other); | |
88 void swap(URLLoader_Dev& other); | |
89 | |
90 // PPB_URLLoader methods: | |
91 int32_t Open(const URLRequestInfo_Dev& request_info, | |
92 const CompletionCallback& cc); | |
93 int32_t FollowRedirect(const CompletionCallback& cc); | |
94 bool GetUploadProgress(int64_t* bytes_sent, | |
95 int64_t* total_bytes_to_be_sent) const; | |
96 bool GetDownloadProgress(int64_t* bytes_received, | |
97 int64_t* total_bytes_to_be_received) const; | |
98 URLResponseInfo_Dev GetResponseInfo() const; | |
99 int32_t ReadResponseBody(char* buffer, | |
100 int32_t bytes_to_read, | |
101 const CompletionCallback& cc); | |
102 int32_t FinishStreamingToFile(const CompletionCallback& cc); | |
103 void Close(); | |
104 }; | |
105 | |
106 } // namespace pp | |
107 | |
108 #endif // PPAPI_CPP_DEV_URL_LOADER_DEV_H_ | |
OLD | NEW |