File: network_3gpp_handler.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 (247 lines) | stat: -rw-r--r-- 7,992 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// 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/network_3gpp_handler.h"

#include <stddef.h>
#include <stdint.h>

#include <algorithm>
#include <memory>
#include <vector>

#include "ash/constants/ash_features.h"
#include "base/containers/circular_deque.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/values.h"
#include "chromeos/ash/components/dbus/shill/modem_3gpp_client.h"
#include "chromeos/ash/components/dbus/shill/shill_device_client.h"
#include "chromeos/ash/components/dbus/shill/shill_manager_client.h"
#include "components/device_event_log/device_event_log.h"
#include "dbus/object_path.h"
#include "third_party/cros_system_api/dbus/service_constants.h"

namespace ash {

class Network3gppHandler::Network3gppDeviceHandler {
 public:
  Network3gppDeviceHandler() = default;
  virtual ~Network3gppDeviceHandler() = default;

  virtual void SetCarrierLock(
      const std::string& config,
      Modem3gppClient::CarrierLockCallback callback) = 0;
};

class Network3gppHandler::Network3gppDeviceHandlerImpl
    : public Network3gppHandler::Network3gppDeviceHandler {
 public:
  Network3gppDeviceHandlerImpl(const std::string& service_name,
                               const dbus::ObjectPath& object_path,
                               Modem3gppClient* modem_client);

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

 private:
  // Network3gppHandler::Network3gppDeviceHandler:
  void SetCarrierLock(const std::string& config,
                      Modem3gppClient::CarrierLockCallback callback) override;

  std::string service_name_;
  dbus::ObjectPath object_path_;
  raw_ptr<Modem3gppClient> modem_client_;

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

Network3gppHandler::Network3gppDeviceHandlerImpl::Network3gppDeviceHandlerImpl(
    const std::string& service_name,
    const dbus::ObjectPath& object_path,
    Modem3gppClient* modem_client)
    : service_name_(service_name),
      object_path_(object_path),
      modem_client_(modem_client) {}

void Network3gppHandler::Network3gppDeviceHandlerImpl::SetCarrierLock(
    const std::string& config,
    Modem3gppClient::CarrierLockCallback callback) {
  if (!modem_client_) {
    NET_LOG(ERROR) << "Modem 3gpp client not initialized.";
    std::move(callback).Run(CarrierLockResult::kNotInitialized);
    return;
  }

  modem_client_->SetCarrierLock(service_name_, object_path_, config,
                                std::move(callback));
}

///////////////////////////////////////////////////////////////////////////////
// Network3gppHandler

Network3gppHandler::Network3gppHandler() {}

Network3gppHandler::~Network3gppHandler() {
  if (!ShillManagerClient::Get()) {
    return;
  }

  ShillManagerClient::Get()->RemovePropertyChangedObserver(this);
  if (!cellular_device_path_.empty()) {
    ShillDeviceClient::Get()->RemovePropertyChangedObserver(
        dbus::ObjectPath(cellular_device_path_), this);
  }
}

void Network3gppHandler::Init() {
  // Add as an observer here so that new devices added after this call are
  // recognized.
  ShillManagerClient::Get()->AddPropertyChangedObserver(this);

  // Request network manager properties so that we can get the list of devices.
  ShillManagerClient::Get()->GetProperties(
      base::BindOnce(&Network3gppHandler::ManagerPropertiesCallback,
                     weak_ptr_factory_.GetWeakPtr()));
}

void Network3gppHandler::SetCarrierLock(
    const std::string& configuration,
    Modem3gppClient::CarrierLockCallback callback) {
  if (!device_handler_) {
    NET_LOG(ERROR) << "ModemManager device handler not initialized.";
    std::move(callback).Run(CarrierLockResult::kNotInitialized);
    return;
  }

  device_handler_->SetCarrierLock(configuration, std::move(callback));
}

void Network3gppHandler::OnPropertyChanged(const std::string& name,
                                           const base::Value& value) {
  // Device property change
  if (name == shill::kDBusObjectProperty) {
    OnObjectPathChanged(value);
    return;
  }

  // Manager property change
  if (name == shill::kDevicesProperty && value.is_list()) {
    UpdateDevices(value.GetList());
  }
}

void Network3gppHandler::ManagerPropertiesCallback(
    std::optional<base::Value::Dict> properties) {
  if (!properties) {
    NET_LOG(ERROR) << "Network3gppHandler: Failed to get manager properties.";
    return;
  }
  const base::Value::List* value =
      properties->FindList(shill::kDevicesProperty);
  if (!value) {
    NET_LOG(EVENT) << "Network3gppHandler: No list value for: "
                   << shill::kDevicesProperty;
    return;
  }
  UpdateDevices(*value);
}

void Network3gppHandler::UpdateDevices(const base::Value::List& devices) {
  for (const auto& item : devices) {
    if (!item.is_string()) {
      continue;
    }

    const std::string device_path = item.GetString();
    if (device_path.empty()) {
      continue;
    }
    // Request device properties.
    NET_LOG(DEBUG) << "GetDeviceProperties: " << device_path;
    ShillDeviceClient::Get()->GetProperties(
        dbus::ObjectPath(device_path),
        base::BindOnce(&Network3gppHandler::DevicePropertiesCallback,
                       weak_ptr_factory_.GetWeakPtr(), device_path));
  }
}

void Network3gppHandler::DevicePropertiesCallback(
    const std::string& device_path,
    std::optional<base::Value::Dict> properties) {
  if (!properties) {
    NET_LOG(ERROR) << "Network3gppHandler error for: " << device_path;
    return;
  }

  const std::string* device_type = properties->FindString(shill::kTypeProperty);
  if (!device_type) {
    NET_LOG(ERROR) << "Network3gppHandler: No type for: " << device_path;
    return;
  }
  if (*device_type != shill::kTypeCellular) {
    return;
  }

  const std::string* service_name =
      properties->FindString(shill::kDBusServiceProperty);
  if (!service_name) {
    NET_LOG(ERROR) << "Device has no DBusService Property: " << device_path;
    return;
  }

  if (*service_name != modemmanager::kModemManager1ServiceName) {
    return;
  }

  // Add observer for object path property
  if (cellular_device_path_ != device_path) {
    if (!cellular_device_path_.empty()) {
      ShillDeviceClient::Get()->RemovePropertyChangedObserver(
          dbus::ObjectPath(cellular_device_path_), this);
    }
    cellular_device_path_ = device_path;
    ShillDeviceClient::Get()->AddPropertyChangedObserver(
        dbus::ObjectPath(cellular_device_path_), this);
  }

  const std::string* object_path_string =
      properties->FindString(shill::kDBusObjectProperty);
  if (!object_path_string || object_path_string->empty()) {
    NET_LOG(ERROR) << "Device has no or empty DBusObject Property: "
                   << device_path;
    return;
  }
  dbus::ObjectPath object_path(*object_path_string);

  modem_client_ = Modem3gppClient::Get();
  if (!modem_client_) {
    NET_LOG(ERROR) << "Modem shill client was not initialized!";
    return;
  }

  device_handler_ = std::make_unique<Network3gppDeviceHandlerImpl>(
      *service_name, object_path, modem_client_);
}

void Network3gppHandler::OnObjectPathChanged(const base::Value& object_path) {
  // Remove the old handler.
  device_handler_.reset();

  const std::string object_path_string =
      object_path.is_string() ? object_path.GetString() : std::string();
  // If the new object path is empty, there is no SIM. Don't create a new
  // handler.
  if (object_path_string.empty() || object_path_string == "/") {
    return;
  }

  // Recreate handler for the new object path.
  ShillManagerClient::Get()->GetProperties(
      base::BindOnce(&Network3gppHandler::ManagerPropertiesCallback,
                     weak_ptr_factory_.GetWeakPtr()));
}

}  // namespace ash