xref: /aosp_15_r20/external/cronet/components/prefs/testing_pref_service.cc (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/prefs/testing_pref_service.h"
6 
7 #include <memory>
8 
9 #include "base/compiler_specific.h"
10 #include "base/functional/bind.h"
11 #include "components/prefs/default_pref_store.h"
12 #include "components/prefs/pref_notifier_impl.h"
13 #include "components/prefs/pref_registry_simple.h"
14 #include "components/prefs/pref_value_store.h"
15 #include "testing/gtest/include/gtest/gtest.h"
16 
17 template <>
TestingPrefServiceBase(scoped_refptr<TestingPrefStore> managed_prefs,scoped_refptr<TestingPrefStore> supervised_user_prefs,scoped_refptr<TestingPrefStore> extension_prefs,scoped_refptr<TestingPrefStore> standalone_browser_prefs,scoped_refptr<TestingPrefStore> user_prefs,scoped_refptr<TestingPrefStore> recommended_prefs,scoped_refptr<PrefRegistry> pref_registry,PrefNotifierImpl * pref_notifier)18 TestingPrefServiceBase<PrefService, PrefRegistry>::TestingPrefServiceBase(
19     scoped_refptr<TestingPrefStore> managed_prefs,
20     scoped_refptr<TestingPrefStore> supervised_user_prefs,
21     scoped_refptr<TestingPrefStore> extension_prefs,
22     scoped_refptr<TestingPrefStore> standalone_browser_prefs,
23     scoped_refptr<TestingPrefStore> user_prefs,
24     scoped_refptr<TestingPrefStore> recommended_prefs,
25     scoped_refptr<PrefRegistry> pref_registry,
26     PrefNotifierImpl* pref_notifier)
27     : PrefService(
28           // Warning: `pref_notifier` is used for 2 arguments and the order of
29           // computation isn't guaranteed. So making it a unique_ptr would cause
30           // std::unique_ptr<>::get() after std::move().
31           std::unique_ptr<PrefNotifierImpl>(pref_notifier),
32           std::make_unique<PrefValueStore>(managed_prefs.get(),
33                                            supervised_user_prefs.get(),
34                                            extension_prefs.get(),
35                                            standalone_browser_prefs.get(),
36                                            /*command_line_prefs=*/nullptr,
37                                            user_prefs.get(),
38                                            recommended_prefs.get(),
39                                            pref_registry->defaults().get(),
40                                            pref_notifier),
41           user_prefs,
42           standalone_browser_prefs,
43           pref_registry,
44           base::BindRepeating(
45               &TestingPrefServiceBase<PrefService,
46                                       PrefRegistry>::HandleReadError),
47           false),
48       managed_prefs_(managed_prefs),
49       supervised_user_prefs_(supervised_user_prefs),
50       extension_prefs_(extension_prefs),
51       standalone_browser_prefs_(standalone_browser_prefs),
52       user_prefs_(user_prefs),
53       recommended_prefs_(recommended_prefs) {}
54 
TestingPrefServiceSimple()55 TestingPrefServiceSimple::TestingPrefServiceSimple()
56     : TestingPrefServiceBase<PrefService, PrefRegistry>(
57           /*managed_prefs=*/base::MakeRefCounted<TestingPrefStore>(),
58           /*supervised_user_prefs=*/base::MakeRefCounted<TestingPrefStore>(),
59           /*extension_prefs=*/base::MakeRefCounted<TestingPrefStore>(),
60           /*standalone_browser_prefs=*/base::MakeRefCounted<TestingPrefStore>(),
61           /*user_prefs=*/base::MakeRefCounted<TestingPrefStore>(),
62           /*recommended_prefs=*/base::MakeRefCounted<TestingPrefStore>(),
63           base::MakeRefCounted<PrefRegistrySimple>(),
64           new PrefNotifierImpl()) {}
65 
~TestingPrefServiceSimple()66 TestingPrefServiceSimple::~TestingPrefServiceSimple() {
67 }
68 
registry()69 PrefRegistrySimple* TestingPrefServiceSimple::registry() {
70   return static_cast<PrefRegistrySimple*>(DeprecatedGetPrefRegistry());
71 }
72