Chromium Code Reviews| 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(base::PlatformFile file, | |
| 18 FileDownloaderCallback cb) | |
| 19 : file_(file), | |
| 20 cb_(cb), | |
| 21 status_code_(-1), | |
| 22 total_bytes_received_(-1), | |
| 23 total_bytes_to_be_received_(-1), | |
| 24 status_(SUCCESS) { | |
| 25 CHECK(!cb.is_null()); | |
| 26 } | |
| 27 | |
| 28 FileDownloader::FileDownloader(base::PlatformFile file, | |
| 29 FileDownloaderCallback cb, | |
| 30 ProgressCallback progress_cb) | |
| 31 : file_(file), | |
| 32 cb_(cb), | |
| 33 progress_cb_(progress_cb), | |
| 34 status_code_(-1), | |
| 35 total_bytes_received_(-1), | |
| 36 total_bytes_to_be_received_(-1), | |
| 37 status_(SUCCESS) { | |
| 38 CHECK(!cb.is_null()); | |
| 39 } | |
| 40 | |
| 41 FileDownloader::~FileDownloader() { | |
| 42 } | |
| 43 | |
| 44 void FileDownloader::didReceiveResponse( | |
| 45 blink::WebURLLoader* loader, | |
| 46 const blink::WebURLResponse& response) { | |
| 47 if (response.httpStatusCode() != 200) | |
| 48 status_ = FAILED; | |
| 49 status_code_ = response.httpStatusCode(); | |
|
bbudge
2014/05/12 22:32:06
Move this before previous statement, and use it in
teravest
2014/05/13 17:05:32
Done.
| |
| 50 | |
| 51 // Sets -1 if the content length is unknown. Send before issuing callback. | |
| 52 total_bytes_to_be_received_ = response.expectedContentLength(); | |
| 53 if (!progress_cb_.is_null()) | |
| 54 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | |
| 55 } | |
| 56 | |
| 57 void FileDownloader::didReceiveData( | |
| 58 blink::WebURLLoader* loader, | |
| 59 const char* data, | |
| 60 int data_length, | |
| 61 int encoded_data_length) { | |
| 62 if (status_ == SUCCESS) { | |
| 63 base::WritePlatformFileAtCurrentPos(file_, data, data_length); | |
| 64 total_bytes_received_ += data_length; | |
| 65 if (!progress_cb_.is_null()) | |
| 66 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | |
| 67 } | |
| 68 } | |
| 69 | |
| 70 void FileDownloader::didFinishLoading( | |
| 71 blink::WebURLLoader* loader, | |
| 72 double finish_time, | |
| 73 int64_t total_encoded_data_length) { | |
| 74 cb_.Run(status_, status_code_); | |
| 75 delete this; | |
| 76 } | |
| 77 | |
| 78 void FileDownloader::didFail( | |
| 79 blink::WebURLLoader* loader, | |
| 80 const blink::WebURLError& error) { | |
| 81 status_ = FAILED; | |
| 82 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { | |
| 83 switch (error.reason) { | |
| 84 case net::ERR_ACCESS_DENIED: | |
| 85 case net::ERR_NETWORK_ACCESS_DENIED: | |
| 86 status_ = ACCESS_DENIED; | |
| 87 break; | |
| 88 } | |
| 89 } else { | |
| 90 // It's a WebKit error. | |
| 91 status_ = ACCESS_DENIED; | |
| 92 } | |
| 93 } | |
| 94 | |
| 95 } // namespace nacl | |
| OLD | NEW |