Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/test/live_sync/live_sync_extension_helper.h" | 5 #include "chrome/test/live_sync/sync_extension_helper.h" |
| 6 | 6 |
| 7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/values.h" | 10 #include "base/values.h" |
| 11 #include "chrome/browser/extensions/extension_service.h" | 11 #include "chrome/browser/extensions/extension_service.h" |
| 12 #include "chrome/browser/extensions/pending_extension_info.h" | 12 #include "chrome/browser/extensions/pending_extension_info.h" |
| 13 #include "chrome/browser/extensions/pending_extension_manager.h" | 13 #include "chrome/browser/extensions/pending_extension_manager.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/extensions/extension_constants.h" | 15 #include "chrome/common/extensions/extension_constants.h" |
| 16 #include "chrome/test/live_sync/live_sync_test.h" | 16 #include "chrome/test/live_sync/live_sync_test.h" |
| 17 #include "chrome/test/live_sync/sync_datatype_helper.h" | |
| 17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 19 |
| 19 LiveSyncExtensionHelper::ExtensionState::ExtensionState() | 20 SyncExtensionHelper::ExtensionState::ExtensionState() |
| 20 : enabled_state(ENABLED), incognito_enabled(false) {} | 21 : enabled_state(ENABLED), incognito_enabled(false) {} |
| 21 | 22 |
| 22 LiveSyncExtensionHelper::ExtensionState::~ExtensionState() {} | 23 SyncExtensionHelper::ExtensionState::~ExtensionState() {} |
| 23 | 24 |
| 24 bool LiveSyncExtensionHelper::ExtensionState::Equals( | 25 bool SyncExtensionHelper::ExtensionState::Equals( |
| 25 const LiveSyncExtensionHelper::ExtensionState &other) const { | 26 const SyncExtensionHelper::ExtensionState &other) const { |
| 26 return ((enabled_state == other.enabled_state) && | 27 return ((enabled_state == other.enabled_state) && |
| 27 (incognito_enabled == other.incognito_enabled)); | 28 (incognito_enabled == other.incognito_enabled)); |
| 28 } | 29 } |
| 29 | 30 |
| 30 LiveSyncExtensionHelper::LiveSyncExtensionHelper() {} | 31 // static |
| 32 SyncExtensionHelper* SyncExtensionHelper::GetInstance() { | |
| 33 SyncExtensionHelper* instance = Singleton<SyncExtensionHelper>::get(); | |
| 34 instance->SetupIfNecessary(sync_datatype_helper::test()); | |
| 35 return instance; | |
| 36 } | |
| 31 | 37 |
| 32 LiveSyncExtensionHelper::~LiveSyncExtensionHelper() {} | 38 SyncExtensionHelper::SyncExtensionHelper() : setup_completed_(false) {} |
| 39 | |
| 40 SyncExtensionHelper::~SyncExtensionHelper() {} | |
| 33 | 41 |
| 34 // static | 42 // static |
| 35 std::string LiveSyncExtensionHelper::NameToId(const std::string& name) { | 43 std::string SyncExtensionHelper::NameToId(const std::string& name) { |
| 36 std::string id; | 44 std::string id; |
| 37 EXPECT_TRUE(Extension::GenerateId(name, &id)); | 45 EXPECT_TRUE(Extension::GenerateId(name, &id)); |
| 38 return id; | 46 return id; |
| 39 } | 47 } |
| 40 | 48 |
| 41 void LiveSyncExtensionHelper::Setup(LiveSyncTest* test) { | 49 void SyncExtensionHelper::SetupIfNecessary(LiveSyncTest* test) { |
| 50 if (setup_completed_) | |
| 51 return; | |
| 52 | |
| 42 for (int i = 0; i < test->num_clients(); ++i) { | 53 for (int i = 0; i < test->num_clients(); ++i) { |
| 43 SetupProfile(test->GetProfile(i)); | 54 SetupProfile(test->GetProfile(i)); |
| 44 } | 55 } |
| 45 SetupProfile(test->verifier()); | 56 SetupProfile(test->verifier()); |
| 57 | |
| 58 setup_completed_ = true; | |
| 46 } | 59 } |
| 47 | 60 |
| 48 void LiveSyncExtensionHelper::InstallExtension( | 61 void SyncExtensionHelper::InstallExtension( |
| 49 Profile* profile, const std::string& name, Extension::Type type) { | 62 Profile* profile, const std::string& name, Extension::Type type) { |
| 50 scoped_refptr<Extension> extension = GetExtension(profile, name, type); | 63 scoped_refptr<Extension> extension = GetExtension(profile, name, type); |
| 51 ASSERT_TRUE(extension.get()) << "Could not get extension " << name | 64 ASSERT_TRUE(extension.get()) << "Could not get extension " << name |
| 52 << " (profile = " << profile << ")"; | 65 << " (profile = " << profile << ")"; |
| 53 profile->GetExtensionService()->OnExtensionInstalled( | 66 profile->GetExtensionService()->OnExtensionInstalled( |
| 54 extension, extension->UpdatesFromGallery()); | 67 extension, extension->UpdatesFromGallery()); |
| 55 } | 68 } |
| 56 | 69 |
| 57 void LiveSyncExtensionHelper::UninstallExtension( | 70 void SyncExtensionHelper::UninstallExtension( |
| 58 Profile* profile, const std::string& name) { | 71 Profile* profile, const std::string& name) { |
| 59 ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(), | 72 ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(), |
| 60 NameToId(name)); | 73 NameToId(name)); |
| 61 } | 74 } |
| 62 | 75 |
| 63 std::vector<std::string> LiveSyncExtensionHelper::GetInstalledExtensionNames( | 76 std::vector<std::string> SyncExtensionHelper::GetInstalledExtensionNames( |
| 64 Profile* profile) const { | 77 Profile* profile) const { |
| 65 std::vector<std::string> names; | 78 std::vector<std::string> names; |
| 66 ExtensionService* extension_service = profile->GetExtensionService(); | 79 ExtensionService* extension_service = profile->GetExtensionService(); |
| 67 | 80 |
| 68 const ExtensionList* extensions = extension_service->extensions(); | 81 const ExtensionList* extensions = extension_service->extensions(); |
| 69 for (ExtensionList::const_iterator it = extensions->begin(); | 82 for (ExtensionList::const_iterator it = extensions->begin(); |
| 70 it != extensions->end(); ++it) { | 83 it != extensions->end(); ++it) { |
| 71 names.push_back((*it)->name()); | 84 names.push_back((*it)->name()); |
| 72 } | 85 } |
| 73 | 86 |
| 74 const ExtensionList* disabled_extensions = | 87 const ExtensionList* disabled_extensions = |
| 75 extension_service->disabled_extensions(); | 88 extension_service->disabled_extensions(); |
| 76 for (ExtensionList::const_iterator it = disabled_extensions->begin(); | 89 for (ExtensionList::const_iterator it = disabled_extensions->begin(); |
| 77 it != disabled_extensions->end(); ++it) { | 90 it != disabled_extensions->end(); ++it) { |
| 78 names.push_back((*it)->name()); | 91 names.push_back((*it)->name()); |
| 79 } | 92 } |
| 80 | 93 |
| 81 const ExtensionList* terminated_extensions = | 94 const ExtensionList* terminated_extensions = |
| 82 extension_service->terminated_extensions(); | 95 extension_service->terminated_extensions(); |
| 83 for (ExtensionList::const_iterator it = terminated_extensions->begin(); | 96 for (ExtensionList::const_iterator it = terminated_extensions->begin(); |
| 84 it != terminated_extensions->end(); ++it) { | 97 it != terminated_extensions->end(); ++it) { |
| 85 names.push_back((*it)->name()); | 98 names.push_back((*it)->name()); |
| 86 } | 99 } |
| 87 | 100 |
| 88 return names; | 101 return names; |
| 89 } | 102 } |
| 90 | 103 |
| 91 void LiveSyncExtensionHelper::EnableExtension(Profile* profile, | 104 void SyncExtensionHelper::EnableExtension(Profile* profile, |
| 92 const std::string& name) { | 105 const std::string& name) { |
|
akalin
2011/08/11 00:08:08
indent
Raghu Simha
2011/08/11 00:30:50
Good catch. Done.
| |
| 93 profile->GetExtensionService()->EnableExtension(NameToId(name)); | 106 profile->GetExtensionService()->EnableExtension(NameToId(name)); |
| 94 } | 107 } |
| 95 | 108 |
| 96 void LiveSyncExtensionHelper::DisableExtension(Profile* profile, | 109 void SyncExtensionHelper::DisableExtension(Profile* profile, |
| 97 const std::string& name) { | 110 const std::string& name) { |
|
akalin
2011/08/11 00:08:08
indent
Raghu Simha
2011/08/11 00:30:50
Good catch. Done.
| |
| 98 profile->GetExtensionService()->DisableExtension(NameToId(name)); | 111 profile->GetExtensionService()->DisableExtension(NameToId(name)); |
| 99 } | 112 } |
| 100 | 113 |
| 101 bool LiveSyncExtensionHelper::IsExtensionEnabled( | 114 bool SyncExtensionHelper::IsExtensionEnabled( |
| 102 Profile* profile, const std::string& name) const { | 115 Profile* profile, const std::string& name) const { |
| 103 return profile->GetExtensionService()->IsExtensionEnabled(NameToId(name)); | 116 return profile->GetExtensionService()->IsExtensionEnabled(NameToId(name)); |
| 104 } | 117 } |
| 105 | 118 |
| 106 void LiveSyncExtensionHelper::IncognitoEnableExtension( | 119 void SyncExtensionHelper::IncognitoEnableExtension( |
| 107 Profile* profile, const std::string& name) { | 120 Profile* profile, const std::string& name) { |
| 108 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), true); | 121 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), true); |
| 109 } | 122 } |
| 110 | 123 |
| 111 void LiveSyncExtensionHelper::IncognitoDisableExtension( | 124 void SyncExtensionHelper::IncognitoDisableExtension( |
| 112 Profile* profile, const std::string& name) { | 125 Profile* profile, const std::string& name) { |
| 113 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), false); | 126 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), false); |
| 114 } | 127 } |
| 115 | 128 |
| 116 bool LiveSyncExtensionHelper::IsIncognitoEnabled( | 129 bool SyncExtensionHelper::IsIncognitoEnabled( |
| 117 Profile* profile, const std::string& name) const { | 130 Profile* profile, const std::string& name) const { |
| 118 return profile->GetExtensionService()->IsIncognitoEnabled(NameToId(name)); | 131 return profile->GetExtensionService()->IsIncognitoEnabled(NameToId(name)); |
| 119 } | 132 } |
| 120 | 133 |
| 121 | 134 |
| 122 bool LiveSyncExtensionHelper::IsExtensionPendingInstallForSync( | 135 bool SyncExtensionHelper::IsExtensionPendingInstallForSync( |
| 123 Profile* profile, const std::string& id) const { | 136 Profile* profile, const std::string& id) const { |
| 124 const PendingExtensionManager* pending_extension_manager = | 137 const PendingExtensionManager* pending_extension_manager = |
| 125 profile->GetExtensionService()->pending_extension_manager(); | 138 profile->GetExtensionService()->pending_extension_manager(); |
| 126 PendingExtensionInfo info; | 139 PendingExtensionInfo info; |
| 127 if (!pending_extension_manager->GetById(id, &info)) { | 140 if (!pending_extension_manager->GetById(id, &info)) { |
| 128 return false; | 141 return false; |
| 129 } | 142 } |
| 130 return info.is_from_sync(); | 143 return info.is_from_sync(); |
| 131 } | 144 } |
| 132 | 145 |
| 133 void LiveSyncExtensionHelper::InstallExtensionsPendingForSync( | 146 void SyncExtensionHelper::InstallExtensionsPendingForSync( |
| 134 Profile* profile, Extension::Type type) { | 147 Profile* profile, Extension::Type type) { |
| 135 // TODO(akalin): Mock out the servers that the extensions auto-update | 148 // TODO(akalin): Mock out the servers that the extensions auto-update |
| 136 // mechanism talk to so as to more closely match what actually happens. | 149 // mechanism talk to so as to more closely match what actually happens. |
| 137 // Background networking will need to be re-enabled for extensions tests. | 150 // Background networking will need to be re-enabled for extensions tests. |
| 138 | 151 |
| 139 // We make a copy here since InstallExtension() removes the | 152 // We make a copy here since InstallExtension() removes the |
| 140 // extension from the extensions service's copy. | 153 // extension from the extensions service's copy. |
| 141 const PendingExtensionManager* pending_extension_manager = | 154 const PendingExtensionManager* pending_extension_manager = |
| 142 profile->GetExtensionService()->pending_extension_manager(); | 155 profile->GetExtensionService()->pending_extension_manager(); |
| 143 PendingExtensionManager::PendingExtensionMap pending_extensions( | 156 PendingExtensionManager::PendingExtensionMap pending_extensions( |
| 144 pending_extension_manager->begin(), | 157 pending_extension_manager->begin(), |
| 145 pending_extension_manager->end()); | 158 pending_extension_manager->end()); |
| 146 for (PendingExtensionManager::const_iterator it = pending_extensions.begin(); | 159 for (PendingExtensionManager::const_iterator it = pending_extensions.begin(); |
| 147 it != pending_extensions.end(); ++it) { | 160 it != pending_extensions.end(); ++it) { |
| 148 if (!it->second.is_from_sync()) { | 161 if (!it->second.is_from_sync()) { |
| 149 continue; | 162 continue; |
| 150 } | 163 } |
| 151 const std::string& id = it->first; | 164 const std::string& id = it->first; |
| 152 StringMap::const_iterator it2 = id_to_name_.find(id); | 165 StringMap::const_iterator it2 = id_to_name_.find(id); |
| 153 if (it2 == id_to_name_.end()) { | 166 if (it2 == id_to_name_.end()) { |
| 154 ADD_FAILURE() << "Could not get name for id " << id | 167 ADD_FAILURE() << "Could not get name for id " << id |
| 155 << " (profile = " << profile->GetDebugName() << ")"; | 168 << " (profile = " << profile->GetDebugName() << ")"; |
| 156 continue; | 169 continue; |
| 157 } | 170 } |
| 158 InstallExtension(profile, it2->second, type); | 171 InstallExtension(profile, it2->second, type); |
| 159 } | 172 } |
| 160 } | 173 } |
| 161 | 174 |
| 162 LiveSyncExtensionHelper::ExtensionStateMap | 175 SyncExtensionHelper::ExtensionStateMap |
| 163 LiveSyncExtensionHelper::GetExtensionStates(Profile* profile) { | 176 SyncExtensionHelper::GetExtensionStates(Profile* profile) { |
| 164 const std::string& profile_debug_name = profile->GetDebugName(); | 177 const std::string& profile_debug_name = profile->GetDebugName(); |
| 165 | 178 |
| 166 ExtensionStateMap extension_state_map; | 179 ExtensionStateMap extension_state_map; |
| 167 | 180 |
| 168 ExtensionService* extension_service = profile->GetExtensionService(); | 181 ExtensionService* extension_service = profile->GetExtensionService(); |
| 169 | 182 |
| 170 const ExtensionList* extensions = extension_service->extensions(); | 183 const ExtensionList* extensions = extension_service->extensions(); |
| 171 for (ExtensionList::const_iterator it = extensions->begin(); | 184 for (ExtensionList::const_iterator it = extensions->begin(); |
| 172 it != extensions->end(); ++it) { | 185 it != extensions->end(); ++it) { |
| 173 const std::string& id = (*it)->id(); | 186 const std::string& id = (*it)->id(); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 199 extension_state_map[id].enabled_state = ExtensionState::PENDING; | 212 extension_state_map[id].enabled_state = ExtensionState::PENDING; |
| 200 extension_state_map[id].incognito_enabled = | 213 extension_state_map[id].incognito_enabled = |
| 201 extension_service->IsIncognitoEnabled(id); | 214 extension_service->IsIncognitoEnabled(id); |
| 202 VLOG(2) << "Extension " << it->first << " in profile " | 215 VLOG(2) << "Extension " << it->first << " in profile " |
| 203 << profile_debug_name << " is pending"; | 216 << profile_debug_name << " is pending"; |
| 204 } | 217 } |
| 205 | 218 |
| 206 return extension_state_map; | 219 return extension_state_map; |
| 207 } | 220 } |
| 208 | 221 |
| 209 bool LiveSyncExtensionHelper::ExtensionStatesMatch( | 222 bool SyncExtensionHelper::ExtensionStatesMatch( |
| 210 Profile* profile1, Profile* profile2) { | 223 Profile* profile1, Profile* profile2) { |
| 211 const ExtensionStateMap& state_map1 = GetExtensionStates(profile1); | 224 const ExtensionStateMap& state_map1 = GetExtensionStates(profile1); |
| 212 const ExtensionStateMap& state_map2 = GetExtensionStates(profile2); | 225 const ExtensionStateMap& state_map2 = GetExtensionStates(profile2); |
| 213 if (state_map1.size() != state_map2.size()) { | 226 if (state_map1.size() != state_map2.size()) { |
| 214 VLOG(1) << "Number of extensions for profile " << profile1->GetDebugName() | 227 VLOG(1) << "Number of extensions for profile " << profile1->GetDebugName() |
| 215 << " does not match profile " << profile2->GetDebugName(); | 228 << " does not match profile " << profile2->GetDebugName(); |
| 216 return false; | 229 return false; |
| 217 } | 230 } |
| 218 | 231 |
| 219 ExtensionStateMap::const_iterator it1 = state_map1.begin(); | 232 ExtensionStateMap::const_iterator it1 = state_map1.begin(); |
| 220 ExtensionStateMap::const_iterator it2 = state_map2.begin(); | 233 ExtensionStateMap::const_iterator it2 = state_map2.begin(); |
| 221 while (it1 != state_map1.end()) { | 234 while (it1 != state_map1.end()) { |
| 222 if (it1->first != it2->first) { | 235 if (it1->first != it2->first) { |
| 223 VLOG(1) << "Extensions for profile " << profile1->GetDebugName() | 236 VLOG(1) << "Extensions for profile " << profile1->GetDebugName() |
| 224 << " do not match profile " << profile2->GetDebugName(); | 237 << " do not match profile " << profile2->GetDebugName(); |
| 225 return false; | 238 return false; |
| 226 } else if (!it1->second.Equals(it2->second)) { | 239 } else if (!it1->second.Equals(it2->second)) { |
| 227 VLOG(1) << "Extension states for profile " << profile1->GetDebugName() | 240 VLOG(1) << "Extension states for profile " << profile1->GetDebugName() |
| 228 << " do not match profile " << profile2->GetDebugName(); | 241 << " do not match profile " << profile2->GetDebugName(); |
| 229 return false; | 242 return false; |
| 230 } | 243 } |
| 231 ++it1; | 244 ++it1; |
| 232 ++it2; | 245 ++it2; |
| 233 } | 246 } |
| 234 return true; | 247 return true; |
| 235 } | 248 } |
| 236 | 249 |
| 237 void LiveSyncExtensionHelper::SetupProfile(Profile* profile) { | 250 void SyncExtensionHelper::SetupProfile(Profile* profile) { |
| 238 profile->InitExtensions(true); | 251 profile->InitExtensions(true); |
| 239 profile_extensions_.insert(make_pair(profile, ExtensionNameMap())); | 252 profile_extensions_.insert(make_pair(profile, ExtensionNameMap())); |
| 240 } | 253 } |
| 241 | 254 |
| 242 namespace { | 255 namespace { |
| 243 | 256 |
| 244 std::string NameToPublicKey(const std::string& name) { | 257 std::string NameToPublicKey(const std::string& name) { |
| 245 std::string public_key; | 258 std::string public_key; |
| 246 std::string pem; | 259 std::string pem; |
| 247 EXPECT_TRUE(Extension::ProducePEM(name, &pem) && | 260 EXPECT_TRUE(Extension::ProducePEM(name, &pem) && |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 } | 317 } |
| 305 if (extension->GetType() != type) { | 318 if (extension->GetType() != type) { |
| 306 EXPECT_EQ(type, extension->GetType()); | 319 EXPECT_EQ(type, extension->GetType()); |
| 307 return NULL; | 320 return NULL; |
| 308 } | 321 } |
| 309 return extension; | 322 return extension; |
| 310 } | 323 } |
| 311 | 324 |
| 312 } // namespace | 325 } // namespace |
| 313 | 326 |
| 314 scoped_refptr<Extension> LiveSyncExtensionHelper::GetExtension( | 327 scoped_refptr<Extension> SyncExtensionHelper::GetExtension( |
| 315 Profile* profile, const std::string& name, | 328 Profile* profile, const std::string& name, |
| 316 Extension::Type type) { | 329 Extension::Type type) { |
| 317 if (name.empty()) { | 330 if (name.empty()) { |
| 318 ADD_FAILURE(); | 331 ADD_FAILURE(); |
| 319 return NULL; | 332 return NULL; |
| 320 } | 333 } |
| 321 ProfileExtensionNameMap::iterator it = profile_extensions_.find(profile); | 334 ProfileExtensionNameMap::iterator it = profile_extensions_.find(profile); |
| 322 if (it == profile_extensions_.end()) { | 335 if (it == profile_extensions_.end()) { |
| 323 ADD_FAILURE(); | 336 ADD_FAILURE(); |
| 324 return NULL; | 337 return NULL; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 339 if (extension->id() != expected_id) { | 352 if (extension->id() != expected_id) { |
| 340 EXPECT_EQ(expected_id, extension->id()); | 353 EXPECT_EQ(expected_id, extension->id()); |
| 341 return NULL; | 354 return NULL; |
| 342 } | 355 } |
| 343 VLOG(2) << "created extension with name = " | 356 VLOG(2) << "created extension with name = " |
| 344 << name << ", id = " << expected_id; | 357 << name << ", id = " << expected_id; |
| 345 (it->second)[name] = extension; | 358 (it->second)[name] = extension; |
| 346 id_to_name_[expected_id] = name; | 359 id_to_name_[expected_id] = name; |
| 347 return extension; | 360 return extension; |
| 348 } | 361 } |
| OLD | NEW |