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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
|
// 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 "chrome/browser/component_updater/translate_kit_component_installer.h"
#include <algorithm>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/values.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/on_device_translation/constants.h"
#include "chrome/browser/on_device_translation/pref_names.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h"
#include "components/services/on_device_translation/public/cpp/features.h"
#include "components/update_client/update_client_errors.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/sha2.h"
namespace component_updater {
namespace {
// The SHA256 of the SubjectPublicKeyInfo used to sign the component.
// The component id is: lbimbicckdokpoicboneldipejkhjgdg
constexpr uint8_t kTranslateKitPublicKeySHA256[32] = {
0xb1, 0x8c, 0x18, 0x22, 0xa3, 0xea, 0xfe, 0x82, 0x1e, 0xd4, 0xb3,
0x8f, 0x49, 0xa7, 0x96, 0x36, 0x55, 0xf3, 0xbc, 0x0d, 0xa5, 0x67,
0x48, 0x09, 0xcd, 0x7b, 0xa9, 0x5f, 0xd8, 0x7f, 0x53, 0xb4};
static_assert(std::size(kTranslateKitPublicKeySHA256) == crypto::kSHA256Length,
"Wrong hash length");
// The location of the libtranslatekit binary within the installation directory.
#if BUILDFLAG(IS_WIN)
constexpr base::FilePath::CharType kTranslateKitBinaryRelativePath[] =
FILE_PATH_LITERAL("TranslateKitFiles/libtranslatekit.dll");
#else
constexpr base::FilePath::CharType kTranslateKitBinaryRelativePath[] =
FILE_PATH_LITERAL("TranslateKitFiles/libtranslatekit.so");
#endif
// The manifest name of the TranslateKit component.
// This matches:
// - the manifest name in Automation.java from
// go/newchromecomponent#server-side-setup.
// - the display name at http://omaharelease/2134318/settings.
constexpr char kTranslateKitManifestName[] = "Chrome TranslateKit";
// Returns the full path where the libtranslatekit binary will be installed.
//
// The installation path is under
// <UserDataDir>/TranslateKit/lib/<version>/TranslateKitFiles/libtranslatekit.xx
// where <User Data Dir> can be determined by following the guide:
// https://chromium.googlesource.com/chromium/src.git/+/HEAD/docs/user_data_dir.md#current-location
base::FilePath GetInstalledPath(const base::FilePath& base) {
return base.Append(kTranslateKitBinaryRelativePath);
}
} // namespace
TranslateKitComponentInstallerPolicy::TranslateKitComponentInstallerPolicy(
PrefService* pref_service)
: pref_service_(pref_service) {}
TranslateKitComponentInstallerPolicy::~TranslateKitComponentInstallerPolicy() =
default;
bool TranslateKitComponentInstallerPolicy::VerifyInstallation(
const base::Value::Dict& manifest,
const base::FilePath& install_dir) const {
return base::PathExists(GetInstalledPath(install_dir));
}
bool TranslateKitComponentInstallerPolicy::
SupportsGroupPolicyEnabledComponentUpdates() const {
return true;
}
bool TranslateKitComponentInstallerPolicy::RequiresNetworkEncryption() const {
return false;
}
update_client::CrxInstaller::Result
TranslateKitComponentInstallerPolicy::OnCustomInstall(
const base::Value::Dict& manifest,
const base::FilePath& install_dir) {
// Nothing custom here.
return update_client::CrxInstaller::Result(0);
}
void TranslateKitComponentInstallerPolicy::OnCustomUninstall() {}
void TranslateKitComponentInstallerPolicy::ComponentReady(
const base::Version& version,
const base::FilePath& install_dir,
base::Value::Dict manifest) {
VLOG(1) << "Component ready, version " << version.GetString() << " in "
<< install_dir.value();
CHECK(pref_service_);
pref_service_->SetFilePath(prefs::kTranslateKitBinaryPath,
GetInstalledPath(install_dir));
}
base::FilePath TranslateKitComponentInstallerPolicy::GetRelativeInstallDir()
const {
return base::FilePath(
on_device_translation::kTranslateKitBinaryInstallationRelativeDir);
}
void TranslateKitComponentInstallerPolicy::GetHash(
std::vector<uint8_t>* hash) const {
hash->assign(std::begin(kTranslateKitPublicKeySHA256),
std::end(kTranslateKitPublicKeySHA256));
}
std::string TranslateKitComponentInstallerPolicy::GetName() const {
return kTranslateKitManifestName;
}
update_client::InstallerAttributes
TranslateKitComponentInstallerPolicy::GetInstallerAttributes() const {
update_client::InstallerAttributes installer_attributes;
#if defined(MEMORY_SANITIZER)
installer_attributes["is_msan"] = "true";
#endif // defined(MEMORY_SANITIZER)
return installer_attributes;
}
// static
void TranslateKitComponentInstallerPolicy::UpdateComponentOnDemand() {
auto crx_id = GetExtensionId();
g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate(
crx_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
base::BindOnce([](update_client::Error error) {
if (error != update_client::Error::NONE &&
error != update_client::Error::UPDATE_IN_PROGRESS) {
LOG(ERROR) << "Failed to uppdate TranslateKit:"
<< static_cast<int>(error);
}
}));
}
// static
const std::string TranslateKitComponentInstallerPolicy::GetExtensionId() {
return crx_file::id_util::GenerateIdFromHash(kTranslateKitPublicKeySHA256);
}
void RegisterTranslateKitComponent(ComponentUpdateService* cus,
PrefService* pref_service,
bool force_install,
base::OnceClosure registered_callback) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
VLOG(1) << "Registering TranslateKit component.";
if (!force_install &&
!pref_service->GetBoolean(prefs::kTranslateKitPreviouslyRegistered)) {
return;
}
// If the component is already installed, do nothing.
const std::vector<std::string> component_ids = cus->GetComponentIDs();
if (std::find(component_ids.begin(), component_ids.end(),
TranslateKitComponentInstallerPolicy::GetExtensionId()) !=
component_ids.end()) {
return;
}
pref_service->SetBoolean(prefs::kTranslateKitPreviouslyRegistered, true);
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<TranslateKitComponentInstallerPolicy>(pref_service));
installer->Register(cus, std::move(registered_callback));
}
} // namespace component_updater
|