File: fwupd_client.h

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (167 lines) | stat: -rw-r--r-- 5,305 bytes parent folder | download | duplicates (7)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef CHROMEOS_ASH_COMPONENTS_DBUS_FWUPD_FWUPD_CLIENT_H_
#define CHROMEOS_ASH_COMPONENTS_DBUS_FWUPD_FWUPD_CLIENT_H_

#include <map>
#include <memory>
#include <string>

#include "base/component_export.h"
#include "base/files/scoped_file.h"
#include "base/observer_list.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_device.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_properties.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_properties_dbus.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_request.h"
#include "chromeos/ash/components/dbus/fwupd/fwupd_update.h"
#include "chromeos/dbus/common/dbus_client.h"

namespace base {
class FilePath;
}

// Enum from ash/webui/firmware_update_ui/firmware_update.mojom mirrored here
// to avoid an illegal include from ash/webui.
enum DeviceRequestId {
  kDoNotPowerOff,
  kReplugInstall,
  kInsertUSBCable,
  kRemoveUSBCable,
  kPressUnlock,
  kRemoveReplug,
  kReplugPower,
};

namespace ash {

// All values returnable by fwupd dbus signal
// The errors are consistent with
// https://fwupd.github.io/libfwupd/error.Error.html
enum class FwupdDbusResult {
  kSuccess,
  kInternalError,
  kVersionNewerError,
  kVersionSameError,
  kAlreadyPendingError,
  kAuthFailedError,
  kReadError,
  kWriteError,
  kInvalidFileError,
  kNotFoundError,
  kNothingToDoError,
  kNotSupportedError,
  kSignatureInvalidError,
  kAcPowerRequiredError,
  kPermissionDeniedError,
  kBrokenSystemError,
  kBatteryLevelTooLowError,
  kNeedsUserActionError,
  kAuthExpiredError,
  kUnknownError,
  kMaxValue = kUnknownError,
};

using FirmwareInstallOptions = std::map<std::string, bool>;
using FwupdStringToRequestIdMap = std::map<std::string, DeviceRequestId>;

// Gets an update path from an update response dict.
//
// Returns a path in URL format. The path will either be a local
// "file://" path or an "https://" path on the CrOS LVFS mirror.
//
// If any error occurs, returns an empty path.
COMPONENT_EXPORT(ASH_DBUS_FWUPD)
base::FilePath GetUpdatePathFromDict(const base::Value::Dict& dict);

class FakeFwupdClient;

// FwupdClient is used for handling signals from the fwupd daemon.
class COMPONENT_EXPORT(ASH_DBUS_FWUPD) FwupdClient
    : public chromeos::DBusClient {
 public:
  class Observer : public base::CheckedObserver {
   public:
    ~Observer() override = default;
    virtual void OnDeviceListResponse(FwupdDeviceList* devices) = 0;
    virtual void OnUpdateListResponse(const std::string& device_id,
                                      FwupdUpdateList* updates) = 0;
    virtual void OnPropertiesChangedResponse(FwupdProperties* properties) = 0;
    virtual void OnDeviceRequestResponse(FwupdRequest request) = 0;
  };

  void AddObserver(Observer* observer);
  void RemoveObserver(Observer* observer);

  FwupdClient(const FwupdClient&) = delete;
  FwupdClient& operator=(const FwupdClient&) = delete;

  // Returns the global instance if initialized. May return null.
  static FwupdClient* Get();

  // Returns the global fake instance if initialized. May return null.
  static FakeFwupdClient* GetFake();

  // Creates and initializes the global instance. |bus| must not be null.
  static void Initialize(dbus::Bus* bus);

  // Creates and initializes a fake global instance.
  static void InitializeFake();

  // Destroys the global instance if it has been initialized.
  static void Shutdown();

  void SetPropertiesForTesting(uint32_t percentage, uint32_t status) {
    properties_->SetPercentage(percentage);
    properties_->SetStatus(status);
  }

  // Query fwupd for updates that are available for a particular device.
  virtual void RequestUpdates(const std::string& device_id) = 0;

  // Query fwupd for devices that are currently connected.
  virtual void RequestDevices() = 0;

  // Install an update for |device_id|. Invokes |callback| when the operation
  // completes.
  virtual void InstallUpdate(
      const std::string& device_id,
      base::ScopedFD file_descriptor,
      FirmwareInstallOptions options,
      base::OnceCallback<void(FwupdDbusResult)> callback) = 0;

  // Updates metadata for |remote_id| with given old and new file descriptors.
  virtual void UpdateMetadata(
      const std::string& remote_id,
      base::ScopedFD data_file_descriptor,
      base::ScopedFD sig_file_descriptor,
      base::OnceCallback<void(FwupdDbusResult)> callback) = 0;

 protected:
  friend class FwupdClientTest;

  // Initialize() should be used instead.
  FwupdClient();
  ~FwupdClient() override;

  // Set fwupd client-capability feature flags.
  // See https://github.com/fwupd/fwupd/blob/main/libfwupd/fwupd-enums.h for the
  // full list of FwupdFeatureFlags.
  virtual void SetFwupdFeatureFlags() = 0;

  // Auxiliary variables for testing.
  // TODO(swifton): Replace this with an observer.
  bool client_is_in_testing_mode_ = false;
  int device_signal_call_count_for_testing_ = 0;

  // Holds the Fwupd Dbus properties for percentage and status.
  std::unique_ptr<FwupdDbusProperties> properties_;

  base::ObserverList<Observer> observers_;
};
}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_DBUS_FWUPD_FWUPD_CLIENT_H_