| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "components/nacl/renderer/file_downloader.h" | 5 #include "components/nacl/renderer/file_downloader.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/callback.h" | 9 #include "base/callback.h" |
| 10 #include "components/nacl/renderer/nexe_load_manager.h" | 10 #include "components/nacl/renderer/nexe_load_manager.h" |
| 11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
| 12 #include "third_party/WebKit/public/platform/WebURLError.h" | 12 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 13 #include "third_party/WebKit/public/platform/WebURLLoader.h" | |
| 14 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 13 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 14 #include "third_party/WebKit/public/web/WebAssociatedURLLoader.h" |
| 15 | 15 |
| 16 namespace nacl { | 16 namespace nacl { |
| 17 | 17 |
| 18 FileDownloader::FileDownloader(std::unique_ptr<blink::WebURLLoader> url_loader, | 18 FileDownloader::FileDownloader( |
| 19 base::File file, | 19 std::unique_ptr<blink::WebAssociatedURLLoader> url_loader, |
| 20 StatusCallback status_cb, | 20 base::File file, |
| 21 ProgressCallback progress_cb) | 21 StatusCallback status_cb, |
| 22 ProgressCallback progress_cb) |
| 22 : url_loader_(std::move(url_loader)), | 23 : url_loader_(std::move(url_loader)), |
| 23 file_(std::move(file)), | 24 file_(std::move(file)), |
| 24 status_cb_(status_cb), | 25 status_cb_(status_cb), |
| 25 progress_cb_(progress_cb), | 26 progress_cb_(progress_cb), |
| 26 http_status_code_(-1), | 27 http_status_code_(-1), |
| 27 total_bytes_received_(0), | 28 total_bytes_received_(0), |
| 28 total_bytes_to_be_received_(-1), | 29 total_bytes_to_be_received_(-1), |
| 29 status_(SUCCESS) { | 30 status_(SUCCESS) { |
| 30 CHECK(!status_cb.is_null()); | 31 CHECK(!status_cb.is_null()); |
| 31 } | 32 } |
| 32 | 33 |
| 33 FileDownloader::~FileDownloader() { | 34 FileDownloader::~FileDownloader() { |
| 34 } | 35 } |
| 35 | 36 |
| 36 void FileDownloader::Load(const blink::WebURLRequest& request) { | 37 void FileDownloader::Load(const blink::WebURLRequest& request) { |
| 37 url_loader_->loadAsynchronously(request, this); | 38 url_loader_->loadAsynchronously(request, this); |
| 38 } | 39 } |
| 39 | 40 |
| 40 void FileDownloader::didReceiveResponse( | 41 void FileDownloader::didReceiveResponse( |
| 41 blink::WebURLLoader* loader, | |
| 42 const blink::WebURLResponse& response) { | 42 const blink::WebURLResponse& response) { |
| 43 http_status_code_ = response.httpStatusCode(); | 43 http_status_code_ = response.httpStatusCode(); |
| 44 if (http_status_code_ != 200) | 44 if (http_status_code_ != 200) |
| 45 status_ = FAILED; | 45 status_ = FAILED; |
| 46 | 46 |
| 47 // Set -1 if the content length is unknown. Set before issuing callback. | 47 // Set -1 if the content length is unknown. Set before issuing callback. |
| 48 total_bytes_to_be_received_ = response.expectedContentLength(); | 48 total_bytes_to_be_received_ = response.expectedContentLength(); |
| 49 if (!progress_cb_.is_null()) | 49 if (!progress_cb_.is_null()) |
| 50 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | 50 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
| 51 } | 51 } |
| 52 | 52 |
| 53 void FileDownloader::didReceiveData(blink::WebURLLoader* loader, | 53 void FileDownloader::didReceiveData(const char* data, int data_length) { |
| 54 const char* data, | |
| 55 int data_length, | |
| 56 int encoded_data_length, | |
| 57 int encoded_body_length) { | |
| 58 if (status_ == SUCCESS) { | 54 if (status_ == SUCCESS) { |
| 59 if (file_.Write(total_bytes_received_, data, data_length) == -1) { | 55 if (file_.Write(total_bytes_received_, data, data_length) == -1) { |
| 60 status_ = FAILED; | 56 status_ = FAILED; |
| 61 return; | 57 return; |
| 62 } | 58 } |
| 63 total_bytes_received_ += data_length; | 59 total_bytes_received_ += data_length; |
| 64 if (!progress_cb_.is_null()) | 60 if (!progress_cb_.is_null()) |
| 65 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | 61 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
| 66 } | 62 } |
| 67 } | 63 } |
| 68 | 64 |
| 69 void FileDownloader::didFinishLoading( | 65 void FileDownloader::didFinishLoading(double finish_time) { |
| 70 blink::WebURLLoader* loader, | |
| 71 double finish_time, | |
| 72 int64_t total_encoded_data_length) { | |
| 73 if (status_ == SUCCESS) { | 66 if (status_ == SUCCESS) { |
| 74 // Seek back to the beginning of the file that was just written so it's | 67 // Seek back to the beginning of the file that was just written so it's |
| 75 // easy for consumers to use. | 68 // easy for consumers to use. |
| 76 if (file_.Seek(base::File::FROM_BEGIN, 0) != 0) | 69 if (file_.Seek(base::File::FROM_BEGIN, 0) != 0) |
| 77 status_ = FAILED; | 70 status_ = FAILED; |
| 78 } | 71 } |
| 79 status_cb_.Run(status_, std::move(file_), http_status_code_); | 72 status_cb_.Run(status_, std::move(file_), http_status_code_); |
| 80 delete this; | 73 delete this; |
| 81 } | 74 } |
| 82 | 75 |
| 83 void FileDownloader::didFail( | 76 void FileDownloader::didFail(const blink::WebURLError& error) { |
| 84 blink::WebURLLoader* loader, | |
| 85 const blink::WebURLError& error) { | |
| 86 status_ = FAILED; | 77 status_ = FAILED; |
| 87 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { | 78 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { |
| 88 switch (error.reason) { | 79 switch (error.reason) { |
| 89 case net::ERR_ACCESS_DENIED: | 80 case net::ERR_ACCESS_DENIED: |
| 90 case net::ERR_NETWORK_ACCESS_DENIED: | 81 case net::ERR_NETWORK_ACCESS_DENIED: |
| 91 status_ = ACCESS_DENIED; | 82 status_ = ACCESS_DENIED; |
| 92 break; | 83 break; |
| 93 } | 84 } |
| 94 } else { | 85 } else { |
| 95 // It's a WebKit error. | 86 // It's a WebKit error. |
| 96 status_ = ACCESS_DENIED; | 87 status_ = ACCESS_DENIED; |
| 97 } | 88 } |
| 98 | 89 |
| 99 // Delete url_loader to prevent didFinishLoading from being called, which | 90 // Delete url_loader to prevent didFinishLoading from being called, which |
| 100 // some implementations of blink::WebURLLoader will do after calling didFail. | 91 // some implementations of blink::WebURLLoader will do after calling didFail. |
| 101 url_loader_.reset(); | 92 url_loader_.reset(); |
| 102 | 93 |
| 103 status_cb_.Run(status_, std::move(file_), http_status_code_); | 94 status_cb_.Run(status_, std::move(file_), http_status_code_); |
| 104 delete this; | 95 delete this; |
| 105 } | 96 } |
| 106 | 97 |
| 107 } // namespace nacl | 98 } // namespace nacl |
| OLD | NEW |