| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2011 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 "chrome/browser/sync/test/live_sync/sync_extension_helper.h" | |
| 6 | |
| 7 #include "base/file_path.h" | |
| 8 #include "base/file_util.h" | |
| 9 #include "base/logging.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/extensions/extension_service.h" | |
| 12 #include "chrome/browser/extensions/pending_extension_info.h" | |
| 13 #include "chrome/browser/extensions/pending_extension_manager.h" | |
| 14 #include "chrome/browser/profiles/profile.h" | |
| 15 #include "chrome/common/extensions/extension_constants.h" | |
| 16 #include "chrome/browser/sync/test/live_sync/live_sync_test.h" | |
| 17 #include "chrome/browser/sync/test/live_sync/sync_datatype_helper.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 SyncExtensionHelper::ExtensionState::ExtensionState() | |
| 21 : enabled_state(ENABLED), incognito_enabled(false) {} | |
| 22 | |
| 23 SyncExtensionHelper::ExtensionState::~ExtensionState() {} | |
| 24 | |
| 25 bool SyncExtensionHelper::ExtensionState::Equals( | |
| 26 const SyncExtensionHelper::ExtensionState &other) const { | |
| 27 return ((enabled_state == other.enabled_state) && | |
| 28 (incognito_enabled == other.incognito_enabled)); | |
| 29 } | |
| 30 | |
| 31 // static | |
| 32 SyncExtensionHelper* SyncExtensionHelper::GetInstance() { | |
| 33 SyncExtensionHelper* instance = Singleton<SyncExtensionHelper>::get(); | |
| 34 instance->SetupIfNecessary(sync_datatype_helper::test()); | |
| 35 return instance; | |
| 36 } | |
| 37 | |
| 38 SyncExtensionHelper::SyncExtensionHelper() : setup_completed_(false) {} | |
| 39 | |
| 40 SyncExtensionHelper::~SyncExtensionHelper() {} | |
| 41 | |
| 42 // static | |
| 43 std::string SyncExtensionHelper::NameToId(const std::string& name) { | |
| 44 std::string id; | |
| 45 EXPECT_TRUE(Extension::GenerateId(name, &id)); | |
| 46 return id; | |
| 47 } | |
| 48 | |
| 49 void SyncExtensionHelper::SetupIfNecessary(LiveSyncTest* test) { | |
| 50 if (setup_completed_) | |
| 51 return; | |
| 52 | |
| 53 for (int i = 0; i < test->num_clients(); ++i) { | |
| 54 SetupProfile(test->GetProfile(i)); | |
| 55 } | |
| 56 SetupProfile(test->verifier()); | |
| 57 | |
| 58 setup_completed_ = true; | |
| 59 } | |
| 60 | |
| 61 void SyncExtensionHelper::InstallExtension( | |
| 62 Profile* profile, const std::string& name, Extension::Type type) { | |
| 63 scoped_refptr<Extension> extension = GetExtension(profile, name, type); | |
| 64 ASSERT_TRUE(extension.get()) << "Could not get extension " << name | |
| 65 << " (profile = " << profile << ")"; | |
| 66 profile->GetExtensionService()->OnExtensionInstalled( | |
| 67 extension, extension->UpdatesFromGallery(), 0); | |
| 68 } | |
| 69 | |
| 70 void SyncExtensionHelper::UninstallExtension( | |
| 71 Profile* profile, const std::string& name) { | |
| 72 ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(), | |
| 73 NameToId(name)); | |
| 74 } | |
| 75 | |
| 76 std::vector<std::string> SyncExtensionHelper::GetInstalledExtensionNames( | |
| 77 Profile* profile) const { | |
| 78 std::vector<std::string> names; | |
| 79 ExtensionService* extension_service = profile->GetExtensionService(); | |
| 80 | |
| 81 const ExtensionList* extensions = extension_service->extensions(); | |
| 82 for (ExtensionList::const_iterator it = extensions->begin(); | |
| 83 it != extensions->end(); ++it) { | |
| 84 names.push_back((*it)->name()); | |
| 85 } | |
| 86 | |
| 87 const ExtensionList* disabled_extensions = | |
| 88 extension_service->disabled_extensions(); | |
| 89 for (ExtensionList::const_iterator it = disabled_extensions->begin(); | |
| 90 it != disabled_extensions->end(); ++it) { | |
| 91 names.push_back((*it)->name()); | |
| 92 } | |
| 93 | |
| 94 const ExtensionList* terminated_extensions = | |
| 95 extension_service->terminated_extensions(); | |
| 96 for (ExtensionList::const_iterator it = terminated_extensions->begin(); | |
| 97 it != terminated_extensions->end(); ++it) { | |
| 98 names.push_back((*it)->name()); | |
| 99 } | |
| 100 | |
| 101 return names; | |
| 102 } | |
| 103 | |
| 104 void SyncExtensionHelper::EnableExtension(Profile* profile, | |
| 105 const std::string& name) { | |
| 106 profile->GetExtensionService()->EnableExtension(NameToId(name)); | |
| 107 } | |
| 108 | |
| 109 void SyncExtensionHelper::DisableExtension(Profile* profile, | |
| 110 const std::string& name) { | |
| 111 profile->GetExtensionService()->DisableExtension(NameToId(name)); | |
| 112 } | |
| 113 | |
| 114 bool SyncExtensionHelper::IsExtensionEnabled( | |
| 115 Profile* profile, const std::string& name) const { | |
| 116 return profile->GetExtensionService()->IsExtensionEnabled(NameToId(name)); | |
| 117 } | |
| 118 | |
| 119 void SyncExtensionHelper::IncognitoEnableExtension( | |
| 120 Profile* profile, const std::string& name) { | |
| 121 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), true); | |
| 122 } | |
| 123 | |
| 124 void SyncExtensionHelper::IncognitoDisableExtension( | |
| 125 Profile* profile, const std::string& name) { | |
| 126 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), false); | |
| 127 } | |
| 128 | |
| 129 bool SyncExtensionHelper::IsIncognitoEnabled( | |
| 130 Profile* profile, const std::string& name) const { | |
| 131 return profile->GetExtensionService()->IsIncognitoEnabled(NameToId(name)); | |
| 132 } | |
| 133 | |
| 134 | |
| 135 bool SyncExtensionHelper::IsExtensionPendingInstallForSync( | |
| 136 Profile* profile, const std::string& id) const { | |
| 137 const PendingExtensionManager* pending_extension_manager = | |
| 138 profile->GetExtensionService()->pending_extension_manager(); | |
| 139 PendingExtensionInfo info; | |
| 140 if (!pending_extension_manager->GetById(id, &info)) { | |
| 141 return false; | |
| 142 } | |
| 143 return info.is_from_sync(); | |
| 144 } | |
| 145 | |
| 146 void SyncExtensionHelper::InstallExtensionsPendingForSync( | |
| 147 Profile* profile, Extension::Type type) { | |
| 148 // TODO(akalin): Mock out the servers that the extensions auto-update | |
| 149 // mechanism talk to so as to more closely match what actually happens. | |
| 150 // Background networking will need to be re-enabled for extensions tests. | |
| 151 | |
| 152 // We make a copy here since InstallExtension() removes the | |
| 153 // extension from the extensions service's copy. | |
| 154 const PendingExtensionManager* pending_extension_manager = | |
| 155 profile->GetExtensionService()->pending_extension_manager(); | |
| 156 PendingExtensionManager::PendingExtensionMap pending_extensions( | |
| 157 pending_extension_manager->begin(), | |
| 158 pending_extension_manager->end()); | |
| 159 for (PendingExtensionManager::const_iterator it = pending_extensions.begin(); | |
| 160 it != pending_extensions.end(); ++it) { | |
| 161 if (!it->second.is_from_sync()) { | |
| 162 continue; | |
| 163 } | |
| 164 const std::string& id = it->first; | |
| 165 StringMap::const_iterator it2 = id_to_name_.find(id); | |
| 166 if (it2 == id_to_name_.end()) { | |
| 167 ADD_FAILURE() << "Could not get name for id " << id | |
| 168 << " (profile = " << profile->GetDebugName() << ")"; | |
| 169 continue; | |
| 170 } | |
| 171 InstallExtension(profile, it2->second, type); | |
| 172 } | |
| 173 } | |
| 174 | |
| 175 SyncExtensionHelper::ExtensionStateMap | |
| 176 SyncExtensionHelper::GetExtensionStates(Profile* profile) { | |
| 177 const std::string& profile_debug_name = profile->GetDebugName(); | |
| 178 | |
| 179 ExtensionStateMap extension_state_map; | |
| 180 | |
| 181 ExtensionService* extension_service = profile->GetExtensionService(); | |
| 182 | |
| 183 const ExtensionList* extensions = extension_service->extensions(); | |
| 184 for (ExtensionList::const_iterator it = extensions->begin(); | |
| 185 it != extensions->end(); ++it) { | |
| 186 const std::string& id = (*it)->id(); | |
| 187 extension_state_map[id].enabled_state = ExtensionState::ENABLED; | |
| 188 extension_state_map[id].incognito_enabled = | |
| 189 extension_service->IsIncognitoEnabled(id); | |
| 190 VLOG(2) << "Extension " << (*it)->id() << " in profile " | |
| 191 << profile_debug_name << " is enabled"; | |
| 192 } | |
| 193 | |
| 194 const ExtensionList* disabled_extensions = | |
| 195 extension_service->disabled_extensions(); | |
| 196 for (ExtensionList::const_iterator it = disabled_extensions->begin(); | |
| 197 it != disabled_extensions->end(); ++it) { | |
| 198 const std::string& id = (*it)->id(); | |
| 199 extension_state_map[id].enabled_state = ExtensionState::DISABLED; | |
| 200 extension_state_map[id].incognito_enabled = | |
| 201 extension_service->IsIncognitoEnabled(id); | |
| 202 VLOG(2) << "Extension " << (*it)->id() << " in profile " | |
| 203 << profile_debug_name << " is disabled"; | |
| 204 } | |
| 205 | |
| 206 const PendingExtensionManager* pending_extension_manager = | |
| 207 extension_service->pending_extension_manager(); | |
| 208 PendingExtensionManager::const_iterator it; | |
| 209 for (it = pending_extension_manager->begin(); | |
| 210 it != pending_extension_manager->end(); ++it) { | |
| 211 const std::string& id = it->first; | |
| 212 extension_state_map[id].enabled_state = ExtensionState::PENDING; | |
| 213 extension_state_map[id].incognito_enabled = | |
| 214 extension_service->IsIncognitoEnabled(id); | |
| 215 VLOG(2) << "Extension " << it->first << " in profile " | |
| 216 << profile_debug_name << " is pending"; | |
| 217 } | |
| 218 | |
| 219 return extension_state_map; | |
| 220 } | |
| 221 | |
| 222 bool SyncExtensionHelper::ExtensionStatesMatch( | |
| 223 Profile* profile1, Profile* profile2) { | |
| 224 const ExtensionStateMap& state_map1 = GetExtensionStates(profile1); | |
| 225 const ExtensionStateMap& state_map2 = GetExtensionStates(profile2); | |
| 226 if (state_map1.size() != state_map2.size()) { | |
| 227 VLOG(1) << "Number of extensions for profile " << profile1->GetDebugName() | |
| 228 << " does not match profile " << profile2->GetDebugName(); | |
| 229 return false; | |
| 230 } | |
| 231 | |
| 232 ExtensionStateMap::const_iterator it1 = state_map1.begin(); | |
| 233 ExtensionStateMap::const_iterator it2 = state_map2.begin(); | |
| 234 while (it1 != state_map1.end()) { | |
| 235 if (it1->first != it2->first) { | |
| 236 VLOG(1) << "Extensions for profile " << profile1->GetDebugName() | |
| 237 << " do not match profile " << profile2->GetDebugName(); | |
| 238 return false; | |
| 239 } else if (!it1->second.Equals(it2->second)) { | |
| 240 VLOG(1) << "Extension states for profile " << profile1->GetDebugName() | |
| 241 << " do not match profile " << profile2->GetDebugName(); | |
| 242 return false; | |
| 243 } | |
| 244 ++it1; | |
| 245 ++it2; | |
| 246 } | |
| 247 return true; | |
| 248 } | |
| 249 | |
| 250 void SyncExtensionHelper::SetupProfile(Profile* profile) { | |
| 251 profile->InitExtensions(true); | |
| 252 profile_extensions_.insert(make_pair(profile, ExtensionNameMap())); | |
| 253 } | |
| 254 | |
| 255 namespace { | |
| 256 | |
| 257 std::string NameToPublicKey(const std::string& name) { | |
| 258 std::string public_key; | |
| 259 std::string pem; | |
| 260 EXPECT_TRUE(Extension::ProducePEM(name, &pem) && | |
| 261 Extension::FormatPEMForFileOutput(pem, &public_key, | |
| 262 true /* is_public */)); | |
| 263 return public_key; | |
| 264 } | |
| 265 | |
| 266 // TODO(akalin): Somehow unify this with MakeExtension() in | |
| 267 // extension_util_unittest.cc. | |
| 268 scoped_refptr<Extension> CreateExtension( | |
| 269 const FilePath& base_dir, const std::string& name, | |
| 270 Extension::Type type) { | |
| 271 DictionaryValue source; | |
| 272 source.SetString(extension_manifest_keys::kName, name); | |
| 273 const std::string& public_key = NameToPublicKey(name); | |
| 274 source.SetString(extension_manifest_keys::kPublicKey, public_key); | |
| 275 source.SetString(extension_manifest_keys::kVersion, "0.0.0.0"); | |
| 276 switch (type) { | |
| 277 case Extension::TYPE_EXTENSION: | |
| 278 // Do nothing. | |
| 279 break; | |
| 280 case Extension::TYPE_THEME: | |
| 281 source.Set(extension_manifest_keys::kTheme, new DictionaryValue()); | |
| 282 break; | |
| 283 case Extension::TYPE_HOSTED_APP: | |
| 284 case Extension::TYPE_PACKAGED_APP: | |
| 285 source.Set(extension_manifest_keys::kApp, new DictionaryValue()); | |
| 286 source.SetString(extension_manifest_keys::kLaunchWebURL, | |
| 287 "http://www.example.com"); | |
| 288 break; | |
| 289 default: | |
| 290 ADD_FAILURE(); | |
| 291 return NULL; | |
| 292 } | |
| 293 const FilePath sub_dir = FilePath().AppendASCII(name); | |
| 294 FilePath extension_dir; | |
| 295 if (!file_util::PathExists(base_dir) && | |
| 296 !file_util::CreateDirectory(base_dir) && | |
| 297 !file_util::CreateTemporaryDirInDir( | |
| 298 base_dir, sub_dir.value(), &extension_dir)) { | |
| 299 ADD_FAILURE(); | |
| 300 return NULL; | |
| 301 } | |
| 302 std::string error; | |
| 303 scoped_refptr<Extension> extension = | |
| 304 Extension::Create(extension_dir, Extension::INTERNAL, | |
| 305 source, Extension::STRICT_ERROR_CHECKS, &error); | |
| 306 if (!error.empty()) { | |
| 307 ADD_FAILURE() << error; | |
| 308 return NULL; | |
| 309 } | |
| 310 if (!extension.get()) { | |
| 311 ADD_FAILURE(); | |
| 312 return NULL; | |
| 313 } | |
| 314 if (extension->name() != name) { | |
| 315 EXPECT_EQ(name, extension->name()); | |
| 316 return NULL; | |
| 317 } | |
| 318 if (extension->GetType() != type) { | |
| 319 EXPECT_EQ(type, extension->GetType()); | |
| 320 return NULL; | |
| 321 } | |
| 322 return extension; | |
| 323 } | |
| 324 | |
| 325 } // namespace | |
| 326 | |
| 327 scoped_refptr<Extension> SyncExtensionHelper::GetExtension( | |
| 328 Profile* profile, const std::string& name, | |
| 329 Extension::Type type) { | |
| 330 if (name.empty()) { | |
| 331 ADD_FAILURE(); | |
| 332 return NULL; | |
| 333 } | |
| 334 ProfileExtensionNameMap::iterator it = profile_extensions_.find(profile); | |
| 335 if (it == profile_extensions_.end()) { | |
| 336 ADD_FAILURE(); | |
| 337 return NULL; | |
| 338 } | |
| 339 ExtensionNameMap::const_iterator it2 = it->second.find(name); | |
| 340 if (it2 != it->second.end()) { | |
| 341 return it2->second; | |
| 342 } | |
| 343 | |
| 344 scoped_refptr<Extension> extension = | |
| 345 CreateExtension(profile->GetExtensionService()->install_directory(), | |
| 346 name, type); | |
| 347 if (!extension.get()) { | |
| 348 ADD_FAILURE(); | |
| 349 return NULL; | |
| 350 } | |
| 351 const std::string& expected_id = NameToId(name); | |
| 352 if (extension->id() != expected_id) { | |
| 353 EXPECT_EQ(expected_id, extension->id()); | |
| 354 return NULL; | |
| 355 } | |
| 356 VLOG(2) << "created extension with name = " | |
| 357 << name << ", id = " << expected_id; | |
| 358 (it->second)[name] = extension; | |
| 359 id_to_name_[expected_id] = name; | |
| 360 return extension; | |
| 361 } | |
| OLD | NEW |