File: fake_nss_service.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (102 lines) | stat: -rw-r--r-- 3,385 bytes parent folder | download | duplicates (6)
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