File: hermes_manager_client.cc

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 (149 lines) | stat: -rw-r--r-- 4,441 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
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"

#include <memory>

#include "base/functional/bind.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "chromeos/ash/components/dbus/hermes/fake_hermes_manager_client.h"
#include "chromeos/ash/components/dbus/hermes/hermes_response_status.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "dbus/object_proxy.h"
#include "dbus/property.h"
#include "third_party/cros_system_api/dbus/hermes/dbus-constants.h"

namespace ash {

namespace {

HermesManagerClient* g_instance = nullptr;

}  // namespace

// The HermesManagerClient implementation.
class HermesManagerClientImpl : public HermesManagerClient {
 public:
  explicit HermesManagerClientImpl(dbus::Bus* bus) {
    dbus::ObjectPath hermes_manager_path(hermes::kHermesManagerPath);
    object_proxy_ =
        bus->GetObjectProxy(hermes::kHermesServiceName, hermes_manager_path);
    object_proxy_->WaitForServiceToBeAvailable(
        base::BindOnce(&HermesManagerClientImpl::OnHermesAvailable,
                       weak_ptr_factory_.GetWeakPtr()));
  }
  explicit HermesManagerClientImpl(const HermesManagerClient&) = delete;
  HermesManagerClientImpl& operator=(const HermesManagerClient&) = delete;
  ~HermesManagerClientImpl() override = default;

  // HermesManagerClient:
  const std::vector<dbus::ObjectPath>& GetAvailableEuiccs() override {
    if (properties_) {
      available_euiccs_ = properties_->available_euiccs().value();
    }
    return available_euiccs_;
  }

  TestInterface* GetTestInterface() override { return nullptr; }

 private:
  // Hermes Manager properties.
  class Properties : public dbus::PropertySet {
   public:
    Properties(dbus::ObjectProxy* object_proxy,
               const PropertyChangedCallback& callback)
        : dbus::PropertySet(object_proxy,
                            hermes::kHermesManagerInterface,
                            callback) {
      RegisterProperty(hermes::manager::kAvailableEuiccsProperty,
                       &available_euiccs_);
    }
    ~Properties() override = default;

    dbus::Property<std::vector<dbus::ObjectPath>>& available_euiccs() {
      return available_euiccs_;
    }

   private:
    // List of euicc objects available on the device.
    dbus::Property<std::vector<dbus::ObjectPath>> available_euiccs_;
  };

  void OnPropertyChanged(const dbus::ObjectPath& object_path,
                         const std::string& property_name) {
    for (auto& observer : observers()) {
      observer.OnAvailableEuiccListChanged();
    }
  }

  void OnHermesAvailable(bool service_is_available) {
    dbus::ObjectPath hermes_manager_path(hermes::kHermesManagerPath);
    properties_ = std::make_unique<Properties>(
        object_proxy_,
        base::BindRepeating(&HermesManagerClientImpl::OnPropertyChanged,
                            weak_ptr_factory_.GetWeakPtr(),
                            hermes_manager_path));
    properties_->ConnectSignals();
    properties_->GetAll();
  }

  raw_ptr<dbus::ObjectProxy> object_proxy_;
  std::unique_ptr<Properties> properties_;
  std::vector<dbus::ObjectPath> available_euiccs_;
  base::WeakPtrFactory<HermesManagerClientImpl> weak_ptr_factory_{this};
};

HermesManagerClient::HermesManagerClient() {
  DCHECK(!g_instance);
  g_instance = this;
}

HermesManagerClient::~HermesManagerClient() {
  DCHECK_EQ(g_instance, this);
  for (auto& observer : observers()) {
    observer.OnShutdown();
  }
  g_instance = nullptr;
}

void HermesManagerClient::AddObserver(HermesManagerClient::Observer* observer) {
  DCHECK(observer);
  observers_.AddObserver(observer);
}

void HermesManagerClient::RemoveObserver(
    HermesManagerClient::Observer* observer) {
  DCHECK(observer);
  observers_.RemoveObserver(observer);
}

// static
void HermesManagerClient::Initialize(dbus::Bus* bus) {
  DCHECK(bus);
  DCHECK(!g_instance);
  new HermesManagerClientImpl(bus);
}

// static
void HermesManagerClient::InitializeFake() {
  new FakeHermesManagerClient();
}

// static
void HermesManagerClient::Shutdown() {
  DCHECK(g_instance);
  delete g_instance;
}

// static
HermesManagerClient* HermesManagerClient::Get() {
  return g_instance;
}

}  // namespace ash