Koji Ishii | 7e4f249 | 2020-10-12 13:31:20 | [diff] [blame] | 1 | // Copyright 2020 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/hyphenation_component_installer.h" |
| 6 | |
| 7 | #include "base/files/file_util.h" |
| 8 | #include "base/logging.h" |
Koji Ishii | 3cbd93a | 2020-10-16 13:48:02 | [diff] [blame] | 9 | #include "base/no_destructor.h" |
| 10 | #include "base/threading/sequenced_task_runner_handle.h" |
Koji Ishii | 7e4f249 | 2020-10-12 13:31:20 | [diff] [blame] | 11 | #include "content/public/browser/browser_task_traits.h" |
| 12 | #include "content/public/browser/browser_thread.h" |
| 13 | |
| 14 | using component_updater::ComponentUpdateService; |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | // The SHA256 of the SubjectPublicKeyInfo used to sign the extension. |
| 19 | // The extension id is: jamhcnnkihinmdlkakkaopbjbbcngflc |
| 20 | constexpr uint8_t kHyphenationPublicKeySHA256[32] = { |
| 21 | 0x90, 0xc7, 0x2d, 0xda, 0x87, 0x8d, 0xc3, 0xba, 0x0a, 0xa0, 0xef, |
| 22 | 0x19, 0x11, 0x2d, 0x65, 0xb2, 0x04, 0xfc, 0x2e, 0x3e, 0xbb, 0x35, |
| 23 | 0x85, 0xae, 0x64, 0xca, 0x07, 0x61, 0x15, 0x12, 0x9e, 0xbc}; |
| 24 | |
| 25 | constexpr char kHyphenationManifestName[] = "Hyphenation"; |
| 26 | |
| 27 | constexpr base::FilePath::CharType kHyphenationRelativeInstallDir[] = |
| 28 | FILE_PATH_LITERAL("hyphen-data"); |
| 29 | |
Koji Ishii | 3cbd93a | 2020-10-16 13:48:02 | [diff] [blame] | 30 | class HyphenationDirectory { |
| 31 | public: |
| 32 | static HyphenationDirectory* Get() { |
| 33 | static base::NoDestructor<HyphenationDirectory> hyphenation_directory; |
| 34 | return hyphenation_directory.get(); |
| 35 | } |
| 36 | |
| 37 | void Get(base::OnceCallback<void(const base::FilePath&)> callback) { |
| 38 | DVLOG(1) << __func__; |
| 39 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 40 | callbacks_.push_back(std::move(callback)); |
| 41 | if (!dir_.empty()) |
| 42 | FireCallbacks(); |
| 43 | } |
| 44 | |
| 45 | void Set(const base::FilePath& new_dir) { |
| 46 | DVLOG(1) << __func__ << "\"" << new_dir << "\""; |
| 47 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 48 | CHECK(!new_dir.empty()); |
| 49 | if (new_dir == dir_) |
| 50 | return; |
| 51 | dir_ = new_dir; |
| 52 | FireCallbacks(); |
| 53 | } |
| 54 | |
| 55 | private: |
| 56 | void FireCallbacks() { |
| 57 | DVLOG(1) << __func__ << " \"" << dir_ |
| 58 | << "\", callbacks=" << callbacks_.size(); |
| 59 | DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 60 | CHECK(!dir_.empty()); |
| 61 | std::vector<base::OnceCallback<void(const base::FilePath&)>> callbacks; |
| 62 | std::swap(callbacks, callbacks_); |
| 63 | for (base::OnceCallback<void(const base::FilePath&)>& callback : |
| 64 | callbacks) { |
| 65 | base::SequencedTaskRunnerHandle::Get()->PostTask( |
| 66 | FROM_HERE, base::BindOnce(std::move(callback), dir_)); |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | base::FilePath dir_; |
| 71 | std::vector<base::OnceCallback<void(const base::FilePath&)>> callbacks_; |
| 72 | }; |
| 73 | |
Koji Ishii | 7e4f249 | 2020-10-12 13:31:20 | [diff] [blame] | 74 | } // namespace |
| 75 | |
| 76 | namespace component_updater { |
| 77 | |
| 78 | HyphenationComponentInstallerPolicy::HyphenationComponentInstallerPolicy() = |
| 79 | default; |
| 80 | |
| 81 | HyphenationComponentInstallerPolicy::~HyphenationComponentInstallerPolicy() = |
| 82 | default; |
| 83 | |
| 84 | bool HyphenationComponentInstallerPolicy:: |
| 85 | SupportsGroupPolicyEnabledComponentUpdates() const { |
| 86 | // False since this is a data, non-binary component. |
| 87 | return false; |
| 88 | } |
| 89 | |
| 90 | bool HyphenationComponentInstallerPolicy::RequiresNetworkEncryption() const { |
| 91 | // Update checks and pings associated with this component do not require |
| 92 | // confidentiality, since the component is identical for all users. |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | update_client::CrxInstaller::Result |
| 97 | HyphenationComponentInstallerPolicy::OnCustomInstall( |
| 98 | const base::DictionaryValue& manifest, |
| 99 | const base::FilePath& install_dir) { |
| 100 | return update_client::CrxInstaller::Result(0); // Nothing custom here. |
| 101 | } |
| 102 | |
| 103 | void HyphenationComponentInstallerPolicy::OnCustomUninstall() {} |
| 104 | |
| 105 | void HyphenationComponentInstallerPolicy::ComponentReady( |
| 106 | const base::Version& version, |
| 107 | const base::FilePath& install_dir, |
| 108 | std::unique_ptr<base::DictionaryValue> manifest) { |
| 109 | VLOG(1) << "Hyphenation Component ready, version " << version.GetString() |
| 110 | << " in " << install_dir.value(); |
Koji Ishii | 3cbd93a | 2020-10-16 13:48:02 | [diff] [blame] | 111 | HyphenationDirectory* hyphenation_directory = HyphenationDirectory::Get(); |
| 112 | hyphenation_directory->Set(install_dir); |
Koji Ishii | 7e4f249 | 2020-10-12 13:31:20 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | // Called during startup and installation before ComponentReady(). |
| 116 | bool HyphenationComponentInstallerPolicy::VerifyInstallation( |
| 117 | const base::DictionaryValue& manifest, |
| 118 | const base::FilePath& install_dir) const { |
| 119 | return true; |
| 120 | } |
| 121 | |
| 122 | base::FilePath HyphenationComponentInstallerPolicy::GetRelativeInstallDir() |
| 123 | const { |
| 124 | return base::FilePath(kHyphenationRelativeInstallDir); |
| 125 | } |
| 126 | |
| 127 | void HyphenationComponentInstallerPolicy::GetHash( |
| 128 | std::vector<uint8_t>* hash) const { |
| 129 | hash->assign( |
| 130 | kHyphenationPublicKeySHA256, |
| 131 | kHyphenationPublicKeySHA256 + base::size(kHyphenationPublicKeySHA256)); |
| 132 | } |
| 133 | |
| 134 | std::string HyphenationComponentInstallerPolicy::GetName() const { |
| 135 | return kHyphenationManifestName; |
| 136 | } |
| 137 | |
| 138 | update_client::InstallerAttributes |
| 139 | HyphenationComponentInstallerPolicy::GetInstallerAttributes() const { |
| 140 | return update_client::InstallerAttributes(); |
| 141 | } |
| 142 | |
| 143 | std::vector<std::string> HyphenationComponentInstallerPolicy::GetMimeTypes() |
| 144 | const { |
| 145 | return {}; |
| 146 | } |
| 147 | |
Koji Ishii | 3cbd93a | 2020-10-16 13:48:02 | [diff] [blame] | 148 | // static |
| 149 | void HyphenationComponentInstallerPolicy::GetHyphenationDictionary( |
| 150 | base::OnceCallback<void(const base::FilePath&)> callback) { |
| 151 | HyphenationDirectory* hyphenation_directory = HyphenationDirectory::Get(); |
| 152 | hyphenation_directory->Get(std::move(callback)); |
| 153 | } |
| 154 | |
Koji Ishii | 7e4f249 | 2020-10-12 13:31:20 | [diff] [blame] | 155 | void RegisterHyphenationComponent(ComponentUpdateService* cus) { |
| 156 | VLOG(1) << "Registering Hyphenation component."; |
| 157 | auto installer = base::MakeRefCounted<ComponentInstaller>( |
| 158 | std::make_unique<HyphenationComponentInstallerPolicy>()); |
| 159 | installer->Register(cus, base::OnceClosure()); |
| 160 | } |
| 161 | |
| 162 | } // namespace component_updater |