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" |
Abigail Klein | 82807c55 | 2020-04-06 19:58:07 | [diff] [blame] | 9 | #include "build/build_config.h" |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 10 | #include "chrome/browser/browser_process.h" |
| 11 | #include "chrome/common/pref_names.h" |
evliu | 8b3cee9 | 2020-07-28 17:17:21 | [diff] [blame] | 12 | #include "chrome/services/speech/buildflags.h" |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 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" |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 18 | #include "content/public/browser/browser_thread.h" |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 19 | #include "crypto/sha2.h" |
| 20 | |
| 21 | using content::BrowserThread; |
| 22 | |
| 23 | namespace component_updater { |
| 24 | |
| 25 | namespace { |
| 26 | |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 27 | // The SHA256 of the SubjectPublicKeyInfo used to sign the component. |
| 28 | // The component id is: icnkogojpkfjeajonkmlplionaamopkf |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 29 | const uint8_t kSODAPublicKeySHA256[32] = { |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 30 | 0x82, 0xda, 0xe6, 0xe9, 0xfa, 0x59, 0x40, 0x9e, 0xda, 0xcb, 0xfb, |
| 31 | 0x8e, 0xd0, 0x0c, 0xef, 0xa5, 0xc0, 0x97, 0x00, 0x84, 0x1c, 0x21, |
| 32 | 0xa6, 0xae, 0xc8, 0x1b, 0x87, 0xfb, 0x12, 0x27, 0x28, 0xb1}; |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 33 | |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 34 | static_assert(base::size(kSODAPublicKeySHA256) == crypto::kSHA256Length, |
| 35 | "Wrong hash length"); |
| 36 | |
| 37 | const char kSODAManifestName[] = "SODA Library"; |
| 38 | |
evliu | 14d897af | 2020-07-30 22:17:21 | [diff] [blame] | 39 | constexpr base::FilePath::CharType kSodaEnUsConfigFileRelativePath[] = |
| 40 | FILE_PATH_LITERAL("SODAFiles/en_us/dictation.ascii_proto"); |
| 41 | |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 42 | } // namespace |
| 43 | |
| 44 | SODAComponentInstallerPolicy::SODAComponentInstallerPolicy( |
Xida Chen | b38f3f76a | 2020-07-02 02:11:14 | [diff] [blame] | 45 | OnSODAComponentReadyCallback callback) |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 46 | : on_component_ready_callback_(callback) {} |
| 47 | |
| 48 | SODAComponentInstallerPolicy::~SODAComponentInstallerPolicy() = default; |
| 49 | |
| 50 | const std::string SODAComponentInstallerPolicy::GetExtensionId() { |
| 51 | return crx_file::id_util::GenerateIdFromHash(kSODAPublicKeySHA256, |
| 52 | sizeof(kSODAPublicKeySHA256)); |
| 53 | } |
| 54 | |
| 55 | void SODAComponentInstallerPolicy::UpdateSODAComponentOnDemand() { |
| 56 | const std::string crx_id = |
| 57 | component_updater::SODAComponentInstallerPolicy::GetExtensionId(); |
| 58 | g_browser_process->component_updater()->GetOnDemandUpdater().OnDemandUpdate( |
| 59 | crx_id, component_updater::OnDemandUpdater::Priority::FOREGROUND, |
| 60 | base::BindOnce([](update_client::Error error) { |
| 61 | if (error != update_client::Error::NONE && |
| 62 | error != update_client::Error::UPDATE_IN_PROGRESS) |
| 63 | LOG(ERROR) << "On demand update of the SODA component failed " |
| 64 | "with error: " |
| 65 | << static_cast<int>(error); |
| 66 | })); |
| 67 | } |
| 68 | |
| 69 | bool SODAComponentInstallerPolicy::VerifyInstallation( |
| 70 | const base::DictionaryValue& manifest, |
| 71 | const base::FilePath& install_dir) const { |
evliu | 2e5dbee4 | 2020-04-09 23:35:29 | [diff] [blame] | 72 | return base::PathExists(install_dir.Append(speech::kSodaBinaryRelativePath)); |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | bool SODAComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates() |
| 76 | const { |
| 77 | return true; |
| 78 | } |
| 79 | |
| 80 | bool SODAComponentInstallerPolicy::RequiresNetworkEncryption() const { |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | update_client::CrxInstaller::Result |
| 85 | SODAComponentInstallerPolicy::OnCustomInstall( |
| 86 | const base::DictionaryValue& manifest, |
| 87 | const base::FilePath& install_dir) { |
| 88 | return update_client::CrxInstaller::Result(0); // Nothing custom here. |
| 89 | } |
| 90 | |
| 91 | void SODAComponentInstallerPolicy::OnCustomUninstall() {} |
| 92 | |
| 93 | void SODAComponentInstallerPolicy::ComponentReady( |
| 94 | const base::Version& version, |
| 95 | const base::FilePath& install_dir, |
| 96 | std::unique_ptr<base::DictionaryValue> manifest) { |
| 97 | VLOG(1) << "Component ready, version " << version.GetString() << " in " |
| 98 | << install_dir.value(); |
| 99 | |
| 100 | on_component_ready_callback_.Run(install_dir); |
| 101 | } |
| 102 | |
| 103 | base::FilePath SODAComponentInstallerPolicy::GetRelativeInstallDir() const { |
evliu | 2e5dbee4 | 2020-04-09 23:35:29 | [diff] [blame] | 104 | return base::FilePath(speech::kSodaInstallationRelativePath); |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | void SODAComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const { |
| 108 | hash->assign(kSODAPublicKeySHA256, |
| 109 | kSODAPublicKeySHA256 + base::size(kSODAPublicKeySHA256)); |
| 110 | } |
| 111 | |
| 112 | std::string SODAComponentInstallerPolicy::GetName() const { |
| 113 | return kSODAManifestName; |
| 114 | } |
| 115 | |
| 116 | update_client::InstallerAttributes |
| 117 | SODAComponentInstallerPolicy::GetInstallerAttributes() const { |
| 118 | return update_client::InstallerAttributes(); |
| 119 | } |
| 120 | |
| 121 | std::vector<std::string> SODAComponentInstallerPolicy::GetMimeTypes() const { |
| 122 | return std::vector<std::string>(); |
| 123 | } |
| 124 | |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 125 | void UpdateSODAInstallDirPref(PrefService* prefs, |
| 126 | const base::FilePath& install_dir) { |
Abigail Klein | 82807c55 | 2020-04-06 19:58:07 | [diff] [blame] | 127 | #if !defined(OS_ANDROID) |
evliu | 14d897af | 2020-07-30 22:17:21 | [diff] [blame] | 128 | prefs->SetFilePath(prefs::kSodaBinaryPath, |
evliu | d498bd8 | 2020-08-12 19:06:02 | [diff] [blame] | 129 | install_dir.Append(speech::kSodaBinaryRelativePath)); |
evliu | 14d897af | 2020-07-30 22:17:21 | [diff] [blame] | 130 | prefs->SetFilePath(prefs::kSodaEnUsConfigPath, |
| 131 | install_dir.Append(kSodaEnUsConfigFileRelativePath)); |
Abigail Klein | 82807c55 | 2020-04-06 19:58:07 | [diff] [blame] | 132 | #endif |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 133 | } |
| 134 | |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 135 | void RegisterSODAComponent(ComponentUpdateService* cus, |
| 136 | PrefService* prefs, |
| 137 | base::OnceClosure callback) { |
| 138 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
evliu | 8b3cee9 | 2020-07-28 17:17:21 | [diff] [blame] | 139 | #if BUILDFLAG(ENABLE_SODA) |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 140 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
| 141 | std::make_unique<SODAComponentInstallerPolicy>(base::BindRepeating( |
evliu | 552d105 | 2020-06-24 16:27:46 | [diff] [blame] | 142 | [](ComponentUpdateService* cus, PrefService* prefs, |
| 143 | const base::FilePath& install_dir) { |
| 144 | if (prefs->GetBoolean(prefs::kLiveCaptionEnabled)) { |
| 145 | content::GetUIThreadTaskRunner({base::TaskPriority::BEST_EFFORT}) |
| 146 | ->PostTask(FROM_HERE, |
| 147 | base::BindOnce(&UpdateSODAInstallDirPref, prefs, |
| 148 | install_dir)); |
| 149 | } |
evliu | 7be3c7d | 2020-01-10 20:17:06 | [diff] [blame] | 150 | }, |
evliu | 552d105 | 2020-06-24 16:27:46 | [diff] [blame] | 151 | cus, prefs))); |
| 152 | |
| 153 | if (prefs->GetBoolean(prefs::kLiveCaptionEnabled)) { |
| 154 | installer->Register(cus, std::move(callback)); |
| 155 | } else { |
| 156 | // Register and uninstall the SODA component to delete the previously |
| 157 | // installed SODA files. |
evliu | 14d897af | 2020-07-30 22:17:21 | [diff] [blame] | 158 | if (!prefs->GetFilePath(prefs::kSodaBinaryPath).empty()) { |
evliu | 552d105 | 2020-06-24 16:27:46 | [diff] [blame] | 159 | installer->Register( |
| 160 | cus, |
| 161 | base::BindOnce( |
| 162 | [](ComponentUpdateService* cus, PrefService* prefs) { |
| 163 | if (component_updater::UninstallSODAComponent(cus, prefs)) { |
evliu | 14d897af | 2020-07-30 22:17:21 | [diff] [blame] | 164 | prefs->SetFilePath(prefs::kSodaBinaryPath, base::FilePath()); |
| 165 | prefs->SetFilePath(prefs::kSodaEnUsConfigPath, |
| 166 | base::FilePath()); |
evliu | 552d105 | 2020-06-24 16:27:46 | [diff] [blame] | 167 | } |
| 168 | }, |
| 169 | cus, prefs)); |
| 170 | } |
| 171 | } |
evliu | 8b3cee9 | 2020-07-28 17:17:21 | [diff] [blame] | 172 | #endif |
evliu | 552d105 | 2020-06-24 16:27:46 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | bool UninstallSODAComponent(ComponentUpdateService* cus, PrefService* prefs) { |
| 176 | return cus->UnregisterComponent( |
| 177 | SODAComponentInstallerPolicy::GetExtensionId()); |
evliu | 7b9481b3 | 2019-12-20 19:39:55 | [diff] [blame] | 178 | } |
| 179 | } // namespace component_updater |