Commit 85a908b8 authored by Vidhan's avatar Vidhan Committed by Commit Bot

[Autofill][States] Add States Data Component

This CL creates and registers the Autofill States Data Component.

Change-Id: Ie6ad98363764101859ec775f64781db4dce82afb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2332585Reviewed-by: default avatarMatthias Körber <koerber@google.com>
Reviewed-by: default avatarJoshua Pawlicki <waffles@chromium.org>
Reviewed-by: default avatarSorin Jianu <sorin@chromium.org>
Commit-Queue: Vidhan Jain <vidhanj@google.com>
Cr-Commit-Position: refs/heads/master@{#803614}
parent 3893615d
......@@ -286,6 +286,8 @@ static_library("browser") {
"complex_tasks/endpoint_fetcher/endpoint_fetcher.h",
"complex_tasks/task_tab_helper.cc",
"complex_tasks/task_tab_helper.h",
"component_updater/autofill_states_component_installer.cc",
"component_updater/autofill_states_component_installer.h",
"component_updater/chrome_component_updater_configurator.cc",
"component_updater/chrome_component_updater_configurator.h",
"component_updater/component_updater_prefs.cc",
......
// 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/autofill_states_component_installer.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/task/post_task.h"
#include "base/util/ranges/algorithm.h"
#include "components/autofill/core/browser/geo/country_data.h"
#include "components/autofill/core/common/autofill_prefs.h"
#include "components/component_updater/component_updater_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/browser_thread.h"
using content::BrowserThread;
namespace {
// The SHA256 of the SubjectPublicKeyInfo used to sign the extension.
// The extension id is: eeigpngbgcognadeebkilcpcaedhellh
const std::array<uint8_t, 32> kAutofillStatesPublicKeySHA256 = {
0x44, 0x86, 0xfd, 0x61, 0x62, 0xe6, 0xd0, 0x34, 0x41, 0xa8, 0xb2,
0xf2, 0x04, 0x37, 0x4b, 0xb7, 0x0b, 0xae, 0x93, 0x12, 0x9d, 0x58,
0x15, 0xb5, 0xdd, 0x89, 0xf2, 0x98, 0x73, 0xd3, 0x08, 0x97};
// Update the files installation path in prefs.
void UpdateAutofillStatesInstallDirPref(PrefService* prefs,
const base::FilePath& install_dir) {
prefs->SetFilePath(autofill::prefs::kAutofillStatesDataDir, install_dir);
}
// Returns the filenames corresponding to the states data.
std::vector<base::FilePath> AutofillStateFileNames() {
std::vector<base::FilePath> filenames;
for (const auto& country_code :
autofill::CountryDataMap::GetInstance()->country_codes()) {
filenames.push_back(base::FilePath().AppendASCII(country_code));
}
return filenames;
}
} // namespace
namespace component_updater {
AutofillStatesComponentInstallerPolicy::AutofillStatesComponentInstallerPolicy(
OnAutofillStatesReadyCallback callback)
: on_component_ready_callback_on_ui_(callback) {}
AutofillStatesComponentInstallerPolicy::
~AutofillStatesComponentInstallerPolicy() = default;
bool AutofillStatesComponentInstallerPolicy::
SupportsGroupPolicyEnabledComponentUpdates() const {
return false;
}
bool AutofillStatesComponentInstallerPolicy::RequiresNetworkEncryption() const {
return false;
}
update_client::CrxInstaller::Result
AutofillStatesComponentInstallerPolicy::OnCustomInstall(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) {
return update_client::CrxInstaller::Result(update_client::InstallError::NONE);
}
void AutofillStatesComponentInstallerPolicy::OnCustomUninstall() {}
void AutofillStatesComponentInstallerPolicy::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();
content::GetUIThreadTaskRunner(
{base::MayBlock(), base::TaskPriority::BEST_EFFORT})
->PostTask(FROM_HERE, base::BindOnce(on_component_ready_callback_on_ui_,
install_dir));
}
// Called during startup and installation before ComponentReady().
bool AutofillStatesComponentInstallerPolicy::VerifyInstallation(
const base::DictionaryValue& manifest,
const base::FilePath& install_dir) const {
// Verify that state files are present.
return util::ranges::count(
AutofillStateFileNames(), true, [&](const auto& filename) {
return base::PathExists(install_dir.Append(filename));
}) > 0;
}
base::FilePath AutofillStatesComponentInstallerPolicy::GetRelativeInstallDir()
const {
return base::FilePath(FILE_PATH_LITERAL("AutofillStates"));
}
void AutofillStatesComponentInstallerPolicy::GetHash(
std::vector<uint8_t>* hash) const {
hash->assign(kAutofillStatesPublicKeySHA256.begin(),
kAutofillStatesPublicKeySHA256.end());
}
std::string AutofillStatesComponentInstallerPolicy::GetName() const {
return "Autofill States Data";
}
update_client::InstallerAttributes
AutofillStatesComponentInstallerPolicy::GetInstallerAttributes() const {
return update_client::InstallerAttributes();
}
std::vector<std::string> AutofillStatesComponentInstallerPolicy::GetMimeTypes()
const {
return std::vector<std::string>();
}
void RegisterAutofillStatesComponent(ComponentUpdateService* cus,
PrefService* prefs) {
DVLOG(1) << "Registering Autofill States data component.";
auto installer = base::MakeRefCounted<ComponentInstaller>(
std::make_unique<AutofillStatesComponentInstallerPolicy>(
base::BindRepeating(&UpdateAutofillStatesInstallDirPref, prefs)));
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_AUTOFILL_STATES_COMPONENT_INSTALLER_H_
#define CHROME_BROWSER_COMPONENT_UPDATER_AUTOFILL_STATES_COMPONENT_INSTALLER_H_
#include <stdint.h>
#include <memory>
#include <string>
#include <vector>
#include "base/values.h"
#include "components/component_updater/component_installer.h"
#include "components/prefs/pref_service.h"
namespace base {
class FilePath;
} // namespace base
namespace component_updater {
// Success callback to be run once the component is downloaded.
using OnAutofillStatesReadyCallback =
base::RepeatingCallback<void(const base::FilePath&)>;
class AutofillStatesComponentInstallerPolicy : public ComponentInstallerPolicy {
public:
explicit AutofillStatesComponentInstallerPolicy(
OnAutofillStatesReadyCallback callback);
~AutofillStatesComponentInstallerPolicy() override;
AutofillStatesComponentInstallerPolicy(
const AutofillStatesComponentInstallerPolicy&) = delete;
AutofillStatesComponentInstallerPolicy& operator=(
const AutofillStatesComponentInstallerPolicy&) = delete;
#if defined(UNIT_TEST)
bool VerifyInstallationForTesting(const base::DictionaryValue& manifest,
const base::FilePath& install_dir) {
return VerifyInstallation(manifest, install_dir);
}
void ComponentReadyForTesting(
const base::Version& version,
const base::FilePath& install_dir,
std::unique_ptr<base::DictionaryValue> manifest) {
return ComponentReady(version, install_dir, std::move(manifest));
}
#endif
private:
// The following methods override ComponentInstallerPolicy.
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;
OnAutofillStatesReadyCallback on_component_ready_callback_on_ui_;
};
// Call once during startup to make the component update service aware of
// the File Type Policies component.
void RegisterAutofillStatesComponent(ComponentUpdateService* cus,
PrefService* prefs);
} // namespace component_updater
#endif // CHROME_BROWSER_COMPONENT_UPDATER_AUTOFILL_STATES_COMPONENT_INSTALLER_H_
// 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/autofill_states_component_installer.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_temp_dir.h"
#include "base/run_loop.h"
#include "base/test/bind_test_util.h"
#include "components/autofill/core/browser/geo/state_names.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace component_updater {
class AutofillStatesDataComponentInstallerPolicyTest : public ::testing::Test {
public:
AutofillStatesDataComponentInstallerPolicyTest() : fake_version_("0.0.1") {}
void SetUp() override {
ASSERT_TRUE(component_install_dir_.CreateUniqueTempDir());
filenames_ = {"US", "IN", "DE", "AB"};
}
const base::Version& version() const { return fake_version_; }
const base::DictionaryValue& manifest() const { return manifest_; }
const base::FilePath& GetPath() const {
return component_install_dir_.GetPath();
}
void CreateEmptyFiles() {
for (const char* filename : filenames_)
base::WriteFile(GetPath().AppendASCII(filename), "");
}
void DeleteCreatedFiles() {
for (const char* filename : filenames_)
base::DeleteFile(GetPath().AppendASCII(filename));
}
protected:
content::BrowserTaskEnvironment task_environment_;
private:
base::DictionaryValue manifest_;
base::ScopedTempDir component_install_dir_;
std::vector<const char*> filenames_;
base::FilePath fake_install_dir_;
base::Version fake_version_;
};
// Tests that VerifyInstallation only returns true when all expected files are
// present.
TEST_F(AutofillStatesDataComponentInstallerPolicyTest, VerifyInstallation) {
AutofillStatesComponentInstallerPolicy policy(
base::BindLambdaForTesting([&](const base::FilePath& path) {}));
// An empty dir lacks all required files.
EXPECT_FALSE(policy.VerifyInstallationForTesting(manifest(), GetPath()));
CreateEmptyFiles();
// Files should exist.
EXPECT_TRUE(policy.VerifyInstallationForTesting(manifest(), GetPath()));
// Delete all the created files.
DeleteCreatedFiles();
EXPECT_FALSE(policy.VerifyInstallationForTesting(manifest(), GetPath()));
}
// Tests that ComponentReady calls Lambda.
TEST_F(AutofillStatesDataComponentInstallerPolicyTest,
ComponentReady_CallsLambda) {
base::FilePath given_path;
AutofillStatesComponentInstallerPolicy policy(base::BindLambdaForTesting(
[&](const base::FilePath& path) { given_path = path; }));
policy.ComponentReadyForTesting(version(), GetPath(),
std::make_unique<base::DictionaryValue>());
base::RunLoop().RunUntilIdle();
ASSERT_EQ(GetPath(), given_path);
}
} // namespace component_updater
......@@ -9,6 +9,7 @@
#include "build/branding_buildflags.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/component_updater/autofill_states_component_installer.h"
#include "chrome/browser/component_updater/crl_set_component_installer.h"
#include "chrome/browser/component_updater/crowd_deny_component_installer.h"
#include "chrome/browser/component_updater/file_type_policies_component_installer.h"
......@@ -195,6 +196,8 @@ void RegisterComponentsForUpdate(bool is_off_the_record_profile,
#endif // !defined(OS_CHROMEOS)
RegisterZxcvbnDataComponent(cus);
RegisterAutofillStatesComponent(cus, profile_prefs);
}
} // namespace component_updater
......@@ -3243,6 +3243,7 @@ test("unit_tests") {
"../browser/command_updater_impl_unittest.cc",
"../browser/complex_tasks/endpoint_fetcher/endpoint_fetcher_unittest.cc",
"../browser/complex_tasks/task_tab_helper_unittest.cc",
"../browser/component_updater/autofill_states_component_installer_unittest.cc",
"../browser/component_updater/chrome_component_updater_configurator_unittest.cc",
"../browser/component_updater/crl_set_component_installer_unittest.cc",
"../browser/component_updater/floc_blocklist_component_installer_unittest.cc",
......
......@@ -10,8 +10,8 @@
namespace autofill {
namespace state_names {
// Returns the abbrevation corresponding to the state named |name|, or the empty
// string if there is no such state.
// Returns the abbreviation corresponding to the state |name|, or the
// empty string if there is no such state.
base::string16 GetAbbreviationForName(const base::string16& name);
// Returns the full state name corresponding to the |abbrevation|, or the empty
......
......@@ -99,6 +99,10 @@ const char kAutofillProfileEnabled[] = "autofill.profile_enabled";
// migrating this back to "autofill." in the future.
const char kAutofillProfileValidity[] = "autofill_profile_validity";
// This pref stores the file path where the autofill states data is
// downloaded to.
const char kAutofillStatesDataDir[] = "autofill.states_data_dir";
// The opt-ins for Sync Transport features for each client.
const char kAutofillSyncTransportOptIn[] = "autofill.sync_transport_opt_ins";
......@@ -181,6 +185,7 @@ void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry) {
registry->RegisterTimePref(prefs::kAutofillUploadEventsLastResetTimestamp,
base::Time());
registry->RegisterDictionaryPref(prefs::kAutofillSyncTransportOptIn);
registry->RegisterStringPref(prefs::kAutofillStatesDataDir, "");
// Deprecated prefs registered for migration.
registry->RegisterBooleanPref(kAutofillJapanCityFieldMigratedDeprecated,
......
......@@ -41,6 +41,7 @@ extern const char kAutofillOrphanRowsRemoved[];
extern const char kAutofillProfileEnabled[];
extern const char kAutofillProfileValidity[];
extern const char kAutofillSyncTransportOptIn[];
extern const char kAutofillStatesDataDir[];
extern const char kAutofillUploadEncodingSeed[];
extern const char kAutofillUploadEvents[];
extern const char kAutofillUploadEventsLastResetTimestamp[];
......
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