ui_broker_impl.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2021 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/quick_pair/ui/ui_broker_impl.h"
  5. #include <memory>
  6. #include "ash/quick_pair/common/device.h"
  7. #include "ash/quick_pair/common/protocol.h"
  8. #include "ash/quick_pair/ui/actions.h"
  9. #include "ash/quick_pair/ui/fast_pair/fast_pair_presenter.h"
  10. #include "ash/quick_pair/ui/fast_pair/fast_pair_presenter_impl.h"
  11. #include "base/bind.h"
  12. #include "ui/message_center/message_center.h"
  13. namespace ash {
  14. namespace quick_pair {
  15. UIBrokerImpl::UIBrokerImpl()
  16. : fast_pair_presenter_(FastPairPresenterImpl::Factory::Create(
  17. message_center::MessageCenter::Get())) {}
  18. UIBrokerImpl::~UIBrokerImpl() = default;
  19. void UIBrokerImpl::AddObserver(Observer* observer) {
  20. observers_.AddObserver(observer);
  21. }
  22. void UIBrokerImpl::RemoveObserver(Observer* observer) {
  23. observers_.RemoveObserver(observer);
  24. }
  25. void UIBrokerImpl::ShowDiscovery(scoped_refptr<Device> device) {
  26. switch (device->protocol) {
  27. case Protocol::kFastPairInitial:
  28. case Protocol::kFastPairSubsequent:
  29. fast_pair_presenter_->ShowDiscovery(
  30. device,
  31. base::BindRepeating(&UIBrokerImpl::NotifyDiscoveryAction,
  32. weak_pointer_factory_.GetWeakPtr(), device));
  33. break;
  34. case Protocol::kFastPairRetroactive:
  35. NOTREACHED();
  36. break;
  37. }
  38. }
  39. void UIBrokerImpl::ShowPairing(scoped_refptr<Device> device) {
  40. switch (device->protocol) {
  41. case Protocol::kFastPairInitial:
  42. case Protocol::kFastPairRetroactive:
  43. case Protocol::kFastPairSubsequent:
  44. fast_pair_presenter_->ShowPairing(std::move(device));
  45. break;
  46. }
  47. }
  48. void UIBrokerImpl::ShowPairingFailed(scoped_refptr<Device> device) {
  49. switch (device->protocol) {
  50. case Protocol::kFastPairInitial:
  51. case Protocol::kFastPairSubsequent:
  52. fast_pair_presenter_->ShowPairingFailed(
  53. device,
  54. base::BindRepeating(&UIBrokerImpl::NotifyPairingFailedAction,
  55. weak_pointer_factory_.GetWeakPtr(), device));
  56. break;
  57. case Protocol::kFastPairRetroactive:
  58. // In this scenario, we don't show the error UI because it would be
  59. // misleading, since a pair failure is a retroactive pair failure, and
  60. // guiding the user back to settings doesn't make sense.
  61. break;
  62. }
  63. }
  64. void UIBrokerImpl::ShowAssociateAccount(scoped_refptr<Device> device) {
  65. switch (device->protocol) {
  66. case Protocol::kFastPairInitial:
  67. case Protocol::kFastPairRetroactive:
  68. fast_pair_presenter_->ShowAssociateAccount(
  69. device,
  70. base::BindRepeating(&UIBrokerImpl::NotifyAssociateAccountAction,
  71. weak_pointer_factory_.GetWeakPtr(), device));
  72. break;
  73. case Protocol::kFastPairSubsequent:
  74. NOTREACHED();
  75. break;
  76. }
  77. }
  78. void UIBrokerImpl::ShowCompanionApp(scoped_refptr<Device> device) {
  79. switch (device->protocol) {
  80. case Protocol::kFastPairInitial:
  81. case Protocol::kFastPairRetroactive:
  82. case Protocol::kFastPairSubsequent:
  83. fast_pair_presenter_->ShowCompanionApp(
  84. device,
  85. base::BindRepeating(&UIBrokerImpl::NotifyCompanionAppAction,
  86. weak_pointer_factory_.GetWeakPtr(), device));
  87. break;
  88. }
  89. }
  90. void UIBrokerImpl::RemoveNotifications() {
  91. fast_pair_presenter_->RemoveNotifications();
  92. }
  93. void UIBrokerImpl::NotifyDiscoveryAction(scoped_refptr<Device> device,
  94. DiscoveryAction action) {
  95. for (auto& observer : observers_)
  96. observer.OnDiscoveryAction(device, action);
  97. }
  98. void UIBrokerImpl::NotifyPairingFailedAction(scoped_refptr<Device> device,
  99. PairingFailedAction action) {
  100. for (auto& observer : observers_)
  101. observer.OnPairingFailureAction(device, action);
  102. }
  103. void UIBrokerImpl::NotifyAssociateAccountAction(scoped_refptr<Device> device,
  104. AssociateAccountAction action) {
  105. for (auto& observer : observers_)
  106. observer.OnAssociateAccountAction(device, action);
  107. }
  108. void UIBrokerImpl::NotifyCompanionAppAction(scoped_refptr<Device> device,
  109. CompanionAppAction action) {
  110. for (auto& observer : observers_)
  111. observer.OnCompanionAppAction(device, action);
  112. }
  113. } // namespace quick_pair
  114. } // namespace ash