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 #include "ppapi/native_client/src/trusted/plugin/file_downloader.h" | 5 #include "ppapi/native_client/src/trusted/plugin/file_downloader.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <string> | 9 #include <string> |
10 | 10 |
11 #include "native_client/src/include/portability_io.h" | 11 #include "native_client/src/include/portability_io.h" |
12 #include "native_client/src/shared/platform/nacl_check.h" | 12 #include "native_client/src/shared/platform/nacl_check.h" |
13 #include "native_client/src/shared/platform/nacl_time.h" | 13 #include "native_client/src/shared/platform/nacl_time.h" |
14 #include "ppapi/c/pp_errors.h" | 14 #include "ppapi/c/pp_errors.h" |
15 #include "ppapi/cpp/url_request_info.h" | 15 #include "ppapi/cpp/url_request_info.h" |
16 #include "ppapi/cpp/url_response_info.h" | 16 #include "ppapi/cpp/url_response_info.h" |
17 #include "ppapi/native_client/src/trusted/plugin/callback_source.h" | 17 #include "ppapi/native_client/src/trusted/plugin/callback_source.h" |
18 #include "ppapi/native_client/src/trusted/plugin/plugin.h" | 18 #include "ppapi/native_client/src/trusted/plugin/plugin.h" |
19 #include "ppapi/native_client/src/trusted/plugin/utility.h" | 19 #include "ppapi/native_client/src/trusted/plugin/utility.h" |
20 | 20 |
21 namespace plugin { | 21 namespace plugin { |
22 | 22 |
23 void FileDownloader::Initialize(Plugin* instance) { | 23 FileDownloader::FileDownloader(Plugin* instance) |
24 PLUGIN_PRINTF(("FileDownloader::FileDownloader (this=%p)\n", | 24 : instance_(instance), |
dmichael (off chromium)
2014/05/28 20:20:19
nit: indentation looks off
| |
25 static_cast<void*>(this))); | 25 file_open_notify_callback_(pp::BlockUntilComplete()), |
26 CHECK(instance != NULL); | 26 stream_finish_callback_(pp::BlockUntilComplete()), |
27 CHECK(instance_ == NULL); // Can only initialize once. | 27 mode_(DOWNLOAD_NONE), |
28 instance_ = instance; | 28 data_stream_callback_source_(NULL) { |
29 callback_factory_.Initialize(this); | 29 callback_factory_.Initialize(this); |
30 temp_buffer_.resize(kTempBufferSize); | 30 temp_buffer_.resize(kTempBufferSize); |
31 } | 31 } |
32 | 32 |
33 bool FileDownloader::OpenStream( | 33 bool FileDownloader::OpenStream( |
34 const nacl::string& url, | 34 const nacl::string& url, |
35 const pp::CompletionCallback& callback, | 35 const pp::CompletionCallback& callback, |
36 StreamCallbackSource* stream_callback_source) { | 36 StreamCallbackSource* stream_callback_source) { |
37 data_stream_callback_source_ = stream_callback_source; | 37 data_stream_callback_source_ = stream_callback_source; |
38 PLUGIN_PRINTF(("FileDownloader::Open (url=%s)\n", url.c_str())); | 38 PLUGIN_PRINTF(("FileDownloader::Open (url=%s)\n", url.c_str())); |
39 if (callback.pp_completion_callback().func == NULL || instance_ == NULL) | 39 if (callback.pp_completion_callback().func == NULL || instance_ == NULL) |
40 return false; | 40 return false; |
41 | 41 |
42 status_code_ = -1; | 42 status_code_ = -1; |
43 url_ = url; | |
44 file_open_notify_callback_ = callback; | 43 file_open_notify_callback_ = callback; |
45 mode_ = DOWNLOAD_TO_BUFFER_AND_STREAM; | 44 mode_ = DOWNLOAD_TO_BUFFER_AND_STREAM; |
46 pp::URLRequestInfo url_request(instance_); | 45 pp::URLRequestInfo url_request(instance_); |
47 | 46 |
48 // Allow CORS. | 47 // Allow CORS. |
49 // Note that "SetAllowCrossOriginRequests" (currently) has the side effect of | 48 // Note that "SetAllowCrossOriginRequests" (currently) has the side effect of |
50 // preventing credentials from being sent on same-origin requests. We | 49 // preventing credentials from being sent on same-origin requests. We |
51 // therefore avoid setting this flag unless we know for sure it is a | 50 // therefore avoid setting this flag unless we know for sure it is a |
52 // cross-origin request, resulting in behavior similar to XMLHttpRequest. | 51 // cross-origin request, resulting in behavior similar to XMLHttpRequest. |
53 if (!instance_->DocumentCanRequest(url)) | 52 if (!instance_->DocumentCanRequest(url)) |
54 url_request.SetAllowCrossOriginRequests(true); | 53 url_request.SetAllowCrossOriginRequests(true); |
55 | 54 |
56 if (!extra_request_headers_.empty()) | 55 if (!extra_request_headers_.empty()) |
57 url_request.SetHeaders(extra_request_headers_); | 56 url_request.SetHeaders(extra_request_headers_); |
58 | 57 |
59 // Reset the url loader and file reader. | 58 // Reset the url loader and file reader. |
60 // Note that we have the only reference to the underlying objects, so | 59 // Note that we have the only reference to the underlying objects, so |
61 // this will implicitly close any pending IO and destroy them. | 60 // this will implicitly close any pending IO and destroy them. |
62 url_loader_ = pp::URLLoader(instance_); | 61 url_loader_ = pp::URLLoader(instance_); |
63 url_request.SetRecordDownloadProgress(true); | 62 url_request.SetRecordDownloadProgress(true); |
64 | 63 |
65 // Prepare the url request. | 64 // Prepare the url request. |
66 url_request.SetURL(url_); | 65 url_request.SetURL(url); |
67 | 66 |
68 // Request asynchronous download of the url providing an on-load callback. | 67 // Request asynchronous download of the url providing an on-load callback. |
69 // As long as this step is guaranteed to be asynchronous, we can call | 68 // As long as this step is guaranteed to be asynchronous, we can call |
70 // synchronously all other internal callbacks that eventually result in the | 69 // synchronously all other internal callbacks that eventually result in the |
71 // invocation of the user callback. The user code will not be reentered. | 70 // invocation of the user callback. The user code will not be reentered. |
72 pp::CompletionCallback onload_callback = | 71 pp::CompletionCallback onload_callback = |
73 callback_factory_.NewCallback(&FileDownloader::URLLoadStartNotify); | 72 callback_factory_.NewCallback(&FileDownloader::URLLoadStartNotify); |
74 int32_t pp_error = url_loader_.Open(url_request, onload_callback); | 73 int32_t pp_error = url_loader_.Open(url_request, onload_callback); |
75 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%" NACL_PRId32 ")\n", | 74 PLUGIN_PRINTF(("FileDownloader::Open (pp_error=%" NACL_PRId32 ")\n", |
76 pp_error)); | 75 pp_error)); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
110 } | 109 } |
111 | 110 |
112 if (!InitialResponseIsValid()) { | 111 if (!InitialResponseIsValid()) { |
113 file_open_notify_callback_.RunAndClear(PP_ERROR_FAILED); | 112 file_open_notify_callback_.RunAndClear(PP_ERROR_FAILED); |
114 return; | 113 return; |
115 } | 114 } |
116 | 115 |
117 file_open_notify_callback_.RunAndClear(PP_OK); | 116 file_open_notify_callback_.RunAndClear(PP_OK); |
118 } | 117 } |
119 | 118 |
120 void FileDownloader::FinishStreaming( | 119 void FileDownloader::BeginStreaming( |
121 const pp::CompletionCallback& callback) { | 120 const pp::CompletionCallback& callback) { |
122 stream_finish_callback_ = callback; | 121 stream_finish_callback_ = callback; |
123 | 122 |
124 // Finish streaming the body providing an optional callback. | 123 // Finish streaming the body providing an optional callback. |
125 pp::CompletionCallback onread_callback = | 124 pp::CompletionCallback onread_callback = |
126 callback_factory_.NewOptionalCallback( | 125 callback_factory_.NewOptionalCallback( |
127 &FileDownloader::URLReadBodyNotify); | 126 &FileDownloader::URLReadBodyNotify); |
128 int32_t temp_size = static_cast<int32_t>(temp_buffer_.size()); | 127 int32_t temp_size = static_cast<int32_t>(temp_buffer_.size()); |
129 int32_t pp_error = url_loader_.ReadResponseBody(&temp_buffer_[0], | 128 int32_t pp_error = url_loader_.ReadResponseBody(&temp_buffer_[0], |
130 temp_size, | 129 temp_size, |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
171 pp::Var headers = url_response_.GetHeaders(); | 170 pp::Var headers = url_response_.GetHeaders(); |
172 if (!headers.is_string()) { | 171 if (!headers.is_string()) { |
173 PLUGIN_PRINTF(( | 172 PLUGIN_PRINTF(( |
174 "FileDownloader::GetResponseHeaders (headers are not a string)\n")); | 173 "FileDownloader::GetResponseHeaders (headers are not a string)\n")); |
175 return nacl::string(); | 174 return nacl::string(); |
176 } | 175 } |
177 return headers.AsString(); | 176 return headers.AsString(); |
178 } | 177 } |
179 | 178 |
180 } // namespace plugin | 179 } // namespace plugin |
OLD | NEW |