fido_device_discovery.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2017 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. #ifndef DEVICE_FIDO_FIDO_DEVICE_DISCOVERY_H_
  5. #define DEVICE_FIDO_FIDO_DEVICE_DISCOVERY_H_
  6. #include <functional>
  7. #include <map>
  8. #include <memory>
  9. #include <string>
  10. #include <utility>
  11. #include <vector>
  12. #include "base/bind.h"
  13. #include "base/component_export.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/strings/string_piece.h"
  16. #include "device/fido/fido_discovery_base.h"
  17. #include "device/fido/fido_transport_protocol.h"
  18. namespace device {
  19. class FidoDevice;
  20. class FidoDeviceAuthenticator;
  21. class COMPONENT_EXPORT(DEVICE_FIDO) FidoDeviceDiscovery
  22. : public FidoDiscoveryBase {
  23. public:
  24. // EventStream is an unbuffered pipe that can be passed around and late-bound
  25. // to the receiver.
  26. template <typename T>
  27. class EventStream {
  28. public:
  29. using Callback = base::RepeatingCallback<void(T)>;
  30. // New returns a callback for writing events, and ownership of an
  31. // |EventStream| that can be connected to in order to receive the events.
  32. // The callback may outlive the |EventStream|. Any events written when
  33. // either the |EventStream| has been deleted, or not yet connected, are
  34. // dropped.
  35. static std::pair<Callback, std::unique_ptr<EventStream<T>>> New() {
  36. auto stream = std::make_unique<EventStream<T>>();
  37. auto cb = base::BindRepeating(&EventStream::Transmit,
  38. stream->weak_factory_.GetWeakPtr());
  39. return std::make_pair(std::move(cb), std::move(stream));
  40. }
  41. void Connect(Callback connection) { connection_ = std::move(connection); }
  42. private:
  43. void Transmit(T t) {
  44. if (connection_) {
  45. connection_.Run(std::move(t));
  46. }
  47. }
  48. Callback connection_;
  49. base::WeakPtrFactory<EventStream<T>> weak_factory_{this};
  50. };
  51. enum class State {
  52. kIdle,
  53. kStarting,
  54. kRunning,
  55. kStopped,
  56. };
  57. FidoDeviceDiscovery(const FidoDeviceDiscovery&) = delete;
  58. FidoDeviceDiscovery& operator=(const FidoDeviceDiscovery&) = delete;
  59. ~FidoDeviceDiscovery() override;
  60. bool is_start_requested() const { return state_ != State::kIdle; }
  61. bool is_running() const { return state_ == State::kRunning; }
  62. std::vector<FidoDeviceAuthenticator*> GetAuthenticatorsForTesting();
  63. std::vector<const FidoDeviceAuthenticator*> GetAuthenticatorsForTesting()
  64. const;
  65. FidoDeviceAuthenticator* GetAuthenticatorForTesting(
  66. base::StringPiece authenticator_id);
  67. // FidoDiscoveryBase:
  68. void Start() override;
  69. bool MaybeStop() override;
  70. protected:
  71. explicit FidoDeviceDiscovery(FidoTransportProtocol transport);
  72. void NotifyDiscoveryStarted(bool success);
  73. // Convenience method that adds a FidoDeviceAuthenticator with the given
  74. // |device|.
  75. bool AddDevice(std::unique_ptr<FidoDevice> device);
  76. bool AddAuthenticator(std::unique_ptr<FidoDeviceAuthenticator> authenticator);
  77. bool RemoveDevice(base::StringPiece device_id);
  78. FidoDeviceAuthenticator* GetAuthenticator(base::StringPiece authenticator_id);
  79. // Subclasses should implement this to actually start the discovery when it is
  80. // requested.
  81. //
  82. // The implementation should asynchronously invoke NotifyDiscoveryStarted when
  83. // the discovery is s tarted.
  84. virtual void StartInternal() = 0;
  85. // Map of ID to authenticator. It is a guarantee to subclasses that the ID of
  86. // the authenticator equals the ID of the device.
  87. std::map<std::string, std::unique_ptr<FidoDeviceAuthenticator>, std::less<>>
  88. authenticators_;
  89. private:
  90. void NotifyAuthenticatorAdded(FidoAuthenticator* authenticator);
  91. void NotifyAuthenticatorRemoved(FidoAuthenticator* authenticator);
  92. State state_ = State::kIdle;
  93. base::WeakPtrFactory<FidoDeviceDiscovery> weak_factory_{this};
  94. };
  95. } // namespace device
  96. #endif // DEVICE_FIDO_FIDO_DEVICE_DISCOVERY_H_