| 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_(true), |
| 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. | 68 // and finish streaming it. |callback| will be run after streaming is |
| 69 // done or if an error prevents streaming from completing. |
| 66 // Returns true when callback is scheduled to be called on success or failure. | 70 // 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 | 71 // Returns false if callback is NULL, Initialize() has not been called or if |
| 68 // the PPB_FileIO_Trusted interface is not available. | 72 // the PPB_FileIO_Trusted interface is not available. |
| 69 // If |record_progress| is true, then download progress will be recorded, | 73 // If |record_progress| is true, then download progress will be recorded, |
| 70 // and can be polled through GetDownloadProgress(). | 74 // and can be polled through GetDownloadProgress(). |
| 71 // If |progress_callback| is not NULL and |record_progress| is true, | 75 // If |progress_callback| is not NULL and |record_progress| is true, |
| 72 // then the callback will be invoked for every progress update received | 76 // then the callback will be invoked for every progress update received |
| 73 // by the loader. | 77 // by the loader. |
| 74 bool Open(const nacl::string& url, | 78 bool Open(const nacl::string& url, |
| 75 DownloadMode mode, | 79 DownloadMode mode, |
| 76 const pp::CompletionCallback& callback, | 80 const pp::CompletionCallback& callback, |
| 77 bool record_progress, | 81 bool record_progress, |
| 78 PP_URLLoaderTrusted_StatusCallback progress_callback); | 82 PP_URLLoaderTrusted_StatusCallback progress_callback); |
| 79 | 83 |
| 80 // Same as Open, but used for streaming the file data directly to the | 84 // Similar to Open(), but used for streaming the |url| data directly to the |
| 81 // caller without buffering it. The callbacks provided by | 85 // caller without writing to a temporary file. The callbacks provided by |
| 82 // |stream_callback_source| are expected to copy the data before returning. | 86 // |stream_callback_source| are expected to copy the data before returning. |
| 83 // |callback| will still be called when the stream is finished. | 87 // |callback| is called once the response headers are received, |
| 88 // and streaming must be completed separately via FinishStreaming(). |
| 84 bool OpenStream(const nacl::string& url, | 89 bool OpenStream(const nacl::string& url, |
| 85 const pp::CompletionCallback& callback, | 90 const pp::CompletionCallback& callback, |
| 86 StreamCallbackSource* stream_callback_source); | 91 StreamCallbackSource* stream_callback_source); |
| 87 | 92 |
| 93 // Finish streaming the response body for a URL request started by either |
| 94 // Open() or OpenStream(). If DownloadMode is DOWNLOAD_TO_FILE, |
| 95 // then the response body is streamed to a file, the file is opened and |
| 96 // a file descriptor is made available. Runs the given |callback| when |
| 97 // streaming is done. |
| 98 void FinishStreaming(const pp::CompletionCallback& callback); |
| 99 |
| 88 // Bypasses downloading and takes a handle to the open file. To get the fd, | 100 // Bypasses downloading and takes a handle to the open file. To get the fd, |
| 89 // call GetFileInfo(). | 101 // call GetFileInfo(). |
| 90 void OpenFast(const nacl::string& url, PP_FileHandle file_handle, | 102 void OpenFast(const nacl::string& url, PP_FileHandle file_handle, |
| 91 uint64_t file_token_lo, uint64_t file_token_hi); | 103 uint64_t file_token_lo, uint64_t file_token_hi); |
| 92 | 104 |
| 93 // Return a structure describing the file opened, including a file desc. | 105 // Return a structure describing the file opened, including a file desc. |
| 94 // If downloading and opening succeeded, this returns a valid read-only | 106 // If downloading and opening succeeded, this returns a valid read-only |
| 95 // POSIX file descriptor. On failure, the return value is an invalid | 107 // POSIX file descriptor. On failure, the return value is an invalid |
| 96 // descriptor. The file descriptor is owned by this instance, so the | 108 // descriptor. The file descriptor is owned by this instance, so the |
| 97 // delegate does not have to close it. | 109 // delegate does not have to close it. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 128 | 140 |
| 129 // Returns the buffer used for DOWNLOAD_TO_BUFFER mode. | 141 // Returns the buffer used for DOWNLOAD_TO_BUFFER mode. |
| 130 const std::deque<char>& buffer() const { return buffer_; } | 142 const std::deque<char>& buffer() const { return buffer_; } |
| 131 | 143 |
| 132 bool streaming_to_file() const; | 144 bool streaming_to_file() const; |
| 133 bool streaming_to_buffer() const; | 145 bool streaming_to_buffer() const; |
| 134 bool streaming_to_user() const; | 146 bool streaming_to_user() const; |
| 135 bool not_streaming() const; | 147 bool not_streaming() const; |
| 136 | 148 |
| 137 int status_code() const { return status_code_; } | 149 int status_code() const { return status_code_; } |
| 150 nacl::string GetResponseHeaders() const; |
| 138 | 151 |
| 139 private: | 152 private: |
| 140 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); | 153 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); |
| 141 // This class loads and opens the file in three steps for DOWNLOAD_TO_FILE: | 154 // 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. | 155 // 1) Ask the browser to start streaming |url_| as a file. |
| 143 // 2) Ask the browser to finish streaming if headers indicate success. | 156 // 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. | 157 // 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: | 158 // For DOWNLOAD_TO_BUFFER, the process is very similar: |
| 146 // 1) Ask the browser to start streaming |url_| to an internal buffer. | 159 // 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. | 160 // 2) Ask the browser to finish streaming to |temp_buffer_| on success. |
| 148 // 3) Wait for streaming to finish, filling |buffer_| incrementally. | 161 // 3) Wait for streaming to finish, filling |buffer_| incrementally. |
| 149 // Each step is done asynchronously using callbacks. We create callbacks | 162 // Each step is done asynchronously using callbacks. We create callbacks |
| 150 // through a factory to take advantage of ref-counting. | 163 // through a factory to take advantage of ref-counting. |
| 151 // DOWNLOAD_STREAM is similar to DOWNLOAD_TO_BUFFER except the downloaded | 164 // 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. | 165 // data is passed directly to the user instead of saved in a buffer. |
| 166 // The public Open*() functions start step 1), and the public FinishStreaming |
| 167 // function proceeds to step 2) and 3). |
| 153 bool InitialResponseIsValid(int32_t pp_error); | 168 bool InitialResponseIsValid(int32_t pp_error); |
| 154 void URLLoadStartNotify(int32_t pp_error); | 169 void URLLoadStartNotify(int32_t pp_error); |
| 155 void URLLoadFinishNotify(int32_t pp_error); | 170 void URLLoadFinishNotify(int32_t pp_error); |
| 156 void URLBufferStartNotify(int32_t pp_error); | 171 void URLBufferStartNotify(int32_t pp_error); |
| 157 void URLReadBodyNotify(int32_t pp_error); | 172 void URLReadBodyNotify(int32_t pp_error); |
| 158 void FileOpenNotify(int32_t pp_error); | 173 void StreamFinishNotify(int32_t pp_error); |
| 159 | 174 |
| 160 Plugin* instance_; | 175 Plugin* instance_; |
| 161 nacl::string url_to_open_; | 176 nacl::string url_to_open_; |
| 162 nacl::string url_; | 177 nacl::string url_; |
| 178 pp::URLResponseInfo url_response_; |
| 163 pp::CompletionCallback file_open_notify_callback_; | 179 pp::CompletionCallback file_open_notify_callback_; |
| 180 pp::CompletionCallback stream_finish_callback_; |
| 164 pp::FileIO file_reader_; | 181 pp::FileIO file_reader_; |
| 165 PP_FileHandle file_handle_; | 182 PP_FileHandle file_handle_; |
| 166 struct NaClFileToken file_token_; | 183 struct NaClFileToken file_token_; |
| 167 const PPB_FileIOTrusted* file_io_trusted_interface_; | 184 const PPB_FileIOTrusted* file_io_trusted_interface_; |
| 168 const PPB_URLLoaderTrusted* url_loader_trusted_interface_; | 185 const PPB_URLLoaderTrusted* url_loader_trusted_interface_; |
| 169 pp::URLLoader url_loader_; | 186 pp::URLLoader url_loader_; |
| 170 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; | 187 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; |
| 171 int64_t open_time_; | 188 int64_t open_time_; |
| 172 int32_t status_code_; | 189 int32_t status_code_; |
| 173 DownloadMode mode_; | 190 DownloadMode mode_; |
| 191 bool open_and_stream_; |
| 174 static const uint32_t kTempBufferSize = 2048; | 192 static const uint32_t kTempBufferSize = 2048; |
| 175 std::vector<char> temp_buffer_; | 193 std::vector<char> temp_buffer_; |
| 176 std::deque<char> buffer_; | 194 std::deque<char> buffer_; |
| 177 UrlSchemeType url_scheme_; | 195 UrlSchemeType url_scheme_; |
| 178 StreamCallbackSource* data_stream_callback_source_; | 196 StreamCallbackSource* data_stream_callback_source_; |
| 179 }; | 197 }; |
| 180 } // namespace plugin; | 198 } // namespace plugin; |
| 181 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | 199 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ |
| OLD | NEW |