blob: 8009a6d3c14a07705519aa52fc5dc1c791f2b41a [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"
14#include "components/update_client/update_client_errors.h"
15#include "content/public/browser/browser_task_traits.h"
16#include "crypto/sha2.h"
17
18using content::BrowserThread;
19
20namespace component_updater {
21
22namespace {
23
24// The SHA256 of the SubjectPublicKeyInfo used to sign the archive.
25// TODO(evliu): The SODA library isn't ready to be exposed to the public yet so
26// we should not check in the SHA256.
27const uint8_t kSODAPublicKeySHA256[32] = {
28 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
29 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
30 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
31
32const base::FilePath::CharType kSODABinaryFileName[] =
33 FILE_PATH_LITERAL("SODAFiles/libsoda.experimental.so");
34
35static_assert(base::size(kSODAPublicKeySHA256) == crypto::kSHA256Length,
36 "Wrong hash length");
37
38const char kSODAManifestName[] = "SODA Library";
39
40} // namespace
41
42SODAComponentInstallerPolicy::SODAComponentInstallerPolicy(
43 const OnSODAComponentReadyCallback& callback)
44 : on_component_ready_callback_(callback) {}
45
46SODAComponentInstallerPolicy::~SODAComponentInstallerPolicy() = default;
47
48const std::string SODAComponentInstallerPolicy::GetExtensionId() {
49 return crx_file::id_util::GenerateIdFromHash(kSODAPublicKeySHA256,
50 sizeof(kSODAPublicKeySHA256));
51}
52
53void SODAComponentInstallerPolicy::UpdateSODAComponentOnDemand() {
54 const std::string crx_id =
55 component_updater::SODAComponentInstallerPolicy::GetExtensionId();
56 g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate(
57 crx_id, component_updater::OnDemandUpdater::Priority::FOREGROUND,
58 base::BindOnce([](update_client::Error error) {
59 if (error != update_client::Error::NONE &&
60 error != update_client::Error::UPDATE_IN_PROGRESS)
61 LOG(ERROR) << "On demand update of the SODA component failed "
62 "with error: "
63 << static_cast<int>(error);
64 }));
65}
66
67bool SODAComponentInstallerPolicy::VerifyInstallation(
68 const base::DictionaryValue& manifest,
69 const base::FilePath& install_dir) const {
70 return base::PathExists(install_dir.Append(kSODABinaryFileName));
71}
72
73bool SODAComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates()
74 const {
75 return true;
76}
77
78bool SODAComponentInstallerPolicy::RequiresNetworkEncryption() const {
79 return false;
80}
81
82update_client::CrxInstaller::Result
83SODAComponentInstallerPolicy::OnCustomInstall(
84 const base::DictionaryValue& manifest,
85 const base::FilePath& install_dir) {
86 return update_client::CrxInstaller::Result(0); // Nothing custom here.
87}
88
89void SODAComponentInstallerPolicy::OnCustomUninstall() {}
90
91void SODAComponentInstallerPolicy::ComponentReady(
92 const base::Version& version,
93 const base::FilePath& install_dir,
94 std::unique_ptr<base::DictionaryValue> manifest) {
95 VLOG(1) << "Component ready, version " << version.GetString() << " in "
96 << install_dir.value();
97
98 on_component_ready_callback_.Run(install_dir);
99}
100
101base::FilePath SODAComponentInstallerPolicy::GetRelativeInstallDir() const {
102 return base::FilePath(FILE_PATH_LITERAL("SODA"));
103}
104
105void SODAComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
106 hash->assign(kSODAPublicKeySHA256,
107 kSODAPublicKeySHA256 + base::size(kSODAPublicKeySHA256));
108}
109
110std::string SODAComponentInstallerPolicy::GetName() const {
111 return kSODAManifestName;
112}
113
114update_client::InstallerAttributes
115SODAComponentInstallerPolicy::GetInstallerAttributes() const {
116 return update_client::InstallerAttributes();
117}
118
119std::vector<std::string> SODAComponentInstallerPolicy::GetMimeTypes() const {
120 return std::vector<std::string>();
121}
122
123void RegisterSODAComponent(ComponentUpdateService* cus,
124 PrefService* prefs,
125 base::OnceClosure callback) {
126 DCHECK_CURRENTLY_ON(BrowserThread::UI);
127 // TODO(evliu): The SODA library isn't ready to be exposed to the public yet
128 // we should not register the component.
129 NOTIMPLEMENTED();
130}
131} // namespace component_updater