1 // Copyright 2019 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 #ifndef CHROME_BROWSER_CHROMEOS_PLUGIN_VM_PLUGIN_VM_IMAGE_DOWNLOAD_CLIENT_H_
6 #define CHROME_BROWSER_CHROMEOS_PLUGIN_VM_PLUGIN_VM_IMAGE_DOWNLOAD_CLIENT_H_
7 
8 #include <set>
9 #include <string>
10 
11 #include "base/macros.h"
12 #include "components/download/public/background_service/client.h"
13 
14 namespace download {
15 struct CompletionInfo;
16 struct DownloadMetaData;
17 }  // namespace download
18 
19 class Profile;
20 
21 namespace plugin_vm {
22 
23 class PluginVmInstaller;
24 
25 class PluginVmImageDownloadClient : public download::Client {
26  public:
27   explicit PluginVmImageDownloadClient(Profile* profile);
28   ~PluginVmImageDownloadClient() override;
29 
30  private:
31   Profile* profile_ = nullptr;
32   int64_t content_length_ = -1;
33 
34   PluginVmInstaller* GetInstaller();
35   // Returns false for cancelled downloads.
36   bool IsCurrentDownload(const std::string& guid);
37 
38   // download::Client implementation.
39   void OnServiceInitialized(
40       bool state_lost,
41       const std::vector<download::DownloadMetaData>& downloads) override;
42   void OnServiceUnavailable() override;
43   void OnDownloadStarted(
44       const std::string& guid,
45       const std::vector<GURL>& url_chain,
46       const scoped_refptr<const net::HttpResponseHeaders>& headers) override;
47   void OnDownloadUpdated(const std::string& guid,
48                          uint64_t bytes_uploaded,
49                          uint64_t bytes_downloaded) override;
50   void OnDownloadFailed(const std::string& guid,
51                         const download::CompletionInfo& completion_info,
52                         download::Client::FailureReason reason) override;
53   void OnDownloadSucceeded(
54       const std::string& guid,
55       const download::CompletionInfo& completion_info) override;
56   bool CanServiceRemoveDownloadedFile(const std::string& guid,
57                                       bool force_delete) override;
58   void GetUploadData(const std::string& guid,
59                      download::GetUploadDataCallback callback) override;
60 
61   base::Optional<double> GetFractionComplete(int64_t bytes_downloaded);
62 
63   DISALLOW_COPY_AND_ASSIGN(PluginVmImageDownloadClient);
64 };
65 
66 }  // namespace plugin_vm
67 
68 #endif  // CHROME_BROWSER_CHROMEOS_PLUGIN_VM_PLUGIN_VM_IMAGE_DOWNLOAD_CLIENT_H_
69