Chromium Code Reviews| Index: chrome/browser/component_updater/cros_component_installer.cc |
| diff --git a/chrome/browser/component_updater/cros_component_installer.cc b/chrome/browser/component_updater/cros_component_installer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..7de5d9184b5d8521ce491a65b114b9053f95cb58 |
| --- /dev/null |
| +++ b/chrome/browser/component_updater/cros_component_installer.cc |
| @@ -0,0 +1,195 @@ |
| +// Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/component_updater/cros_component_installer.h" |
| +#include "chrome/browser/browser_process.h" |
| +#include "components/component_updater/component_updater_paths.h" |
| + |
| +using content::BrowserThread; |
| +using content::PluginService; |
| + |
| +namespace component_updater { |
| + |
| +#if defined(OS_CHROMEOS) |
| +void LogRegistrationResult(chromeos::DBusMethodCallStatus call_status, |
| + bool result) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + if (call_status != chromeos::DBUS_METHOD_CALL_SUCCESS) { |
| + LOG(ERROR) << "Call to imageloader service failed."; |
| + return; |
| + } |
| + if (!result) { |
| + LOG(ERROR) << "Component registration failed"; |
| + return; |
| + } |
| +} |
| +void ImageLoaderRegistration(const std::string& version, |
| + const base::FilePath& install_dir, |
| + const std::string& name) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + chromeos::ImageLoaderClient* loader = |
| + chromeos::DBusThreadManager::Get()->GetImageLoaderClient(); |
| + |
| + if (loader) { |
| + loader->RegisterComponent(name, version, install_dir.value(), |
| + base::Bind(&LogRegistrationResult)); |
| + } else { |
| + LOG(ERROR) << "Failed to get ImageLoaderClient object."; |
| + } |
| +} |
| + |
| +// Determine whether or not to skip registering this cros component updates. |
| +bool SkipCupsRegistration(ComponentUpdateService* cus) { |
| + return false; |
| +} |
| +#endif // defined(OS_CHROMEOS) |
| + |
| +CrOSComponentInstallerTraits::CrOSComponentInstallerTraits( |
| + std::string dir_name, |
| + std::string name, |
| + std::string sha2HashStr) |
| + : dir_name(dir_name), name(name) { |
| + if (sha2HashStr.length() != 64) |
| + return; |
| + for (unsigned int i = 0; i < sizeof(kSha2Hash_); i++) { |
| + kSha2Hash_[i] = stoul(sha2HashStr.substr(i * 2, 2), nullptr, 16); |
| + } |
| +} |
| + |
| +bool CrOSComponentInstallerTraits::SupportsGroupPolicyEnabledComponentUpdates() |
| + const { |
| + return true; |
| +} |
| + |
| +bool CrOSComponentInstallerTraits::RequiresNetworkEncryption() const { |
| + return false; |
| +} |
| + |
| +update_client::CrxInstaller::Result |
| +CrOSComponentInstallerTraits::OnCustomInstall( |
| + const base::DictionaryValue& manifest, |
| + const base::FilePath& install_dir) { |
| + DVLOG(1) << "[CrOSComponentInstallerTraits::OnCustomInstall]"; |
| +#if defined(OS_CHROMEOS) |
| + std::string version; |
| + if (!manifest.GetString("version", &version)) { |
| + return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); |
| + } |
| + BrowserThread::PostTask( |
| + BrowserThread::UI, FROM_HERE, |
| + base::Bind(&ImageLoaderRegistration, version, install_dir, name)); |
| + return update_client::CrxInstaller::Result(update_client::InstallError::NONE); |
| +#else |
| + return ToInstallerResult(update_client::InstallError::GENERIC_ERROR); |
| +#endif // defined(OS_CHROMEOS) |
| +} |
| + |
| +void CrOSComponentInstallerTraits::ComponentReady( |
| + const base::Version& version, |
| + const base::FilePath& path, |
| + std::unique_ptr<base::DictionaryValue> manifest) {} |
| + |
| +bool CrOSComponentInstallerTraits::VerifyInstallation( |
| + const base::DictionaryValue& manifest, |
| + const base::FilePath& install_dir) const { |
| + return true; |
| +} |
| + |
| +base::FilePath CrOSComponentInstallerTraits::GetRelativeInstallDir() const { |
| + return base::FilePath(FILE_PATH_LITERAL(dir_name)); |
| +} |
| + |
| +void CrOSComponentInstallerTraits::GetHash(std::vector<uint8_t>* hash) const { |
| + hash->assign(kSha2Hash_, kSha2Hash_ + arraysize(kSha2Hash_)); |
| +} |
| + |
| +std::string CrOSComponentInstallerTraits::GetName() const { |
| + return name; |
| +} |
| + |
| +update_client::InstallerAttributes |
| +CrOSComponentInstallerTraits::GetInstallerAttributes() const { |
| + return update_client::InstallerAttributes(); |
| +} |
| + |
| +std::vector<std::string> CrOSComponentInstallerTraits::GetMimeTypes() const { |
| + std::vector<std::string> mime_types; |
| + return mime_types; |
| +} |
| + |
| +void RegisterCrOSComponentInternal(ComponentUpdateService* cus, |
| + const ComponentConfig& config) { |
| + std::unique_ptr<ComponentInstallerTraits> traits( |
| + new CrOSComponentInstallerTraits(config.dir, config.name, |
| + config.sha2hashstr)); |
| + // |cus| will take ownership of |installer| during |
| + // installer->Register(cus). |
| + DefaultComponentInstaller* installer = |
| + new DefaultComponentInstaller(std::move(traits)); |
| + installer->Register(cus, base::Closure()); |
| +} |
| + |
| +// an example config file content: |
| +//{ |
| +// "components":{ |
| +// "escpr": { |
| +// "dir":"epson-inkjet-printer-escpr", |
| +// "sha2hashstr":"1913a5e0a6cad30b6f03e176177e0d7ed62c5d6700a9c66da556d7c3f5d6a47e" |
| +// } |
| +// } |
| +//} |
| +bool RegisterCrOSComponentInternal(ComponentUpdateService* cus, |
| + const std::string& name) { |
| + if (name.length() == 0) { |
| + DVLOG(1) << "[RegisterCrOSComponents] name is empty."; |
| + return false; |
| + } |
| + if (!componentConfigReaderRoot.get()) { |
| + DVLOG(1) << "[RegisterCrOSComponents] configuration is not loaded."; |
| + return false; |
| + } |
| + auto root_dic = std::unique_ptr<base::DictionaryValue>( |
| + static_cast<base::DictionaryValue*>(componentConfigReaderRoot.get())); |
| + base::DictionaryValue* components = nullptr; |
| + if (root_dic->GetDictionary("components", &components)) { |
| + base::DictionaryValue* component = nullptr; |
| + if (components->GetDictionary(name, &component)) { |
| + std::string dir, sha2hashstr; |
| + if (component->GetString("dir", &dir) && |
| + component->GetString("sha2hashstr", &sha2hashstr) && |
| + dir.length() > 0 && sha2hashstr.length() == 64) { |
| + ComponentConfig config; |
| + config.name = name; |
| + config.dir = dir; |
| + config.sha2hashstr = sha2hashstr; |
| + DVLOG(1) << "[RegisterCrOSComponents] register component:" << name; |
| + RegisterCrOSComponentInternal(cus, config); |
| + return true; |
| + } |
| + } |
| + } |
| + DVLOG(1) << "[RegisterCrOSComponents] " |
| + "component " |
|
waffles
2017/03/01 18:49:20
Why not combine with the above line?
xiaochu
2017/03/02 01:50:05
Done.
|
| + << name << " is not in configuration file."; |
| + return false; |
| +} |
| + |
| +void LoadCrOSConfigInternal() { |
| + if (!componentConfigReaderRoot.get()) { |
| + base::FilePath configFilePath(componentConfigReaderFilepathstr); |
| + JSONFileValueDeserializer deserializer(configFilePath); |
| + std::string error; |
| + componentConfigReaderRoot = deserializer.Deserialize(NULL, &error); |
|
waffles
2017/03/01 18:49:20
Consider DCHECKing that error.empty().
If you don'
xiaochu
2017/03/02 01:50:05
Done.
|
| + } |
| +} |
| +void LoadCrOSConfig(ComponentUpdateService* cus) { |
| + cus->GetSequencedTaskRunner()->PostTask(FROM_HERE, |
| + base::Bind(&LoadCrOSConfigInternal)); |
|
waffles
2017/03/01 18:49:20
I recommend instead doing:
base::PostTaskWithTrai
xiaochu
2017/03/02 01:50:05
Done.
|
| +} |
| + |
| +bool RegisterCrOSComponent(ComponentUpdateService* cus, |
| + const std::string& name) { |
| + return RegisterCrOSComponentInternal(cus, name); |
| +} |
| +} // namespace component_updater |