trust_token_key_commitments_component_installer_policy.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2020 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 COMPONENTS_COMPONENT_UPDATER_INSTALLER_POLICIES_TRUST_TOKEN_KEY_COMMITMENTS_COMPONENT_INSTALLER_POLICY_H_
  5. #define COMPONENTS_COMPONENT_UPDATER_INSTALLER_POLICIES_TRUST_TOKEN_KEY_COMMITMENTS_COMPONENT_INSTALLER_POLICY_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/callback.h"
  12. #include "base/files/file_path.h"
  13. #include "base/gtest_prod_util.h"
  14. #include "base/values.h"
  15. #include "components/component_updater/component_installer.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. namespace base {
  18. class FilePath;
  19. } // namespace base
  20. namespace component_updater {
  21. // This file name must be in sync with the server-side configuration, or updates
  22. // will fail.
  23. const base::FilePath::CharType kTrustTokenKeyCommitmentsFileName[] =
  24. FILE_PATH_LITERAL("keys.json");
  25. // TrustTokenKeyCommitmentsComponentInstallerPolicy defines an installer
  26. // responsible for receiving updated Trust Tokens
  27. // (https://github.com/wicg/trust-token-api) key commitments and passing them to
  28. // the network service via Mojo.
  29. class TrustTokenKeyCommitmentsComponentInstallerPolicy
  30. : public ComponentInstallerPolicy {
  31. public:
  32. // |on_commitments_ready| will be called on the UI thread when
  33. // key commitments become ready; it's exposed for testing.
  34. explicit TrustTokenKeyCommitmentsComponentInstallerPolicy(
  35. base::RepeatingCallback<void(const std::string&)> on_commitments_ready);
  36. ~TrustTokenKeyCommitmentsComponentInstallerPolicy() override;
  37. TrustTokenKeyCommitmentsComponentInstallerPolicy(
  38. const TrustTokenKeyCommitmentsComponentInstallerPolicy&) = delete;
  39. TrustTokenKeyCommitmentsComponentInstallerPolicy& operator=(
  40. const TrustTokenKeyCommitmentsComponentInstallerPolicy&) = delete;
  41. // Returns the component's SHA2 hash as raw bytes.
  42. static void GetPublicKeyHash(std::vector<uint8_t>* hash);
  43. // Loads trust tokens from string using the given `load_keys_from_disk` call.
  44. //
  45. // static to allow sharing with the Android `ComponentLoaderPolicy`.
  46. //
  47. // `load_keys_from_disk` a callback that read trust tokens from file and
  48. // return them as an optional string. `on_commitments_ready` loads trust
  49. // tokens in network service.
  50. static void LoadTrustTokensFromString(
  51. base::OnceCallback<absl::optional<std::string>()> load_keys_from_disk,
  52. base::OnceCallback<void(const std::string&)> on_commitments_ready);
  53. protected:
  54. void GetHash(std::vector<uint8_t>* hash) const override;
  55. private:
  56. FRIEND_TEST_ALL_PREFIXES(TrustTokenKeyCommitmentsComponentInstallerTest,
  57. LoadsCommitments);
  58. FRIEND_TEST_ALL_PREFIXES(TrustTokenKeyCommitmentsComponentInstallerTest,
  59. LoadsCommitmentsFromOverriddenPath);
  60. // The following methods override ComponentInstallerPolicy.
  61. bool SupportsGroupPolicyEnabledComponentUpdates() const override;
  62. bool RequiresNetworkEncryption() const override;
  63. update_client::CrxInstaller::Result OnCustomInstall(
  64. const base::Value& manifest,
  65. const base::FilePath& install_dir) override;
  66. void OnCustomUninstall() override;
  67. bool VerifyInstallation(const base::Value& manifest,
  68. const base::FilePath& install_dir) const override;
  69. void ComponentReady(const base::Version& version,
  70. const base::FilePath& install_dir,
  71. base::Value manifest) override;
  72. base::FilePath GetRelativeInstallDir() const override;
  73. std::string GetName() const override;
  74. update_client::InstallerAttributes GetInstallerAttributes() const override;
  75. static base::FilePath GetInstalledPath(const base::FilePath& base);
  76. base::RepeatingCallback<void(const std::string&)> on_commitments_ready_;
  77. };
  78. } // namespace component_updater
  79. #endif // COMPONENTS_COMPONENT_UPDATER_INSTALLER_POLICIES_TRUST_TOKEN_KEY_COMMITMENTS_COMPONENT_INSTALLER_POLICY_H_