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 status_cb, | |
20 ProgressCallback progress_cb) | |
21 : url_loader_(url_loader.Pass()), | |
22 file_(file), | |
23 status_cb_(status_cb), | |
24 progress_cb_(progress_cb), | |
25 http_status_code_(-1), | |
26 total_bytes_received_(0), | |
27 total_bytes_to_be_received_(-1), | |
28 status_(SUCCESS) { | |
29 CHECK(!status_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 http_status_code_ = response.httpStatusCode(); | |
43 if (http_status_code_ != 200) | |
44 status_ = FAILED; | |
45 | |
46 // Sets -1 if the content length is unknown. Send before issuing callback. | |
bbudge
2014/05/16 17:20:51
s/Sets -1/Set to -1
s/Send/Set ?
| |
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 if (base::WritePlatformFile(file_, total_bytes_received_, data, | |
59 data_length) == -1) { | |
60 status_ = FAILED; | |
61 return; | |
62 } | |
63 total_bytes_received_ += data_length; | |
64 if (!progress_cb_.is_null()) | |
65 progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); | |
66 } | |
67 } | |
68 | |
69 void FileDownloader::didFinishLoading( | |
70 blink::WebURLLoader* loader, | |
71 double finish_time, | |
72 int64_t total_encoded_data_length) { | |
73 status_cb_.Run(status_, http_status_code_); | |
74 delete this; | |
75 } | |
76 | |
77 void FileDownloader::didFail( | |
78 blink::WebURLLoader* loader, | |
79 const blink::WebURLError& error) { | |
80 status_ = FAILED; | |
81 if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { | |
82 switch (error.reason) { | |
83 case net::ERR_ACCESS_DENIED: | |
84 case net::ERR_NETWORK_ACCESS_DENIED: | |
85 status_ = ACCESS_DENIED; | |
86 break; | |
87 } | |
88 } else { | |
89 // It's a WebKit error. | |
90 status_ = ACCESS_DENIED; | |
91 } | |
92 } | |
93 | |
94 } // namespace nacl | |
OLD | NEW |