OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/nacl/renderer/file_downloader.h" |
| 6 |
| 7 #include "base/callback.h" |
| 8 #include "base/platform_file.h" |
| 9 #include "components/nacl/renderer/nexe_load_manager.h" |
| 10 #include "net/base/net_errors.h" |
| 11 #include "third_party/WebKit/public/platform/WebURLError.h" |
| 12 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
| 13 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
| 14 |
| 15 namespace nacl { |
| 16 |
| 17 FileDownloader::FileDownloader(scoped_ptr<blink::WebURLLoader> url_loader, |
| 18 base::PlatformFile file, |
| 19 StatusCallback cb, |
| 20 ProgressCallback progress_cb) |
| 21 : url_loader_(url_loader.Pass()), |
| 22 file_(file), |
| 23 cb_(cb), |
| 24 progress_cb_(progress_cb), |
| 25 status_code_(-1), |
| 26 total_bytes_received_(-1), |
| 27 total_bytes_to_be_received_(-1), |
| 28 status_(SUCCESS) { |
| 29 CHECK(!cb.is_null()); |
| 30 } |
| 31 |
| 32 FileDownloader::~FileDownloader() { |
| 33 } |
| 34 |
| 35 void FileDownloader::Load(const blink::WebURLRequest& request) { |
| 36 url_loader_->loadAsynchronously(request, this); |
| 37 } |
| 38 |
| 39 void FileDownloader::didReceiveResponse( |
| 40 blink::WebURLLoader* loader, |
| 41 const blink::WebURLResponse& response) { |
| 42 status_code_ = response.httpStatusCode(); |
| 43 if (status_code_ != 200) |
| 44 status_ = FAILED; |
| 45 |
| 46 // Sets -1 if the content length is unknown. Send before issuing callback. |
| 47 total_bytes_to_be_received_ = response.expectedContentLength(); |
| 48 if (!progress_cb_.is_null()) |
| 49 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
| 50 } |
| 51 |
| 52 void FileDownloader::didReceiveData( |
| 53 blink::WebURLLoader* loader, |
| 54 const char* data, |
| 55 int data_length, |
| 56 int encoded_data_length) { |
| 57 if (status_ == SUCCESS) { |
| 58 base::WritePlatformFileAtCurrentPos(file_, data, data_length); |
| 59 total_bytes_received_ += data_length; |
| 60 if (!progress_cb_.is_null()) |
| 61 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
| 62 } |
| 63 } |
| 64 |
| 65 void FileDownloader::didFinishLoading( |
| 66 blink::WebURLLoader* loader, |
| 67 double finish_time, |
| 68 int64_t total_encoded_data_length) { |
| 69 cb_.Run(status_, status_code_); |
| 70 delete this; |
| 71 } |
| 72 |
| 73 void FileDownloader::didFail( |
| 74 blink::WebURLLoader* loader, |
| 75 const blink::WebURLError& error) { |
| 76 status_ = FAILED; |
| 77 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { |
| 78 switch (error.reason) { |
| 79 case net::ERR_ACCESS_DENIED: |
| 80 case net::ERR_NETWORK_ACCESS_DENIED: |
| 81 status_ = ACCESS_DENIED; |
| 82 break; |
| 83 } |
| 84 } else { |
| 85 // It's a WebKit error. |
| 86 status_ = ACCESS_DENIED; |
| 87 } |
| 88 } |
| 89 |
| 90 } // namespace nacl |
OLD | NEW |