Commit 2f045cd9 authored by Vadym Doroshenko's avatar Vadym Doroshenko Committed by Commit Bot

FieldInfoManager implementation.

FieldInfoManager is a KeyedService which keeps password related field
information. Namely it contains
map: (FormSignature, FieldSignature)->field type

(FormSignature, FieldSignature) is unique identifiers of fields, type
is PASSWORD, USERNAME, NEW_PASSWORD etc.

Bug: 959776
Change-Id: I21d2044d619a6ad3b1788c8c98ed75cd58316bf2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1872224
Commit-Queue: Vadym Doroshenko <dvadym@chromium.org>
Reviewed-by: default avatarVasilii Sukhanov <vasilii@chromium.org>
Cr-Commit-Position: refs/heads/master@{#714256}
parent bea41853
...@@ -1061,6 +1061,8 @@ jumbo_static_library("browser") { ...@@ -1061,6 +1061,8 @@ jumbo_static_library("browser") {
"password_manager/account_storage/account_password_store_factory.h", "password_manager/account_storage/account_password_store_factory.h",
"password_manager/chrome_password_manager_client.cc", "password_manager/chrome_password_manager_client.cc",
"password_manager/chrome_password_manager_client.h", "password_manager/chrome_password_manager_client.h",
"password_manager/field_info_manager_factory.cc",
"password_manager/field_info_manager_factory.h",
"password_manager/password_manager_util_mac.h", "password_manager/password_manager_util_mac.h",
"password_manager/password_manager_util_mac.mm", "password_manager/password_manager_util_mac.mm",
"password_manager/password_manager_util_win.cc", "password_manager/password_manager_util_win.cc",
......
// Copyright 2019 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/password_manager/field_info_manager_factory.h"
#include "base/memory/singleton.h"
#include "chrome/browser/password_manager/password_store_factory.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/password_manager/core/browser/field_info_manager.h"
#include "components/password_manager/core/browser/password_store.h"
#include "content/public/browser/browser_context.h"
using password_manager::FieldInfoManager;
// static
FieldInfoManagerFactory* FieldInfoManagerFactory::GetInstance() {
return base::Singleton<FieldInfoManagerFactory>::get();
}
// static
FieldInfoManager* FieldInfoManagerFactory::GetForBrowserContext(
content::BrowserContext* context) {
return static_cast<FieldInfoManager*>(
GetInstance()->GetServiceForBrowserContext(context, true /* create */));
}
FieldInfoManagerFactory::FieldInfoManagerFactory()
: BrowserContextKeyedServiceFactory(
"FieldInfoManagerFactory",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(PasswordStoreFactory::GetInstance());
}
FieldInfoManagerFactory::~FieldInfoManagerFactory() = default;
// BrowserContextKeyedServiceFactory overrides:
KeyedService* FieldInfoManagerFactory::BuildServiceInstanceFor(
content::BrowserContext* context) const {
Profile* profile = static_cast<Profile*>(context);
return new FieldInfoManager(PasswordStoreFactory::GetForProfile(
profile, ServiceAccessType::EXPLICIT_ACCESS));
}
// Copyright 2019 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_PASSWORD_MANAGER_FIELD_INFO_MANAGER_FACTORY_H_
#define CHROME_BROWSER_PASSWORD_MANAGER_FIELD_INFO_MANAGER_FACTORY_H_
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"
namespace password_manager {
class FieldInfoManager;
}
class FieldInfoManagerFactory : public BrowserContextKeyedServiceFactory {
public:
static FieldInfoManagerFactory* GetInstance();
// Returns the FieldInfoManager associated with |context|.
// This may be nullptr for an incognito |context|.
static password_manager::FieldInfoManager* GetForBrowserContext(
content::BrowserContext* context);
private:
friend struct base::DefaultSingletonTraits<FieldInfoManagerFactory>;
FieldInfoManagerFactory();
~FieldInfoManagerFactory() override;
FieldInfoManagerFactory(const FieldInfoManagerFactory&) = delete;
FieldInfoManagerFactory& operator=(const FieldInfoManagerFactory&) = delete;
// BrowserContextKeyedServiceFactory overrides:
KeyedService* BuildServiceInstanceFor(
content::BrowserContext* context) const override;
};
#endif // CHROME_BROWSER_PASSWORD_MANAGER_FIELD_INFO_MANAGER_FACTORY_H_
...@@ -80,6 +80,8 @@ jumbo_static_library("browser") { ...@@ -80,6 +80,8 @@ jumbo_static_library("browser") {
"export/password_csv_writer.h", "export/password_csv_writer.h",
"export/password_manager_exporter.cc", "export/password_manager_exporter.cc",
"export/password_manager_exporter.h", "export/password_manager_exporter.h",
"field_info_manager.cc",
"field_info_manager.h",
"field_info_table.cc", "field_info_table.cc",
"field_info_table.h", "field_info_table.h",
"form_fetcher.h", "form_fetcher.h",
...@@ -510,6 +512,7 @@ source_set("unit_tests") { ...@@ -510,6 +512,7 @@ source_set("unit_tests") {
"export/csv_writer_unittest.cc", "export/csv_writer_unittest.cc",
"export/password_csv_writer_unittest.cc", "export/password_csv_writer_unittest.cc",
"export/password_manager_exporter_unittest.cc", "export/password_manager_exporter_unittest.cc",
"field_info_manager_unittest.cc",
"field_info_table_unittest.cc", "field_info_table_unittest.cc",
"form_fetcher_impl_unittest.cc", "form_fetcher_impl_unittest.cc",
"form_saver_impl_unittest.cc", "form_saver_impl_unittest.cc",
......
// Copyright 2019 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 "components/password_manager/core/browser/field_info_manager.h"
#include "components/password_manager/core/browser/field_info_table.h"
#include "components/password_manager/core/browser/password_store.h"
namespace password_manager {
FieldInfoManager::FieldInfoManager(
scoped_refptr<password_manager::PasswordStore> store)
: store_(store) {
store_->GetAllFieldInfo(this);
}
FieldInfoManager::~FieldInfoManager() = default;
void FieldInfoManager::AddFieldType(uint64_t form_signature,
uint32_t field_signature,
autofill::ServerFieldType field_type) {
field_types_[std::make_pair(form_signature, field_signature)] = field_type;
store_->AddFieldInfo(
{form_signature, field_signature, field_type, base::Time::Now()});
}
autofill::ServerFieldType FieldInfoManager::GetFieldType(
uint64_t form_signature,
uint32_t field_signature) const {
auto it = field_types_.find(std::make_pair(form_signature, field_signature));
return it == field_types_.end() ? autofill::UNKNOWN_TYPE : it->second;
}
void FieldInfoManager::OnGetPasswordStoreResults(
std::vector<std::unique_ptr<autofill::PasswordForm>> results) {
NOTREACHED();
}
void FieldInfoManager::OnGetAllFieldInfo(std::vector<FieldInfo> field_infos) {
for (const auto& field : field_infos) {
field_types_[std::make_pair(field.form_signature, field.field_signature)] =
field.field_type;
}
}
} // namespace password_manager
// Copyright 2019 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 COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FIELD_INFO_MANAGER_H_
#define COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FIELD_INFO_MANAGER_H_
#include <map>
#include "components/autofill/core/browser/field_types.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/password_manager/core/browser/password_store_consumer.h"
namespace password_manager {
class PasswordStore;
// Keeps semantic types of web forms fields. Fields are specified with a pair
// (FormSignature, FieldSignature), which uniquely defines fields in the web
// (more details on the signature calculation are in signature_util.cc). Types
// might be PASSWORD, USERNAME, NEW_PASSWORD etc.
class FieldInfoManager : public KeyedService, public PasswordStoreConsumer {
public:
FieldInfoManager(scoped_refptr<password_manager::PasswordStore> store);
~FieldInfoManager() override;
void AddFieldType(uint64_t form_signature,
uint32_t field_signature,
autofill::ServerFieldType field_type);
autofill::ServerFieldType GetFieldType(uint64_t form_signature,
uint32_t field_signature) const;
private:
// PasswordStoreConsumer:
void OnGetPasswordStoreResults(
std::vector<std::unique_ptr<autofill::PasswordForm>> results) override;
void OnGetAllFieldInfo(std::vector<FieldInfo>) override;
std::map<std::pair<uint64_t, uint32_t>, autofill::ServerFieldType>
field_types_;
scoped_refptr<password_manager::PasswordStore> store_;
};
} // namespace password_manager
#endif // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_FIELD_INFO_MANAGER_H_
// Copyright 2019 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 "components/password_manager/core/browser/field_info_manager.h"
#include <vector>
#include "base/test/task_environment.h"
#include "base/time/time.h"
#include "components/autofill/core/browser/field_types.h"
#include "components/password_manager/core/browser/field_info_table.h"
#include "components/password_manager/core/browser/mock_password_store.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using autofill::PASSWORD;
using autofill::SINGLE_USERNAME;
using autofill::UNKNOWN_TYPE;
using autofill::USERNAME;
using base::Time;
namespace password_manager {
namespace {
MATCHER_P3(FieldInfoHasData, form_signature, field_signature, field_type, "") {
return arg.form_signature == form_signature &&
arg.field_signature == field_signature &&
arg.field_type == field_type && arg.create_time != base::Time();
}
class FieldInfoManagerTest : public testing::Test {
public:
FieldInfoManagerTest() {
test_data_.push_back({101u, 1u, USERNAME, Time::FromTimeT(1)});
test_data_.push_back({101u, 10u, PASSWORD, Time::FromTimeT(5)});
test_data_.push_back({102u, 1u, SINGLE_USERNAME, Time::FromTimeT(10)});
store_ = new MockPasswordStore;
store_->Init(syncer::SyncableService::StartSyncFlare(), /*prefs=*/nullptr);
EXPECT_CALL(*store_, GetAllFieldInfoImpl());
field_info_manager_ = std::make_unique<FieldInfoManager>(store_);
task_environment_.RunUntilIdle();
}
~FieldInfoManagerTest() override { store_->ShutdownOnUIThread(); }
protected:
base::test::TaskEnvironment task_environment_{
base::test::TaskEnvironment::MainThreadType::UI};
scoped_refptr<MockPasswordStore> store_;
std::vector<FieldInfo> test_data_;
std::unique_ptr<FieldInfoManager> field_info_manager_;
};
TEST_F(FieldInfoManagerTest, AddFieldType) {
EXPECT_EQ(UNKNOWN_TYPE, field_info_manager_->GetFieldType(101u, 1u));
EXPECT_CALL(*store_, AddFieldInfoImpl(FieldInfoHasData(101u, 1u, PASSWORD)));
field_info_manager_->AddFieldType(101u, 1u, PASSWORD);
task_environment_.RunUntilIdle();
EXPECT_EQ(PASSWORD, field_info_manager_->GetFieldType(101u, 1u));
}
TEST_F(FieldInfoManagerTest, OnGetAllFieldInfo) {
auto* field_info_manager_as_password_consumer =
static_cast<PasswordStoreConsumer*>(field_info_manager_.get());
field_info_manager_as_password_consumer->OnGetAllFieldInfo(test_data_);
for (const FieldInfo& field_info : test_data_) {
EXPECT_EQ(field_info.field_type,
field_info_manager_->GetFieldType(field_info.form_signature,
field_info.field_signature));
}
EXPECT_EQ(UNKNOWN_TYPE, field_info_manager_->GetFieldType(1234u, 1u));
}
} // namespace
} // namespace password_manager
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