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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | |
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | |
7 | |
8 #include <deque> | |
9 | |
10 #include "native_client/src/include/nacl_macros.h" | |
11 #include "native_client/src/include/nacl_string.h" | |
12 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | |
13 #include "ppapi/cpp/completion_callback.h" | |
14 #include "ppapi/cpp/file_io.h" | |
15 #include "ppapi/cpp/url_loader.h" | |
16 #include "ppapi/cpp/instance.h" | |
17 | |
18 namespace plugin { | |
19 | |
20 class Plugin; | |
21 | |
22 typedef enum { | |
23 DOWNLOAD_TO_FILE = 0 << 0, | |
24 DOWNLOAD_TO_BUFFER = 1 << 0 | |
25 } DownloadFlags; | |
26 | |
27 typedef enum { | |
28 SCHEME_CHROME_EXTENSION, | |
29 SCHEME_DATA, | |
30 SCHEME_OTHER | |
31 } UrlSchemeType; | |
32 | |
33 // A class that wraps PPAPI URLLoader and FileIO functionality for downloading | |
34 // the url into a file and providing an open file descriptor. | |
35 class FileDownloader { | |
36 public: | |
37 // Ctor initializes |instance_| to NULL, be sure to call Initialize() before | |
38 // calling Open(), or Open() will fail. | |
39 FileDownloader() | |
40 : instance_(NULL), | |
41 file_open_notify_callback_(pp::BlockUntilComplete()), | |
42 file_io_trusted_interface_(NULL), | |
43 open_time_(-1) {} | |
44 ~FileDownloader() {} | |
45 | |
46 // Initialize() can only be called once during the lifetime of this instance. | |
47 void Initialize(Plugin* instance); | |
48 | |
49 // Issues a GET on |url| downloading the response into a file. The file is | |
50 // then opened and a file descriptor is made available. | |
51 // Returns true when callback is scheduled to be called on success or failure. | |
52 // Returns false if callback is NULL, Initialize() has not been called or if | |
53 // the PPB_FileIO_Trusted interface is not available. | |
54 bool Open(const nacl::string& url, | |
55 DownloadFlags flags, | |
56 const pp::CompletionCallback& callback); | |
57 | |
58 // If downloading and opening succeeded, this returns a valid read-only | |
59 // POSIX file descriptor. On failure, the return value is an invalid | |
60 // descriptor. The file descriptor is owned by this instance, so the | |
61 // delegate does not have to close it. | |
62 int32_t GetPOSIXFileDescriptor(); | |
63 | |
64 // Returns the time delta between the call to Open() and this function. | |
65 int64_t TimeSinceOpenMilliseconds() const; | |
66 | |
67 // The value of |url_| changes over the life of this instance. When the file | |
68 // is first opened, |url_| is a copy of the URL used to open the file, which | |
69 // can be a relative URL. Once the GET request has finished, and the contents | |
70 // of the file represented by |url_| are available, |url_| is the full URL | |
71 // including the scheme, host and full path. | |
72 const nacl::string& url() const { return url_; } | |
73 | |
74 // Returns the url passed to Open(). | |
75 const nacl::string& url_to_open() const { return url_to_open_; } | |
76 | |
77 // Returns the buffer used for DOWNLOAD_TO_BUFFER mode. | |
78 const std::deque<char>& buffer() const { return buffer_; } | |
79 | |
80 bool streaming_to_file() const; | |
81 bool streaming_to_buffer() const; | |
82 | |
83 private: | |
84 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); | |
85 // This class loads and opens the file in three steps for DOWNLOAD_TO_FILE: | |
86 // 1) Ask the browser to start streaming |url_| as a file. | |
87 // 2) Ask the browser to finish streaming if headers indicate success. | |
88 // 3) Ask the browser to open the file, so we can get the file descriptor. | |
89 // For DOWNLOAD_TO_BUFFER, the process is very similar: | |
90 // 1) Ask the browser to start streaming |url_| to an internal buffer. | |
91 // 2) Ask the browser to finish streaming to |temp_buffer_| on success. | |
92 // 3) Wait for streaming to finish, filling |buffer_| incrementally. | |
93 // Each step is done asynchronously using callbacks. We create callbacks | |
94 // through a factory to take advantage of ref-counting. | |
95 bool InitialResponseIsValid(int32_t pp_error); | |
96 void URLLoadStartNotify(int32_t pp_error); | |
97 void URLLoadFinishNotify(int32_t pp_error); | |
98 void URLBufferStartNotify(int32_t pp_error); | |
99 void URLReadBodyNotify(int32_t pp_error); | |
100 void FileOpenNotify(int32_t pp_error); | |
101 | |
102 Plugin* instance_; | |
103 nacl::string url_to_open_; | |
104 nacl::string url_; | |
105 pp::CompletionCallback file_open_notify_callback_; | |
106 pp::FileIO file_reader_; | |
107 const PPB_FileIOTrusted* file_io_trusted_interface_; | |
108 pp::URLLoader url_loader_; | |
109 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; | |
110 int64_t open_time_; | |
111 DownloadFlags flags_; | |
112 static const uint32_t kTempBufferSize = 1024; | |
113 char temp_buffer_[kTempBufferSize]; | |
114 std::deque<char> buffer_; | |
115 UrlSchemeType url_scheme_; | |
116 }; | |
117 } // namespace plugin; | |
118 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | |
OLD | NEW |