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 "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/platform_file.h" | |
9 #include "components/nacl/renderer/nexe_load_manager.h" | 8 #include "components/nacl/renderer/nexe_load_manager.h" |
10 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
11 #include "third_party/WebKit/public/platform/WebURLError.h" | 10 #include "third_party/WebKit/public/platform/WebURLError.h" |
12 #include "third_party/WebKit/public/platform/WebURLLoader.h" | 11 #include "third_party/WebKit/public/platform/WebURLLoader.h" |
13 #include "third_party/WebKit/public/platform/WebURLResponse.h" | 12 #include "third_party/WebKit/public/platform/WebURLResponse.h" |
14 | 13 |
15 namespace nacl { | 14 namespace nacl { |
16 | 15 |
17 FileDownloader::FileDownloader(scoped_ptr<blink::WebURLLoader> url_loader, | 16 FileDownloader::FileDownloader(scoped_ptr<blink::WebURLLoader> url_loader, |
18 base::PlatformFile file, | 17 base::File file, |
19 StatusCallback status_cb, | 18 StatusCallback status_cb, |
20 ProgressCallback progress_cb) | 19 ProgressCallback progress_cb) |
21 : url_loader_(url_loader.Pass()), | 20 : url_loader_(url_loader.Pass()), |
22 file_(file), | 21 file_(file.Pass()), |
23 status_cb_(status_cb), | 22 status_cb_(status_cb), |
24 progress_cb_(progress_cb), | 23 progress_cb_(progress_cb), |
25 http_status_code_(-1), | 24 http_status_code_(-1), |
26 total_bytes_received_(0), | 25 total_bytes_received_(0), |
27 total_bytes_to_be_received_(-1), | 26 total_bytes_to_be_received_(-1), |
28 status_(SUCCESS) { | 27 status_(SUCCESS) { |
29 CHECK(!status_cb.is_null()); | 28 CHECK(!status_cb.is_null()); |
30 } | 29 } |
31 | 30 |
32 FileDownloader::~FileDownloader() { | 31 FileDownloader::~FileDownloader() { |
(...skipping 15 matching lines...) Expand all Loading... |
48 if (!progress_cb_.is_null()) | 47 if (!progress_cb_.is_null()) |
49 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | 48 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
50 } | 49 } |
51 | 50 |
52 void FileDownloader::didReceiveData( | 51 void FileDownloader::didReceiveData( |
53 blink::WebURLLoader* loader, | 52 blink::WebURLLoader* loader, |
54 const char* data, | 53 const char* data, |
55 int data_length, | 54 int data_length, |
56 int encoded_data_length) { | 55 int encoded_data_length) { |
57 if (status_ == SUCCESS) { | 56 if (status_ == SUCCESS) { |
58 if (base::WritePlatformFile(file_, total_bytes_received_, data, | 57 if (file_.Write(total_bytes_received_, data, data_length) == -1) { |
59 data_length) == -1) { | |
60 status_ = FAILED; | 58 status_ = FAILED; |
61 return; | 59 return; |
62 } | 60 } |
63 total_bytes_received_ += data_length; | 61 total_bytes_received_ += data_length; |
64 if (!progress_cb_.is_null()) | 62 if (!progress_cb_.is_null()) |
65 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | 63 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
66 } | 64 } |
67 } | 65 } |
68 | 66 |
69 void FileDownloader::didFinishLoading( | 67 void FileDownloader::didFinishLoading( |
70 blink::WebURLLoader* loader, | 68 blink::WebURLLoader* loader, |
71 double finish_time, | 69 double finish_time, |
72 int64_t total_encoded_data_length) { | 70 int64_t total_encoded_data_length) { |
73 if (status_ == SUCCESS) { | 71 if (status_ == SUCCESS) { |
74 // Seek back to the beginning of the file that was just written so it's | 72 // Seek back to the beginning of the file that was just written so it's |
75 // easy for consumers to use. | 73 // easy for consumers to use. |
76 if (base::SeekPlatformFile(file_, base::PLATFORM_FILE_FROM_BEGIN, 0) != 0) | 74 if (file_.Seek(base::File::FROM_BEGIN, 0) != 0) |
77 status_ = FAILED; | 75 status_ = FAILED; |
78 } | 76 } |
79 status_cb_.Run(status_, http_status_code_); | 77 status_cb_.Run(status_, file_.Pass(), http_status_code_); |
80 delete this; | 78 delete this; |
81 } | 79 } |
82 | 80 |
83 void FileDownloader::didFail( | 81 void FileDownloader::didFail( |
84 blink::WebURLLoader* loader, | 82 blink::WebURLLoader* loader, |
85 const blink::WebURLError& error) { | 83 const blink::WebURLError& error) { |
86 status_ = FAILED; | 84 status_ = FAILED; |
87 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { | 85 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { |
88 switch (error.reason) { | 86 switch (error.reason) { |
89 case net::ERR_ACCESS_DENIED: | 87 case net::ERR_ACCESS_DENIED: |
90 case net::ERR_NETWORK_ACCESS_DENIED: | 88 case net::ERR_NETWORK_ACCESS_DENIED: |
91 status_ = ACCESS_DENIED; | 89 status_ = ACCESS_DENIED; |
92 break; | 90 break; |
93 } | 91 } |
94 } else { | 92 } else { |
95 // It's a WebKit error. | 93 // It's a WebKit error. |
96 status_ = ACCESS_DENIED; | 94 status_ = ACCESS_DENIED; |
97 } | 95 } |
98 } | 96 } |
99 | 97 |
100 } // namespace nacl | 98 } // namespace nacl |
OLD | NEW |