[email protected] | a52c0e9 | 2012-03-23 06:02:24 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 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 "chrome/browser/extensions/test_extension_prefs.h" |
| 6 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 7 | #include <memory> |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 8 | #include <utility> |
| 9 | |
[email protected] | 406af39 | 2011-11-25 18:19:38 | [diff] [blame] | 10 | #include "base/bind.h" |
| 11 | #include "base/bind_helpers.h" |
thestig | 18dfb7a5 | 2014-08-26 10:44:04 | [diff] [blame] | 12 | #include "base/files/file_util.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 13 | #include "base/macros.h" |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 14 | #include "base/run_loop.h" |
[email protected] | fb44196 | 2013-05-08 05:35:24 | [diff] [blame] | 15 | #include "base/sequenced_task_runner.h" |
[email protected] | 6cad5bf | 2011-03-10 21:21:55 | [diff] [blame] | 16 | #include "base/synchronization/waitable_event.h" |
gab | b15e1907 | 2016-05-11 20:45:41 | [diff] [blame] | 17 | #include "base/threading/thread_task_runner_handle.h" |
Devlin Cronin | 4a3c38f | 2017-09-21 02:32:00 | [diff] [blame] | 18 | #include "base/time/clock.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 19 | #include "base/values.h" |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 20 | #include "chrome/browser/extensions/chrome_app_sorting.h" |
| 21 | #include "chrome/browser/extensions/test_extension_system.h" |
Minh X. Nguyen | 3aa4069 | 2018-03-28 01:15:59 | [diff] [blame] | 22 | #include "chrome/browser/prefs/browser_prefs.h" |
sdefresne | 0b1722f0 | 2015-09-14 18:12:12 | [diff] [blame] | 23 | #include "chrome/browser/prefs/pref_service_syncable_util.h" |
[email protected] | e96a060 | 2014-02-15 08:27:42 | [diff] [blame] | 24 | #include "chrome/common/chrome_constants.h" |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 25 | #include "components/crx_file/id_util.h" |
[email protected] | f0c8c499 | 2014-05-15 17:37:26 | [diff] [blame] | 26 | #include "components/pref_registry/pref_registry_syncable.h" |
brettw | b1fc1b8 | 2016-02-02 00:19:08 | [diff] [blame] | 27 | #include "components/prefs/json_pref_store.h" |
| 28 | #include "components/prefs/pref_value_store.h" |
skym | 7160384 | 2016-10-10 18:17:31 | [diff] [blame] | 29 | #include "components/sync/model/string_ordinal.h" |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 30 | #include "components/sync_preferences/pref_service_mock_factory.h" |
| 31 | #include "components/sync_preferences/pref_service_syncable.h" |
[email protected] | c38831a1 | 2011-10-28 12:44:49 | [diff] [blame] | 32 | #include "content/public/browser/browser_thread.h" |
[email protected] | 489db084 | 2014-01-22 18:20:03 | [diff] [blame] | 33 | #include "extensions/browser/extension_pref_store.h" |
| 34 | #include "extensions/browser/extension_pref_value_map.h" |
| 35 | #include "extensions/browser/extension_prefs.h" |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 36 | #include "extensions/browser/extension_prefs_factory.h" |
| 37 | #include "extensions/browser/extension_system.h" |
[email protected] | bf5ee7cc | 2013-11-23 20:48:44 | [diff] [blame] | 38 | #include "extensions/browser/extensions_browser_client.h" |
[email protected] | e4452d3 | 2013-11-15 23:07:41 | [diff] [blame] | 39 | #include "extensions/common/extension.h" |
[email protected] | 6bf9061 | 2013-08-15 00:36:27 | [diff] [blame] | 40 | #include "extensions/common/manifest_constants.h" |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 41 | #include "testing/gtest/include/gtest/gtest.h" |
| 42 | |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 43 | using content::BrowserThread; |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 44 | |
| 45 | namespace extensions { |
[email protected] | 631bb74 | 2011-11-02 11:29:39 | [diff] [blame] | 46 | |
Devlin Cronin | 4a3c38f | 2017-09-21 02:32:00 | [diff] [blame] | 47 | // A Clock which returns an incrementally later time each time Now() is called. |
tzik | 9a2f9c0 | 2018-03-22 04:37:31 | [diff] [blame] | 48 | class TestExtensionPrefs::IncrementalClock : public base::Clock { |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 49 | public: |
Devlin Cronin | 4a3c38f | 2017-09-21 02:32:00 | [diff] [blame] | 50 | IncrementalClock() : current_time_(base::Time::Now()) {} |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 51 | |
Devlin Cronin | 4a3c38f | 2017-09-21 02:32:00 | [diff] [blame] | 52 | ~IncrementalClock() override {} |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 53 | |
tzik | ee78e496 | 2018-04-13 12:25:46 | [diff] [blame] | 54 | base::Time Now() const override { |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 55 | current_time_ += base::TimeDelta::FromSeconds(10); |
| 56 | return current_time_; |
| 57 | } |
| 58 | |
| 59 | private: |
tzik | ee78e496 | 2018-04-13 12:25:46 | [diff] [blame] | 60 | mutable base::Time current_time_; |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 61 | |
Devlin Cronin | 4a3c38f | 2017-09-21 02:32:00 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(IncrementalClock); |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 63 | }; |
| 64 | |
dcheng | 605ef8d | 2014-08-28 18:29:44 | [diff] [blame] | 65 | TestExtensionPrefs::TestExtensionPrefs( |
| 66 | const scoped_refptr<base::SequencedTaskRunner>& task_runner) |
tzik | 9a2f9c0 | 2018-03-22 04:37:31 | [diff] [blame] | 67 | : task_runner_(task_runner), |
| 68 | clock_(std::make_unique<IncrementalClock>()), |
| 69 | extensions_disabled_(false) { |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 70 | EXPECT_TRUE(temp_dir_.CreateUniqueTempDir()); |
vabr | 9142fe2 | 2016-09-08 13:19:22 | [diff] [blame] | 71 | preferences_file_ = temp_dir_.GetPath().Append(chrome::kPreferencesFilename); |
| 72 | extensions_dir_ = temp_dir_.GetPath().AppendASCII("Extensions"); |
[email protected] | 426d1c9 | 2013-12-03 20:08:54 | [diff] [blame] | 73 | EXPECT_TRUE(base::CreateDirectory(extensions_dir_)); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 74 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 75 | ResetPrefRegistry(); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 76 | RecreateExtensionPrefs(); |
| 77 | } |
| 78 | |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 79 | TestExtensionPrefs::~TestExtensionPrefs() { |
| 80 | } |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 81 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 82 | ExtensionPrefs* TestExtensionPrefs::prefs() { |
| 83 | return ExtensionPrefs::Get(&profile_); |
| 84 | } |
| 85 | |
Minh X. Nguyen | 3aa4069 | 2018-03-28 01:15:59 | [diff] [blame] | 86 | TestingProfile* TestExtensionPrefs::profile() { |
| 87 | return &profile_; |
| 88 | } |
| 89 | |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 90 | PrefService* TestExtensionPrefs::pref_service() { |
| 91 | return pref_service_.get(); |
| 92 | } |
| 93 | |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 94 | const scoped_refptr<user_prefs::PrefRegistrySyncable>& |
| 95 | TestExtensionPrefs::pref_registry() { |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 96 | return pref_registry_; |
| 97 | } |
| 98 | |
| 99 | void TestExtensionPrefs::ResetPrefRegistry() { |
[email protected] | 443e931 | 2013-05-06 06:17:34 | [diff] [blame] | 100 | pref_registry_ = new user_prefs::PrefRegistrySyncable; |
Minh X. Nguyen | 3aa4069 | 2018-03-28 01:15:59 | [diff] [blame] | 101 | RegisterUserProfilePrefs(pref_registry_.get()); |
[email protected] | c753f14 | 2013-02-10 13:14:04 | [diff] [blame] | 102 | } |
| 103 | |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 104 | void TestExtensionPrefs::RecreateExtensionPrefs() { |
[email protected] | 73c4793 | 2010-12-06 18:13:43 | [diff] [blame] | 105 | // We persist and reload the PrefService's PrefStores because this process |
| 106 | // deletes all empty dictionaries. The ExtensionPrefs implementation |
| 107 | // needs to be able to handle this situation. |
[email protected] | 3eeddd89 | 2013-04-17 17:00:11 | [diff] [blame] | 108 | if (pref_service_) { |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 109 | // Commit a pending write (which posts a task to task_runner_) and wait for |
| 110 | // it to finish. |
| 111 | pref_service_->CommitPendingWrite(); |
| 112 | base::RunLoop run_loop; |
Peter Kasting | 341e1fb | 2018-02-24 00:03:01 | [diff] [blame] | 113 | ASSERT_TRUE(task_runner_->PostTaskAndReply(FROM_HERE, base::DoNothing(), |
| 114 | run_loop.QuitClosure())); |
[email protected] | 0de615a | 2012-11-08 04:40:59 | [diff] [blame] | 115 | run_loop.Run(); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 116 | } |
| 117 | |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 118 | extension_pref_value_map_.reset(new ExtensionPrefValueMap); |
maxbogue | ea16ff41 | 2016-10-28 16:35:29 | [diff] [blame] | 119 | sync_preferences::PrefServiceMockFactory factory; |
[email protected] | e90a01f | 2013-11-19 04:22:12 | [diff] [blame] | 120 | factory.SetUserPrefsFile(preferences_file_, task_runner_.get()); |
| 121 | factory.set_extension_prefs( |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 122 | new ExtensionPrefStore(extension_pref_value_map_.get(), false)); |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 123 | pref_service_ = factory.CreateSyncable(pref_registry_.get()); |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 124 | std::unique_ptr<ExtensionPrefs> prefs(ExtensionPrefs::Create( |
vabr | 9142fe2 | 2016-09-08 13:19:22 | [diff] [blame] | 125 | &profile_, pref_service_.get(), temp_dir_.GetPath(), |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 126 | extension_pref_value_map_.get(), extensions_disabled_, |
Nigel Tao | 650b731 | 2019-04-27 00:18:32 | [diff] [blame] | 127 | std::vector<EarlyExtensionPrefsObserver*>(), |
[email protected] | fdd679b | 2012-11-15 20:49:39 | [diff] [blame] | 128 | // Guarantee that no two extensions get the same installation time |
| 129 | // stamp and we can reliably assert the installation order in the tests. |
tzik | 9a2f9c0 | 2018-03-22 04:37:31 | [diff] [blame] | 130 | clock_.get())); |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 131 | ExtensionPrefsFactory::GetInstance()->SetInstanceForTesting(&profile_, |
dcheng | 1fc00f1 | 2015-12-26 22:18:03 | [diff] [blame] | 132 | std::move(prefs)); |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 133 | // Hack: After recreating ExtensionPrefs, the AppSorting also needs to be |
| 134 | // recreated. (ExtensionPrefs is never recreated in non-test code.) |
| 135 | static_cast<TestExtensionSystem*>(ExtensionSystem::Get(&profile_)) |
| 136 | ->RecreateAppSorting(); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 137 | } |
| 138 | |
[email protected] | 0bb29bd | 2014-04-30 21:39:18 | [diff] [blame] | 139 | scoped_refptr<Extension> TestExtensionPrefs::AddExtension( |
| 140 | const std::string& name) { |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 141 | base::DictionaryValue dictionary; |
[email protected] | 6bf9061 | 2013-08-15 00:36:27 | [diff] [blame] | 142 | dictionary.SetString(manifest_keys::kName, name); |
| 143 | dictionary.SetString(manifest_keys::kVersion, "0.1"); |
Devlin Cronin | 1fa235b6 | 2018-04-12 14:04:07 | [diff] [blame] | 144 | dictionary.SetInteger(manifest_keys::kManifestVersion, 2); |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 145 | return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 146 | } |
| 147 | |
[email protected] | 0bb29bd | 2014-04-30 21:39:18 | [diff] [blame] | 148 | scoped_refptr<Extension> TestExtensionPrefs::AddApp(const std::string& name) { |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 149 | base::DictionaryValue dictionary; |
[email protected] | 6bf9061 | 2013-08-15 00:36:27 | [diff] [blame] | 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"); |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 154 | return AddExtensionWithManifest(dictionary, Manifest::INTERNAL); |
[email protected] | f1b6665 | 2011-08-25 23:30:58 | [diff] [blame] | 155 | |
| 156 | } |
| 157 | |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 158 | scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifest( |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 159 | const base::DictionaryValue& manifest, Manifest::Location location) { |
[email protected] | e805baf | 2011-07-26 18:23:05 | [diff] [blame] | 160 | return AddExtensionWithManifestAndFlags(manifest, location, |
[email protected] | ed3b9b1 | 2012-05-31 18:37:51 | [diff] [blame] | 161 | Extension::NO_FLAGS); |
[email protected] | e805baf | 2011-07-26 18:23:05 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | scoped_refptr<Extension> TestExtensionPrefs::AddExtensionWithManifestAndFlags( |
[email protected] | cb1078de | 2013-12-23 20:04:22 | [diff] [blame] | 165 | const base::DictionaryValue& manifest, |
[email protected] | 1d5e58b | 2013-01-31 08:41:40 | [diff] [blame] | 166 | Manifest::Location location, |
[email protected] | e805baf | 2011-07-26 18:23:05 | [diff] [blame] | 167 | int extra_flags) { |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 168 | std::string name; |
[email protected] | 6bf9061 | 2013-08-15 00:36:27 | [diff] [blame] | 169 | EXPECT_TRUE(manifest.GetString(manifest_keys::kName, &name)); |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 170 | base::FilePath path = extensions_dir_.AppendASCII(name); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 171 | std::string errors; |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 172 | scoped_refptr<Extension> extension = Extension::Create( |
[email protected] | e805baf | 2011-07-26 18:23:05 | [diff] [blame] | 173 | path, location, manifest, extra_flags, &errors); |
[email protected] | dc24976f | 2013-06-02 21:15:09 | [diff] [blame] | 174 | EXPECT_TRUE(extension.get()) << errors; |
| 175 | if (!extension.get()) |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 176 | return NULL; |
| 177 | |
[email protected] | fdd2837 | 2014-08-21 02:27:26 | [diff] [blame] | 178 | EXPECT_TRUE(crx_file::id_util::IdIsValid(extension->id())); |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 179 | prefs()->OnExtensionInstalled(extension.get(), |
| 180 | Extension::ENABLED, |
| 181 | syncer::StringOrdinal::CreateInitialOrdinal(), |
| 182 | std::string()); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 183 | return extension; |
| 184 | } |
| 185 | |
[email protected] | 0bb29bd | 2014-04-30 21:39:18 | [diff] [blame] | 186 | std::string TestExtensionPrefs::AddExtensionAndReturnId( |
| 187 | const std::string& name) { |
[email protected] | 66e4eb3 | 2010-10-27 20:37:41 | [diff] [blame] | 188 | scoped_refptr<Extension> extension(AddExtension(name)); |
[email protected] | 63c64d1 | 2010-04-27 21:21:34 | [diff] [blame] | 189 | return extension->id(); |
| 190 | } |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 191 | |
Devlin Cronin | 8e5892f | 2018-10-04 00:13:43 | [diff] [blame] | 192 | void TestExtensionPrefs::AddExtension(const Extension* extension) { |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 193 | prefs()->OnExtensionInstalled(extension, |
| 194 | Extension::ENABLED, |
| 195 | syncer::StringOrdinal::CreateInitialOrdinal(), |
| 196 | std::string()); |
deepak.m1 | 75277b0 | 2015-04-15 06:30:11 | [diff] [blame] | 197 | } |
| 198 | |
François Degros | 9d35fc57 | 2018-02-05 22:32:45 | [diff] [blame] | 199 | std::unique_ptr<PrefService> TestExtensionPrefs::CreateIncognitoPrefService() |
| 200 | const { |
sdefresne | 0b1722f0 | 2015-09-14 18:12:12 | [diff] [blame] | 201 | return CreateIncognitoPrefServiceSyncable( |
| 202 | pref_service_.get(), |
Sam McNally | 538fca1e | 2017-07-14 03:10:43 | [diff] [blame] | 203 | new ExtensionPrefStore(extension_pref_value_map_.get(), true), nullptr); |
[email protected] | 9a8c402 | 2011-01-25 14:25:33 | [diff] [blame] | 204 | } |
[email protected] | 9e33d7e | 2011-09-30 16:43:54 | [diff] [blame] | 205 | |
| 206 | void TestExtensionPrefs::set_extensions_disabled(bool extensions_disabled) { |
| 207 | extensions_disabled_ = extensions_disabled; |
| 208 | } |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 209 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 210 | ChromeAppSorting* TestExtensionPrefs::app_sorting() { |
| 211 | return static_cast<ChromeAppSorting*>( |
| 212 | ExtensionSystem::Get(&profile_)->app_sorting()); |
| 213 | } |
| 214 | |
[email protected] | 4575961 | 2012-07-10 17:21:23 | [diff] [blame] | 215 | } // namespace extensions |