secure_message_delegate_impl.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // Copyright 2015 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 ASH_COMPONENTS_MULTIDEVICE_SECURE_MESSAGE_DELEGATE_IMPL_H_
  5. #define ASH_COMPONENTS_MULTIDEVICE_SECURE_MESSAGE_DELEGATE_IMPL_H_
  6. #include "ash/components/multidevice/secure_message_delegate.h"
  7. #include "base/memory/weak_ptr.h"
  8. namespace ash {
  9. class EasyUnlockClient;
  10. namespace multidevice {
  11. // Concrete SecureMessageDelegate implementation.
  12. // Note: Callbacks are guaranteed to *not* be invoked after
  13. // SecureMessageDelegateImpl is destroyed.
  14. class SecureMessageDelegateImpl : public SecureMessageDelegate {
  15. public:
  16. class Factory {
  17. public:
  18. static std::unique_ptr<SecureMessageDelegate> Create();
  19. static void SetFactoryForTesting(Factory* test_factory);
  20. protected:
  21. virtual ~Factory();
  22. virtual std::unique_ptr<SecureMessageDelegate> CreateInstance() = 0;
  23. private:
  24. static Factory* test_factory_instance_;
  25. };
  26. SecureMessageDelegateImpl(const SecureMessageDelegateImpl&) = delete;
  27. SecureMessageDelegateImpl& operator=(const SecureMessageDelegateImpl&) =
  28. delete;
  29. ~SecureMessageDelegateImpl() override;
  30. // SecureMessageDelegate:
  31. void GenerateKeyPair(GenerateKeyPairCallback callback) override;
  32. void DeriveKey(const std::string& private_key,
  33. const std::string& public_key,
  34. DeriveKeyCallback callback) override;
  35. void CreateSecureMessage(const std::string& payload,
  36. const std::string& key,
  37. const CreateOptions& create_options,
  38. CreateSecureMessageCallback callback) override;
  39. void UnwrapSecureMessage(const std::string& serialized_message,
  40. const std::string& key,
  41. const UnwrapOptions& unwrap_options,
  42. UnwrapSecureMessageCallback callback) override;
  43. private:
  44. SecureMessageDelegateImpl();
  45. // Processes results returned from the dbus client, if necessary, and invokes
  46. // the SecureMessageDelegate callbacks with the processed results. Note: When
  47. // invoking dbus client methods, we bind these functions to a weak pointer to
  48. // ensure that these functions are not called after the
  49. // SecureMessageDelegateImpl object is destroyed.
  50. void OnGenerateKeyPairResult(GenerateKeyPairCallback callback,
  51. const std::string& private_key,
  52. const std::string& public_key);
  53. void OnDeriveKeyResult(DeriveKeyCallback callback,
  54. const std::string& derived_key);
  55. void OnCreateSecureMessageResult(CreateSecureMessageCallback callback,
  56. const std::string& secure_message);
  57. void OnUnwrapSecureMessageResult(UnwrapSecureMessageCallback callback,
  58. const std::string& unwrap_result);
  59. // Not owned by this instance.
  60. EasyUnlockClient* dbus_client_;
  61. base::WeakPtrFactory<SecureMessageDelegateImpl> weak_ptr_factory_{this};
  62. };
  63. } // namespace multidevice
  64. } // namespace ash
  65. #endif // ASH_COMPONENTS_MULTIDEVICE_SECURE_MESSAGE_DELEGATE_IMPL_H_