OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | 5 #ifndef NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ |
6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | 6 #define NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ |
7 | 7 |
8 #include <deque> | 8 #include <deque> |
9 | 9 |
10 #include "native_client/src/include/nacl_macros.h" | 10 #include "native_client/src/include/nacl_macros.h" |
11 #include "native_client/src/include/nacl_string.h" | 11 #include "native_client/src/include/nacl_string.h" |
12 #include "native_client/src/trusted/plugin/callback_source.h" | 12 #include "native_client/src/trusted/plugin/callback_source.h" |
13 #include "native_client/src/trusted/validator/nacl_file_info.h" | 13 #include "native_client/src/trusted/validator/nacl_file_info.h" |
14 #include "ppapi/c/private/pp_file_handle.h" | 14 #include "ppapi/c/private/pp_file_handle.h" |
15 #include "ppapi/c/trusted/ppb_file_io_trusted.h" | 15 #include "ppapi/c/trusted/ppb_file_io_trusted.h" |
16 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" | 16 #include "ppapi/c/trusted/ppb_url_loader_trusted.h" |
17 #include "ppapi/cpp/file_io.h" | 17 #include "ppapi/cpp/file_io.h" |
18 #include "ppapi/cpp/instance.h" | |
18 #include "ppapi/cpp/url_loader.h" | 19 #include "ppapi/cpp/url_loader.h" |
19 #include "ppapi/cpp/instance.h" | 20 #include "ppapi/cpp/url_response_info.h" |
20 #include "ppapi/utility/completion_callback_factory.h" | 21 #include "ppapi/utility/completion_callback_factory.h" |
21 | 22 |
22 namespace plugin { | 23 namespace plugin { |
23 | 24 |
24 class Plugin; | 25 class Plugin; |
25 | 26 |
26 typedef enum { | 27 typedef enum { |
27 DOWNLOAD_TO_FILE = 0, | 28 DOWNLOAD_TO_FILE = 0, |
28 DOWNLOAD_TO_BUFFER, | 29 DOWNLOAD_TO_BUFFER, |
29 DOWNLOAD_STREAM, | 30 DOWNLOAD_STREAM, |
(...skipping 12 matching lines...) Expand all Loading... | |
42 | 43 |
43 // A class that wraps PPAPI URLLoader and FileIO functionality for downloading | 44 // A class that wraps PPAPI URLLoader and FileIO functionality for downloading |
44 // the url into a file and providing an open file descriptor. | 45 // the url into a file and providing an open file descriptor. |
45 class FileDownloader { | 46 class FileDownloader { |
46 public: | 47 public: |
47 // Ctor initializes |instance_| to NULL, be sure to call Initialize() before | 48 // Ctor initializes |instance_| to NULL, be sure to call Initialize() before |
48 // calling Open(), or Open() will fail. | 49 // calling Open(), or Open() will fail. |
49 FileDownloader() | 50 FileDownloader() |
50 : instance_(NULL), | 51 : instance_(NULL), |
51 file_open_notify_callback_(pp::BlockUntilComplete()), | 52 file_open_notify_callback_(pp::BlockUntilComplete()), |
53 stream_finish_callback_(pp::BlockUntilComplete()), | |
52 file_handle_(PP_kInvalidFileHandle), | 54 file_handle_(PP_kInvalidFileHandle), |
53 file_io_trusted_interface_(NULL), | 55 file_io_trusted_interface_(NULL), |
54 url_loader_trusted_interface_(NULL), | 56 url_loader_trusted_interface_(NULL), |
55 open_time_(-1), | 57 open_time_(-1), |
56 mode_(DOWNLOAD_NONE), | 58 mode_(DOWNLOAD_NONE), |
59 open_and_stream_(false), | |
57 url_scheme_(SCHEME_OTHER), | 60 url_scheme_(SCHEME_OTHER), |
58 data_stream_callback_source_(NULL) {} | 61 data_stream_callback_source_(NULL) {} |
59 ~FileDownloader() {} | 62 ~FileDownloader() {} |
60 | 63 |
61 // Initialize() can only be called once during the lifetime of this instance. | 64 // Initialize() can only be called once during the lifetime of this instance. |
62 void Initialize(Plugin* instance); | 65 void Initialize(Plugin* instance); |
63 | 66 |
64 // Issues a GET on |url| downloading the response into a file. The file is | 67 // Issues a GET on |url| to *start* downloading the response into a file. |
65 // then opened and a file descriptor is made available. | |
66 // Returns true when callback is scheduled to be called on success or failure. | 68 // Returns true when callback is scheduled to be called on success or failure. |
67 // Returns false if callback is NULL, Initialize() has not been called or if | 69 // Returns false if callback is NULL, Initialize() has not been called or if |
68 // the PPB_FileIO_Trusted interface is not available. | 70 // the PPB_FileIO_Trusted interface is not available. |
69 // If |record_progress| is true, then download progress will be recorded, | 71 // If |record_progress| is true, then download progress will be recorded, |
70 // and can be polled through GetDownloadProgress(). | 72 // and can be polled through GetDownloadProgress(). |
71 // If |progress_callback| is not NULL and |record_progress| is true, | 73 // If |progress_callback| is not NULL and |record_progress| is true, |
72 // then the callback will be invoked for every progress update received | 74 // then the callback will be invoked for every progress update received |
73 // by the loader. | 75 // by the loader. |
76 // If |finish_streaming| is false, this will only initiate the get request | |
77 // and |callback| will be called if the response headers are okay. | |
Derek Schuff
2013/06/07 21:38:04
don't we call the callback either way?
jvoung (off chromium)
2013/06/07 23:46:25
Done.
| |
78 // To finish downloading, call FinishStreaming() with another callback. | |
79 // If |finish_streaming| is true, this will open and call FinishStreaming(). | |
80 // The |callback| will be run after streaming is done. | |
74 bool Open(const nacl::string& url, | 81 bool Open(const nacl::string& url, |
75 DownloadMode mode, | 82 DownloadMode mode, |
76 const pp::CompletionCallback& callback, | 83 const pp::CompletionCallback& callback, |
77 bool record_progress, | 84 bool record_progress, |
78 PP_URLLoaderTrusted_StatusCallback progress_callback); | 85 PP_URLLoaderTrusted_StatusCallback progress_callback, |
86 bool finish_streaming); | |
Derek Schuff
2013/06/07 21:38:04
since we already have an OpenStream interface espe
jvoung (off chromium)
2013/06/07 23:46:25
OpenStream() calls Open(), which is why I added a
| |
79 | 87 |
80 // Same as Open, but used for streaming the file data directly to the | 88 // Same as Open(), but used for streaming the |url| data directly to the |
81 // caller without buffering it. The callbacks provided by | 89 // caller without writing to a temporary file. The callbacks provided by |
82 // |stream_callback_source| are expected to copy the data before returning. | 90 // |stream_callback_source| are expected to copy the data before returning. |
83 // |callback| will still be called when the stream is finished. | 91 // Like Open(), if |finish_streaming| is true, |callback| will still be |
92 // called when the stream is finished. Otherwise |callback| is called | |
93 // once the response headers are received, and streaming must be | |
94 // completed separately via FinishStreaming. | |
84 bool OpenStream(const nacl::string& url, | 95 bool OpenStream(const nacl::string& url, |
85 const pp::CompletionCallback& callback, | 96 const pp::CompletionCallback& callback, |
86 StreamCallbackSource* stream_callback_source); | 97 StreamCallbackSource* stream_callback_source, |
98 bool finish_streaming); | |
99 | |
100 // Finish streaming the response body for a URL request started by either | |
101 // Open() or OpenStream(). If DownloadMode is DOWNLOAD_TO_FILE, | |
102 // then the response body is streamed to a file, the file is opened and | |
103 // a file descriptor is made available. Runs the given |callback| when | |
104 // streaming is done. | |
105 void FinishStreaming(const pp::CompletionCallback& callback); | |
87 | 106 |
88 // Bypasses downloading and takes a handle to the open file. To get the fd, | 107 // Bypasses downloading and takes a handle to the open file. To get the fd, |
89 // call GetFileInfo(). | 108 // call GetFileInfo(). |
90 void OpenFast(const nacl::string& url, PP_FileHandle file_handle, | 109 void OpenFast(const nacl::string& url, PP_FileHandle file_handle, |
91 uint64_t file_token_lo, uint64_t file_token_hi); | 110 uint64_t file_token_lo, uint64_t file_token_hi); |
92 | 111 |
93 // Return a structure describing the file opened, including a file desc. | 112 // Return a structure describing the file opened, including a file desc. |
94 // If downloading and opening succeeded, this returns a valid read-only | 113 // If downloading and opening succeeded, this returns a valid read-only |
95 // POSIX file descriptor. On failure, the return value is an invalid | 114 // POSIX file descriptor. On failure, the return value is an invalid |
96 // descriptor. The file descriptor is owned by this instance, so the | 115 // descriptor. The file descriptor is owned by this instance, so the |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
128 | 147 |
129 // Returns the buffer used for DOWNLOAD_TO_BUFFER mode. | 148 // Returns the buffer used for DOWNLOAD_TO_BUFFER mode. |
130 const std::deque<char>& buffer() const { return buffer_; } | 149 const std::deque<char>& buffer() const { return buffer_; } |
131 | 150 |
132 bool streaming_to_file() const; | 151 bool streaming_to_file() const; |
133 bool streaming_to_buffer() const; | 152 bool streaming_to_buffer() const; |
134 bool streaming_to_user() const; | 153 bool streaming_to_user() const; |
135 bool not_streaming() const; | 154 bool not_streaming() const; |
136 | 155 |
137 int status_code() const { return status_code_; } | 156 int status_code() const { return status_code_; } |
157 nacl::string GetResponseHeaders() const; | |
138 | 158 |
139 private: | 159 private: |
140 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); | 160 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); |
141 // This class loads and opens the file in three steps for DOWNLOAD_TO_FILE: | 161 // This class loads and opens the file in three steps for DOWNLOAD_TO_FILE: |
142 // 1) Ask the browser to start streaming |url_| as a file. | 162 // 1) Ask the browser to start streaming |url_| as a file. |
143 // 2) Ask the browser to finish streaming if headers indicate success. | 163 // 2) Ask the browser to finish streaming if headers indicate success. |
144 // 3) Ask the browser to open the file, so we can get the file descriptor. | 164 // 3) Ask the browser to open the file, so we can get the file descriptor. |
145 // For DOWNLOAD_TO_BUFFER, the process is very similar: | 165 // For DOWNLOAD_TO_BUFFER, the process is very similar: |
146 // 1) Ask the browser to start streaming |url_| to an internal buffer. | 166 // 1) Ask the browser to start streaming |url_| to an internal buffer. |
147 // 2) Ask the browser to finish streaming to |temp_buffer_| on success. | 167 // 2) Ask the browser to finish streaming to |temp_buffer_| on success. |
148 // 3) Wait for streaming to finish, filling |buffer_| incrementally. | 168 // 3) Wait for streaming to finish, filling |buffer_| incrementally. |
149 // Each step is done asynchronously using callbacks. We create callbacks | 169 // Each step is done asynchronously using callbacks. We create callbacks |
150 // through a factory to take advantage of ref-counting. | 170 // through a factory to take advantage of ref-counting. |
151 // DOWNLOAD_STREAM is similar to DOWNLOAD_TO_BUFFER except the downloaded | 171 // DOWNLOAD_STREAM is similar to DOWNLOAD_TO_BUFFER except the downloaded |
152 // data is passed directly to the user instead of saved in a buffer. | 172 // data is passed directly to the user instead of saved in a buffer. |
173 // The public Open*() functions start step 1), and the public FinishStreaming | |
174 // function proceeds to step 2) and 3). | |
153 bool InitialResponseIsValid(int32_t pp_error); | 175 bool InitialResponseIsValid(int32_t pp_error); |
154 void URLLoadStartNotify(int32_t pp_error); | 176 void URLLoadStartNotify(int32_t pp_error); |
155 void URLLoadFinishNotify(int32_t pp_error); | 177 void URLLoadFinishNotify(int32_t pp_error); |
156 void URLBufferStartNotify(int32_t pp_error); | 178 void URLBufferStartNotify(int32_t pp_error); |
157 void URLReadBodyNotify(int32_t pp_error); | 179 void URLReadBodyNotify(int32_t pp_error); |
158 void FileOpenNotify(int32_t pp_error); | 180 void StreamFinishNotify(int32_t pp_error); |
159 | 181 |
160 Plugin* instance_; | 182 Plugin* instance_; |
161 nacl::string url_to_open_; | 183 nacl::string url_to_open_; |
162 nacl::string url_; | 184 nacl::string url_; |
185 pp::URLResponseInfo url_response_; | |
163 pp::CompletionCallback file_open_notify_callback_; | 186 pp::CompletionCallback file_open_notify_callback_; |
187 pp::CompletionCallback stream_finish_callback_; | |
164 pp::FileIO file_reader_; | 188 pp::FileIO file_reader_; |
165 PP_FileHandle file_handle_; | 189 PP_FileHandle file_handle_; |
166 struct NaClFileToken file_token_; | 190 struct NaClFileToken file_token_; |
167 const PPB_FileIOTrusted* file_io_trusted_interface_; | 191 const PPB_FileIOTrusted* file_io_trusted_interface_; |
168 const PPB_URLLoaderTrusted* url_loader_trusted_interface_; | 192 const PPB_URLLoaderTrusted* url_loader_trusted_interface_; |
169 pp::URLLoader url_loader_; | 193 pp::URLLoader url_loader_; |
170 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; | 194 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; |
171 int64_t open_time_; | 195 int64_t open_time_; |
172 int32_t status_code_; | 196 int32_t status_code_; |
173 DownloadMode mode_; | 197 DownloadMode mode_; |
198 bool open_and_stream_; | |
174 static const uint32_t kTempBufferSize = 2048; | 199 static const uint32_t kTempBufferSize = 2048; |
175 std::vector<char> temp_buffer_; | 200 std::vector<char> temp_buffer_; |
176 std::deque<char> buffer_; | 201 std::deque<char> buffer_; |
177 UrlSchemeType url_scheme_; | 202 UrlSchemeType url_scheme_; |
178 StreamCallbackSource* data_stream_callback_source_; | 203 StreamCallbackSource* data_stream_callback_source_; |
179 }; | 204 }; |
180 } // namespace plugin; | 205 } // namespace plugin; |
181 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | 206 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ |
OLD | NEW |