File: amount_extraction_heuristic_regexes_component_installer.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (145 lines) | stat: -rw-r--r-- 5,387 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/component_updater/installer_policies/amount_extraction_heuristic_regexes_component_installer.h"

#include <optional>
#include <vector>

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/task/thread_pool.h"
#include "base/values.h"
#include "components/autofill/core/browser/metrics/payments/amount_extraction_metrics.h"
#include "components/autofill/core/browser/payments/amount_extraction_heuristic_regexes.h"

namespace component_updater {
namespace {

const base::FilePath::CharType
    kAmountExtractionHeuristicRegexesBinaryPbFileName[] =
        FILE_PATH_LITERAL("heuristic_regexes.binarypb");

// The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
// The extension id is: hajigopbbjhghbfimgkfmpenfkclmohk
constexpr std::array<uint8_t, 32>
    kAmountExtractionHeuristicRegexesPublicKeySHA256 = {
        0x70, 0x98, 0x6e, 0xf1, 0x19, 0x76, 0x71, 0x58, 0xc6, 0xa5, 0xcf,
        0x4d, 0x5a, 0x2b, 0xce, 0x7a, 0x37, 0x44, 0x42, 0x77, 0x6c, 0x2d,
        0x2e, 0xa2, 0x9c, 0x08, 0x7a, 0x2c, 0x0f, 0x07, 0xe5, 0x89};

base::FilePath GetInstalledPath(const base::FilePath& base) {
  return base.Append(kAmountExtractionHeuristicRegexesBinaryPbFileName);
}

static std::optional<std::string> LoadAmountExtractionHeuristicRegexesFromDisk(
    const base::FilePath& pb_path) {
  std::string binary_pb;
  if (!base::ReadFileToString(pb_path, &binary_pb)) {
    autofill::autofill_metrics::LogAmountExtractionComponentInstallationResult(
        autofill::autofill_metrics::
            AmountExtractionComponentInstallationResult::
                kReadingBinaryFileFailed);
    return std::nullopt;
  }

  return binary_pb;
}

static void PopulateAmountExtractionHeuristicRegexesData(
    const base::FilePath& pb_path,
    std::optional<std::string> binary_pb) {
  if (!binary_pb || binary_pb.value().empty()) {
    autofill::autofill_metrics::LogAmountExtractionComponentInstallationResult(
        autofill::autofill_metrics::
            AmountExtractionComponentInstallationResult::kEmptyBinaryFile);
    return;
  }

  autofill::payments::AmountExtractionHeuristicRegexes::GetInstance()
      .PopulateStringFromComponent(std::move(*binary_pb));
}

}  // namespace

AmountExtractionHeuristicRegexesInstallerPolicy::
    AmountExtractionHeuristicRegexesInstallerPolicy() = default;

AmountExtractionHeuristicRegexesInstallerPolicy::
    ~AmountExtractionHeuristicRegexesInstallerPolicy() = default;

bool AmountExtractionHeuristicRegexesInstallerPolicy::
    SupportsGroupPolicyEnabledComponentUpdates() const {
  return true;
}

bool AmountExtractionHeuristicRegexesInstallerPolicy::
    RequiresNetworkEncryption() const {
  return false;
}

update_client::CrxInstaller::Result
AmountExtractionHeuristicRegexesInstallerPolicy::OnCustomInstall(
    const base::Value::Dict& /* manifest */,
    const base::FilePath& /* install_dir */) {
  return update_client::CrxInstaller::Result(0);  // Nothing custom here.
}

void AmountExtractionHeuristicRegexesInstallerPolicy::OnCustomUninstall() {}

void AmountExtractionHeuristicRegexesInstallerPolicy::ComponentReady(
    const base::Version& version,
    const base::FilePath& install_dir,
    base::Value::Dict /* manifest */) {
  const base::FilePath pb_path = GetInstalledPath(install_dir);

  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock(), base::TaskPriority::BEST_EFFORT},
      base::BindOnce(&LoadAmountExtractionHeuristicRegexesFromDisk, pb_path),
      base::BindOnce(&PopulateAmountExtractionHeuristicRegexesData, pb_path));
}

// Called during startup and installation before ComponentReady().
bool AmountExtractionHeuristicRegexesInstallerPolicy::VerifyInstallation(
    const base::Value::Dict& /* manifest */,
    const base::FilePath& install_dir) const {
  bool installation_result = base::PathExists(GetInstalledPath(install_dir));
  if (!installation_result) {
    autofill::autofill_metrics::LogAmountExtractionComponentInstallationResult(
        autofill::autofill_metrics::
            AmountExtractionComponentInstallationResult::
                kInvalidInstallationPath);
  }
  return installation_result;
}

base::FilePath
AmountExtractionHeuristicRegexesInstallerPolicy::GetRelativeInstallDir() const {
  return base::FilePath(FILE_PATH_LITERAL("AmountExtractionHeuristicRegexes"));
}

void AmountExtractionHeuristicRegexesInstallerPolicy::GetHash(
    std::vector<uint8_t>* hash) const {
  hash->assign(std::begin(kAmountExtractionHeuristicRegexesPublicKeySHA256),
               std::end(kAmountExtractionHeuristicRegexesPublicKeySHA256));
}

std::string AmountExtractionHeuristicRegexesInstallerPolicy::GetName() const {
  return "Amount Extraction Heuristic Regexes";
}

update_client::InstallerAttributes
AmountExtractionHeuristicRegexesInstallerPolicy::GetInstallerAttributes()
    const {
  return update_client::InstallerAttributes();
}

void RegisterAmountExtractionHeuristicRegexesComponent(
    ComponentUpdateService* cus) {
  auto installer = base::MakeRefCounted<ComponentInstaller>(
      std::make_unique<AmountExtractionHeuristicRegexesInstallerPolicy>());
  installer->Register(cus, base::OnceClosure());
}

}  // namespace component_updater