OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 The Native Client 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 WEB_RESOURCE_LOADER_H_ | |
6 #define WEB_RESOURCE_LOADER_H_ | |
7 | |
8 #include <string> | |
9 | |
10 #include "experimental/conways_life/threading/ref_count.h" | |
11 #include "ppapi/cpp/completion_callback.h" | |
12 #include "ppapi/cpp/url_loader.h" | |
13 | |
14 namespace pp { | |
15 class Instance; | |
16 class URLRequestInfo; | |
17 } // namespace pp | |
18 | |
19 namespace life { | |
20 | |
21 // WebResourceLoader handles downloading resources from URLs. It handles the | |
22 // callbacks. It uses a delegate pattern to connect to the class that | |
23 // is aggregating and interpreting the resource data. This Delegate class must | |
24 // declare the following methods: | |
25 // | |
26 // OnWebResourceLoaderCallback: | |
27 // Called at various times during the download process. The op_code indicates | |
28 // what step has just been completed. The Delegate should return true to | |
29 // continue the download process, or false to abort. | |
30 // bool OnWebResourceLoaderCallback(DispatchOpCode op_code, | |
31 // WebResourceLoader* loader); | |
32 // | |
33 // OnWebResourceLoaderError: | |
34 // Called when an error occurs. | |
35 // void OnWebResourceLoaderError(int32_t error, WebResourceLoader* loader); | |
36 // | |
37 // OnWebResourceLoaderDone: | |
38 // This is always the last method called, either after a successful download or | |
39 // after an error. | |
40 // void OnWebResourceLoaderDone(WebResourceLoader* loader); | |
41 template <class Delegate> | |
42 class WebResourceLoader { | |
43 public: | |
44 // DispatchOpCode used with Delegate::OnWebResourceLoaderCallback. | |
45 enum DispatchOpCode { | |
46 kUrlResponseInfoReady, // URL response ready; query with GetResponseInfo. | |
47 kDataReceived, // A chunk of data has been received in buffer. | |
48 kDownloadComplete // Download done, data/file is ready for consumption. | |
49 }; | |
50 | |
51 WebResourceLoader(pp::Instance* instance, Delegate* delegate); | |
52 virtual ~WebResourceLoader(); | |
53 | |
54 // You can call this function from any thread or from Delegate method | |
55 // OnWebResourceLoaderDone to safely delete this loader instance. | |
56 void CloseAndDeleteSelf(); | |
57 | |
58 // Initiate a download from the given URL. | |
59 void LoadURL(const std::string& url); | |
60 | |
61 // Buffer to receive the resource data. If no buffer is provided, a 4K | |
62 // internal buffer is used. Method set_content_buffer should be called | |
63 // before starting a download or from within one of the delegate methods. | |
64 void set_content_buffer(char* buffer, size_t size); | |
65 const char* buffer() const { return buffer_; } | |
66 int32_t data_size() const { return data_size_; } | |
67 | |
68 // Get info about the current download. | |
69 const std::string url() const; | |
70 int32_t GetHttpStatus() const; | |
71 pp::URLResponseInfo GetResponseInfo() const; | |
72 | |
73 // Close the current connection. It is ok to use the same loader to download | |
74 // from several URLs. However, the current connection must be closed before | |
75 // starting a new one. | |
76 void Close(); | |
77 | |
78 private: | |
79 // We use a single completion callback with a dispatch op-code to distinguish | |
80 // between the various operations. | |
81 void CompletionCallbackFunc(int32_t result, DispatchOpCode op_code); | |
82 // Create a completion callback taking the given op-code. | |
83 pp::CompletionCallback MakeCallback(DispatchOpCode op_code); | |
84 | |
85 // Helpers. | |
86 void InitializeRequest(const std::string& url, pp::URLRequestInfo* request); | |
87 void ReadNextDataBlock(); | |
88 void StartDownload(int32_t result, const std::string& url); | |
89 | |
90 // Main-thread callback for CloseAndDeleteSelf. | |
91 static void DeleteOnMainThread(void* user_data, int32_t err); | |
92 | |
93 pp::CompletionCallbackFactory<WebResourceLoader, | |
94 threading::RefCount> factory_; | |
95 pp::Instance* instance_; | |
96 pp::URLLoader url_loader_; | |
97 bool connected_; | |
98 | |
99 char* buffer_; | |
100 size_t buffer_size_; | |
101 int32_t data_size_; | |
102 | |
103 static const int32_t kInternalBufferSize = 4096; | |
104 char internal_buffer_[kInternalBufferSize]; | |
105 | |
106 Delegate* delegate_; | |
107 }; | |
108 | |
109 | |
110 } // namespace life | |
111 | |
112 #endif // WEB_RESOURCE_LOADER_H_ | |
OLD | NEW |