reset_request_handler.cc 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 <utility>
  5. #include "base/bind.h"
  6. #include "base/check_op.h"
  7. #include "device/fido/fido_authenticator.h"
  8. #include "device/fido/fido_constants.h"
  9. #include "device/fido/pin.h"
  10. #include "device/fido/reset_request_handler.h"
  11. namespace device {
  12. ResetRequestHandler::ResetRequestHandler(
  13. const base::flat_set<FidoTransportProtocol>& supported_transports,
  14. ResetSentCallback reset_sent_callback,
  15. FinishedCallback finished_callback,
  16. std::unique_ptr<FidoDiscoveryFactory> fido_discovery_factory)
  17. : FidoRequestHandlerBase(fido_discovery_factory.get(),
  18. supported_transports),
  19. reset_sent_callback_(std::move(reset_sent_callback)),
  20. finished_callback_(std::move(finished_callback)),
  21. fido_discovery_factory_(std::move(fido_discovery_factory)) {
  22. Start();
  23. }
  24. ResetRequestHandler::~ResetRequestHandler() {
  25. DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
  26. }
  27. void ResetRequestHandler::DispatchRequest(FidoAuthenticator* authenticator) {
  28. DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
  29. authenticator->GetTouch(base::BindOnce(&ResetRequestHandler::OnTouch,
  30. weak_factory_.GetWeakPtr(),
  31. authenticator));
  32. }
  33. void ResetRequestHandler::OnTouch(FidoAuthenticator* authenticator) {
  34. DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
  35. if (processed_touch_) {
  36. return;
  37. }
  38. processed_touch_ = true;
  39. CancelActiveAuthenticators(authenticator->GetId());
  40. if (authenticator->SupportedProtocol() != ProtocolVersion::kCtap2) {
  41. std::move(finished_callback_)
  42. .Run(CtapDeviceResponseCode::kCtap1ErrInvalidCommand);
  43. return;
  44. }
  45. authenticator->Reset(base::BindOnce(&ResetRequestHandler::OnResetComplete,
  46. weak_factory_.GetWeakPtr()));
  47. std::move(reset_sent_callback_).Run();
  48. }
  49. void ResetRequestHandler::OnResetComplete(
  50. CtapDeviceResponseCode status,
  51. absl::optional<pin::EmptyResponse> response) {
  52. DCHECK_CALLED_ON_VALID_SEQUENCE(my_sequence_checker_);
  53. DCHECK(processed_touch_);
  54. std::move(finished_callback_).Run(status);
  55. }
  56. } // namespace device