File: cellular_esim_profile_waiter.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 (109 lines) | stat: -rw-r--r-- 4,655 bytes parent folder | download | duplicates (8)
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
// 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 CHROMEOS_ASH_COMPONENTS_NETWORK_CELLULAR_ESIM_PROFILE_WAITER_H_
#define CHROMEOS_ASH_COMPONENTS_NETWORK_CELLULAR_ESIM_PROFILE_WAITER_H_

#include <string>

#include "base/check.h"
#include "base/component_export.h"
#include "base/containers/flat_map.h"
#include "base/functional/callback_forward.h"
#include "base/scoped_observation.h"
#include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"
#include "chromeos/ash/components/dbus/hermes/hermes_profile_client.h"
#include "dbus/object_path.h"
#include "dbus/property.h"

namespace ash {

// This class performs a best-effort attempt at waiting until specified Hermes
// profiles meet specified conditions regarding their properties to invoke a
// provided callback. The provided callback will be invoked if the destructor is
// called and the callback hadn't previously been invoked. This guarantee allows
// clients to pass ownership of instances of this class to delayed tasks,
// resulting in the callback being called as soon as the conditions are met or
// upon destruction when the task completes.
class COMPONENT_EXPORT(CHROMEOS_NETWORK) CellularESimProfileWaiter
    : public HermesManagerClient::Observer,
      public HermesProfileClient::Observer {
 public:
  // Comparison function that should return |true| IFF the provided Hermes
  // properties have reached a desired state.
  using Condition =
      base::RepeatingCallback<bool(HermesProfileClient::Properties*)>;

  CellularESimProfileWaiter();
  CellularESimProfileWaiter(const CellularESimProfileWaiter&) = delete;
  CellularESimProfileWaiter& operator=(const CellularESimProfileWaiter&) =
      delete;
  ~CellularESimProfileWaiter() override;

  // This is a helper function used to add a condition that the profile at
  // |profile_path| has the properties that are expected of a pending eSIM
  // profile to be set.
  void RequirePendingProfile(const dbus::ObjectPath& profile_path);

  // This function is used to begin watching for Hermes profile property
  // changes. This function will perform an initial check of whether all of the
  // conditions have already been met; if so, the |on_success| callback will be
  // immediately called. Clients should confirm whether they need to continue
  // waiting after calling this method since all conditions may be satisfied
  // without delay.
  //
  // If the Hermes clients are shut down and the specified conditions have not
  // been met the |on_shutdown| callback will be called.
  void Wait(base::OnceCallback<void()> on_success,
            base::OnceCallback<void()> on_shutdown);

  // Returns whether we are waiting for all conditions to be met.
  bool waiting() const;

 private:
  friend class CellularESimProfileWaiterTest;

  // HermesManagerClient::Observer:
  void OnShutdown() override;

  // HermesProfileClient::Observer:
  void OnCarrierProfilePropertyChanged(
      const dbus::ObjectPath& object_path,
      const std::string& property_name) override;

  // This function is used to add a condition to wait on. The |profile_path|
  // argument should be the path to an Hermes profile and |condition| should be
  // a function that evaluates |true| IFF the profile is in the desired state.
  // The profile does not need to exist at the time this method is called.
  void AddCondition(const dbus::ObjectPath& profile_path, Condition condition);

  // This function returns whether |condition| is satisfied for the Hermes
  // profile at |profile_path|.
  bool EvaluateCondition(const dbus::ObjectPath& profile_path,
                         const Condition& condition);

  // This function checks whether all conditions have been met and is
  // responsible for stopping observing Hermes profile property changes and
  // invoking |on_success_|.
  void MaybeFinish();

  base::flat_map<dbus::ObjectPath, Condition> profile_path_to_condition_;

  // This will be called when all specified conditions have been met, or upon
  // destruction.
  base::OnceCallback<void()> on_success_;

  // This will be called IFF all specified conditions have not been met and the
  // Hermes clients are being shut down.
  base::OnceCallback<void()> on_shutdown_;

  base::ScopedObservation<HermesManagerClient, HermesManagerClient::Observer>
      hermes_manager_client_observer_{this};
  base::ScopedObservation<HermesProfileClient, HermesProfileClient::Observer>
      hermes_profile_client_observer_{this};
};

}  // namespace ash

#endif  // CHROMEOS_ASH_COMPONENTS_NETWORK_CELLULAR_ESIM_PROFILE_WAITER_H_