1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/trusted_vault/trusted_vault_service_factory.h"
#include <memory>
#include "base/functional/callback.h"
#include "build/build_config.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/signin/identity_manager_factory.h"
#include "components/trusted_vault/trusted_vault_server_constants.h"
#include "components/trusted_vault/trusted_vault_service.h"
#include "device/fido/features.h"
#if BUILDFLAG(IS_ANDROID)
#include "chrome/browser/trusted_vault/trusted_vault_client_android.h"
#include "components/signin/public/identity_manager/account_info.h"
#include "components/signin/public/identity_manager/identity_manager.h"
#else
#include "base/files/file_path.h"
#include "components/trusted_vault/standalone_trusted_vault_client.h"
#include "content/public/browser/storage_partition.h"
#endif
#if BUILDFLAG(IS_MAC)
#include "chrome/common/chrome_version.h"
#endif
namespace {
#if BUILDFLAG(IS_MAC)
constexpr char kICloudKeychainAccessGroupPrefix[] = MAC_TEAM_IDENTIFIER_STRING;
#endif
#if !BUILDFLAG(IS_ANDROID)
std::unique_ptr<trusted_vault::TrustedVaultClient>
CreateChromeSyncStandaloneTrustedVaultClient(Profile* profile) {
return std::make_unique<trusted_vault::StandaloneTrustedVaultClient>(
#if BUILDFLAG(IS_MAC)
kICloudKeychainAccessGroupPrefix,
#endif
trusted_vault::SecurityDomainId::kChromeSync,
/*base_dir=*/profile->GetPath(),
IdentityManagerFactory::GetForProfile(profile),
profile->GetDefaultStoragePartition()
->GetURLLoaderFactoryForBrowserProcess());
}
#endif
std::unique_ptr<trusted_vault::TrustedVaultClient>
CreateChromeSyncTrustedVaultClient(Profile* profile) {
#if BUILDFLAG(IS_ANDROID)
return std::make_unique<
TrustedVaultClientAndroid>(/*gaia_account_info_by_gaia_id_cb=*/
base::BindRepeating(
[](signin::IdentityManager*
identity_manager,
const GaiaId& gaia_id)
-> CoreAccountInfo {
return identity_manager
->FindExtendedAccountInfoByGaiaId(
gaia_id);
},
IdentityManagerFactory::GetForProfile(
profile)));
#else
return CreateChromeSyncStandaloneTrustedVaultClient(profile);
#endif
}
std::unique_ptr<KeyedService> BuildTrustedVaultService(
content::BrowserContext* context) {
Profile* profile = Profile::FromBrowserContext(context);
CHECK(!profile->IsOffTheRecord());
return std::make_unique<trusted_vault::TrustedVaultService>(
CreateChromeSyncTrustedVaultClient(profile));
}
} // namespace
// static
trusted_vault::TrustedVaultService* TrustedVaultServiceFactory::GetForProfile(
Profile* profile) {
return static_cast<trusted_vault::TrustedVaultService*>(
GetInstance()->GetServiceForBrowserContext(profile, /*create=*/true));
}
// static
TrustedVaultServiceFactory* TrustedVaultServiceFactory::GetInstance() {
static base::NoDestructor<TrustedVaultServiceFactory> instance;
return instance.get();
}
// static
BrowserContextKeyedServiceFactory::TestingFactory
TrustedVaultServiceFactory::GetDefaultFactory() {
return base::BindRepeating(&BuildTrustedVaultService);
}
TrustedVaultServiceFactory::TrustedVaultServiceFactory()
: ProfileKeyedServiceFactory(
"TrustedVaultService",
ProfileSelections::Builder()
.WithRegular(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/40257657): Check if this service is needed in
// Guest mode. Currently it is required due to dependant services
// (e.g. SyncService) that have similar TODO, if they stop being
// used in Guest mode, this service could stop to be used as well.
.WithGuest(ProfileSelection::kOriginalOnly)
// TODO(crbug.com/41488885): Check if this service is needed for
// Ash Internals.
.WithAshInternals(ProfileSelection::kOriginalOnly)
.Build()) {
DependsOn(IdentityManagerFactory::GetInstance());
}
TrustedVaultServiceFactory::~TrustedVaultServiceFactory() = default;
std::unique_ptr<KeyedService>
TrustedVaultServiceFactory::BuildServiceInstanceForBrowserContext(
content::BrowserContext* context) const {
return BuildTrustedVaultService(context);
}
bool TrustedVaultServiceFactory::ServiceIsNULLWhileTesting() const {
return true;
}
|