File: accessibility_dlc_installer.h

package info (click to toggle)
chromium 139.0.7258.127-1~deb13u1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,096 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 (102 lines) | stat: -rw-r--r-- 3,611 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
96
97
98
99
100
101
102
// Copyright 2022 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_ACCESSIBILITY_ACCESSIBILITY_DLC_INSTALLER_H_
#define CHROME_BROWSER_ASH_ACCESSIBILITY_ACCESSIBILITY_DLC_INSTALLER_H_

#include <memory>
#include <set>
#include <string_view>

#include "base/containers/flat_map.h"
#include "base/functional/callback.h"
#include "base/memory/weak_ptr.h"
#include "chromeos/ash/components/dbus/dlcservice/dlcservice_client.h"

namespace base {
class Time;
}  // namespace base

namespace ash {

// This class encapsulates all logic involving the installation of accessibility
// DLCs. It communicates install success, progress, and error using callbacks.
class AccessibilityDlcInstaller {
  using InstalledCallback =
      base::OnceCallback<void(const bool success,
                              const std::string& root_path)>;
  using ProgressCallback = base::RepeatingCallback<void(double progress)>;
  using ErrorCallback = base::OnceCallback<void(std::string_view error)>;

 public:
  enum class DlcType { kFaceGazeAssets, kPumpkin };

  class Callbacks {
   public:
    Callbacks(InstalledCallback on_installed,
              ProgressCallback on_progress,
              ErrorCallback on_error);
    ~Callbacks();
    Callbacks(const Callbacks&) = delete;
    Callbacks& operator=(const Callbacks&) = delete;

    void RunOnInstalled(const bool success, std::string root_path);
    void RunOnProgress(double progress);
    void RunOnError(std::string_view error);

   private:
    // A callback that is run when a DLC is installed.
    InstalledCallback on_installed_;
    // A callback that is run when DLC download progress is updated.
    ProgressCallback on_progress_;
    // A callback that is run when a DLC download encounters an error.
    ErrorCallback on_error_;
  };

  AccessibilityDlcInstaller();
  ~AccessibilityDlcInstaller();
  AccessibilityDlcInstaller(const AccessibilityDlcInstaller&) = delete;
  AccessibilityDlcInstaller& operator=(const AccessibilityDlcInstaller&) =
      delete;

  // Installs a DLC if it isn't already downloaded.
  void MaybeInstall(DlcType type,
                    InstalledCallback on_installed,
                    ProgressCallback on_progress,
                    ErrorCallback on_error);

  bool IsFaceGazeAssetsInstalled() const;
  bool IsPumpkinInstalled() const;

 private:
  // A helper function that is run once we've grabbed the state of a DLC from
  // the DLC service.
  void MaybeInstallHelper(DlcType type,
                          std::string_view error,
                          const dlcservice::DlcState& dlc_state);
  void OnInstalled(DlcType type,
                   const base::Time start_time,
                   const DlcserviceClient::InstallResult& install_result);
  void OnProgress(DlcType type, double progress);

  Callbacks* GetCallbacks(DlcType type);

  std::string GetDlcName(DlcType type);
  std::string GetDlcInstallingErrorMessage(DlcType type);
  std::string GetPendingDlcRequestErrorMessage(DlcType type);

  base::WeakPtr<AccessibilityDlcInstaller> GetWeakPtr();

  std::set<DlcType> installed_dlcs_;
  base::flat_map<DlcType, std::unique_ptr<Callbacks>> callbacks_;
  // Requests to DlcserviceClient are async. This is true if we've made a
  // request and are still waiting for a response.
  base::flat_map<DlcType, bool> pending_requests_;

  base::WeakPtrFactory<AccessibilityDlcInstaller> weak_ptr_factory_{this};
};

}  // namespace ash

#endif  // CHROME_BROWSER_ASH_ACCESSIBILITY_ACCESSIBILITY_DLC_INSTALLER_H_