File: test_extension_prefs.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 (240 lines) | stat: -rw-r--r-- 8,875 bytes parent folder | download | duplicates (5)
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// Copyright 2012 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/extensions/test_extension_prefs.h"

#include <memory>
#include <utility>

#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/functional/callback_helpers.h"
#include "base/run_loop.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequenced_task_runner.h"
#include "base/time/clock.h"
#include "base/time/time.h"
#include "base/values.h"
#include "chrome/browser/extensions/test_extension_system.h"
#include "chrome/browser/prefs/browser_prefs.h"
#include "chrome/browser/prefs/pref_service_syncable_util.h"
#include "chrome/common/chrome_constants.h"
#include "components/crx_file/id_util.h"
#include "components/pref_registry/pref_registry_syncable.h"
#include "components/prefs/json_pref_store.h"
#include "components/prefs/pref_value_store.h"
#include "components/sync/model/string_ordinal.h"
#include "components/sync_preferences/pref_service_mock_factory.h"
#include "components/sync_preferences/pref_service_syncable.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/browser/extension_pref_store.h"
#include "extensions/browser/extension_pref_value_map.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_prefs_factory.h"
#include "extensions/browser/extension_system.h"
#include "extensions/browser/extensions_browser_client.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/chrome_app_sorting.h"
#endif

using content::BrowserThread;
using extensions::mojom::ManifestLocation;

namespace extensions {

// A Clock which returns an incrementally later time each time Now() is called.
class TestExtensionPrefs::IncrementalClock : public base::Clock {
 public:
  IncrementalClock() : current_time_(base::Time::Now()) {}

  IncrementalClock(const IncrementalClock&) = delete;
  IncrementalClock& operator=(const IncrementalClock&) = delete;

  ~IncrementalClock() override = default;

  base::Time Now() const override {
    current_time_ += base::Seconds(10);
    return current_time_;
  }

 private:
  mutable base::Time current_time_;
};

TestExtensionPrefs::TestExtensionPrefs(
    const scoped_refptr<base::SequencedTaskRunner>& task_runner)
    : task_runner_(task_runner),
      clock_(std::make_unique<IncrementalClock>()),
      extensions_disabled_(false) {
  EXPECT_TRUE(temp_dir_.CreateUniqueTempDir());
  preferences_file_ = temp_dir_.GetPath().Append(chrome::kPreferencesFilename);
  extensions_dir_ = temp_dir_.GetPath().AppendASCII("Extensions");
  EXPECT_TRUE(base::CreateDirectory(extensions_dir_));

  ResetPrefRegistry();
  RecreateExtensionPrefs();
}

TestExtensionPrefs::~TestExtensionPrefs() = default;

ExtensionPrefs* TestExtensionPrefs::prefs() {
  return ExtensionPrefs::Get(&profile_);
}

TestingProfile* TestExtensionPrefs::profile() {
  return &profile_;
}

PrefService* TestExtensionPrefs::pref_service() {
  return pref_service_.get();
}

const scoped_refptr<user_prefs::PrefRegistrySyncable>&
TestExtensionPrefs::pref_registry() {
  return pref_registry_;
}

void TestExtensionPrefs::ResetPrefRegistry() {
  pref_registry_ = new user_prefs::PrefRegistrySyncable;
  RegisterUserProfilePrefs(pref_registry_.get());
}

void TestExtensionPrefs::RecreateExtensionPrefs() {
  // We persist and reload the PrefService's PrefStores because this process
  // deletes all empty dictionaries. The ExtensionPrefs implementation
  // needs to be able to handle this situation.
  if (pref_service_) {
    // Commit a pending write (which posts a task to task_runner_) and wait for
    // it to finish.
    pref_service_->CommitPendingWrite();
    base::RunLoop run_loop;
    ASSERT_TRUE(task_runner_->PostTaskAndReply(FROM_HERE, base::DoNothing(),
                                               run_loop.QuitClosure()));
    run_loop.Run();
  }

  extension_pref_value_map_ = std::make_unique<ExtensionPrefValueMap>();
  sync_preferences::PrefServiceMockFactory factory;
  factory.SetUserPrefsFile(preferences_file_, task_runner_.get());
  factory.set_extension_prefs(
      new ExtensionPrefStore(extension_pref_value_map_.get(), false));
  // Don't replace `pref_service_` until after re-assigning the `ExtensionPrefs`
  // testing instance to avoid a dangling pointer.
  std::unique_ptr<sync_preferences::PrefServiceSyncable> new_pref_service =
      factory.CreateSyncable(pref_registry_.get());
  std::unique_ptr<ExtensionPrefs> prefs(ExtensionPrefs::Create(
      &profile_, new_pref_service.get(), temp_dir_.GetPath(),
      extension_pref_value_map_.get(), extensions_disabled_,
      std::vector<EarlyExtensionPrefsObserver*>(),
      // Guarantee that no two extensions get the same installation time
      // stamp and we can reliably assert the installation order in the tests.
      clock_.get()));
  ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_,
                                                              std::move(prefs));
  pref_service_ = std::move(new_pref_service);
  // Hack: After recreating ExtensionPrefs, the AppSorting also needs to be
  // recreated. (ExtensionPrefs is never recreated in non-test code.)
  static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_))
      ->RecreateAppSorting();
}

scoped_refptr<Extension> TestExtensionPrefs::AddExtension(
    const std::string& name) {
  return AddExtensionWithLocation(name, ManifestLocation::kInternal);
}

scoped_refptr<Extension> TestExtensionPrefs::AddApp(const std::string& name) {
  base::Value::Dict dictionary;
  AddDefaultManifestKeys(name, dictionary);

  dictionary.SetByDottedPath(manifest_keys::kLaunchWebURL,
                             "http://example.com");

  return AddExtensionWithManifest(dictionary, ManifestLocation::kInternal);
}

scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithLocation(
    const std::string& name,
    ManifestLocation location) {
  base::Value::Dict dictionary;
  AddDefaultManifestKeys(name, dictionary);
  return AddExtensionWithManifest(dictionary, location);
}

scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest(
    const base::Value::Dict& manifest,
    ManifestLocation location) {
  return AddExtensionWithManifestAndFlags(manifest, location,
                                          Extension::NO_FLAGS);
}

scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
    const base::Value::Dict& manifest,
    ManifestLocation location,
    int extra_flags) {
  const std::string* name = manifest.FindString(manifest_keys::kName);
  EXPECT_TRUE(name);
  base::FilePath path = extensions_dir_.AppendASCII(*name);
  std::string errors;
  scoped_refptr<Extension> extension =
      Extension::Create(path, location, manifest, extra_flags, &errors);
  EXPECT_TRUE(extension.get()) << errors;
  if (!extension.get()) {
    return nullptr;
  }

  EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id()));
  prefs()->OnExtensionInstalled(extension.get(),
                                /*disable_reasons=*/{},
                                syncer::StringOrdinal::CreateInitialOrdinal(),
                                std::string());
  return extension;
}

std::string TestExtensionPrefs::AddExtensionAndReturnId(
    const std::string& name) {
  scoped_refptr<Extension> extension(AddExtension(name));
  return extension->id();
}

void TestExtensionPrefs::AddExtension(const Extension* extension) {
  prefs()->OnExtensionInstalled(extension,
                                /*disable_reasons=*/{},
                                syncer::StringOrdinal::CreateInitialOrdinal(),
                                std::string());
}

std::unique_ptr<PrefService> TestExtensionPrefs::CreateIncognitoPrefService()
    const {
  return CreateIncognitoPrefServiceSyncable(
      pref_service_.get(),
      new ExtensionPrefStore(extension_pref_value_map_.get(), true));
}

void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) {
  extensions_disabled_ = extensions_disabled;
}

ChromeAppSorting* TestExtensionPrefs::app_sorting() {
#if BUILDFLAG(ENABLE_EXTENSIONS)
  return static_cast<ChromeAppSorting*>(
      ExtensionSystem::Get(&profile_)->app_sorting());
#else
  // Android doesn't support Chrome Apps, hence has no app sorting.
  NOTREACHED();
#endif
}

void TestExtensionPrefs::AddDefaultManifestKeys(const std::string& name,
                                                base::Value::Dict& dict) {
  dict.Set(manifest_keys::kName, name);
  dict.Set(manifest_keys::kVersion, "0.1");
  dict.Set(manifest_keys::kManifestVersion, 2);
}

}  // namespace extensions