blob: e30d3085c0ada0ba104633bdad9340d516480269 [file] [log] [blame]
evliu7b9481b32019-12-20 19:39:551// 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
5#include "chrome/browser/component_updater/soda_component_installer.h"
6
7#include "base/bind.h"
8#include "base/files/file_util.h"
9#include "base/task/post_task.h"
Abigail Klein82807c552020-04-06 19:58:0710#include "build/build_config.h"
evliu7b9481b32019-12-20 19:39:5511#include "chrome/browser/browser_process.h"
12#include "chrome/common/pref_names.h"
13#include "components/component_updater/component_updater_service.h"
14#include "components/crx_file/id_util.h"
evliu415179ab2020-01-24 20:50:5315#include "components/soda/constants.h"
evliu7b9481b32019-12-20 19:39:5516#include "components/update_client/update_client_errors.h"
17#include "content/public/browser/browser_task_traits.h"
18#include "crypto/sha2.h"
19
20using content::BrowserThread;
21
22namespace component_updater {
23
24namespace {
25
evliu7be3c7d2020-01-10 20:17:0626// The SHA256 of the SubjectPublicKeyInfo used to sign the component.
27// The component id is: icnkogojpkfjeajonkmlplionaamopkf
evliu7b9481b32019-12-20 19:39:5528const uint8_t kSODAPublicKeySHA256[32] = {
evliu7be3c7d2020-01-10 20:17:0629 0x82, 0xda, 0xe6, 0xe9, 0xfa, 0x59, 0x40, 0x9e, 0xda, 0xcb, 0xfb,
30 0x8e, 0xd0, 0x0c, 0xef, 0xa5, 0xc0, 0x97, 0x00, 0x84, 0x1c, 0x21,
31 0xa6, 0xae, 0xc8, 0x1b, 0x87, 0xfb, 0x12, 0x27, 0x28, 0xb1};
evliu7b9481b32019-12-20 19:39:5532
evliu7b9481b32019-12-20 19:39:5533static_assert(base::size(kSODAPublicKeySHA256) == crypto::kSHA256Length,
34 "Wrong hash length");
35
36const char kSODAManifestName[] = "SODA Library";
37
38} // namespace
39
40SODAComponentInstallerPolicy::SODAComponentInstallerPolicy(
41 const OnSODAComponentReadyCallback& callback)
42 : on_component_ready_callback_(callback) {}
43
44SODAComponentInstallerPolicy::~SODAComponentInstallerPolicy() = default;
45
46const std::string SODAComponentInstallerPolicy::GetExtensionId() {
47 return crx_file::id_util::GenerateIdFromHash(kSODAPublicKeySHA256,
48 sizeof(kSODAPublicKeySHA256));
49}
50
51void SODAComponentInstallerPolicy::UpdateSODAComponentOnDemand() {
52 const std::string crx_id =
53 component_updater::SODAComponentInstallerPolicy::GetExtensionId();
54 g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate(
55 crx_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
56 base::BindOnce([](update_client::Error error) {
57 if (error != update_client::Error::NONE &&
58 error != update_client::Error::UPDATE_IN_PROGRESS)
59 LOG(ERROR) << "On demand update of the SODA component failed "
60 "with error: "
61 << static_cast<int>(error);
62 }));
63}
64
65bool SODAComponentInstallerPolicy::VerifyInstallation(
66 const base::DictionaryValue& manifest,
67 const base::FilePath& install_dir) const {
evliu415179ab2020-01-24 20:50:5368 return base::PathExists(install_dir.Append(soda::kSodaBinaryRelativePath));
evliu7b9481b32019-12-20 19:39:5569}
70
71bool SODAComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates()
72 const {
73 return true;
74}
75
76bool SODAComponentInstallerPolicy::RequiresNetworkEncryption() const {
77 return false;
78}
79
80update_client::CrxInstaller::Result
81SODAComponentInstallerPolicy::OnCustomInstall(
82 const base::DictionaryValue& manifest,
83 const base::FilePath& install_dir) {
84 return update_client::CrxInstaller::Result(0); // Nothing custom here.
85}
86
87void SODAComponentInstallerPolicy::OnCustomUninstall() {}
88
89void SODAComponentInstallerPolicy::ComponentReady(
90 const base::Version& version,
91 const base::FilePath& install_dir,
92 std::unique_ptr<base::DictionaryValue> manifest) {
93 VLOG(1) << "Component ready, version " << version.GetString() << " in "
94 << install_dir.value();
95
96 on_component_ready_callback_.Run(install_dir);
97}
98
99base::FilePath SODAComponentInstallerPolicy::GetRelativeInstallDir() const {
evliu415179ab2020-01-24 20:50:53100 return base::FilePath(soda::kSodaInstallationRelativePath);
evliu7b9481b32019-12-20 19:39:55101}
102
103void SODAComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
104 hash->assign(kSODAPublicKeySHA256,
105 kSODAPublicKeySHA256 + base::size(kSODAPublicKeySHA256));
106}
107
108std::string SODAComponentInstallerPolicy::GetName() const {
109 return kSODAManifestName;
110}
111
112update_client::InstallerAttributes
113SODAComponentInstallerPolicy::GetInstallerAttributes() const {
114 return update_client::InstallerAttributes();
115}
116
117std::vector<std::string> SODAComponentInstallerPolicy::GetMimeTypes() const {
118 return std::vector<std::string>();
119}
120
evliu7be3c7d2020-01-10 20:17:06121void UpdateSODAInstallDirPref(PrefService* prefs,
122 const base::FilePath& install_dir) {
Abigail Klein82807c552020-04-06 19:58:07123#if !defined(OS_ANDROID)
evliu415179ab2020-01-24 20:50:53124 prefs->SetFilePath(prefs::kSODAPath,
125 install_dir.Append(soda::kSodaBinaryRelativePath));
Abigail Klein82807c552020-04-06 19:58:07126#endif
evliu7be3c7d2020-01-10 20:17:06127}
128
evliu7b9481b32019-12-20 19:39:55129void RegisterSODAComponent(ComponentUpdateService* cus,
130 PrefService* prefs,
131 base::OnceClosure callback) {
132 DCHECK_CURRENTLY_ON(BrowserThread::UI);
evliu7be3c7d2020-01-10 20:17:06133
134 auto installer = base::MakeRefCounted<ComponentInstaller>(
135 std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating(
136 [](PrefService* prefs, const base::FilePath& install_dir) {
137 base::PostTask(
138 FROM_HERE, {BrowserThread::UI, base::TaskPriority::BEST_EFFORT},
139 base::BindOnce(&UpdateSODAInstallDirPref, prefs, install_dir));
140 },
141 prefs)));
142 installer->Register(cus, std::move(callback));
evliu7b9481b32019-12-20 19:39:55143}
144} // namespace component_updater