123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205 |
- // Copyright 2020 The Chromium Authors. All rights reserved.
- // Use of this source code is governed by a BSD-style license that can be
- // found in the LICENSE file.
- #include "ash/services/cellular_setup/esim_manager.h"
- #include <sstream>
- #include "ash/constants/ash_features.h"
- #include "ash/services/cellular_setup/esim_profile.h"
- #include "ash/services/cellular_setup/euicc.h"
- #include "ash/services/cellular_setup/public/mojom/esim_manager.mojom.h"
- #include "base/strings/utf_string_conversions.h"
- #include "chromeos/ash/components/dbus/hermes/hermes_manager_client.h"
- #include "chromeos/ash/components/network/network_event_log.h"
- #include "chromeos/ash/components/network/network_handler.h"
- #include "components/device_event_log/device_event_log.h"
- #include "dbus/object_path.h"
- #include "third_party/cros_system_api/dbus/hermes/dbus-constants.h"
- namespace ash::cellular_setup {
- namespace {
- // The Stork SM-DS Prod server used to fetch pending ESim profiles.
- const char kStorkSmdsServerAddress[] = "prod.smds.rsp.goog";
- void LogEuiccPaths(const std::set<dbus::ObjectPath>& new_euicc_paths) {
- if (new_euicc_paths.empty()) {
- NET_LOG(EVENT) << "EUICC list updated; no EUICCs present";
- return;
- }
- std::stringstream ss("[");
- for (const auto& new_path : new_euicc_paths)
- ss << new_path.value() << ", ";
- ss.seekp(-2, ss.cur); // Remove last ", " from the stream.
- ss << "]";
- NET_LOG(EVENT) << "EUICC list updated; paths: " << ss.str();
- }
- } // namespace
- // static
- std::string ESimManager::GetRootSmdsAddress() {
- // An empty string indicates that the root GSMA address should be used.
- return features::IsUseStorkSmdsServerAddressEnabled()
- ? kStorkSmdsServerAddress
- : std::string();
- }
- ESimManager::ESimManager()
- : ESimManager(NetworkHandler::Get()->cellular_connection_handler(),
- NetworkHandler::Get()->cellular_esim_installer(),
- NetworkHandler::Get()->cellular_esim_profile_handler(),
- NetworkHandler::Get()->cellular_esim_uninstall_handler(),
- NetworkHandler::Get()->cellular_inhibitor(),
- NetworkHandler::Get()->network_connection_handler(),
- NetworkHandler::Get()->network_state_handler()) {}
- ESimManager::ESimManager(
- CellularConnectionHandler* cellular_connection_handler,
- CellularESimInstaller* cellular_esim_installer,
- CellularESimProfileHandler* cellular_esim_profile_handler,
- CellularESimUninstallHandler* cellular_esim_uninstall_handler,
- CellularInhibitor* cellular_inhibitor,
- NetworkConnectionHandler* network_connection_handler,
- NetworkStateHandler* network_state_handler)
- : cellular_connection_handler_(cellular_connection_handler),
- cellular_esim_installer_(cellular_esim_installer),
- cellular_esim_profile_handler_(cellular_esim_profile_handler),
- cellular_esim_uninstall_handler_(cellular_esim_uninstall_handler),
- cellular_inhibitor_(cellular_inhibitor),
- network_connection_handler_(network_connection_handler),
- network_state_handler_(network_state_handler) {
- HermesManagerClient::Get()->AddObserver(this);
- HermesEuiccClient::Get()->AddObserver(this);
- cellular_esim_profile_handler_->AddObserver(this);
- OnESimProfileListUpdated();
- }
- ESimManager::~ESimManager() {
- HermesManagerClient::Get()->RemoveObserver(this);
- HermesEuiccClient::Get()->RemoveObserver(this);
- cellular_esim_profile_handler_->RemoveObserver(this);
- }
- void ESimManager::AddObserver(
- mojo::PendingRemote<mojom::ESimManagerObserver> observer) {
- observers_.Add(std::move(observer));
- }
- void ESimManager::GetAvailableEuiccs(GetAvailableEuiccsCallback callback) {
- std::vector<mojo::PendingRemote<mojom::Euicc>> euicc_list;
- for (auto const& euicc : available_euiccs_)
- euicc_list.push_back(euicc->CreateRemote());
- std::move(callback).Run(std::move(euicc_list));
- }
- void ESimManager::OnAvailableEuiccListChanged() {
- UpdateAvailableEuiccs();
- }
- void ESimManager::OnEuiccPropertyChanged(const dbus::ObjectPath& euicc_path,
- const std::string& property_name) {
- Euicc* euicc = GetEuiccFromPath(euicc_path);
- // Skip notifying observers if the euicc object is not tracked.
- if (!euicc)
- return;
- // Profile lists are handled in OnESimProfileListUpdated notification from
- // CellularESimProfileHandler.
- if (property_name == hermes::euicc::kPendingProfilesProperty ||
- property_name == hermes::euicc::kInstalledProfilesProperty) {
- return;
- }
- euicc->UpdateProperties();
- for (auto& observer : observers_)
- observer->OnEuiccChanged(euicc->CreateRemote());
- }
- void ESimManager::OnESimProfileListUpdated() {
- // Force update available euiccs in case OnAvailableEuiccListChanged on this
- // class was not called before this handler
- UpdateAvailableEuiccs();
- std::vector<CellularESimProfile> esim_profile_states =
- cellular_esim_profile_handler_->GetESimProfiles();
- for (auto& euicc : available_euiccs_) {
- euicc->UpdateProfileList(esim_profile_states);
- }
- }
- void ESimManager::BindReceiver(
- mojo::PendingReceiver<mojom::ESimManager> receiver) {
- NET_LOG(EVENT) << "ESimManager::BindReceiver()";
- receivers_.Add(this, std::move(receiver));
- }
- void ESimManager::NotifyESimProfileChanged(ESimProfile* esim_profile) {
- for (auto& observer : observers_)
- observer->OnProfileChanged(esim_profile->CreateRemote());
- }
- void ESimManager::NotifyESimProfileListChanged(Euicc* euicc) {
- for (auto& observer : observers_)
- observer->OnProfileListChanged(euicc->CreateRemote());
- }
- void ESimManager::UpdateAvailableEuiccs() {
- NET_LOG(DEBUG) << "Updating available Euiccs";
- std::set<dbus::ObjectPath> new_euicc_paths;
- bool available_euiccs_changed = false;
- for (auto& euicc_path : HermesManagerClient::Get()->GetAvailableEuiccs()) {
- available_euiccs_changed |= CreateEuiccIfNew(euicc_path);
- new_euicc_paths.insert(euicc_path);
- }
- available_euiccs_changed |= RemoveUntrackedEuiccs(new_euicc_paths);
- if (!available_euiccs_changed)
- return;
- LogEuiccPaths(new_euicc_paths);
- for (auto& observer : observers_)
- observer->OnAvailableEuiccListChanged();
- }
- bool ESimManager::RemoveUntrackedEuiccs(
- const std::set<dbus::ObjectPath> new_euicc_paths) {
- bool removed = false;
- for (auto euicc_it = available_euiccs_.begin();
- euicc_it != available_euiccs_.end();) {
- if (new_euicc_paths.find((*euicc_it)->path()) == new_euicc_paths.end()) {
- removed = true;
- euicc_it = available_euiccs_.erase(euicc_it);
- } else {
- euicc_it++;
- }
- }
- return removed;
- }
- bool ESimManager::CreateEuiccIfNew(const dbus::ObjectPath& euicc_path) {
- Euicc* euicc_info = GetEuiccFromPath(euicc_path);
- if (euicc_info)
- return false;
- available_euiccs_.push_back(std::make_unique<Euicc>(euicc_path, this));
- return true;
- }
- Euicc* ESimManager::GetEuiccFromPath(const dbus::ObjectPath& path) {
- for (auto& euicc : available_euiccs_) {
- if (euicc->path() == path) {
- return euicc.get();
- }
- }
- return nullptr;
- }
- } // namespace ash::cellular_setup
|