1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/extensions/test_extension_prefs.h"
8 #include "base/bind_helpers.h"
9 #include "base/files/file_util.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/prefs/json_pref_store.h"
13 #include "base/prefs/pref_value_store.h"
14 #include "base/run_loop.h"
15 #include "base/sequenced_task_runner.h"
16 #include "base/synchronization/waitable_event.h"
17 #include "base/thread_task_runner_handle.h"
18 #include "base/values.h"
19 #include "chrome/browser/extensions/chrome_app_sorting.h"
20 #include "chrome/browser/extensions/test_extension_system.h"
21 #include "chrome/browser/prefs/pref_service_mock_factory.h"
22 #include "chrome/browser/prefs/pref_service_syncable.h"
23 #include "chrome/common/chrome_constants.h"
24 #include "components/crx_file/id_util.h"
25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "extensions/browser/extension_pref_store.h"
28 #include "extensions/browser/extension_pref_value_map.h"
29 #include "extensions/browser/extension_prefs.h"
30 #include "extensions/browser/extension_prefs_factory.h"
31 #include "extensions/browser/extension_system.h"
32 #include "extensions/browser/extensions_browser_client.h"
33 #include "extensions/common/extension.h"
34 #include "extensions/common/manifest_constants.h"
35 #include "sync/api/string_ordinal.h"
36 #include "testing/gtest/include/gtest/gtest.h"
38 using content::BrowserThread
;
40 namespace extensions
{
44 // A TimeProvider which returns an incrementally later time each time
45 // GetCurrentTime is called.
46 class IncrementalTimeProvider
: public ExtensionPrefs::TimeProvider
{
48 IncrementalTimeProvider() : current_time_(base::Time::Now()) {
51 ~IncrementalTimeProvider() override
{}
53 base::Time
GetCurrentTime() const override
{
54 current_time_
+= base::TimeDelta::FromSeconds(10);
59 DISALLOW_COPY_AND_ASSIGN(IncrementalTimeProvider
);
61 mutable base::Time current_time_
;
66 TestExtensionPrefs::TestExtensionPrefs(
67 const scoped_refptr
<base::SequencedTaskRunner
>& task_runner
)
68 : task_runner_(task_runner
), extensions_disabled_(false) {
69 EXPECT_TRUE(temp_dir_
.CreateUniqueTempDir());
70 preferences_file_
= temp_dir_
.path().Append(chrome::kPreferencesFilename
);
71 extensions_dir_
= temp_dir_
.path().AppendASCII("Extensions");
72 EXPECT_TRUE(base::CreateDirectory(extensions_dir_
));
75 RecreateExtensionPrefs();
78 TestExtensionPrefs::~TestExtensionPrefs() {
81 ExtensionPrefs
* TestExtensionPrefs::prefs() {
82 return ExtensionPrefs::Get(&profile_
);
85 PrefService
* TestExtensionPrefs::pref_service() {
86 return pref_service_
.get();
89 const scoped_refptr
<user_prefs::PrefRegistrySyncable
>&
90 TestExtensionPrefs::pref_registry() {
91 return pref_registry_
;
94 void TestExtensionPrefs::ResetPrefRegistry() {
95 pref_registry_
= new user_prefs::PrefRegistrySyncable
;
96 ExtensionPrefs::RegisterProfilePrefs(pref_registry_
.get());
99 void TestExtensionPrefs::RecreateExtensionPrefs() {
100 // We persist and reload the PrefService's PrefStores because this process
101 // deletes all empty dictionaries. The ExtensionPrefs implementation
102 // needs to be able to handle this situation.
104 // Commit a pending write (which posts a task to task_runner_) and wait for
106 pref_service_
->CommitPendingWrite();
107 base::RunLoop run_loop
;
109 task_runner_
->PostTaskAndReply(
111 base::Bind(&base::DoNothing
),
112 run_loop
.QuitClosure()));
116 extension_pref_value_map_
.reset(new ExtensionPrefValueMap
);
117 PrefServiceMockFactory factory
;
118 factory
.SetUserPrefsFile(preferences_file_
, task_runner_
.get());
119 factory
.set_extension_prefs(
120 new ExtensionPrefStore(extension_pref_value_map_
.get(), false));
121 pref_service_
= factory
.CreateSyncable(pref_registry_
.get()).Pass();
122 scoped_ptr
<ExtensionPrefs
> prefs(ExtensionPrefs::Create(
126 extension_pref_value_map_
.get(),
127 extensions_disabled_
,
128 std::vector
<ExtensionPrefsObserver
*>(),
129 // Guarantee that no two extensions get the same installation time
130 // stamp and we can reliably assert the installation order in the tests.
131 scoped_ptr
<ExtensionPrefs::TimeProvider
>(new IncrementalTimeProvider())));
132 ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_
,
134 // Hack: After recreating ExtensionPrefs, the AppSorting also needs to be
135 // recreated. (ExtensionPrefs is never recreated in non-test code.)
136 static_cast<TestExtensionSystem
*>(ExtensionSystem::Get(&profile_
))
137 ->RecreateAppSorting();
140 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtension(
141 const std::string
& name
) {
142 base::DictionaryValue dictionary
;
143 dictionary
.SetString(manifest_keys::kName
, name
);
144 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
145 return AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
148 scoped_refptr
<Extension
> TestExtensionPrefs::AddApp(const std::string
& name
) {
149 base::DictionaryValue dictionary
;
150 dictionary
.SetString(manifest_keys::kName
, name
);
151 dictionary
.SetString(manifest_keys::kVersion
, "0.1");
152 dictionary
.SetString(manifest_keys::kApp
, "true");
153 dictionary
.SetString(manifest_keys::kLaunchWebURL
, "http://example.com");
154 return AddExtensionWithManifest(dictionary
, Manifest::INTERNAL
);
158 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtensionWithManifest(
159 const base::DictionaryValue
& manifest
, Manifest::Location location
) {
160 return AddExtensionWithManifestAndFlags(manifest
, location
,
161 Extension::NO_FLAGS
);
164 scoped_refptr
<Extension
> TestExtensionPrefs::AddExtensionWithManifestAndFlags(
165 const base::DictionaryValue
& manifest
,
166 Manifest::Location location
,
169 EXPECT_TRUE(manifest
.GetString(manifest_keys::kName
, &name
));
170 base::FilePath path
= extensions_dir_
.AppendASCII(name
);
172 scoped_refptr
<Extension
> extension
= Extension::Create(
173 path
, location
, manifest
, extra_flags
, &errors
);
174 EXPECT_TRUE(extension
.get()) << errors
;
175 if (!extension
.get())
178 EXPECT_TRUE(crx_file::id_util::IdIsValid(extension
->id()));
179 prefs()->OnExtensionInstalled(extension
.get(),
181 syncer::StringOrdinal::CreateInitialOrdinal(),
186 std::string
TestExtensionPrefs::AddExtensionAndReturnId(
187 const std::string
& name
) {
188 scoped_refptr
<Extension
> extension(AddExtension(name
));
189 return extension
->id();
192 void TestExtensionPrefs::AddExtension(Extension
* extension
) {
193 prefs()->OnExtensionInstalled(extension
,
195 syncer::StringOrdinal::CreateInitialOrdinal(),
199 PrefService
* TestExtensionPrefs::CreateIncognitoPrefService() const {
200 return pref_service_
->CreateIncognitoPrefService(
201 new ExtensionPrefStore(extension_pref_value_map_
.get(), true));
204 void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled
) {
205 extensions_disabled_
= extensions_disabled
;
208 ChromeAppSorting
* TestExtensionPrefs::app_sorting() {
209 return static_cast<ChromeAppSorting
*>(
210 ExtensionSystem::Get(&profile_
)->app_sorting());
213 } // namespace extensions