blob: c4d2e115f6c0e67f57e7e65757454d6661567c13 [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"
10#include "chrome/browser/browser_process.h"
11#include "chrome/common/pref_names.h"
12#include "components/component_updater/component_updater_service.h"
13#include "components/crx_file/id_util.h"
evliu415179ab2020-01-24 20:50:5314#include "components/soda/constants.h"
evliu7b9481b32019-12-20 19:39:5515#include "components/update_client/update_client_errors.h"
16#include "content/public/browser/browser_task_traits.h"
17#include "crypto/sha2.h"
18
19using content::BrowserThread;
20
21namespace component_updater {
22
23namespace {
24
evliu7be3c7d2020-01-10 20:17:0625// The SHA256 of the SubjectPublicKeyInfo used to sign the component.
26// The component id is: icnkogojpkfjeajonkmlplionaamopkf
evliu7b9481b32019-12-20 19:39:5527const uint8_t kSODAPublicKeySHA256[32] = {
evliu7be3c7d2020-01-10 20:17:0628 0x82, 0xda, 0xe6, 0xe9, 0xfa, 0x59, 0x40, 0x9e, 0xda, 0xcb, 0xfb,
29 0x8e, 0xd0, 0x0c, 0xef, 0xa5, 0xc0, 0x97, 0x00, 0x84, 0x1c, 0x21,
30 0xa6, 0xae, 0xc8, 0x1b, 0x87, 0xfb, 0x12, 0x27, 0x28, 0xb1};
evliu7b9481b32019-12-20 19:39:5531
evliu7b9481b32019-12-20 19:39:5532static_assert(base::size(kSODAPublicKeySHA256) == crypto::kSHA256Length,
33 "Wrong hash length");
34
35const char kSODAManifestName[] = "SODA Library";
36
37} // namespace
38
39SODAComponentInstallerPolicy::SODAComponentInstallerPolicy(
40 const OnSODAComponentReadyCallback& callback)
41 : on_component_ready_callback_(callback) {}
42
43SODAComponentInstallerPolicy::~SODAComponentInstallerPolicy() = default;
44
45const std::string SODAComponentInstallerPolicy::GetExtensionId() {
46 return crx_file::id_util::GenerateIdFromHash(kSODAPublicKeySHA256,
47 sizeof(kSODAPublicKeySHA256));
48}
49
50void SODAComponentInstallerPolicy::UpdateSODAComponentOnDemand() {
51 const std::string crx_id =
52 component_updater::SODAComponentInstallerPolicy::GetExtensionId();
53 g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate(
54 crx_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
55 base::BindOnce([](update_client::Error error) {
56 if (error != update_client::Error::NONE &&
57 error != update_client::Error::UPDATE_IN_PROGRESS)
58 LOG(ERROR) << "On demand update of the SODA component failed "
59 "with error: "
60 << static_cast<int>(error);
61 }));
62}
63
64bool SODAComponentInstallerPolicy::VerifyInstallation(
65 const base::DictionaryValue& manifest,
66 const base::FilePath& install_dir) const {
evliu415179ab2020-01-24 20:50:5367 return base::PathExists(install_dir.Append(soda::kSodaBinaryRelativePath));
evliu7b9481b32019-12-20 19:39:5568}
69
70bool SODAComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates()
71 const {
72 return true;
73}
74
75bool SODAComponentInstallerPolicy::RequiresNetworkEncryption() const {
76 return false;
77}
78
79update_client::CrxInstaller::Result
80SODAComponentInstallerPolicy::OnCustomInstall(
81 const base::DictionaryValue& manifest,
82 const base::FilePath& install_dir) {
83 return update_client::CrxInstaller::Result(0); // Nothing custom here.
84}
85
86void SODAComponentInstallerPolicy::OnCustomUninstall() {}
87
88void SODAComponentInstallerPolicy::ComponentReady(
89 const base::Version& version,
90 const base::FilePath& install_dir,
91 std::unique_ptr<base::DictionaryValue> manifest) {
92 VLOG(1) << "Component ready, version " << version.GetString() << " in "
93 << install_dir.value();
94
95 on_component_ready_callback_.Run(install_dir);
96}
97
98base::FilePath SODAComponentInstallerPolicy::GetRelativeInstallDir() const {
evliu415179ab2020-01-24 20:50:5399 return base::FilePath(soda::kSodaInstallationRelativePath);
evliu7b9481b32019-12-20 19:39:55100}
101
102void SODAComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
103 hash->assign(kSODAPublicKeySHA256,
104 kSODAPublicKeySHA256 + base::size(kSODAPublicKeySHA256));
105}
106
107std::string SODAComponentInstallerPolicy::GetName() const {
108 return kSODAManifestName;
109}
110
111update_client::InstallerAttributes
112SODAComponentInstallerPolicy::GetInstallerAttributes() const {
113 return update_client::InstallerAttributes();
114}
115
116std::vector<std::string> SODAComponentInstallerPolicy::GetMimeTypes() const {
117 return std::vector<std::string>();
118}
119
evliu7be3c7d2020-01-10 20:17:06120void UpdateSODAInstallDirPref(PrefService* prefs,
121 const base::FilePath& install_dir) {
evliu415179ab2020-01-24 20:50:53122 prefs->SetFilePath(prefs::kSODAPath,
123 install_dir.Append(soda::kSodaBinaryRelativePath));
evliu7be3c7d2020-01-10 20:17:06124}
125
evliu7b9481b32019-12-20 19:39:55126void RegisterSODAComponent(ComponentUpdateService* cus,
127 PrefService* prefs,
128 base::OnceClosure callback) {
129 DCHECK_CURRENTLY_ON(BrowserThread::UI);
evliu7be3c7d2020-01-10 20:17:06130
131 auto installer = base::MakeRefCounted<ComponentInstaller>(
132 std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating(
133 [](PrefService* prefs, const base::FilePath& install_dir) {
134 base::PostTask(
135 FROM_HERE, {BrowserThread::UI, base::TaskPriority::BEST_EFFORT},
136 base::BindOnce(&UpdateSODAInstallDirPref, prefs, install_dir));
137 },
138 prefs)));
139 installer->Register(cus, std::move(callback));
evliu7b9481b32019-12-20 19:39:55140}
141} // namespace component_updater