File: guest_os_dlc_helper.h

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 3,238 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROME_BROWSER_ASH_GUEST_OS_GUEST_OS_DLC_HELPER_H_
#define CHROME_BROWSER_ASH_GUEST_OS_GUEST_OS_DLC_HELPER_H_

#include <ostream>
#include <string_view>

#include "base/files/file_path.h"
#include "base/functional/callback_forward.h"
#include "base/memory/weak_ptr.h"
#include "base/types/expected.h"
#include "chromeos/ash/components/dbus/dlcservice/dlcservice_client.h"

namespace guest_os {

// This object represents an in-progress DLC installation.
//
// Deleting this object cancels the installation.
//
// Once the installation is completed, deleting the object has no effect.
class GuestOsDlcInstallation {
 public:
  enum class Error {
    Cancelled,
    // Errors actionable by the user
    Offline,
    NeedUpdate,
    NeedReboot,
    DiskFull,
    // Transient errors, can be retried automatically
    Busy,
    Internal,
    // Other errors, if you see this in practice something has gone very wrong.
    Invalid,
    UnknownFailure,
  };

  using Result = base::expected<base::FilePath, Error>;

  // A callback for reporting progress in the range [0,1].
  using ProgressCallback = base::RepeatingCallback<void(double)>;

  // Installs the |dlc_id| DLC with some added conveniences. Deleting this
  // object will cancel the installation if it is in-progress. Invokes
  // |completion_callback| when the installation finishes (or is cancelled)
  // either with the path on-disk to the DLC's root if it succeeds, or with an
  // error if it fails.
  //
  // During installation |progress_callback| will be invoked repeatedly with a
  // value in [0,1] to indicate the installation's progress.
  GuestOsDlcInstallation(std::string dlc_id,
                         base::OnceCallback<void(Result)> completion_callback,
                         ProgressCallback progress_callback);

  // This object can not be moved or copied.
  GuestOsDlcInstallation(const GuestOsDlcInstallation&) = delete;
  GuestOsDlcInstallation& operator=(const GuestOsDlcInstallation&) = delete;

  ~GuestOsDlcInstallation();

  // If you intend to uninstall immediately after canceling, prefer this
  // method. Normally you can just delete the object to cancel the installation,
  // but dlcservice may still try to mount it in the background. Using this
  // cancel ensures dlcservice won't be busy with the current installation.
  void CancelGracefully();

 private:
  void CheckState();

  void OnGetDlcStateCompleted(std::string_view err,
                              const dlcservice::DlcState& dlc_state);

  void StartInstall();

  void OnDlcInstallCompleted(
      const ash::DlcserviceClient::InstallResult& install_result);

  std::string dlc_id_;
  int retries_remaining_;
  base::OnceCallback<void(Result)> completion_callback_;
  ProgressCallback progress_callback_;
  bool gracefully_cancelled_ = false;

  base::WeakPtrFactory<GuestOsDlcInstallation> weak_factory_{this};
};

}  // namespace guest_os

std::ostream& operator<<(std::ostream& stream,
                         guest_os::GuestOsDlcInstallation::Error err);

#endif  // CHROME_BROWSER_ASH_GUEST_OS_GUEST_OS_DLC_HELPER_H_