• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef COMPONENTS_NACL_RENDERER_FILE_DOWNLOADER_H_
6 #define COMPONENTS_NACL_RENDERER_FILE_DOWNLOADER_H_
7 
8 #include <stdint.h>
9 
10 #include "base/files/file.h"
11 #include "base/functional/callback.h"
12 #include "components/nacl/renderer/ppb_nacl_private.h"
13 #include "third_party/blink/public/web/web_associated_url_loader_client.h"
14 
15 namespace blink {
16 class WebAssociatedURLLoader;
17 struct WebURLError;
18 class WebURLRequest;
19 class WebURLResponse;
20 }
21 
22 namespace nacl {
23 
24 // Downloads a file and writes the contents to a specified file open for
25 // writing.
26 class FileDownloader : public blink::WebAssociatedURLLoaderClient {
27  public:
28   enum Status {
29     SUCCESS,
30     ACCESS_DENIED,
31     FAILED
32   };
33 
34   // Provides the FileDownloader status and the HTTP status code.
35   typedef base::OnceCallback<void(Status, base::File, int)> StatusCallback;
36 
37   // Provides the bytes received so far, and the total bytes expected to be
38   // received.
39   typedef base::RepeatingCallback<void(int64_t, int64_t)> ProgressCallback;
40 
41   FileDownloader(std::unique_ptr<blink::WebAssociatedURLLoader> url_loader,
42                  base::File file,
43                  StatusCallback status_cb,
44                  ProgressCallback progress_cb);
45 
46   ~FileDownloader() override;
47 
48   void Load(const blink::WebURLRequest& request);
49 
50  private:
51   // WebAssociatedURLLoaderClient implementation.
52   void DidReceiveResponse(const blink::WebURLResponse& response) override;
53   void DidReceiveData(const char* data, int data_length) override;
54   void DidFinishLoading() override;
55   void DidFail(const blink::WebURLError& error) override;
56 
57   std::unique_ptr<blink::WebAssociatedURLLoader> url_loader_;
58   base::File file_;
59   StatusCallback status_cb_;
60   ProgressCallback progress_cb_;
61   int http_status_code_;
62   int64_t total_bytes_received_;
63   int64_t total_bytes_to_be_received_;
64   Status status_;
65 };
66 
67 }  // namespace nacl
68 
69 #endif  // COMPONENTS_NACL_RENDERER_FILE_DOWNLOADER_H_
70