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
|
// Copyright 2022 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/net/fake_nss_service.h"
#include <memory>
#include "chrome/browser/net/nss_service_factory.h"
#include "content/public/browser/browser_thread.h"
#include "crypto/scoped_test_nss_db.h"
#include "net/cert/nss_cert_database.h"
#include "nss_service.h"
#if BUILDFLAG(IS_CHROMEOS)
#include "net/cert/nss_cert_database_chromeos.h"
#endif
namespace {
net::NSSCertDatabase* NssGetterForIOThread(
net::NSSCertDatabase* result,
base::OnceCallback<void(net::NSSCertDatabase*)>) {
// The check is here because the real NSS getter must also be run on the IO
// thread.
DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
return result;
}
std::unique_ptr<KeyedService> CreateService(bool enable_system_slot,
content::BrowserContext* context) {
return std::make_unique<FakeNssService>(context, enable_system_slot);
}
} // namespace
#if BUILDFLAG(IS_CHROMEOS)
// static
FakeNssService* FakeNssService::InitializeForBrowserContext(
content::BrowserContext* context,
bool enable_system_slot) {
KeyedService* service =
NssServiceFactory::GetInstance()->SetTestingFactoryAndUse(
context, base::BindRepeating(&CreateService, enable_system_slot));
return static_cast<FakeNssService*>(service);
}
#else
// static
FakeNssService* FakeNssService::InitializeForBrowserContext(
content::BrowserContext* context) {
KeyedService* service =
NssServiceFactory::GetInstance()->SetTestingFactoryAndUse(
context, base::BindRepeating(&CreateService, false));
return static_cast<FakeNssService*>(service);
}
#endif
FakeNssService::FakeNssService(content::BrowserContext* context,
bool enable_system_slot)
: NssService(context) {
public_slot_ = std::make_unique<crypto::ScopedTestNSSDB>();
#if BUILDFLAG(IS_CHROMEOS)
private_slot_ = std::make_unique<crypto::ScopedTestNSSDB>();
auto cert_db = std::make_unique<net::NSSCertDatabaseChromeOS>(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(public_slot_->slot())),
crypto::ScopedPK11Slot(PK11_ReferenceSlot(private_slot_->slot())));
if (enable_system_slot) {
system_slot_ = std::make_unique<crypto::ScopedTestNSSDB>();
cert_db->SetSystemSlot(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(system_slot_->slot())));
}
nss_cert_database_ = std::move(cert_db);
#else
nss_cert_database_ = std::make_unique<net::NSSCertDatabase>(
crypto::ScopedPK11Slot(PK11_ReferenceSlot(public_slot_->slot())),
crypto::ScopedPK11Slot(PK11_ReferenceSlot(public_slot_->slot())));
#endif
}
FakeNssService::~FakeNssService() {
content::GetIOThreadTaskRunner({})->DeleteSoon(FROM_HERE,
std::move(nss_cert_database_));
}
NssCertDatabaseGetter FakeNssService::CreateNSSCertDatabaseGetterForIOThread() {
return base::BindOnce(&NssGetterForIOThread, nss_cert_database_.get());
}
PK11SlotInfo* FakeNssService::GetPublicSlot() const {
return public_slot_->slot();
}
#if BUILDFLAG(IS_CHROMEOS)
PK11SlotInfo* FakeNssService::GetPrivateSlot() const {
return private_slot_->slot();
}
PK11SlotInfo* FakeNssService::GetSystemSlot() const {
return system_slot_->slot();
}
#endif
|