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" |
Abigail Klein | 82807c55 | 2020-04-06 19:58:07 | [diff] [blame^] | 10 | #include "build/build_config.h" |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 11 | #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" |
evliu | 415179ab | 2020-01-24 20:50:53 | [diff] [blame] | 15 | #include "components/soda/constants.h" |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 16 | #include "components/update_client/update_client_errors.h" |
| 17 | #include "content/public/browser/browser_task_traits.h" |
| 18 | #include "crypto/sha2.h" |
| 19 | |
| 20 | using content::BrowserThread; |
| 21 | |
| 22 | namespace component_updater { |
| 23 | |
| 24 | namespace { |
| 25 | |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 26 | // The SHA256 of the SubjectPublicKeyInfo used to sign the component. |
| 27 | // The component id is: icnkogojpkfjeajonkmlplionaamopkf |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 28 | const uint8_t kSODAPublicKeySHA256[32] = { |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 29 | 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}; |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 32 | |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 33 | static_assert(base::size(kSODAPublicKeySHA256) == crypto::kSHA256Length, |
| 34 | "Wrong hash length"); |
| 35 | |
| 36 | const char kSODAManifestName[] = "SODA Library"; |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | SODAComponentInstallerPolicy::SODAComponentInstallerPolicy( |
| 41 | const OnSODAComponentReadyCallback& callback) |
| 42 | : on_component_ready_callback_(callback) {} |
| 43 | |
| 44 | SODAComponentInstallerPolicy::~SODAComponentInstallerPolicy() = default; |
| 45 | |
| 46 | const std::string SODAComponentInstallerPolicy::GetExtensionId() { |
| 47 | return crx_file::id_util::GenerateIdFromHash(kSODAPublicKeySHA256, |
| 48 | sizeof(kSODAPublicKeySHA256)); |
| 49 | } |
| 50 | |
| 51 | void 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 | |
| 65 | bool SODAComponentInstallerPolicy::VerifyInstallation( |
| 66 | const base::DictionaryValue& manifest, |
| 67 | const base::FilePath& install_dir) const { |
evliu | 415179ab | 2020-01-24 20:50:53 | [diff] [blame] | 68 | return base::PathExists(install_dir.Append(soda::kSodaBinaryRelativePath)); |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | bool SODAComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates() |
| 72 | const { |
| 73 | return true; |
| 74 | } |
| 75 | |
| 76 | bool SODAComponentInstallerPolicy::RequiresNetworkEncryption() const { |
| 77 | return false; |
| 78 | } |
| 79 | |
| 80 | update_client::CrxInstaller::Result |
| 81 | SODAComponentInstallerPolicy::OnCustomInstall( |
| 82 | const base::DictionaryValue& manifest, |
| 83 | const base::FilePath& install_dir) { |
| 84 | return update_client::CrxInstaller::Result(0); // Nothing custom here. |
| 85 | } |
| 86 | |
| 87 | void SODAComponentInstallerPolicy::OnCustomUninstall() {} |
| 88 | |
| 89 | void 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 | |
| 99 | base::FilePath SODAComponentInstallerPolicy::GetRelativeInstallDir() const { |
evliu | 415179ab | 2020-01-24 20:50:53 | [diff] [blame] | 100 | return base::FilePath(soda::kSodaInstallationRelativePath); |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | void SODAComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const { |
| 104 | hash->assign(kSODAPublicKeySHA256, |
| 105 | kSODAPublicKeySHA256 + base::size(kSODAPublicKeySHA256)); |
| 106 | } |
| 107 | |
| 108 | std::string SODAComponentInstallerPolicy::GetName() const { |
| 109 | return kSODAManifestName; |
| 110 | } |
| 111 | |
| 112 | update_client::InstallerAttributes |
| 113 | SODAComponentInstallerPolicy::GetInstallerAttributes() const { |
| 114 | return update_client::InstallerAttributes(); |
| 115 | } |
| 116 | |
| 117 | std::vector<std::string> SODAComponentInstallerPolicy::GetMimeTypes() const { |
| 118 | return std::vector<std::string>(); |
| 119 | } |
| 120 | |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 121 | void UpdateSODAInstallDirPref(PrefService* prefs, |
| 122 | const base::FilePath& install_dir) { |
Abigail Klein | 82807c55 | 2020-04-06 19:58:07 | [diff] [blame^] | 123 | #if !defined(OS_ANDROID) |
evliu | 415179ab | 2020-01-24 20:50:53 | [diff] [blame] | 124 | prefs->SetFilePath(prefs::kSODAPath, |
| 125 | install_dir.Append(soda::kSodaBinaryRelativePath)); |
Abigail Klein | 82807c55 | 2020-04-06 19:58:07 | [diff] [blame^] | 126 | #endif |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 127 | } |
| 128 | |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 129 | void RegisterSODAComponent(ComponentUpdateService* cus, |
| 130 | PrefService* prefs, |
| 131 | base::OnceClosure callback) { |
| 132 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 133 | |
| 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)); |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 143 | } |
| 144 | } // namespace component_updater |