Commit cbbf9ad6 authored by alanlxl's avatar alanlxl Committed by Commit Bot

Add smart dim component updater

This CL adds SmartDimComponentInstallerPolicy which takes care of
downloading, installing smart dim components and feeding them into
SmartDimMlAgent to update its download_worker_.

It also takes care of the smart dim experiments, concretely, it
fetches and uses exact expected_version from the server, while
expected_version can be configured by a finch flag. Therefore we
can use finch to control the version it uses.

expected_version from the server, no matter what other versions
already exist in the disk.

Bug: 1018065
Test: Tested on DUT, it successfully fetches and uses the
Change-Id: I2759d55291e9424c255b977ccba2f1632dc44f43
Cq-Depend: chromium:1903168
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1990885
Commit-Queue: Xinglong Luan <alanlxl@chromium.org>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Reviewed-by: default avatarAndrew Moylan <amoylan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751810}
parent 62c1a22b
......@@ -3820,6 +3820,8 @@ jumbo_static_library("browser") {
"component_updater/cros_component_manager.h",
"component_updater/metadata_table_chromeos.cc",
"component_updater/metadata_table_chromeos.h",
"component_updater/smart_dim_component_installer.cc",
"component_updater/smart_dim_component_installer.h",
"device_identity/chromeos/device_oauth2_token_store_chromeos.cc",
"device_identity/chromeos/device_oauth2_token_store_chromeos.h",
"device_identity/device_identity_provider.cc",
......
......@@ -49,6 +49,10 @@
#include "chrome/browser/resource_coordinator/tab_manager.h"
#endif
#if defined(OS_CHROMEOS)
#include "chrome/browser/component_updater/smart_dim_component_installer.h"
#endif // defined(OS_CHROMEOS)
#if BUILDFLAG(ENABLE_NACL)
#include "chrome/browser/component_updater/pnacl_component_installer.h"
#endif // BUILDFLAG(ENABLE_NACL)
......@@ -179,6 +183,9 @@ void RegisterComponentsForUpdate(bool is_off_the_record_profile,
if (profile_prefs->GetBoolean(prefs::kLiveCaptionEnabled))
component_updater::RegisterSODAComponent(cus, profile_prefs,
base::OnceClosure());
#if defined(OS_CHROMEOS)
RegisterSmartDimComponent(cus);
#endif // !defined(OS_CHROMEOS)
}
} // namespace component_updater
// Copyright 2020 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/smart_dim_component_installer.h"
#include <cstddef>
#include "base/bind.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/metrics/field_trial_params.h"
#include "base/task/post_task.h"
#include "base/version.h"
#include "chrome/browser/chromeos/power/ml/smart_dim/ml_agent.h"
#include "chromeos/constants/chromeos_features.h"
#include "components/component_updater/component_updater_service.h"
namespace {
const base::FilePath::CharType kSmartDimFeaturePreprocessorConfigFileName[] =
FILE_PATH_LITERAL("example_preprocessor_config.pb");
const base::FilePath::CharType kSmartDimModelFileName[] =
FILE_PATH_LITERAL("mlservice-model-smart_dim.tflite");
const base::FilePath::CharType kSmartDimMetaJsonFileName[] =
FILE_PATH_LITERAL("smart_dim_meta.json");
constexpr base::FeatureParam<std::string> kVersion{
&chromeos::features::kSmartDimExperimentalComponent,
"smart_dim_experimental_version", "2019.11.12.0"};
// The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
// The extension id is: ghiclnejioiofblmbphpgbhaojnkempa
const uint8_t kSmartDimPublicKeySHA256[32] = {
0x67, 0x82, 0xbd, 0x49, 0x8e, 0x8e, 0x51, 0xbc, 0x1f, 0x7f, 0x61,
0x70, 0xe9, 0xda, 0x4c, 0xf0, 0x30, 0x2e, 0x24, 0x4d, 0x68, 0x17,
0x19, 0xad, 0x26, 0x6e, 0xd0, 0x33, 0x03, 0xb3, 0xe5, 0xff};
const char kMLSmartDimManifestName[] = "Smart Dim";
void UpdateSmartDimMlAgent(const base::FilePath& meta_json_path,
const base::FilePath& preprocessor_pb_path,
const base::FilePath& model_path) {
auto& smart_dim_ml_agent =
*chromeos::power::ml::SmartDimMlAgent::GetInstance();
// If IsDownloadWorkerReady(), newly downloaded components will take effect
// on next reboot. This makes sure the updating happens at most once.
if (smart_dim_ml_agent.IsDownloadWorkerReady()) {
DVLOG(1) << "Download_worker in SmartDimMlAgent is ready, does nothing.";
return;
}
if (meta_json_path.empty() || preprocessor_pb_path.empty() ||
model_path.empty()) {
DLOG(ERROR) << "Necessary paths are empty!";
return;
}
std::string metadata_json, preprocessor_proto, model_flatbuffer;
if (!base::ReadFileToString(meta_json_path, &metadata_json) ||
!base::ReadFileToString(preprocessor_pb_path, &preprocessor_proto) ||
!base::ReadFileToString(model_path, &model_flatbuffer)) {
DLOG(ERROR) << "Failed reading component files.";
return;
}
smart_dim_ml_agent.OnComponentReady(metadata_json, preprocessor_proto,
model_flatbuffer);
}
} // namespace
namespace component_updater {
SmartDimComponentInstallerPolicy::SmartDimComponentInstallerPolicy(
std::string expected_version)
: expected_version_(expected_version) {}
SmartDimComponentInstallerPolicy::~SmartDimComponentInstallerPolicy() = default;
bool SmartDimComponentInstallerPolicy::
SupportsGroupPolicyEnabledComponentUpdates() const {
return false;
}
bool SmartDimComponentInstallerPolicy::RequiresNetworkEncryption() const {
return false;
}
update_client::CrxInstaller::Result
SmartDimComponentInstallerPolicy::OnCustomInstall(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) {
return update_client::CrxInstaller::Result(0); // Nothing custom here.
}
void SmartDimComponentInstallerPolicy::OnCustomUninstall() {}
void SmartDimComponentInstallerPolicy::ComponentReady(
const base::Version& version,
const base::FilePath& install_dir,
std::unique_ptr<base::DictionaryValue> manifest) {
DVLOG(1) << "Component ready, version " << version.GetString() << " in "
<< install_dir.value();
base::PostTask(
FROM_HERE,
{base::ThreadPool(), base::MayBlock(), base::TaskPriority::BEST_EFFORT},
base::BindOnce(
&UpdateSmartDimMlAgent, install_dir.Append(kSmartDimMetaJsonFileName),
install_dir.Append(kSmartDimFeaturePreprocessorConfigFileName),
install_dir.Append(kSmartDimModelFileName)));
}
// Called during startup and installation before ComponentReady().
bool SmartDimComponentInstallerPolicy::VerifyInstallation(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) const {
// Get component version from manifest and compare to the expected_version_.
// Note: versions should not be treated as simple strings, for example,
// base::Version("2020.02.06") == base::Version("2020.2.6").
const auto* version_value = manifest.FindKey("version");
DCHECK(version_value);
const base::Version component_version(version_value->GetString());
const base::Version expected_version(expected_version_);
if (component_version != expected_version) {
DVLOG(1) << "Version " << component_version
<< " doesn't match expected_version " << expected_version;
return false;
}
// No need to actually validate the pb and tflite files here, since we'll do
// the checking in UpdateSmartDimMlAgent.
return base::PathExists(
install_dir.Append(kSmartDimFeaturePreprocessorConfigFileName)) &&
base::PathExists(install_dir.Append(kSmartDimModelFileName)) &&
base::PathExists(install_dir.Append(kSmartDimMetaJsonFileName));
}
base::FilePath SmartDimComponentInstallerPolicy::GetRelativeInstallDir() const {
return base::FilePath(FILE_PATH_LITERAL("SmartDim"));
}
void SmartDimComponentInstallerPolicy::GetHash(
std::vector<uint8_t>* hash) const {
DCHECK(hash);
hash->assign(kSmartDimPublicKeySHA256,
kSmartDimPublicKeySHA256 + base::size(kSmartDimPublicKeySHA256));
}
std::string SmartDimComponentInstallerPolicy::GetName() const {
return kMLSmartDimManifestName;
}
update_client::InstallerAttributes
SmartDimComponentInstallerPolicy::GetInstallerAttributes() const {
update_client::InstallerAttributes attrs;
// Append a '$' for exact matching.
attrs["targetversionprefix"] = expected_version_ + "$";
return attrs;
}
std::vector<std::string> SmartDimComponentInstallerPolicy::GetMimeTypes()
const {
return std::vector<std::string>();
}
void RegisterSmartDimComponent(ComponentUpdateService* cus) {
if (!base::FeatureList::IsEnabled(chromeos::features::kSmartDimNewMlAgent))
return;
DVLOG(1) << "Registering smart dim component.";
const std::string expected_version = kVersion.Get();
if (expected_version.empty()) {
DLOG(ERROR) << "expected_version is empty.";
return;
}
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<SmartDimComponentInstallerPolicy>(expected_version));
installer->Register(cus, base::OnceClosure());
}
} // namespace component_updater
// Copyright 2020 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.
#ifndef CHROME_BROWSER_COMPONENT_UPDATER_SMART_DIM_COMPONENT_INSTALLER_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_SMART_DIM_COMPONENT_INSTALLER_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/macros.h"
#include "base/values.h"
#include "components/component_updater/component_installer.h"
namespace base {
class FilePath;
} // namespace base
namespace component_updater {
class ComponentUpdateService;
class SmartDimComponentInstallerPolicy : public ComponentInstallerPolicy {
public:
explicit SmartDimComponentInstallerPolicy(std::string expected_version);
~SmartDimComponentInstallerPolicy() override;
private:
// ComponentInstallerPolicy overrides:
bool SupportsGroupPolicyEnabledComponentUpdates() const override;
bool RequiresNetworkEncryption() const override;
update_client::CrxInstaller::Result OnCustomInstall(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) override;
void OnCustomUninstall() override;
bool VerifyInstallation(const base::DictionaryValue& manifest,
const base::FilePath& install_dir) const override;
void ComponentReady(const base::Version& version,
const base::FilePath& install_dir,
std::unique_ptr<base::DictionaryValue> manifest) override;
base::FilePath GetRelativeInstallDir() const override;
void GetHash(std::vector<uint8_t>* hash) const override;
std::string GetName() const override;
update_client::InstallerAttributes GetInstallerAttributes() const override;
std::vector<std::string> GetMimeTypes() const override;
// This installer requests exact expected_version_ from the server.
// Only expected_version_ can pass VerifyInstallation and be fed to
// SmartDimMlAgent.
std::string expected_version_;
DISALLOW_COPY_AND_ASSIGN(SmartDimComponentInstallerPolicy);
};
// Call once during startup to make the component update service aware of
// the smart dim component.
void RegisterSmartDimComponent(ComponentUpdateService* cus);
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_SMART_DIM_COMPONENT_INSTALLER_H_
......@@ -285,6 +285,10 @@ const base::Feature kShowPlayInDemoMode{"ShowPlayInDemoMode",
const base::Feature kShowProgressBarInDemoModeSetup{
"ShowProgressBarInDemoModeSetup", base::FEATURE_DISABLED_BY_DEFAULT};
// Uses experimental component version for smart dim.
const base::Feature kSmartDimExperimentalComponent{
"SmartDimExperimentalComponent", base::FEATURE_DISABLED_BY_DEFAULT};
// Uses the smart dim component updater to provide smart dim model and
// preprocessor configuration.
const base::Feature kSmartDimNewMlAgent{"SmartDimNewMlAgent",
......
......@@ -130,6 +130,8 @@ extern const base::Feature kShowPlayInDemoMode;
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const base::Feature kShowProgressBarInDemoModeSetup;
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const base::Feature kSmartDimExperimentalComponent;
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const base::Feature kSmartDimNewMlAgent;
COMPONENT_EXPORT(CHROMEOS_CONSTANTS)
extern const base::Feature kSmartDimModelV3;
......
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