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 <string>
6
7#include "base/callback.h"
8#include "base/files/file.h"
9#include "ppapi/c/private/ppb_nacl_private.h"
10#include "third_party/WebKit/public/platform/WebURLLoaderClient.h"
11
12namespace blink {
13struct WebURLError;
14class WebURLLoader;
15class WebURLResponse;
16}
17
18namespace nacl {
19
20// Downloads a file and writes the contents to a specified file open for
21// writing.
22class FileDownloader : public blink::WebURLLoaderClient {
23 public:
24  enum Status {
25    SUCCESS,
26    ACCESS_DENIED,
27    FAILED
28  };
29
30  // Provides the FileDownloader status and the HTTP status code.
31  typedef base::Callback<void(Status, base::File, int)> StatusCallback;
32
33  // Provides the bytes received so far, and the total bytes expected to be
34  // received.
35  typedef base::Callback<void(int64_t, int64_t)> ProgressCallback;
36
37  FileDownloader(scoped_ptr<blink::WebURLLoader> url_loader,
38                 base::File file,
39                 StatusCallback status_cb,
40                 ProgressCallback progress_cb);
41
42  virtual ~FileDownloader();
43
44  void Load(const blink::WebURLRequest& request);
45
46 private:
47  // WebURLLoaderClient implementation.
48  virtual void didReceiveResponse(blink::WebURLLoader* loader,
49                                  const blink::WebURLResponse& response);
50  virtual void didReceiveData(blink::WebURLLoader* loader,
51                              const char* data,
52                              int data_length,
53                              int encoded_data_length);
54  virtual void didFinishLoading(blink::WebURLLoader* loader,
55                                double finish_time,
56                                int64_t total_encoded_data_length);
57  virtual void didFail(blink::WebURLLoader* loader,
58                       const blink::WebURLError& error);
59
60  scoped_ptr<blink::WebURLLoader> url_loader_;
61  base::File file_;
62  StatusCallback status_cb_;
63  ProgressCallback progress_cb_;
64  int http_status_code_;
65  int64_t total_bytes_received_;
66  int64_t total_bytes_to_be_received_;
67  Status status_;
68};
69
70}  // namespace nacl
71