safety_tips_component_installer.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 "components/component_updater/installer_policies/safety_tips_component_installer.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "base/bind.h"
  8. #include "base/callback.h"
  9. #include "base/files/file_util.h"
  10. #include "base/logging.h"
  11. #include "base/memory/ref_counted.h"
  12. #include "base/task/thread_pool.h"
  13. #include "components/reputation/core/safety_tips.pb.h"
  14. #include "components/reputation/core/safety_tips_config.h"
  15. using component_updater::ComponentUpdateService;
  16. namespace {
  17. const base::FilePath::CharType kSafetyTipsConfigBinaryPbFileName[] =
  18. FILE_PATH_LITERAL("safety_tips.pb");
  19. // The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
  20. // The extension id is: jflookgnkcckhobaglndicnbbgbonegd
  21. const uint8_t kSafetyTipsPublicKeySHA256[32] = {
  22. 0x95, 0xbe, 0xea, 0x6d, 0xa2, 0x2a, 0x7e, 0x10, 0x6b, 0xd3, 0x82,
  23. 0xd1, 0x16, 0x1e, 0xd4, 0x63, 0x21, 0xfe, 0x79, 0x5d, 0x02, 0x30,
  24. 0xc2, 0xcf, 0x4a, 0x9c, 0x8a, 0x39, 0xcc, 0x4a, 0x00, 0xce};
  25. std::unique_ptr<reputation::SafetyTipsConfig> LoadSafetyTipsProtoFromDisk(
  26. const base::FilePath& pb_path) {
  27. std::string binary_pb;
  28. if (!base::ReadFileToString(pb_path, &binary_pb)) {
  29. // The file won't exist on new installations, so this is not always an
  30. // error.
  31. DVLOG(1) << "Failed reading from " << pb_path.value();
  32. return nullptr;
  33. }
  34. auto proto = std::make_unique<reputation::SafetyTipsConfig>();
  35. if (!proto->ParseFromString(binary_pb)) {
  36. DVLOG(1) << "Failed parsing proto " << pb_path.value();
  37. return nullptr;
  38. }
  39. return proto;
  40. }
  41. } // namespace
  42. namespace component_updater {
  43. SafetyTipsComponentInstallerPolicy::SafetyTipsComponentInstallerPolicy() =
  44. default;
  45. SafetyTipsComponentInstallerPolicy::~SafetyTipsComponentInstallerPolicy() =
  46. default;
  47. bool SafetyTipsComponentInstallerPolicy::
  48. SupportsGroupPolicyEnabledComponentUpdates() const {
  49. return true;
  50. }
  51. bool SafetyTipsComponentInstallerPolicy::RequiresNetworkEncryption() const {
  52. return false;
  53. }
  54. update_client::CrxInstaller::Result
  55. SafetyTipsComponentInstallerPolicy::OnCustomInstall(
  56. const base::Value& /* manifest */,
  57. const base::FilePath& /* install_dir */) {
  58. return update_client::CrxInstaller::Result(0); // Nothing custom here.
  59. }
  60. void SafetyTipsComponentInstallerPolicy::OnCustomUninstall() {}
  61. base::FilePath SafetyTipsComponentInstallerPolicy::GetInstalledPath(
  62. const base::FilePath& base) {
  63. return base.Append(kSafetyTipsConfigBinaryPbFileName);
  64. }
  65. void SafetyTipsComponentInstallerPolicy::ComponentReady(
  66. const base::Version& version,
  67. const base::FilePath& install_dir,
  68. base::Value /* manifest */) {
  69. DVLOG(1) << "Component ready, version " << version.GetString() << " in "
  70. << install_dir.value();
  71. const base::FilePath pb_path = GetInstalledPath(install_dir);
  72. if (pb_path.empty())
  73. return;
  74. // The default proto will always be a placeholder since the updated versions
  75. // are not checked in to the repo. Simply load whatever the component updater
  76. // gave us without checking the default proto from the resource bundle.
  77. base::ThreadPool::PostTaskAndReplyWithResult(
  78. FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
  79. base::BindOnce(&LoadSafetyTipsProtoFromDisk, pb_path),
  80. base::BindOnce(&reputation::SetSafetyTipsRemoteConfigProto));
  81. }
  82. // Called during startup and installation before ComponentReady().
  83. bool SafetyTipsComponentInstallerPolicy::VerifyInstallation(
  84. const base::Value& /* manifest */,
  85. const base::FilePath& install_dir) const {
  86. // No need to actually validate the proto here, since we'll do the checking
  87. // in PopulateFromDynamicUpdate().
  88. return base::PathExists(GetInstalledPath(install_dir));
  89. }
  90. base::FilePath SafetyTipsComponentInstallerPolicy::GetRelativeInstallDir()
  91. const {
  92. return base::FilePath(FILE_PATH_LITERAL("SafetyTips"));
  93. }
  94. void SafetyTipsComponentInstallerPolicy::GetHash(
  95. std::vector<uint8_t>* hash) const {
  96. hash->assign(
  97. kSafetyTipsPublicKeySHA256,
  98. kSafetyTipsPublicKeySHA256 + std::size(kSafetyTipsPublicKeySHA256));
  99. }
  100. std::string SafetyTipsComponentInstallerPolicy::GetName() const {
  101. return "Safety Tips";
  102. }
  103. update_client::InstallerAttributes
  104. SafetyTipsComponentInstallerPolicy::GetInstallerAttributes() const {
  105. return update_client::InstallerAttributes();
  106. }
  107. void RegisterSafetyTipsComponent(ComponentUpdateService* cus) {
  108. DVLOG(1) << "Registering Safety Tips component.";
  109. auto installer = base::MakeRefCounted<ComponentInstaller>(
  110. std::make_unique<SafetyTipsComponentInstallerPolicy>());
  111. installer->Register(cus, base::OnceClosure());
  112. }
  113. } // namespace component_updater