evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame^] | 1 | // 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 | |
| 18 | using content::BrowserThread; |
| 19 | |
| 20 | namespace component_updater { |
| 21 | |
| 22 | namespace { |
| 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. |
| 27 | const 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 | |
| 32 | const base::FilePath::CharType kSODABinaryFileName[] = |
| 33 | FILE_PATH_LITERAL("SODAFiles/libsoda.experimental.so"); |
| 34 | |
| 35 | static_assert(base::size(kSODAPublicKeySHA256) == crypto::kSHA256Length, |
| 36 | "Wrong hash length"); |
| 37 | |
| 38 | const char kSODAManifestName[] = "SODA Library"; |
| 39 | |
| 40 | } // namespace |
| 41 | |
| 42 | SODAComponentInstallerPolicy::SODAComponentInstallerPolicy( |
| 43 | const OnSODAComponentReadyCallback& callback) |
| 44 | : on_component_ready_callback_(callback) {} |
| 45 | |
| 46 | SODAComponentInstallerPolicy::~SODAComponentInstallerPolicy() = default; |
| 47 | |
| 48 | const std::string SODAComponentInstallerPolicy::GetExtensionId() { |
| 49 | return crx_file::id_util::GenerateIdFromHash(kSODAPublicKeySHA256, |
| 50 | sizeof(kSODAPublicKeySHA256)); |
| 51 | } |
| 52 | |
| 53 | void 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 | |
| 67 | bool SODAComponentInstallerPolicy::VerifyInstallation( |
| 68 | const base::DictionaryValue& manifest, |
| 69 | const base::FilePath& install_dir) const { |
| 70 | return base::PathExists(install_dir.Append(kSODABinaryFileName)); |
| 71 | } |
| 72 | |
| 73 | bool SODAComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates() |
| 74 | const { |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool SODAComponentInstallerPolicy::RequiresNetworkEncryption() const { |
| 79 | return false; |
| 80 | } |
| 81 | |
| 82 | update_client::CrxInstaller::Result |
| 83 | SODAComponentInstallerPolicy::OnCustomInstall( |
| 84 | const base::DictionaryValue& manifest, |
| 85 | const base::FilePath& install_dir) { |
| 86 | return update_client::CrxInstaller::Result(0); // Nothing custom here. |
| 87 | } |
| 88 | |
| 89 | void SODAComponentInstallerPolicy::OnCustomUninstall() {} |
| 90 | |
| 91 | void 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 | |
| 101 | base::FilePath SODAComponentInstallerPolicy::GetRelativeInstallDir() const { |
| 102 | return base::FilePath(FILE_PATH_LITERAL("SODA")); |
| 103 | } |
| 104 | |
| 105 | void SODAComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const { |
| 106 | hash->assign(kSODAPublicKeySHA256, |
| 107 | kSODAPublicKeySHA256 + base::size(kSODAPublicKeySHA256)); |
| 108 | } |
| 109 | |
| 110 | std::string SODAComponentInstallerPolicy::GetName() const { |
| 111 | return kSODAManifestName; |
| 112 | } |
| 113 | |
| 114 | update_client::InstallerAttributes |
| 115 | SODAComponentInstallerPolicy::GetInstallerAttributes() const { |
| 116 | return update_client::InstallerAttributes(); |
| 117 | } |
| 118 | |
| 119 | std::vector<std::string> SODAComponentInstallerPolicy::GetMimeTypes() const { |
| 120 | return std::vector<std::string>(); |
| 121 | } |
| 122 | |
| 123 | void 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 |