OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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. |
| 4 |
| 5 #include "base/file_util.h" |
| 6 #include "base/prefs/scoped_user_pref_update.h" |
| 7 #include "base/prefs/testing_pref_service.h" |
| 8 #include "base/strings/string_util.h" |
| 9 #include "base/threading/sequenced_worker_pool.h" |
| 10 #include "base/values.h" |
| 11 #include "chrome/browser/chromeos/login/users/fake_user_manager.h" |
| 12 #include "chrome/browser/chromeos/login/users/user_manager.h" |
| 13 #include "chrome/browser/extensions/extension_assets_manager_chromeos.h" |
| 14 #include "chrome/browser/extensions/extension_garbage_collector_chromeos.h" |
| 15 #include "chrome/browser/extensions/extension_service_unittest.h" |
| 16 #include "chrome/browser/prefs/browser_prefs.h" |
| 17 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/test/base/testing_browser_process.h" |
| 19 #include "chrome/test/base/testing_profile.h" |
| 20 #include "content/public/browser/browser_thread.h" |
| 21 #include "content/public/browser/plugin_service.h" |
| 22 #include "extensions/browser/extension_prefs.h" |
| 23 #include "extensions/common/manifest_constants.h" |
| 24 |
| 25 namespace { |
| 26 const char kExtensionId1[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 27 const char kExtensionId2[] = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"; |
| 28 } // namespace |
| 29 |
| 30 namespace extensions { |
| 31 |
| 32 class ExtensionGarbageCollectorChromeOSUnitTest |
| 33 : public ExtensionServiceTestBase { |
| 34 protected: |
| 35 PrefService& local_state() { return local_state_; } |
| 36 const base::FilePath& cache_dir() { return cache_dir_.path(); } |
| 37 |
| 38 virtual void SetUp() OVERRIDE { |
| 39 TestingBrowserProcess::GetGlobal()->SetLocalState(&local_state_); |
| 40 chrome::RegisterLocalState(local_state_.registry()); |
| 41 |
| 42 #if defined(ENABLE_PLUGINS) |
| 43 content::PluginService::GetInstance()->Init(); |
| 44 #endif |
| 45 InitializeGoodInstalledExtensionService(); |
| 46 |
| 47 // Need real IO thread. |
| 48 service_->SetFileTaskRunnerForTesting( |
| 49 content::BrowserThread::GetBlockingPool() |
| 50 ->GetSequencedTaskRunnerWithShutdownBehavior( |
| 51 content::BrowserThread::GetBlockingPool() |
| 52 ->GetNamedSequenceToken("ext_install-"), |
| 53 base::SequencedWorkerPool::SKIP_ON_SHUTDOWN)); |
| 54 |
| 55 CHECK(cache_dir_.CreateUniqueTempDir()); |
| 56 ExtensionAssetsManagerChromeOS::SetSharedInstallDirForTesting(cache_dir()); |
| 57 ExtensionGarbageCollectorChromeOS::ClearGarbageCollectedForTesting(); |
| 58 |
| 59 // Initialize the UserManager singleton to a fresh FakeUserManager instance. |
| 60 user_manager_enabler_.reset( |
| 61 new chromeos::ScopedUserManagerEnabler(new chromeos::FakeUserManager)); |
| 62 |
| 63 GetFakeUserManager()->AddUser(chromeos::UserManager::kStubUser); |
| 64 GetFakeUserManager()->LoginUser(chromeos::UserManager::kStubUser); |
| 65 GetFakeUserManager()->SetProfileForUser( |
| 66 GetFakeUserManager()->GetActiveUser(), profile_.get()); |
| 67 } |
| 68 |
| 69 virtual void TearDown() OVERRIDE { |
| 70 TestingBrowserProcess::GetGlobal()->SetLocalState(NULL); |
| 71 } |
| 72 |
| 73 void GarbageCollectExtensions() { |
| 74 ExtensionGarbageCollector::Get(profile_.get()) |
| 75 ->GarbageCollectExtensionsForTest(); |
| 76 // Wait for GarbageCollectExtensions task to complete. |
| 77 content::BrowserThread::GetBlockingPool()->FlushForTesting(); |
| 78 } |
| 79 |
| 80 base::FilePath CreateSharedExtensionDir(const std::string& id, |
| 81 const std::string& version, |
| 82 const base::FilePath& shared_dir) { |
| 83 base::FilePath path = shared_dir.AppendASCII(id).AppendASCII(version); |
| 84 CreateDirectory(path); |
| 85 return path; |
| 86 } |
| 87 |
| 88 void CreateSharedExtensionPrefs(const std::string& id, |
| 89 const std::string& version, |
| 90 const std::string& users_string, |
| 91 const base::FilePath& path) { |
| 92 DictionaryPrefUpdate shared_extensions(&local_state_, |
| 93 ExtensionAssetsManagerChromeOS::kSharedExtensions); |
| 94 |
| 95 base::DictionaryValue* extension_info = NULL; |
| 96 if (!shared_extensions->GetDictionary(id, &extension_info)) { |
| 97 extension_info = new base::DictionaryValue; |
| 98 shared_extensions->Set(id, extension_info); |
| 99 } |
| 100 |
| 101 base::DictionaryValue* version_info = new base::DictionaryValue; |
| 102 extension_info->SetWithoutPathExpansion(version, version_info); |
| 103 version_info->SetString( |
| 104 ExtensionAssetsManagerChromeOS::kSharedExtensionPath, path.value()); |
| 105 |
| 106 base::ListValue* users = new base::ListValue; |
| 107 version_info->Set(ExtensionAssetsManagerChromeOS::kSharedExtensionUsers, |
| 108 users); |
| 109 std::vector<std::string> users_list; |
| 110 if (Tokenize(users_string, ",", &users_list)) { |
| 111 for (size_t i = 0; i < users_list.size(); i++) { |
| 112 users->AppendString(users_list[i]); |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 scoped_refptr<Extension> CreateExtension(const std::string& id, |
| 118 const std::string& version, |
| 119 const base::FilePath& path) { |
| 120 base::DictionaryValue manifest; |
| 121 manifest.SetString(manifest_keys::kName, "test"); |
| 122 manifest.SetString(manifest_keys::kVersion, version); |
| 123 |
| 124 std::string error; |
| 125 scoped_refptr<Extension> extension = Extension::Create( |
| 126 path, Manifest::INTERNAL, manifest, Extension::NO_FLAGS, id, &error); |
| 127 CHECK(extension.get()) << error; |
| 128 CHECK_EQ(id, extension->id()); |
| 129 |
| 130 return extension; |
| 131 } |
| 132 |
| 133 ExtensionPrefs* GetExtensionPrefs() { |
| 134 return ExtensionPrefs::Get(profile_.get()); |
| 135 } |
| 136 |
| 137 chromeos::FakeUserManager* GetFakeUserManager() { |
| 138 return static_cast<chromeos::FakeUserManager*>( |
| 139 chromeos::UserManager::Get()); |
| 140 } |
| 141 |
| 142 private: |
| 143 scoped_ptr<chromeos::ScopedUserManagerEnabler> user_manager_enabler_; |
| 144 TestingPrefServiceSimple local_state_; |
| 145 base::ScopedTempDir cache_dir_; |
| 146 }; |
| 147 |
| 148 // Test shared extensions clean up. |
| 149 TEST_F(ExtensionGarbageCollectorChromeOSUnitTest, SharedExtensions) { |
| 150 // Version for non-existing user. |
| 151 base::FilePath path_id1_1 = CreateSharedExtensionDir( |
| 152 kExtensionId1, "1.0", cache_dir()); |
| 153 CreateSharedExtensionPrefs(kExtensionId1, "1.0", "test@test.com", path_id1_1); |
| 154 EXPECT_TRUE(base::PathExists(path_id1_1)); |
| 155 |
| 156 // Version for current user but the extension is not installed. |
| 157 base::FilePath path_id1_2 = CreateSharedExtensionDir( |
| 158 kExtensionId1, "2.0", cache_dir()); |
| 159 CreateSharedExtensionPrefs( |
| 160 kExtensionId1, "2.0", chromeos::UserManager::kStubUser, path_id1_2); |
| 161 EXPECT_TRUE(base::PathExists(path_id1_2)); |
| 162 |
| 163 // Version for current user that delayed install. |
| 164 base::FilePath path_id2_1 = CreateSharedExtensionDir( |
| 165 kExtensionId2, "1.0", cache_dir()); |
| 166 CreateSharedExtensionPrefs( |
| 167 kExtensionId2, "1.0", chromeos::UserManager::kStubUser, path_id2_1); |
| 168 scoped_refptr<Extension> extension2 = CreateExtension(kExtensionId2, "1.0", |
| 169 path_id2_1); |
| 170 GetExtensionPrefs()->SetDelayedInstallInfo( |
| 171 extension2.get(), |
| 172 Extension::ENABLED, |
| 173 false, |
| 174 false, |
| 175 ExtensionPrefs::DELAY_REASON_WAIT_FOR_IDLE, |
| 176 syncer::StringOrdinal(), |
| 177 std::string()); |
| 178 EXPECT_TRUE(base::PathExists(path_id2_1)); |
| 179 |
| 180 GarbageCollectExtensions(); |
| 181 |
| 182 EXPECT_FALSE(base::PathExists(path_id1_1)); |
| 183 EXPECT_FALSE(base::PathExists(path_id1_2)); |
| 184 EXPECT_FALSE(base::PathExists(cache_dir().AppendASCII(kExtensionId1))); |
| 185 |
| 186 EXPECT_TRUE(base::PathExists(path_id2_1)); |
| 187 |
| 188 const base::DictionaryValue* shared_extensions = local_state().GetDictionary( |
| 189 ExtensionAssetsManagerChromeOS::kSharedExtensions); |
| 190 ASSERT_TRUE(shared_extensions); |
| 191 |
| 192 EXPECT_FALSE(shared_extensions->HasKey(kExtensionId1)); |
| 193 EXPECT_TRUE(shared_extensions->HasKey(kExtensionId2)); |
| 194 } |
| 195 |
| 196 } // namespace extensions |
OLD | NEW |