client_side_phishing_component_loader_policy.cc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #include "weblayer/browser/component_updater/client_side_phishing_component_loader_policy.h"
  5. #include <stdint.h>
  6. #include <memory>
  7. #include <string>
  8. #include <utility>
  9. #include <vector>
  10. #include "base/check.h"
  11. #include "base/containers/flat_map.h"
  12. #include "base/files/file.h"
  13. #include "base/files/file_util.h"
  14. #include "base/files/scoped_file.h"
  15. #include "base/location.h"
  16. #include "base/task/task_traits.h"
  17. #include "base/task/thread_pool.h"
  18. #include "base/values.h"
  19. #include "base/version.h"
  20. #include "components/component_updater/android/component_loader_policy.h"
  21. #include "components/component_updater/installer_policies/client_side_phishing_component_installer_policy.h"
  22. #include "components/safe_browsing/content/browser/client_side_phishing_model.h"
  23. #include "weblayer/common/features.h"
  24. namespace weblayer {
  25. namespace {
  26. // Persisted to logs, should never change.
  27. constexpr char kClientSidePhishingComponentMetricsSuffix[] =
  28. "ClientSidePhishing";
  29. void LoadFromDisk(base::ScopedFD pb_fd, base::ScopedFD visual_tflite_model_fd) {
  30. std::string binary_pb;
  31. base::ScopedFILE pb_file_stream(
  32. base::FileToFILE(base::File(std::move(pb_fd)), "r"));
  33. if (!base::ReadStreamToString(pb_file_stream.get(), &binary_pb))
  34. binary_pb.clear();
  35. base::File visual_tflite_model(std::move(visual_tflite_model_fd),
  36. base::File::FLAG_OPEN | base::File::FLAG_READ);
  37. // The ClientSidePhishingModel singleton will react appropriately if the
  38. // |binary_pb| is empty or |visual_tflite_model| is invalid.
  39. safe_browsing::ClientSidePhishingModel::GetInstance()
  40. ->PopulateFromDynamicUpdate(binary_pb, std::move(visual_tflite_model));
  41. }
  42. } // namespace
  43. void ClientSidePhishingComponentLoaderPolicy::ComponentLoaded(
  44. const base::Version& version,
  45. base::flat_map<std::string, base::ScopedFD>& fd_map,
  46. std::unique_ptr<base::DictionaryValue> manifest) {
  47. DCHECK(version.IsValid());
  48. auto pb_iterator =
  49. fd_map.find(component_updater::kClientModelBinaryPbFileName);
  50. if (pb_iterator == fd_map.end())
  51. return;
  52. auto visual_tflite_model_iterator =
  53. fd_map.find(component_updater::kVisualTfLiteModelFileName);
  54. base::ThreadPool::PostTask(
  55. FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
  56. base::BindOnce(&LoadFromDisk, std::move(pb_iterator->second),
  57. visual_tflite_model_iterator == fd_map.end()
  58. ? base::ScopedFD()
  59. : std::move(visual_tflite_model_iterator->second)));
  60. }
  61. void ClientSidePhishingComponentLoaderPolicy::ComponentLoadFailed(
  62. component_updater::ComponentLoadResult /*error*/) {}
  63. void ClientSidePhishingComponentLoaderPolicy::GetHash(
  64. std::vector<uint8_t>* hash) const {
  65. component_updater::ClientSidePhishingComponentInstallerPolicy::GetPublicHash(
  66. hash);
  67. }
  68. std::string ClientSidePhishingComponentLoaderPolicy::GetMetricsSuffix() const {
  69. return kClientSidePhishingComponentMetricsSuffix;
  70. }
  71. void LoadClientSidePhishingComponent(
  72. component_updater::ComponentLoaderPolicyVector& policies) {
  73. if (!base::FeatureList::IsEnabled(
  74. weblayer::features::kWebLayerClientSidePhishingDetection)) {
  75. return;
  76. }
  77. policies.push_back(
  78. std::make_unique<ClientSidePhishingComponentLoaderPolicy>());
  79. }
  80. } // namespace weblayer