File: cellular_esim_profile_waiter.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 (143 lines) | stat: -rw-r--r-- 4,444 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
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
// 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.

#include "chromeos/ash/components/network/cellular_esim_profile_waiter.h"

#include <string>
#include "base/check.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_profile_client.h"
#include "components/device_event_log/device_event_log.h"
#include "dbus/object_path.h"
#include "dbus/property.h"

namespace ash {

CellularESimProfileWaiter::CellularESimProfileWaiter() = default;

CellularESimProfileWaiter::~CellularESimProfileWaiter() {
  hermes_manager_client_observer_.Reset();
  hermes_profile_client_observer_.Reset();
  if (!on_success_.is_null()) {
    NET_LOG(EVENT)
        << "CellularESimProfileWaiter was destructed while waiting for "
        << "all of the conditions to be satisfied";
    std::move(on_success_).Run();
  }
  on_shutdown_.Reset();
}

void CellularESimProfileWaiter::RequirePendingProfile(
    const dbus::ObjectPath& profile_path) {
  AddCondition(
      profile_path,
      base::BindRepeating(
          [](HermesProfileClient::Properties* profile_properties) {
            return profile_properties &&
                   profile_properties->state().value() ==
                       hermes::profile::State::kPending &&
                   !profile_properties->name().value().empty() &&
                   !profile_properties->activation_code().value().empty();
          }));
}

void CellularESimProfileWaiter::Wait(base::OnceCallback<void()> on_success,
                                     base::OnceCallback<void()> on_shutdown) {
  DCHECK(on_success_.is_null() && !on_success.is_null());
  DCHECK(on_shutdown_.is_null() && !on_shutdown.is_null());
  on_success_ = std::move(on_success);
  on_shutdown_ = std::move(on_shutdown);

  if (profile_path_to_condition_.empty()) {
    std::move(on_success_).Run();
    on_shutdown_.Reset();
    return;
  }

  hermes_manager_client_observer_.Observe(HermesManagerClient::Get());
  hermes_profile_client_observer_.Observe(HermesProfileClient::Get());

  for (auto entry = profile_path_to_condition_.begin();
       entry != profile_path_to_condition_.end();) {
    if (EvaluateCondition(entry->first, entry->second)) {
      entry = profile_path_to_condition_.erase(entry);
    } else {
      entry++;
    }
  }
  MaybeFinish();
}

bool CellularESimProfileWaiter::waiting() const {
  return !profile_path_to_condition_.empty();
}

void CellularESimProfileWaiter::OnShutdown() {
  NET_LOG(EVENT) << "Hermes clients shut down while waiting for all of the "
                 << "conditions to be satisfied";

  hermes_manager_client_observer_.Reset();
  hermes_profile_client_observer_.Reset();

  profile_path_to_condition_.clear();

  DCHECK(!on_shutdown_.is_null());

  on_success_.Reset();
  std::move(on_shutdown_).Run();
}

void CellularESimProfileWaiter::OnCarrierProfilePropertyChanged(
    const dbus::ObjectPath& object_path,
    const std::string& property_name) {
  auto entry = profile_path_to_condition_.find(object_path);
  if (entry == profile_path_to_condition_.end()) {
    return;
  }
  if (EvaluateCondition(/*profile_path=*/entry->first,
                        /*condition=*/entry->second)) {
    profile_path_to_condition_.erase(entry->first);
  }
  MaybeFinish();
}

void CellularESimProfileWaiter::AddCondition(
    const dbus::ObjectPath& profile_path,
    Condition condition) {
  DCHECK(!condition.is_null());
  profile_path_to_condition_.insert_or_assign(profile_path,
                                              std::move(condition));
}

bool CellularESimProfileWaiter::EvaluateCondition(
    const dbus::ObjectPath& profile_path,
    const Condition& condition) {
  HermesProfileClient::Properties* profile_properties =
      HermesProfileClient::Get()->GetProperties(profile_path);
  if (!profile_properties) {
    return false;
  }
  return condition.Run(profile_properties);
}

void CellularESimProfileWaiter::MaybeFinish() {
  if (!profile_path_to_condition_.empty()) {
    return;
  }
  hermes_manager_client_observer_.Reset();
  hermes_profile_client_observer_.Reset();

  if (on_success_.is_null()) {
    return;
  }

  NET_LOG(EVENT) << "All conditions have been satisfied";

  std::move(on_success_).Run();
  on_shutdown_.Reset();
}

}  // namespace ash