Commit f997361d authored by Michael Ershov's avatar Michael Ershov Committed by Chromium LUCI CQ

[Lacros] Extract CertDbInitializer interface

Extract CertDbInitializer interface from the implementation
to allow writing a mock for it. This is a refactoring with no
intentional change to behavior.

Bug: 1145946
Change-Id: If08abcdcd1257d574137d2d5d1264dc389e868f0
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2581925
Commit-Queue: Michael Ershov <miersh@google.com>
Reviewed-by: default avatarErik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#836081}
parent 3b638dc3
...@@ -4571,10 +4571,11 @@ static_library("browser") { ...@@ -4571,10 +4571,11 @@ static_library("browser") {
"lacros/account_manager_facade_lacros.h", "lacros/account_manager_facade_lacros.h",
"lacros/account_manager_util.cc", "lacros/account_manager_util.cc",
"lacros/account_manager_util.h", "lacros/account_manager_util.h",
"lacros/cert_db_initializer.cc",
"lacros/cert_db_initializer.h", "lacros/cert_db_initializer.h",
"lacros/cert_db_initializer_factory.cc", "lacros/cert_db_initializer_factory.cc",
"lacros/cert_db_initializer_factory.h", "lacros/cert_db_initializer_factory.h",
"lacros/cert_db_initializer_impl.cc",
"lacros/cert_db_initializer_impl.h",
"lacros/feedback_util.cc", "lacros/feedback_util.cc",
"lacros/feedback_util.h", "lacros/feedback_util.h",
"lacros/immersive_context_lacros.cc", "lacros/immersive_context_lacros.cc",
......
...@@ -5,44 +5,10 @@ ...@@ -5,44 +5,10 @@
#ifndef CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_H_ #ifndef CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_H_
#define CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_H_ #define CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_H_
#include "base/memory/weak_ptr.h" class CertDbInitializer {
#include "chromeos/crosapi/mojom/cert_database.mojom.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "mojo/public/cpp/bindings/remote.h"
class Profile;
class IdentityManagerObserver;
// Initializes certificate database in Lacros-Chrome.
class CertDbInitializer : public KeyedService {
public: public:
CertDbInitializer( // TODO(miersh): implement `WaitUntilReady`.
mojo::Remote<crosapi::mojom::CertDatabase>& cert_database_remote, // virtual void WaitUntilReady() = 0;
Profile* profile);
~CertDbInitializer() override;
// Starts the initialization. The first step is to wait for
// IdentityManager.
void Start(signin::IdentityManager* identity_manager);
private:
// It is called when IdentityManager is ready.
void OnRefreshTokensLoaded();
// Checks that the current profile is the main profile and, if yes, makes a
// mojo request to Ash-Chrome to get information about certificate database.
void WaitForCertDbReady();
// Checks from the result that the certificate database should be initialized.
// If yes, loads Chaps and opens user's certificate database.
void OnCertDbInfoReceived(
crosapi::mojom::GetCertDatabaseInfoResultPtr result);
mojo::Remote<crosapi::mojom::CertDatabase>& cert_database_remote_;
Profile* profile_ = nullptr;
std::unique_ptr<IdentityManagerObserver> identity_manager_observer_;
base::WeakPtrFactory<CertDbInitializer> weak_factory_{this};
}; };
#endif // CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_H_ #endif // CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_H_
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
#include "chrome/browser/lacros/cert_db_initializer_factory.h" #include "chrome/browser/lacros/cert_db_initializer_factory.h"
#include "chrome/browser/lacros/cert_db_initializer.h" #include "chrome/browser/lacros/cert_db_initializer_impl.h"
#include "chrome/browser/profiles/profile.h" #include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h" #include "chrome/browser/signin/identity_manager_factory.h"
#include "chromeos/lacros/lacros_chrome_service_impl.h" #include "chromeos/lacros/lacros_chrome_service_impl.h"
...@@ -37,9 +37,7 @@ KeyedService* CertDbInitializerFactory::BuildServiceInstanceFor( ...@@ -37,9 +37,7 @@ KeyedService* CertDbInitializerFactory::BuildServiceInstanceFor(
return nullptr; return nullptr;
} }
CertDbInitializer* result = new CertDbInitializer( CertDbInitializerImpl* result = new CertDbInitializerImpl(profile);
chromeos::LacrosChromeServiceImpl::Get()->cert_database_remote(),
profile);
// TODO(crbug.com/1145946): Enable certificate database initialization when // TODO(crbug.com/1145946): Enable certificate database initialization when
// the policy stack is ready (expected to happen before Feb 2021). // the policy stack is ready (expected to happen before Feb 2021).
if (/* DISABLES CODE */ (false)) { if (/* DISABLES CODE */ (false)) {
......
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
#include "chrome/browser/lacros/cert_db_initializer.h" #include "chrome/browser/lacros/cert_db_initializer_impl.h"
#include "base/callback_forward.h" #include "base/callback_forward.h"
#include "base/check.h" #include "base/check.h"
...@@ -94,16 +94,16 @@ class IdentityManagerObserver : public signin::IdentityManager::Observer { ...@@ -94,16 +94,16 @@ class IdentityManagerObserver : public signin::IdentityManager::Observer {
// ============================================================================= // =============================================================================
CertDbInitializer::CertDbInitializer( CertDbInitializerImpl::CertDbInitializerImpl(Profile* profile)
mojo::Remote<crosapi::mojom::CertDatabase>& cert_database_remote, : profile_(profile) {
Profile* profile) DCHECK(chromeos::LacrosChromeServiceImpl::Get()->IsCertDbAvailable());
: cert_database_remote_(cert_database_remote), profile_(profile) {} }
CertDbInitializer::~CertDbInitializer() { CertDbInitializerImpl::~CertDbInitializerImpl() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
} }
void CertDbInitializer::Start(signin::IdentityManager* identity_manager) { void CertDbInitializerImpl::Start(signin::IdentityManager* identity_manager) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
DCHECK(identity_manager); DCHECK(identity_manager);
// TODO(crbug.com/1148300): This is temporary. Until ~2021 // TODO(crbug.com/1148300): This is temporary. Until ~2021
...@@ -114,31 +114,35 @@ void CertDbInitializer::Start(signin::IdentityManager* identity_manager) { ...@@ -114,31 +114,35 @@ void CertDbInitializer::Start(signin::IdentityManager* identity_manager) {
if (!identity_manager->AreRefreshTokensLoaded()) { if (!identity_manager->AreRefreshTokensLoaded()) {
identity_manager_observer_ = identity_manager_observer_ =
std::make_unique<IdentityManagerObserver>(identity_manager); std::make_unique<IdentityManagerObserver>(identity_manager);
identity_manager_observer_->WaitForRefreshTokensLoaded(base::BindOnce( identity_manager_observer_->WaitForRefreshTokensLoaded(
&CertDbInitializer::OnRefreshTokensLoaded, weak_factory_.GetWeakPtr())); base::BindOnce(&CertDbInitializerImpl::OnRefreshTokensLoaded,
weak_factory_.GetWeakPtr()));
return; return;
} }
WaitForCertDbReady(); WaitForCertDbReady();
} }
void CertDbInitializer::OnRefreshTokensLoaded() { void CertDbInitializerImpl::OnRefreshTokensLoaded() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
identity_manager_observer_.reset(); identity_manager_observer_.reset();
WaitForCertDbReady(); WaitForCertDbReady();
} }
void CertDbInitializer::WaitForCertDbReady() { void CertDbInitializerImpl::WaitForCertDbReady() {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
if (!profile_->IsMainProfile()) { if (!profile_->IsMainProfile()) {
return; return;
} }
cert_database_remote_->GetCertDatabaseInfo(base::BindOnce( chromeos::LacrosChromeServiceImpl::Get()
&CertDbInitializer::OnCertDbInfoReceived, weak_factory_.GetWeakPtr())); ->cert_database_remote()
->GetCertDatabaseInfo(
base::BindOnce(&CertDbInitializerImpl::OnCertDbInfoReceived,
weak_factory_.GetWeakPtr()));
} }
void CertDbInitializer::OnCertDbInfoReceived( void CertDbInitializerImpl::OnCertDbInfoReceived(
crosapi::mojom::GetCertDatabaseInfoResultPtr cert_db_info) { crosapi::mojom::GetCertDatabaseInfoResultPtr cert_db_info) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI); DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
......
// 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_LACROS_CERT_DB_INITIALIZER_IMPL_H_
#define CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_IMPL_H_
#include "base/memory/weak_ptr.h"
#include "chrome/browser/lacros/cert_db_initializer.h"
#include "chromeos/crosapi/mojom/cert_database.mojom.h"
#include "chromeos/lacros/lacros_chrome_service_impl.h"
#include "components/keyed_service/core/keyed_service.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#include "mojo/public/cpp/bindings/remote.h"
class Profile;
class IdentityManagerObserver;
// Initializes certificate database in Lacros-Chrome. Public methods should be
// called from the UI thread. Relies on CertDatabase mojo interface to be
// available.
class CertDbInitializerImpl : public CertDbInitializer, public KeyedService {
public:
explicit CertDbInitializerImpl(Profile* profile);
~CertDbInitializerImpl() override;
// Starts the initialization. The first step is to wait for
// IdentityManager.
void Start(signin::IdentityManager* identity_manager);
private:
// It is called when IdentityManager is ready.
void OnRefreshTokensLoaded();
// Checks that the current profile is the main profile and, if yes, makes a
// mojo request to Ash-Chrome to get information about certificate database.
void WaitForCertDbReady();
// Checks from the result that the certificate database should be initialized.
// If yes, loads Chaps and opens user's certificate database.
void OnCertDbInfoReceived(
crosapi::mojom::GetCertDatabaseInfoResultPtr result);
// This class is `KeyedService` based on the `Profile`. An instance is
// created together with a new profile and never outlives it.`
Profile* profile_ = nullptr;
std::unique_ptr<IdentityManagerObserver> identity_manager_observer_;
base::WeakPtrFactory<CertDbInitializerImpl> weak_factory_{this};
};
#endif // CHROME_BROWSER_LACROS_CERT_DB_INITIALIZER_IMPL_H_
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