cellular_setup_impl.cc 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "ash/services/cellular_setup/cellular_setup_impl.h"
  5. #include <utility>
  6. #include "ash/services/cellular_setup/ota_activator_impl.h"
  7. #include "base/bind.h"
  8. #include "base/memory/ptr_util.h"
  9. #include "chromeos/ash/components/network/network_handler.h"
  10. #include "mojo/public/cpp/bindings/self_owned_receiver.h"
  11. namespace ash::cellular_setup {
  12. // static
  13. void CellularSetupImpl::CreateAndBindToReciever(
  14. mojo::PendingReceiver<mojom::CellularSetup> receiver) {
  15. mojo::MakeSelfOwnedReceiver(base::WrapUnique(new CellularSetupImpl()),
  16. std::move(receiver));
  17. }
  18. CellularSetupImpl::CellularSetupImpl() = default;
  19. CellularSetupImpl::~CellularSetupImpl() = default;
  20. void CellularSetupImpl::StartActivation(
  21. mojo::PendingRemote<mojom::ActivationDelegate> delegate,
  22. StartActivationCallback callback) {
  23. size_t request_id = next_request_id_;
  24. ++next_request_id_;
  25. NetworkHandler* network_handler = NetworkHandler::Get();
  26. std::unique_ptr<OtaActivator> ota_activator =
  27. OtaActivatorImpl::Factory::Create(
  28. std::move(delegate),
  29. base::BindOnce(&CellularSetupImpl::OnActivationAttemptFinished,
  30. weak_ptr_factory_.GetWeakPtr(), request_id),
  31. network_handler->network_state_handler(),
  32. network_handler->network_connection_handler(),
  33. network_handler->network_activation_handler());
  34. std::move(callback).Run(ota_activator->GenerateRemote());
  35. // Store the OtaActivator instance in a map indexed by request ID; once the
  36. // attempt has finished, the map entry will be deleted in
  37. // OnActivationAttemptFinished().
  38. ota_activator_map_.AddWithID(std::move(ota_activator), request_id);
  39. }
  40. void CellularSetupImpl::OnActivationAttemptFinished(size_t request_id) {
  41. ota_activator_map_.Remove(request_id);
  42. }
  43. } // namespace ash::cellular_setup