Chromium Code Reviews| 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" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 28 } DownloadMode; | 28 } DownloadMode; |
| 29 | 29 |
| 30 typedef std::vector<char>* FileStreamData; | 30 typedef std::vector<char>* FileStreamData; |
| 31 typedef CallbackSource<FileStreamData> StreamCallbackSource; | 31 typedef CallbackSource<FileStreamData> StreamCallbackSource; |
| 32 typedef pp::CompletionCallbackWithOutput<FileStreamData> StreamCallback; | 32 typedef pp::CompletionCallbackWithOutput<FileStreamData> StreamCallback; |
| 33 | 33 |
| 34 // A class that wraps PPAPI URLLoader and FileIO functionality for downloading | 34 // A class that wraps PPAPI URLLoader and FileIO functionality for downloading |
| 35 // the url into a file and providing an open file descriptor. | 35 // the url into a file and providing an open file descriptor. |
| 36 class FileDownloader { | 36 class FileDownloader { |
| 37 public: | 37 public: |
| 38 // Ctor initializes |instance_| to NULL, be sure to call Initialize() before | 38 FileDownloader(Plugin* instance); |
|
dmichael (off chromium)
2014/05/28 20:20:19
nit: explicit
| |
| 39 // calling Open(), or Open() will fail. | |
| 40 FileDownloader() | |
| 41 : instance_(NULL), | |
| 42 file_open_notify_callback_(pp::BlockUntilComplete()), | |
| 43 stream_finish_callback_(pp::BlockUntilComplete()), | |
| 44 mode_(DOWNLOAD_NONE), | |
| 45 data_stream_callback_source_(NULL) {} | |
| 46 ~FileDownloader() {} | 39 ~FileDownloader() {} |
| 47 | 40 |
| 48 // Initialize() can only be called once during the lifetime of this instance. | |
| 49 void Initialize(Plugin* instance); | |
| 50 | |
| 51 // Issues a GET on |url| to start downloading the response into a file, | 41 // Issues a GET on |url| to start downloading the response into a file, |
| 52 // and finish streaming it. |callback| will be run after streaming is | 42 // and finish streaming it. |callback| will be run after streaming is |
| 53 // done or if an error prevents streaming from completing. | 43 // done or if an error prevents streaming from completing. |
| 54 // Returns true when callback is scheduled to be called on success or failure. | 44 // Returns true when callback is scheduled to be called on success or failure. |
| 55 // Returns false if callback is NULL, Initialize() has not been called or if | 45 // Returns false if callback is NULL, or if the PPB_FileIO_Trusted interface |
| 56 // the PPB_FileIO_Trusted interface is not available. | 46 // is not available. |
| 57 // If |record_progress| is true, then download progress will be recorded, | 47 // If |record_progress| is true, then download progress will be recorded, |
| 58 // and can be polled through GetDownloadProgress(). | 48 // and can be polled through GetDownloadProgress(). |
| 59 // If |progress_callback| is not NULL and |record_progress| is true, | 49 // If |progress_callback| is not NULL and |record_progress| is true, |
| 60 // then the callback will be invoked for every progress update received | 50 // then the callback will be invoked for every progress update received |
| 61 // by the loader. | 51 // by the loader. |
| 62 | 52 |
| 63 // Similar to Open(), but used for streaming the |url| data directly to the | 53 // Similar to Open(), but used for streaming the |url| data directly to the |
| 64 // caller without writing to a temporary file. The callbacks provided by | 54 // caller without writing to a temporary file. The callbacks provided by |
| 65 // |stream_callback_source| are expected to copy the data before returning. | 55 // |stream_callback_source| are expected to copy the data before returning. |
| 66 // |callback| is called once the response headers are received, | 56 // |callback| is called once the response headers are received, |
| 67 // and streaming must be completed separately via FinishStreaming(). | 57 // and streaming must be completed separately via BeginStreaming(). |
| 68 bool OpenStream(const nacl::string& url, | 58 bool OpenStream(const nacl::string& url, |
| 69 const pp::CompletionCallback& callback, | 59 const pp::CompletionCallback& callback, |
| 70 StreamCallbackSource* stream_callback_source); | 60 StreamCallbackSource* stream_callback_source); |
| 71 | 61 |
| 72 // Finish streaming the response body for a URL request started by either | 62 // Finish streaming the response body for a URL request started by either |
| 73 // OpenStream(). Runs the given |callback| when streaming is done. | 63 // OpenStream(). Runs the given |callback| when streaming is done. |
| 74 void FinishStreaming(const pp::CompletionCallback& callback); | 64 void BeginStreaming(const pp::CompletionCallback& callback); |
| 75 | |
| 76 // Returns the url passed to Open(). | |
| 77 const nacl::string& url() const { return url_; } | |
| 78 | 65 |
| 79 // Once the GET request has finished, and the contents of the file | 66 // Once the GET request has finished, and the contents of the file |
| 80 // represented by |url_| are available, |full_url_| is the full URL including | 67 // represented by |url_| are available, |full_url_| is the full URL including |
| 81 // the scheme, host and full path. | 68 // the scheme, host and full path. |
| 82 // Returns an empty string before the GET request has finished. | 69 // Returns an empty string before the GET request has finished. |
| 83 const nacl::string& full_url() const { return full_url_; } | 70 const nacl::string& full_url() const { return full_url_; } |
| 84 | 71 |
| 85 // Returns the PP_Resource of the active URL loader, or kInvalidResource. | |
| 86 PP_Resource url_loader() const { return url_loader_.pp_resource(); } | |
| 87 | |
| 88 // GetDownloadProgress() returns the current download progress, which is | 72 // GetDownloadProgress() returns the current download progress, which is |
| 89 // meaningful after Open() has been called. Progress only refers to the | 73 // meaningful after Open() has been called. Progress only refers to the |
| 90 // response body and does not include the headers. | 74 // response body and does not include the headers. |
| 91 // | 75 // |
| 92 // This data is only available if the |record_progress| true in the | 76 // This data is only available if the |record_progress| true in the |
| 93 // Open() call. If progress is being recorded, then |bytes_received| | 77 // Open() call. If progress is being recorded, then |bytes_received| |
| 94 // will be set to the number of bytes received thus far, | 78 // will be set to the number of bytes received thus far, |
| 95 // and |total_bytes_to_be_received| will be set to the total number | 79 // and |total_bytes_to_be_received| will be set to the total number |
| 96 // of bytes to be received. The total bytes to be received may be unknown, | 80 // of bytes to be received. The total bytes to be received may be unknown, |
| 97 // in which case |total_bytes_to_be_received| will be set to -1. | 81 // in which case |total_bytes_to_be_received| will be set to -1. |
| 98 bool GetDownloadProgress(int64_t* bytes_received, | 82 bool GetDownloadProgress(int64_t* bytes_received, |
| 99 int64_t* total_bytes_to_be_received) const; | 83 int64_t* total_bytes_to_be_received) const; |
| 100 | 84 |
| 101 int status_code() const { return status_code_; } | 85 int status_code() const { return status_code_; } |
| 102 nacl::string GetResponseHeaders() const; | 86 nacl::string GetResponseHeaders() const; |
| 103 | 87 |
| 104 void set_request_headers(const nacl::string& extra_request_headers) { | 88 void set_request_headers(const nacl::string& extra_request_headers) { |
| 105 extra_request_headers_ = extra_request_headers; | 89 extra_request_headers_ = extra_request_headers; |
| 106 } | 90 } |
| 107 | 91 |
| 108 | |
| 109 private: | 92 private: |
| 110 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); | 93 NACL_DISALLOW_COPY_AND_ASSIGN(FileDownloader); |
| 94 | |
| 111 // For DOWNLOAD_TO_BUFFER_AND_STREAM, the process is very similar: | 95 // For DOWNLOAD_TO_BUFFER_AND_STREAM, the process is very similar: |
| 112 // 1) Ask the browser to start streaming |url_| to an internal buffer. | 96 // 1) Ask the browser to start streaming |url_| to an internal buffer. |
| 113 // 2) Ask the browser to finish streaming to |temp_buffer_| on success. | 97 // 2) Ask the browser to finish streaming to |temp_buffer_| on success. |
| 114 // 3) Wait for streaming to finish, passing the data directly to the user. | 98 // 3) Wait for streaming to finish, passing the data directly to the user. |
| 115 // Each step is done asynchronously using callbacks. We create callbacks | 99 // Each step is done asynchronously using callbacks. We create callbacks |
| 116 // through a factory to take advantage of ref-counting. | 100 // through a factory to take advantage of ref-counting. |
| 117 // The public Open*() functions start step 1), and the public FinishStreaming | 101 // The public Open*() functions start step 1), and the public BeginStreaming |
| 118 // function proceeds to step 2) and 3). | 102 // function proceeds to step 2) and 3). |
| 119 bool InitialResponseIsValid(); | 103 bool InitialResponseIsValid(); |
| 120 void URLLoadStartNotify(int32_t pp_error); | 104 void URLLoadStartNotify(int32_t pp_error); |
| 121 void URLReadBodyNotify(int32_t pp_error); | 105 void URLReadBodyNotify(int32_t pp_error); |
| 122 | 106 |
| 123 Plugin* instance_; | 107 Plugin* instance_; |
| 124 nacl::string url_; | |
| 125 nacl::string full_url_; | 108 nacl::string full_url_; |
| 126 | 109 |
| 127 nacl::string extra_request_headers_; | 110 nacl::string extra_request_headers_; |
| 128 pp::URLResponseInfo url_response_; | 111 pp::URLResponseInfo url_response_; |
| 129 pp::CompletionCallback file_open_notify_callback_; | 112 pp::CompletionCallback file_open_notify_callback_; |
| 130 pp::CompletionCallback stream_finish_callback_; | 113 pp::CompletionCallback stream_finish_callback_; |
| 131 pp::URLLoader url_loader_; | 114 pp::URLLoader url_loader_; |
| 132 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; | 115 pp::CompletionCallbackFactory<FileDownloader> callback_factory_; |
| 133 int32_t status_code_; | 116 int32_t status_code_; |
| 134 DownloadMode mode_; | 117 DownloadMode mode_; |
| 135 static const uint32_t kTempBufferSize = 16384; | 118 static const uint32_t kTempBufferSize = 16384; |
| 136 std::vector<char> temp_buffer_; | 119 std::vector<char> temp_buffer_; |
| 137 StreamCallbackSource* data_stream_callback_source_; | 120 StreamCallbackSource* data_stream_callback_source_; |
| 138 }; | 121 }; |
| 139 } // namespace plugin; | 122 |
| 123 } // namespace plugin | |
| 124 | |
| 140 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ | 125 #endif // NATIVE_CLIENT_SRC_TRUSTED_PLUGIN_FILE_DOWNLOADER_H_ |
| OLD | NEW |