Index: components/nacl/renderer/file_downloader.cc |
diff --git a/components/nacl/renderer/file_downloader.cc b/components/nacl/renderer/file_downloader.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b531c62101c870ddf1f503b1594cd9927772b0c0 |
--- /dev/null |
+++ b/components/nacl/renderer/file_downloader.cc |
@@ -0,0 +1,95 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "components/nacl/renderer/file_downloader.h" |
+ |
+#include "base/callback.h" |
+#include "base/platform_file.h" |
+#include "components/nacl/renderer/nexe_load_manager.h" |
+#include "net/base/net_errors.h" |
+#include "third_party/WebKit/public/platform/WebURLError.h" |
+#include "third_party/WebKit/public/platform/WebURLLoader.h" |
+#include "third_party/WebKit/public/platform/WebURLResponse.h" |
+ |
+namespace nacl { |
+ |
+FileDownloader::FileDownloader(base::PlatformFile file, |
+ FileDownloaderCallback cb) |
+ : file_(file), |
+ cb_(cb), |
+ status_code_(-1), |
+ total_bytes_received_(-1), |
+ total_bytes_to_be_received_(-1), |
+ status_(SUCCESS) { |
+ CHECK(!cb.is_null()); |
+} |
+ |
+FileDownloader::FileDownloader(base::PlatformFile file, |
+ FileDownloaderCallback cb, |
+ ProgressCallback progress_cb) |
+ : file_(file), |
+ cb_(cb), |
+ progress_cb_(progress_cb), |
+ status_code_(-1), |
+ total_bytes_received_(-1), |
+ total_bytes_to_be_received_(-1), |
+ status_(SUCCESS) { |
+ CHECK(!cb.is_null()); |
+} |
+ |
+FileDownloader::~FileDownloader() { |
+} |
+ |
+void FileDownloader::didReceiveResponse( |
+ blink::WebURLLoader* loader, |
+ const blink::WebURLResponse& response) { |
+ if (response.httpStatusCode() != 200) |
+ status_ = FAILED; |
+ 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.
|
+ |
+ // Sets -1 if the content length is unknown. Send before issuing callback. |
+ total_bytes_to_be_received_ = response.expectedContentLength(); |
+ if (!progress_cb_.is_null()) |
+ progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
+} |
+ |
+void FileDownloader::didReceiveData( |
+ blink::WebURLLoader* loader, |
+ const char* data, |
+ int data_length, |
+ int encoded_data_length) { |
+ if (status_ == SUCCESS) { |
+ base::WritePlatformFileAtCurrentPos(file_, data, data_length); |
+ total_bytes_received_ += data_length; |
+ if (!progress_cb_.is_null()) |
+ progress_cb_.Run(total_bytes_received_, total_bytes_to_be_received_); |
+ } |
+} |
+ |
+void FileDownloader::didFinishLoading( |
+ blink::WebURLLoader* loader, |
+ double finish_time, |
+ int64_t total_encoded_data_length) { |
+ cb_.Run(status_, status_code_); |
+ delete this; |
+} |
+ |
+void FileDownloader::didFail( |
+ blink::WebURLLoader* loader, |
+ const blink::WebURLError& error) { |
+ status_ = FAILED; |
+ if (error.domain.equals(blink::WebString::fromUTF8(net::kErrorDomain))) { |
+ switch (error.reason) { |
+ case net::ERR_ACCESS_DENIED: |
+ case net::ERR_NETWORK_ACCESS_DENIED: |
+ status_ = ACCESS_DENIED; |
+ break; |
+ } |
+ } else { |
+ // It's a WebKit error. |
+ status_ = ACCESS_DENIED; |
+ } |
+} |
+ |
+} // namespace nacl |