remote_webauthn_message_handler.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef REMOTING_HOST_WEBAUTHN_REMOTE_WEBAUTHN_MESSAGE_HANDLER_H_
  5. #define REMOTING_HOST_WEBAUTHN_REMOTE_WEBAUTHN_MESSAGE_HANDLER_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include "base/containers/flat_map.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/thread_annotations.h"
  12. #include "mojo/public/cpp/bindings/pending_receiver.h"
  13. #include "mojo/public/cpp/bindings/receiver_set.h"
  14. #include "remoting/host/mojom/webauthn_proxy.mojom.h"
  15. #include "remoting/protocol/named_message_pipe_handler.h"
  16. namespace remoting {
  17. namespace protocol {
  18. class RemoteWebAuthn_CancelResponse;
  19. class RemoteWebAuthn_CreateResponse;
  20. class RemoteWebAuthn_GetResponse;
  21. class RemoteWebAuthn_IsUvpaaResponse;
  22. } // namespace protocol
  23. class RemoteWebAuthnStateChangeNotifier;
  24. class RemoteWebAuthnMessageHandler final
  25. : public mojom::WebAuthnProxy,
  26. public mojom::WebAuthnRequestCanceller,
  27. public protocol::NamedMessagePipeHandler {
  28. public:
  29. RemoteWebAuthnMessageHandler(
  30. const std::string& name,
  31. std::unique_ptr<protocol::MessagePipe> pipe,
  32. std::unique_ptr<RemoteWebAuthnStateChangeNotifier> state_change_notifier);
  33. RemoteWebAuthnMessageHandler(const RemoteWebAuthnMessageHandler&) = delete;
  34. RemoteWebAuthnMessageHandler& operator=(const RemoteWebAuthnMessageHandler&) =
  35. delete;
  36. ~RemoteWebAuthnMessageHandler() override;
  37. // protocol::NamedMessagePipeHandler implementation.
  38. void OnConnected() override;
  39. void OnIncomingMessage(std::unique_ptr<CompoundBuffer> message) override;
  40. void OnDisconnecting() override;
  41. // mojom::WebAuthnProxy implementation.
  42. void IsUserVerifyingPlatformAuthenticatorAvailable(
  43. IsUserVerifyingPlatformAuthenticatorAvailableCallback callback) override;
  44. void Create(
  45. const std::string& request_data,
  46. mojo::PendingReceiver<mojom::WebAuthnRequestCanceller> request_canceller,
  47. CreateCallback callback) override;
  48. void Get(
  49. const std::string& request_data,
  50. mojo::PendingReceiver<mojom::WebAuthnRequestCanceller> request_canceller,
  51. GetCallback callback) override;
  52. // mojom::WebAuthnRequestCanceller implementation.
  53. void Cancel(CancelCallback callback) override;
  54. void AddReceiver(mojo::PendingReceiver<mojom::WebAuthnProxy> receiver);
  55. void ClearReceivers();
  56. // Notifies the WebAuthn proxy extension that the availablitiy of WebAuthn
  57. // proxying may have changed.
  58. void NotifyWebAuthnStateChange();
  59. base::WeakPtr<RemoteWebAuthnMessageHandler> GetWeakPtr();
  60. private:
  61. template <typename CallbackType>
  62. using CallbackMap = base::flat_map<uint64_t, CallbackType>;
  63. friend class RemoteWebAuthnMessageHandlerTest;
  64. void OnReceiverDisconnected();
  65. void OnIsUvpaaResponse(
  66. uint64_t id,
  67. const protocol::RemoteWebAuthn_IsUvpaaResponse& response);
  68. void OnCreateResponse(
  69. uint64_t id,
  70. const protocol::RemoteWebAuthn_CreateResponse& response);
  71. void OnGetResponse(uint64_t id,
  72. const protocol::RemoteWebAuthn_GetResponse& response);
  73. void OnCancelResponse(
  74. uint64_t id,
  75. const protocol::RemoteWebAuthn_CancelResponse& response);
  76. uint64_t AssignNextMessageId();
  77. void AddRequestCanceller(
  78. uint64_t message_id,
  79. mojo::PendingReceiver<mojom::WebAuthnRequestCanceller> request_canceller);
  80. void RemoveRequestCancellerByMessageId(uint64_t message_id);
  81. void OnRequestCancellerDisconnected();
  82. SEQUENCE_CHECKER(sequence_checker_);
  83. std::unique_ptr<RemoteWebAuthnStateChangeNotifier> state_change_notifier_;
  84. mojo::ReceiverSet<mojom::WebAuthnProxy> receiver_set_;
  85. // message ID => mojo callback mappings.
  86. CallbackMap<IsUserVerifyingPlatformAuthenticatorAvailableCallback>
  87. is_uvpaa_callbacks_ GUARDED_BY_CONTEXT(sequence_checker_);
  88. CallbackMap<CreateCallback> create_callbacks_
  89. GUARDED_BY_CONTEXT(sequence_checker_);
  90. CallbackMap<GetCallback> get_callbacks_ GUARDED_BY_CONTEXT(sequence_checker_);
  91. CallbackMap<CancelCallback> cancel_callbacks_
  92. GUARDED_BY_CONTEXT(sequence_checker_);
  93. // The receiver context is the message ID associated with the request to be
  94. // canceled.
  95. mojo::ReceiverSet<mojom::WebAuthnRequestCanceller, uint64_t>
  96. request_cancellers_ GUARDED_BY_CONTEXT(sequence_checker_);
  97. base::flat_map<uint64_t, mojo::ReceiverId> message_id_to_request_canceller_
  98. GUARDED_BY_CONTEXT(sequence_checker_);
  99. uint64_t current_message_id_ GUARDED_BY_CONTEXT(sequence_checker_) = 0u;
  100. base::WeakPtrFactory<RemoteWebAuthnMessageHandler> weak_factory_{this};
  101. };
  102. } // namespace remoting
  103. #endif // REMOTING_HOST_WEBAUTHN_REMOTE_WEBAUTHN_MESSAGE_HANDLER_H_