Commit ea83d0c4 authored by Lei Zhang's avatar Lei Zhang Committed by Commit Bot

Fix nits in CrOSComponentManager.

Change-Id: I22a02aaf461e425d5a1742370036a08c60319d69
Reviewed-on: https://chromium-review.googlesource.com/935757
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Cr-Commit-Position: refs/heads/master@{#539303}
parent 57acbdd7
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/chromeos/dbus/chrome_component_updater_service_provider_delegate.h" #include "chrome/browser/chromeos/dbus/chrome_component_updater_service_provider_delegate.h"
#include "base/bind.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/cros_component_installer.h" #include "chrome/browser/component_updater/cros_component_installer.h"
namespace chromeos { namespace chromeos {
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/md5.h" #include "base/md5.h"
#include "base/memory/ref_counted.h" #include "base/memory/ref_counted.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chromeos/printing/ppd_provider_factory.h" #include "chrome/browser/chromeos/printing/ppd_provider_factory.h"
#include "chrome/browser/component_updater/cros_component_installer.h" #include "chrome/browser/component_updater/cros_component_installer.h"
#include "chrome/browser/local_discovery/endpoint_resolver.h" #include "chrome/browser/local_discovery/endpoint_resolver.h"
...@@ -195,8 +196,8 @@ class PrinterConfigurerImpl : public PrinterConfigurer { ...@@ -195,8 +196,8 @@ class PrinterConfigurerImpl : public PrinterConfigurer {
const std::vector<std::string>& ppd_filters) { const std::vector<std::string>& ppd_filters) {
if (base::FeatureList::IsEnabled(features::kCrOSComponent)) { if (base::FeatureList::IsEnabled(features::kCrOSComponent)) {
std::set<std::string> components_requested; std::set<std::string> components_requested;
for (auto& ppd_filter : ppd_filters) { for (const auto& ppd_filter : ppd_filters) {
for (auto& component : GetComponentizedFilters()) { for (const auto& component : GetComponentizedFilters()) {
if (component.first == ppd_filter) { if (component.first == ppd_filter) {
components_requested.insert(component.second); components_requested.insert(component.second);
} }
...@@ -212,7 +213,8 @@ class PrinterConfigurerImpl : public PrinterConfigurer { ...@@ -212,7 +213,8 @@ class PrinterConfigurerImpl : public PrinterConfigurer {
weak_factory_.GetWeakPtr(), printer, ppd_contents, weak_factory_.GetWeakPtr(), printer, ppd_contents,
std::move(cb))); std::move(cb)));
return; return;
} else if (components_requested.size() > 1) { }
if (components_requested.size() > 1) {
LOG(ERROR) << "More than one filter component is requested."; LOG(ERROR) << "More than one filter component is requested.";
std::move(cb).Run(PrinterSetupResult::kFatalError); std::move(cb).Run(PrinterSetupResult::kFatalError);
return; return;
......
...@@ -4,18 +4,22 @@ ...@@ -4,18 +4,22 @@
#include "chrome/browser/component_updater/cros_component_installer.h" #include "chrome/browser/component_updater/cros_component_installer.h"
#include <map>
#include <utility> #include <utility>
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/optional.h"
#include "base/path_service.h" #include "base/path_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/task_scheduler/post_task.h" #include "base/task_scheduler/post_task.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/component_installer_errors.h" #include "chrome/browser/component_updater/component_installer_errors.h"
#include "chromeos/dbus/dbus_thread_manager.h" #include "chromeos/dbus/dbus_thread_manager.h"
#include "chromeos/dbus/image_loader_client.h" #include "chromeos/dbus/image_loader_client.h"
#include "components/component_updater/component_updater_paths.h" #include "components/component_updater/component_updater_paths.h"
#include "components/component_updater/component_updater_service.h"
#include "components/crx_file/id_util.h" #include "components/crx_file/id_util.h"
#include "content/public/browser/browser_thread.h" #include "content/public/browser/browser_thread.h"
#include "crypto/sha2.h"
// ConfigMap list-initialization expression for all downloadable // ConfigMap list-initialization expression for all downloadable
// Chrome OS components. // Chrome OS components.
...@@ -45,11 +49,14 @@ ...@@ -45,11 +49,14 @@
{"sha2hashstr", \ {"sha2hashstr", \
"5714811c04f0a63aac96b39096faa759ace4c04e9b68291e7c9716128f5a2722"}}}}; "5714811c04f0a63aac96b39096faa759ace4c04e9b68291e7c9716128f5a2722"}}}};
#define COMPONENTS_ROOT_PATH "cros-components"
namespace component_updater { namespace component_updater {
using ConfigMap = std::map<std::string, std::map<std::string, std::string>>;
namespace { namespace {
constexpr char kComponentsRootPath[] = "cros-components";
// TODO(xiaochu): add metrics for component usage (crbug.com/793052). // TODO(xiaochu): add metrics for component usage (crbug.com/793052).
void LogCustomUninstall(base::Optional<bool> result) {} void LogCustomUninstall(base::Optional<bool> result) {}
...@@ -70,28 +77,51 @@ void CleanUpOldInstalls(const std::string& name) { ...@@ -70,28 +77,51 @@ void CleanUpOldInstalls(const std::string& name) {
if (base::PathExists(path)) if (base::PathExists(path))
base::DeleteFile(path, true); base::DeleteFile(path, true);
} }
} // namespace
using ConfigMap = std::map<std::string, std::map<std::string, std::string>>; // Returns all installed components.
std::vector<ComponentConfig> GetInstalled() {
std::vector<ComponentConfig> configs;
base::FilePath root;
if (!PathService::Get(DIR_COMPONENT_USER, &root))
return configs;
root = root.Append(kComponentsRootPath);
const ConfigMap components = CONFIG_MAP_CONTENT;
for (auto it : components) {
const std::string& name = it.first;
const std::map<std::string, std::string>& props = it.second;
base::FilePath component_path = root.Append(name);
if (base::PathExists(component_path)) {
ComponentConfig config(name, props.find("env_version")->second,
props.find("sha2hashstr")->second);
configs.push_back(config);
}
}
return configs;
}
} // namespace
ComponentConfig::ComponentConfig(const std::string& name, ComponentConfig::ComponentConfig(const std::string& name,
const std::string& env_version, const std::string& env_version,
const std::string& sha2hashstr) const std::string& sha2hashstr)
: name(name), env_version(env_version), sha2hashstr(sha2hashstr) {} : name(name), env_version(env_version), sha2hashstr(sha2hashstr) {}
ComponentConfig::~ComponentConfig() {} ComponentConfig::~ComponentConfig() {}
CrOSComponentInstallerPolicy::CrOSComponentInstallerPolicy( CrOSComponentInstallerPolicy::CrOSComponentInstallerPolicy(
const ComponentConfig& config) const ComponentConfig& config)
: name(config.name), env_version(config.env_version) { : name_(config.name), env_version_(config.env_version) {
if (config.sha2hashstr.length() != 64) if (config.sha2hashstr.length() != crypto::kSHA256Length * 2)
return; return;
auto strstream = config.sha2hashstr;
for (auto& cell : kSha2Hash_) { bool converted = base::HexStringToBytes(config.sha2hashstr, &sha2_hash_);
cell = stoul(strstream.substr(0, 2), nullptr, 16); DCHECK(converted);
strstream.erase(0, 2); DCHECK_EQ(crypto::kSHA256Length, sha2_hash_.size());
}
} }
CrOSComponentInstallerPolicy::~CrOSComponentInstallerPolicy() = default;
bool CrOSComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates() bool CrOSComponentInstallerPolicy::SupportsGroupPolicyEnabledComponentUpdates()
const { const {
return true; return true;
...@@ -105,8 +135,8 @@ update_client::CrxInstaller::Result ...@@ -105,8 +135,8 @@ update_client::CrxInstaller::Result
CrOSComponentInstallerPolicy::OnCustomInstall( CrOSComponentInstallerPolicy::OnCustomInstall(
const base::DictionaryValue& manifest, const base::DictionaryValue& manifest,
const base::FilePath& install_dir) { const base::FilePath& install_dir) {
// TODO(xiaochu): remove this at M66 (crbug.com/792203). // TODO(xiaochu): remove after M66 ships to stable. https://crbug.com/792203
CleanUpOldInstalls(name); CleanUpOldInstalls(name_);
return update_client::CrxInstaller::Result(update_client::InstallError::NONE); return update_client::CrxInstaller::Result(update_client::InstallError::NONE);
} }
...@@ -114,10 +144,10 @@ CrOSComponentInstallerPolicy::OnCustomInstall( ...@@ -114,10 +144,10 @@ CrOSComponentInstallerPolicy::OnCustomInstall(
void CrOSComponentInstallerPolicy::OnCustomUninstall() { void CrOSComponentInstallerPolicy::OnCustomUninstall() {
g_browser_process->platform_part() g_browser_process->platform_part()
->cros_component_manager() ->cros_component_manager()
->UnregisterCompatiblePath(name); ->UnregisterCompatiblePath(name_);
chromeos::DBusThreadManager::Get()->GetImageLoaderClient()->UnmountComponent( chromeos::DBusThreadManager::Get()->GetImageLoaderClient()->UnmountComponent(
name, base::BindOnce(&LogCustomUninstall)); name_, base::BindOnce(&LogCustomUninstall));
} }
void CrOSComponentInstallerPolicy::ComponentReady( void CrOSComponentInstallerPolicy::ComponentReady(
...@@ -125,13 +155,15 @@ void CrOSComponentInstallerPolicy::ComponentReady( ...@@ -125,13 +155,15 @@ void CrOSComponentInstallerPolicy::ComponentReady(
const base::FilePath& path, const base::FilePath& path,
std::unique_ptr<base::DictionaryValue> manifest) { std::unique_ptr<base::DictionaryValue> manifest) {
std::string min_env_version; std::string min_env_version;
if (manifest && manifest->GetString("min_env_version", &min_env_version)) { if (!manifest || !manifest->GetString("min_env_version", &min_env_version))
if (IsCompatible(env_version, min_env_version)) { return;
g_browser_process->platform_part()
->cros_component_manager() if (!IsCompatible(env_version_, min_env_version))
->RegisterCompatiblePath(GetName(), path); return;
}
} g_browser_process->platform_part()
->cros_component_manager()
->RegisterCompatiblePath(GetName(), path);
} }
bool CrOSComponentInstallerPolicy::VerifyInstallation( bool CrOSComponentInstallerPolicy::VerifyInstallation(
...@@ -141,22 +173,22 @@ bool CrOSComponentInstallerPolicy::VerifyInstallation( ...@@ -141,22 +173,22 @@ bool CrOSComponentInstallerPolicy::VerifyInstallation(
} }
base::FilePath CrOSComponentInstallerPolicy::GetRelativeInstallDir() const { base::FilePath CrOSComponentInstallerPolicy::GetRelativeInstallDir() const {
base::FilePath path = base::FilePath(COMPONENTS_ROOT_PATH); base::FilePath path = base::FilePath(kComponentsRootPath);
return path.Append(name); return path.Append(name_);
} }
void CrOSComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const { void CrOSComponentInstallerPolicy::GetHash(std::vector<uint8_t>* hash) const {
hash->assign(kSha2Hash_, kSha2Hash_ + arraysize(kSha2Hash_)); *hash = sha2_hash_;
} }
std::string CrOSComponentInstallerPolicy::GetName() const { std::string CrOSComponentInstallerPolicy::GetName() const {
return name; return name_;
} }
update_client::InstallerAttributes update_client::InstallerAttributes
CrOSComponentInstallerPolicy::GetInstallerAttributes() const { CrOSComponentInstallerPolicy::GetInstallerAttributes() const {
update_client::InstallerAttributes attrs; update_client::InstallerAttributes attrs;
attrs["_env_version"] = env_version; attrs["_env_version"] = env_version_;
return attrs; return attrs;
} }
...@@ -203,19 +235,15 @@ bool CrOSComponentManager::Unload(const std::string& name) { ...@@ -203,19 +235,15 @@ bool CrOSComponentManager::Unload(const std::string& name) {
// Component |name| does not exist. // Component |name| does not exist.
return false; return false;
} }
component_updater::ComponentUpdateService* updater = ComponentUpdateService* updater = g_browser_process->component_updater();
g_browser_process->component_updater();
const std::string id = GenerateId(it->second.find("sha2hashstr")->second); const std::string id = GenerateId(it->second.find("sha2hashstr")->second);
return updater->UnregisterComponent(id); return updater->UnregisterComponent(id);
} }
void CrOSComponentManager::RegisterInstalled() { void CrOSComponentManager::RegisterInstalled() {
base::PostTaskWithTraitsAndReplyWithResult( base::PostTaskWithTraitsAndReplyWithResult(
FROM_HERE, {base::MayBlock()}, FROM_HERE, {base::MayBlock()}, base::BindOnce(GetInstalled),
base::BindOnce(&component_updater::CrOSComponentManager::GetInstalled, base::BindOnce(&CrOSComponentManager::RegisterN, base::Unretained(this)));
base::Unretained(this)),
base::BindOnce(&component_updater::CrOSComponentManager::RegisterN,
base::Unretained(this)));
} }
void CrOSComponentManager::RegisterCompatiblePath(const std::string& name, void CrOSComponentManager::RegisterCompatiblePath(const std::string& name,
...@@ -322,32 +350,10 @@ void CrOSComponentManager::FinishLoad(LoadCallback load_callback, ...@@ -322,32 +350,10 @@ void CrOSComponentManager::FinishLoad(LoadCallback load_callback,
} }
} }
std::vector<ComponentConfig> CrOSComponentManager::GetInstalled() {
std::vector<ComponentConfig> configs;
base::FilePath root;
if (!PathService::Get(DIR_COMPONENT_USER, &root))
return configs;
root = root.Append(COMPONENTS_ROOT_PATH);
const ConfigMap components = CONFIG_MAP_CONTENT;
for (auto it : components) {
const std::string& name = it.first;
const std::map<std::string, std::string>& props = it.second;
base::FilePath component_path = root.Append(name);
if (base::PathExists(component_path)) {
ComponentConfig config(name, props.find("env_version")->second,
props.find("sha2hashstr")->second);
configs.push_back(config);
}
}
return configs;
}
void CrOSComponentManager::RegisterN( void CrOSComponentManager::RegisterN(
const std::vector<ComponentConfig>& configs) { const std::vector<ComponentConfig>& configs) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
component_updater::ComponentUpdateService* updater = ComponentUpdateService* updater = g_browser_process->component_updater();
g_browser_process->component_updater();
for (const auto& config : configs) { for (const auto& config : configs) {
Register(updater, config, base::OnceClosure()); Register(updater, config, base::OnceClosure());
} }
......
...@@ -5,41 +5,43 @@ ...@@ -5,41 +5,43 @@
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ #ifndef CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_ #define CHROME_BROWSER_COMPONENT_UPDATER_CROS_COMPONENT_INSTALLER_H_
#include <map>
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "chrome/browser/browser_process.h" #include "base/containers/flat_map.h"
#include "chromeos/dbus/dbus_method_call_status.h" #include "base/gtest_prod_util.h"
#include "base/optional.h"
#include "components/component_updater/component_installer.h" #include "components/component_updater/component_installer.h"
#include "components/component_updater/component_updater_service.h"
#include "components/update_client/update_client.h" #include "components/update_client/update_client.h"
#include "crypto/sha2.h"
namespace component_updater { namespace component_updater {
class ComponentUpdateService;
struct ComponentConfig { struct ComponentConfig {
std::string name;
std::string env_version;
std::string sha2hashstr;
ComponentConfig(const std::string& name, ComponentConfig(const std::string& name,
const std::string& env_version, const std::string& env_version,
const std::string& sha2hashstr); const std::string& sha2hashstr);
~ComponentConfig(); ~ComponentConfig();
std::string name;
std::string env_version;
std::string sha2hashstr;
}; };
class CrOSComponentInstallerPolicy : public ComponentInstallerPolicy { class CrOSComponentInstallerPolicy : public ComponentInstallerPolicy {
public: public:
explicit CrOSComponentInstallerPolicy(const ComponentConfig& config); explicit CrOSComponentInstallerPolicy(const ComponentConfig& config);
~CrOSComponentInstallerPolicy() override {} ~CrOSComponentInstallerPolicy() override;
private: private:
FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, IsCompatibleOrNot); FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, IsCompatibleOrNot);
FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, CompatibilityOK); FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, CompatibilityOK);
FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest, FRIEND_TEST_ALL_PREFIXES(CrOSComponentInstallerTest,
CompatibilityMissingManifest); CompatibilityMissingManifest);
// The following methods override ComponentInstallerPolicy.
// ComponentInstallerPolicy:
bool SupportsGroupPolicyEnabledComponentUpdates() const override; bool SupportsGroupPolicyEnabledComponentUpdates() const override;
bool RequiresNetworkEncryption() const override; bool RequiresNetworkEncryption() const override;
update_client::CrxInstaller::Result OnCustomInstall( update_client::CrxInstaller::Result OnCustomInstall(
...@@ -57,11 +59,13 @@ class CrOSComponentInstallerPolicy : public ComponentInstallerPolicy { ...@@ -57,11 +59,13 @@ class CrOSComponentInstallerPolicy : public ComponentInstallerPolicy {
update_client::InstallerAttributes GetInstallerAttributes() const override; update_client::InstallerAttributes GetInstallerAttributes() const override;
std::vector<std::string> GetMimeTypes() const override; std::vector<std::string> GetMimeTypes() const override;
// This is virtual so unit tests can override it.
virtual bool IsCompatible(const std::string& env_version_str, virtual bool IsCompatible(const std::string& env_version_str,
const std::string& min_env_version_str); const std::string& min_env_version_str);
std::string name;
std::string env_version; const std::string name_;
uint8_t kSha2Hash_[crypto::kSHA256Length] = {}; const std::string env_version_;
std::vector<uint8_t> sha2_hash_;
DISALLOW_COPY_AND_ASSIGN(CrOSComponentInstallerPolicy); DISALLOW_COPY_AND_ASSIGN(CrOSComponentInstallerPolicy);
}; };
...@@ -85,6 +89,7 @@ class CrOSComponentManager { ...@@ -85,6 +89,7 @@ class CrOSComponentManager {
CrOSComponentManager(); CrOSComponentManager();
~CrOSComponentManager(); ~CrOSComponentManager();
// Installs a component and keeps it up-to-date. |load_callback| returns the // Installs a component and keeps it up-to-date. |load_callback| returns the
// mount point path. // mount point path.
void Load(const std::string& name, void Load(const std::string& name,
...@@ -148,9 +153,6 @@ class CrOSComponentManager { ...@@ -148,9 +153,6 @@ class CrOSComponentManager {
void FinishLoad(LoadCallback load_callback, void FinishLoad(LoadCallback load_callback,
base::Optional<base::FilePath> result); base::Optional<base::FilePath> result);
// Returns all installed components.
std::vector<ComponentConfig> GetInstalled();
// Registers component |configs| to be updated. // Registers component |configs| to be updated.
void RegisterN(const std::vector<ComponentConfig>& configs); void RegisterN(const std::vector<ComponentConfig>& configs);
......
...@@ -4,6 +4,8 @@ ...@@ -4,6 +4,8 @@
#include "chrome/browser/extensions/api/media_perception_private/media_perception_api_delegate_chromeos.h" #include "chrome/browser/extensions/api/media_perception_private/media_perception_api_delegate_chromeos.h"
#include "base/bind.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/cros_component_installer.h" #include "chrome/browser/component_updater/cros_component_installer.h"
namespace media_perception = extensions::api::media_perception_private; namespace media_perception = extensions::api::media_perception_private;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment