Skip to content

Commit

Permalink
[Fast Pair] Connect RetroactivePairingDetector to Mediator
Browse files Browse the repository at this point in the history
Connects RetroactivePairingDetector to the Mediator, which
OnRetroactivePairFound triggers the ShowAssociateAccount
in the UIBroker. Right now, no logic is added when the
user presses Save on the notification, and the account
key write will be added in a follow up change.

Change-Id: Ic24f249e53c3fd1ca665b0919d1a1b883235add1
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/3206332
Commit-Queue: Juliet Levesque <julietlevesque@google.com>
Reviewed-by: Shane Fitzpatrick <shanefitz@google.com>
Cr-Commit-Position: refs/heads/main@{#940456}
  • Loading branch information
julietlevesque authored and Chromium LUCI CQ committed Nov 10, 2021
1 parent a2ec5a4 commit c4fb9f2
Show file tree
Hide file tree
Showing 10 changed files with 158 additions and 18 deletions.
3 changes: 3 additions & 0 deletions ash/quick_pair/keyed_service/BUILD.gn
Expand Up @@ -20,6 +20,7 @@ static_library("keyed_service") {
deps = [
"//ash/quick_pair/common",
"//ash/quick_pair/feature_status_tracker",
"//ash/quick_pair/message_stream",
"//ash/quick_pair/pairing",
"//ash/quick_pair/repository",
"//ash/quick_pair/scanning",
Expand All @@ -45,6 +46,8 @@ source_set("unit_tests") {
":keyed_service",
"//ash/quick_pair/common",
"//ash/quick_pair/feature_status_tracker:test_support",
"//ash/quick_pair/message_stream",
"//ash/quick_pair/message_stream:test_support",
"//ash/quick_pair/pairing:test_support",
"//ash/quick_pair/repository",
"//ash/quick_pair/scanning:test_support",
Expand Down
44 changes: 37 additions & 7 deletions ash/quick_pair/keyed_service/quick_pair_mediator.cc
Expand Up @@ -13,7 +13,10 @@
#include "ash/quick_pair/feature_status_tracker/quick_pair_feature_status_tracker.h"
#include "ash/quick_pair/feature_status_tracker/quick_pair_feature_status_tracker_impl.h"
#include "ash/quick_pair/keyed_service/quick_pair_metrics_logger.h"
#include "ash/quick_pair/message_stream/message_stream_lookup.h"
#include "ash/quick_pair/message_stream/message_stream_lookup_impl.h"
#include "ash/quick_pair/pairing/pairer_broker_impl.h"
#include "ash/quick_pair/pairing/retroactive_pairing_detector_impl.h"
#include "ash/quick_pair/repository/fast_pair/pending_write_store.h"
#include "ash/quick_pair/repository/fast_pair/saved_device_registry.h"
#include "ash/quick_pair/repository/fast_pair_repository_impl.h"
Expand All @@ -40,11 +43,16 @@ std::unique_ptr<Mediator> Mediator::Factory::Create() {
return g_test_factory->BuildInstance();

auto process_manager = std::make_unique<QuickPairProcessManagerImpl>();
auto pairer_broker = std::make_unique<PairerBrokerImpl>();
auto message_stream_lookup = std::make_unique<MessageStreamLookupImpl>();

return std::make_unique<Mediator>(
std::make_unique<FeatureStatusTrackerImpl>(),
std::make_unique<ScannerBrokerImpl>(process_manager.get()),
std::make_unique<PairerBrokerImpl>(), std::make_unique<UIBrokerImpl>(),
std::make_unique<RetroactivePairingDetectorImpl>(
pairer_broker.get(), message_stream_lookup.get()),
std::move(message_stream_lookup), std::move(pairer_broker),
std::make_unique<UIBrokerImpl>(),
std::make_unique<FastPairRepositoryImpl>(), std::move(process_manager));
}

Expand All @@ -53,14 +61,19 @@ void Mediator::Factory::SetFactoryForTesting(Factory* factory) {
g_test_factory = factory;
}

Mediator::Mediator(std::unique_ptr<FeatureStatusTracker> feature_status_tracker,
std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<PairerBroker> pairer_broker,
std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository,
std::unique_ptr<QuickPairProcessManager> process_manager)
Mediator::Mediator(
std::unique_ptr<FeatureStatusTracker> feature_status_tracker,
std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<RetroactivePairingDetector> retroactive_pairing_detector,
std::unique_ptr<MessageStreamLookup> message_stream_lookup,
std::unique_ptr<PairerBroker> pairer_broker,
std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository,
std::unique_ptr<QuickPairProcessManager> process_manager)
: feature_status_tracker_(std::move(feature_status_tracker)),
scanner_broker_(std::move(scanner_broker)),
retroactive_pairing_detector_(std::move(retroactive_pairing_detector)),
message_stream_lookup(std::move(message_stream_lookup)),
pairer_broker_(std::move(pairer_broker)),
ui_broker_(std::move(ui_broker)),
fast_pair_repository_(std::move(fast_pair_repository)),
Expand All @@ -70,6 +83,8 @@ Mediator::Mediator(std::unique_ptr<FeatureStatusTracker> feature_status_tracker,

feature_status_tracker_observation_.Observe(feature_status_tracker_.get());
scanner_broker_observation_.Observe(scanner_broker_.get());
retroactive_pairing_detector_observation_.Observe(
retroactive_pairing_detector_.get());
pairer_broker_observation_.Observe(pairer_broker_.get());
ui_broker_observation_.Observe(ui_broker_.get());

Expand Down Expand Up @@ -109,6 +124,11 @@ void Mediator::OnDeviceLost(scoped_refptr<Device> device) {
ui_broker_->RemoveNotifications(std::move(device));
}

void Mediator::OnRetroactivePairFound(scoped_refptr<Device> device) {
QP_LOG(INFO) << __func__ << ": " << device;
ui_broker_->ShowAssociateAccount(std::move(device));
}

void Mediator::SetFastPairState(bool is_enabled) {
QP_LOG(VERBOSE) << __func__ << ": " << is_enabled;

Expand Down Expand Up @@ -172,6 +192,16 @@ void Mediator::OnCompanionAppAction(scoped_refptr<Device> device,
void Mediator::OnAssociateAccountAction(scoped_refptr<Device> device,
AssociateAccountAction action) {
QP_LOG(INFO) << __func__ << ": Device=" << device << ", Action=" << action;

switch (action) {
case AssociateAccountAction::kAssoicateAccount:
break;
case AssociateAccountAction::kLearnMore:
break;
case AssociateAccountAction::kDismissedByUser:
case AssociateAccountAction::kDismissed:
break;
}
}

} // namespace quick_pair
Expand Down
28 changes: 21 additions & 7 deletions ash/quick_pair/keyed_service/quick_pair_mediator.h
Expand Up @@ -9,6 +9,7 @@

#include "ash/quick_pair/feature_status_tracker/quick_pair_feature_status_tracker.h"
#include "ash/quick_pair/pairing/pairer_broker.h"
#include "ash/quick_pair/pairing/retroactive_pairing_detector.h"
#include "ash/quick_pair/scanning/scanner_broker.h"
#include "ash/quick_pair/ui/ui_broker.h"
#include "base/memory/scoped_refptr.h"
Expand All @@ -29,13 +30,15 @@ class FastPairRepository;
struct Device;
class QuickPairProcessManager;
class QuickPairMetricsLogger;
class MessageStreamLookup;

// Implements the Mediator design pattern for the components in the Quick Pair
// system, e.g. the UI Broker, Scanning Broker and Pairing Broker.
class Mediator final : public FeatureStatusTracker::Observer,
public ScannerBroker::Observer,
public PairerBroker::Observer,
public UIBroker::Observer {
public UIBroker::Observer,
public RetroactivePairingDetector::Observer {
public:
class Factory {
public:
Expand All @@ -47,12 +50,15 @@ class Mediator final : public FeatureStatusTracker::Observer,
virtual std::unique_ptr<Mediator> BuildInstance() = 0;
};

Mediator(std::unique_ptr<FeatureStatusTracker> feature_status_tracker,
std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<PairerBroker> pairer_broker,
std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository,
std::unique_ptr<QuickPairProcessManager> process_manager);
Mediator(
std::unique_ptr<FeatureStatusTracker> feature_status_tracker,
std::unique_ptr<ScannerBroker> scanner_broker,
std::unique_ptr<RetroactivePairingDetector> retroactive_pairing_detector,
std::unique_ptr<MessageStreamLookup> message_stream_lookup,
std::unique_ptr<PairerBroker> pairer_broker,
std::unique_ptr<UIBroker> ui_broker,
std::unique_ptr<FastPairRepository> fast_pair_repository,
std::unique_ptr<QuickPairProcessManager> process_manager);
Mediator(const Mediator&) = delete;
Mediator& operator=(const Mediator&) = delete;
~Mediator() override;
Expand Down Expand Up @@ -85,11 +91,16 @@ class Mediator final : public FeatureStatusTracker::Observer,
void OnAssociateAccountAction(scoped_refptr<Device> device,
AssociateAccountAction action) override;

// RetroactivePairingDetector::Observer
void OnRetroactivePairFound(scoped_refptr<Device> device) override;

private:
void SetFastPairState(bool is_enabled);

std::unique_ptr<FeatureStatusTracker> feature_status_tracker_;
std::unique_ptr<ScannerBroker> scanner_broker_;
std::unique_ptr<RetroactivePairingDetector> retroactive_pairing_detector_;
std::unique_ptr<MessageStreamLookup> message_stream_lookup;
std::unique_ptr<PairerBroker> pairer_broker_;
std::unique_ptr<UIBroker> ui_broker_;
std::unique_ptr<FastPairRepository> fast_pair_repository_;
Expand All @@ -100,6 +111,9 @@ class Mediator final : public FeatureStatusTracker::Observer,
feature_status_tracker_observation_{this};
base::ScopedObservation<ScannerBroker, ScannerBroker::Observer>
scanner_broker_observation_{this};
base::ScopedObservation<RetroactivePairingDetector,
RetroactivePairingDetector::Observer>
retroactive_pairing_detector_observation_{this};
base::ScopedObservation<PairerBroker, PairerBroker::Observer>
pairer_broker_observation_{this};
base::ScopedObservation<UIBroker, UIBroker::Observer> ui_broker_observation_{
Expand Down
21 changes: 20 additions & 1 deletion ash/quick_pair/keyed_service/quick_pair_mediator_unittest.cc
Expand Up @@ -12,8 +12,12 @@
#include "ash/quick_pair/feature_status_tracker/fake_feature_status_tracker.h"
#include "ash/quick_pair/feature_status_tracker/mock_quick_pair_feature_status_tracker.h"
#include "ash/quick_pair/feature_status_tracker/quick_pair_feature_status_tracker.h"
#include "ash/quick_pair/message_stream/fake_message_stream_lookup.h"
#include "ash/quick_pair/message_stream/message_stream_lookup.h"
#include "ash/quick_pair/pairing/fake_retroactive_pairing_detector.h"
#include "ash/quick_pair/pairing/mock_pairer_broker.h"
#include "ash/quick_pair/pairing/pairer_broker.h"
#include "ash/quick_pair/pairing/retroactive_pairing_detector.h"
#include "ash/quick_pair/repository/fast_pair_repository.h"
#include "ash/quick_pair/scanning/mock_scanner_broker.h"
#include "ash/quick_pair/scanning/scanner_broker.h"
Expand Down Expand Up @@ -48,6 +52,12 @@ class MediatorTest : public testing::Test {
mock_scanner_broker_ =
static_cast<MockScannerBroker*>(scanner_broker.get());

std::unique_ptr<RetroactivePairingDetector> retroactive_pairing_detector =
std::make_unique<FakeRetroactivePairingDetector>();
fake_retroactive_pairing_detector_ =
static_cast<FakeRetroactivePairingDetector*>(
retroactive_pairing_detector.get());

std::unique_ptr<PairerBroker> pairer_broker =
std::make_unique<MockPairerBroker>();
mock_pairer_broker_ = static_cast<MockPairerBroker*>(pairer_broker.get());
Expand All @@ -56,7 +66,9 @@ class MediatorTest : public testing::Test {
mock_ui_broker_ = static_cast<MockUIBroker*>(ui_broker.get());

mediator_ = std::make_unique<Mediator>(
std::move(tracker), std::move(scanner_broker), std::move(pairer_broker),
std::move(tracker), std::move(scanner_broker),
std::move(retroactive_pairing_detector),
std::make_unique<FakeMessageStreamLookup>(), std::move(pairer_broker),
std::move(ui_broker), std::unique_ptr<FastPairRepository>(),
std::make_unique<QuickPairProcessManagerImpl>());

Expand All @@ -68,6 +80,7 @@ class MediatorTest : public testing::Test {
scoped_refptr<Device> device_;
FakeFeatureStatusTracker* feature_status_tracker_;
MockScannerBroker* mock_scanner_broker_;
FakeRetroactivePairingDetector* fake_retroactive_pairing_detector_;
MockPairerBroker* mock_pairer_broker_;
MockUIBroker* mock_ui_broker_;
std::unique_ptr<Mediator> mediator_;
Expand Down Expand Up @@ -105,6 +118,12 @@ TEST_F(MediatorTest, NotifyPairFailure) {
device_, PairFailure::kKeyBasedPairingCharacteristicDiscovery);
}

TEST_F(MediatorTest, InvokesShowAssociateAccount) {
feature_status_tracker_->SetIsFastPairEnabled(true);
EXPECT_CALL(*mock_ui_broker_, ShowAssociateAccount);
fake_retroactive_pairing_detector_->NotifyRetroactivePairFound(device_);
}

TEST_F(MediatorTest, RemoveNotificationOnPaired) {
feature_status_tracker_->SetIsFastPairEnabled(true);
EXPECT_CALL(*mock_ui_broker_, RemoveNotifications);
Expand Down
4 changes: 4 additions & 0 deletions ash/quick_pair/pairing/BUILD.gn
Expand Up @@ -52,15 +52,19 @@ static_library("test_support") {
testonly = true

sources = [
"fake_retroactive_pairing_detector.cc",
"fake_retroactive_pairing_detector.h",
"mock_pairer_broker.cc",
"mock_pairer_broker.h",
"pairer_broker.h",
"retroactive_pairing_detector.h",
]

deps = [
"//ash/quick_pair/common",
"//base",
"//base/test:test_support",
"//device/bluetooth",
"//testing/gtest",
]
}
Expand Down
31 changes: 31 additions & 0 deletions ash/quick_pair/pairing/fake_retroactive_pairing_detector.cc
@@ -0,0 +1,31 @@
// Copyright 2021 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/quick_pair/pairing/fake_retroactive_pairing_detector.h"

#include "ash/quick_pair/common/device.h"

namespace ash {
namespace quick_pair {

FakeRetroactivePairingDetector::FakeRetroactivePairingDetector() = default;

FakeRetroactivePairingDetector::~FakeRetroactivePairingDetector() = default;

void FakeRetroactivePairingDetector::AddObserver(Observer* observer) {
observers_.AddObserver(observer);
}

void FakeRetroactivePairingDetector::RemoveObserver(Observer* observer) {
observers_.RemoveObserver(observer);
}

void FakeRetroactivePairingDetector::NotifyRetroactivePairFound(
scoped_refptr<Device> device) {
for (auto& obs : observers_)
obs.OnRetroactivePairFound(device);
}

} // namespace quick_pair
} // namespace ash
37 changes: 37 additions & 0 deletions ash/quick_pair/pairing/fake_retroactive_pairing_detector.h
@@ -0,0 +1,37 @@
// Copyright 2021 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.

#ifndef ASH_QUICK_PAIR_PAIRING_FAKE_RETROACTIVE_PAIRING_DETECTOR_H_
#define ASH_QUICK_PAIR_PAIRING_FAKE_RETROACTIVE_PAIRING_DETECTOR_H_

#include "ash/quick_pair/pairing/retroactive_pairing_detector.h"
#include "base/observer_list.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace ash {
namespace quick_pair {

struct Device;

class FakeRetroactivePairingDetector : public RetroactivePairingDetector {
public:
FakeRetroactivePairingDetector();
FakeRetroactivePairingDetector(const FakeRetroactivePairingDetector&) =
delete;
FakeRetroactivePairingDetector& operator=(
const FakeRetroactivePairingDetector&) = delete;
~FakeRetroactivePairingDetector() override;

void AddObserver(Observer* observer) override;
void RemoveObserver(Observer* observer) override;
void NotifyRetroactivePairFound(scoped_refptr<Device> device);

private:
base::ObserverList<Observer> observers_;
};

} // namespace quick_pair
} // namespace ash

#endif // ASH_QUICK_PAIR_PAIRING_FAKE_RETROACTIVE_PAIRING_DETECTOR_H_
Expand Up @@ -15,6 +15,7 @@
#include "base/containers/contains.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/time/time.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "device/bluetooth/bluetooth_adapter_factory.h"
#include "device/bluetooth/bluetooth_device.h"
Expand Down
1 change: 1 addition & 0 deletions ash/quick_pair/pairing/retroactive_pairing_detector_impl.h
Expand Up @@ -21,6 +21,7 @@
#include "base/memory/weak_ptr.h"
#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "base/timer/timer.h"
#include "device/bluetooth/bluetooth_adapter.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

Expand Down
6 changes: 3 additions & 3 deletions ash/quick_pair/ui/fast_pair/fast_pair_presenter.cc
Expand Up @@ -38,9 +38,9 @@ void FastPairPresenter::ShowDiscovery(scoped_refptr<Device> device,
const auto metadata_id = device->metadata_id;
FastPairRepository::Get()->GetDeviceMetadata(
metadata_id,
base::BindOnce(&FastPairPresenter::OnDiscoveryMetadataRetrieved,
weak_pointer_factory_.GetWeakPtr(), std::move(device),
std::move(callback)));
base::BindRepeating(&FastPairPresenter::OnDiscoveryMetadataRetrieved,
weak_pointer_factory_.GetWeakPtr(), std::move(device),
std::move(callback)));
}

void FastPairPresenter::OnDiscoveryMetadataRetrieved(
Expand Down

0 comments on commit c4fb9f2

Please sign in to comment.