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