| 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/live_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 "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 18 | 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 |
| 19 LiveSyncExtensionHelper::LiveSyncExtensionHelper() {} | 30 LiveSyncExtensionHelper::LiveSyncExtensionHelper() {} |
| 20 | 31 |
| 21 LiveSyncExtensionHelper::~LiveSyncExtensionHelper() {} | 32 LiveSyncExtensionHelper::~LiveSyncExtensionHelper() {} |
| 22 | 33 |
| 23 // static | 34 // static |
| 24 std::string LiveSyncExtensionHelper::NameToId(const std::string& name) { | 35 std::string LiveSyncExtensionHelper::NameToId(const std::string& name) { |
| 25 std::string id; | 36 std::string id; |
| 26 EXPECT_TRUE(Extension::GenerateId(name, &id)); | 37 EXPECT_TRUE(Extension::GenerateId(name, &id)); |
| 27 return id; | 38 return id; |
| 28 } | 39 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 41 << " (profile = " << profile << ")"; | 52 << " (profile = " << profile << ")"; |
| 42 profile->GetExtensionService()->OnExtensionInstalled(extension); | 53 profile->GetExtensionService()->OnExtensionInstalled(extension); |
| 43 } | 54 } |
| 44 | 55 |
| 45 void LiveSyncExtensionHelper::UninstallExtension( | 56 void LiveSyncExtensionHelper::UninstallExtension( |
| 46 Profile* profile, const std::string& name) { | 57 Profile* profile, const std::string& name) { |
| 47 ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(), | 58 ExtensionService::UninstallExtensionHelper(profile->GetExtensionService(), |
| 48 NameToId(name)); | 59 NameToId(name)); |
| 49 } | 60 } |
| 50 | 61 |
| 62 void LiveSyncExtensionHelper::EnableExtension(Profile* profile, |
| 63 const std::string& name) { |
| 64 profile->GetExtensionService()->EnableExtension(NameToId(name)); |
| 65 } |
| 66 |
| 67 void LiveSyncExtensionHelper::DisableExtension(Profile* profile, |
| 68 const std::string& name) { |
| 69 profile->GetExtensionService()->DisableExtension(NameToId(name)); |
| 70 } |
| 71 |
| 72 void LiveSyncExtensionHelper::IncognitoEnableExtension( |
| 73 Profile* profile, const std::string& name) { |
| 74 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), true); |
| 75 } |
| 76 |
| 77 void LiveSyncExtensionHelper::IncognitoDisableExtension( |
| 78 Profile* profile, const std::string& name) { |
| 79 profile->GetExtensionService()->SetIsIncognitoEnabled(NameToId(name), false); |
| 80 } |
| 81 |
| 51 bool LiveSyncExtensionHelper::IsExtensionPendingInstallForSync( | 82 bool LiveSyncExtensionHelper::IsExtensionPendingInstallForSync( |
| 52 Profile* profile, const std::string& id) const { | 83 Profile* profile, const std::string& id) const { |
| 53 const PendingExtensionManager* pending_extension_manager = | 84 const PendingExtensionManager* pending_extension_manager = |
| 54 profile->GetExtensionService()->pending_extension_manager(); | 85 profile->GetExtensionService()->pending_extension_manager(); |
| 55 PendingExtensionInfo info; | 86 PendingExtensionInfo info; |
| 56 if (!pending_extension_manager->GetById(id, &info)) { | 87 if (!pending_extension_manager->GetById(id, &info)) { |
| 57 return false; | 88 return false; |
| 58 } | 89 } |
| 59 return info.is_from_sync(); | 90 return info.is_from_sync(); |
| 60 } | 91 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 82 if (it2 == id_to_name_.end()) { | 113 if (it2 == id_to_name_.end()) { |
| 83 ADD_FAILURE() << "Could not get name for id " << id | 114 ADD_FAILURE() << "Could not get name for id " << id |
| 84 << " (profile = " << profile->GetDebugName() << ")"; | 115 << " (profile = " << profile->GetDebugName() << ")"; |
| 85 continue; | 116 continue; |
| 86 } | 117 } |
| 87 InstallExtension(profile, it2->second, type); | 118 InstallExtension(profile, it2->second, type); |
| 88 } | 119 } |
| 89 } | 120 } |
| 90 | 121 |
| 91 LiveSyncExtensionHelper::ExtensionStateMap | 122 LiveSyncExtensionHelper::ExtensionStateMap |
| 92 LiveSyncExtensionHelper::GetExtensionStates( | 123 LiveSyncExtensionHelper::GetExtensionStates(Profile* profile) { |
| 93 Profile* profile) const { | |
| 94 const std::string& profile_debug_name = profile->GetDebugName(); | 124 const std::string& profile_debug_name = profile->GetDebugName(); |
| 95 | 125 |
| 96 ExtensionStateMap extension_state_map; | 126 ExtensionStateMap extension_state_map; |
| 97 | 127 |
| 98 ExtensionService* extension_service = profile->GetExtensionService(); | 128 ExtensionService* extension_service = profile->GetExtensionService(); |
| 99 | 129 |
| 100 const ExtensionList* extensions = extension_service->extensions(); | 130 const ExtensionList* extensions = extension_service->extensions(); |
| 101 for (ExtensionList::const_iterator it = extensions->begin(); | 131 for (ExtensionList::const_iterator it = extensions->begin(); |
| 102 it != extensions->end(); ++it) { | 132 it != extensions->end(); ++it) { |
| 103 extension_state_map[(*it)->id()] = ENABLED; | 133 const std::string& id = (*it)->id(); |
| 134 extension_state_map[id].enabled_state = ExtensionState::ENABLED; |
| 135 extension_state_map[id].incognito_enabled = |
| 136 extension_service->IsIncognitoEnabled(id); |
| 104 VLOG(2) << "Extension " << (*it)->id() << " in profile " | 137 VLOG(2) << "Extension " << (*it)->id() << " in profile " |
| 105 << profile_debug_name << " is enabled"; | 138 << profile_debug_name << " is enabled"; |
| 106 } | 139 } |
| 107 | 140 |
| 108 const ExtensionList* disabled_extensions = | 141 const ExtensionList* disabled_extensions = |
| 109 extension_service->disabled_extensions(); | 142 extension_service->disabled_extensions(); |
| 110 for (ExtensionList::const_iterator it = disabled_extensions->begin(); | 143 for (ExtensionList::const_iterator it = disabled_extensions->begin(); |
| 111 it != disabled_extensions->end(); ++it) { | 144 it != disabled_extensions->end(); ++it) { |
| 112 extension_state_map[(*it)->id()] = DISABLED; | 145 const std::string& id = (*it)->id(); |
| 146 extension_state_map[id].enabled_state = ExtensionState::DISABLED; |
| 147 extension_state_map[id].incognito_enabled = |
| 148 extension_service->IsIncognitoEnabled(id); |
| 113 VLOG(2) << "Extension " << (*it)->id() << " in profile " | 149 VLOG(2) << "Extension " << (*it)->id() << " in profile " |
| 114 << profile_debug_name << " is disabled"; | 150 << profile_debug_name << " is disabled"; |
| 115 } | 151 } |
| 116 | 152 |
| 117 const PendingExtensionManager* pending_extension_manager = | 153 const PendingExtensionManager* pending_extension_manager = |
| 118 extension_service->pending_extension_manager(); | 154 extension_service->pending_extension_manager(); |
| 119 PendingExtensionManager::const_iterator it; | 155 PendingExtensionManager::const_iterator it; |
| 120 for (it = pending_extension_manager->begin(); | 156 for (it = pending_extension_manager->begin(); |
| 121 it != pending_extension_manager->end(); ++it) { | 157 it != pending_extension_manager->end(); ++it) { |
| 122 extension_state_map[it->first] = PENDING; | 158 const std::string& id = it->first; |
| 159 extension_state_map[id].enabled_state = ExtensionState::PENDING; |
| 160 extension_state_map[id].incognito_enabled = |
| 161 extension_service->IsIncognitoEnabled(id); |
| 123 VLOG(2) << "Extension " << it->first << " in profile " | 162 VLOG(2) << "Extension " << it->first << " in profile " |
| 124 << profile_debug_name << " is pending"; | 163 << profile_debug_name << " is pending"; |
| 125 } | 164 } |
| 126 | 165 |
| 127 return extension_state_map; | 166 return extension_state_map; |
| 128 } | 167 } |
| 129 | 168 |
| 169 bool LiveSyncExtensionHelper::ExtensionStatesMatch( |
| 170 Profile* profile1, Profile* profile2) { |
| 171 const ExtensionStateMap& state_map1 = GetExtensionStates(profile1); |
| 172 const ExtensionStateMap& state_map2 = GetExtensionStates(profile2); |
| 173 if (state_map1.size() != state_map2.size()) { |
| 174 VLOG(1) << "Number of extensions for profile " << profile1->GetDebugName() |
| 175 << " does not match profile " << profile2->GetDebugName(); |
| 176 return false; |
| 177 } |
| 178 |
| 179 ExtensionStateMap::const_iterator it1 = state_map1.begin(); |
| 180 ExtensionStateMap::const_iterator it2 = state_map2.begin(); |
| 181 while (it1 != state_map1.end()) { |
| 182 if (it1->first != it2->first) { |
| 183 VLOG(1) << "Extensions for profile " << profile1->GetDebugName() |
| 184 << " do not match profile " << profile2->GetDebugName(); |
| 185 return false; |
| 186 } else if (!it1->second.Equals(it2->second)) { |
| 187 VLOG(1) << "Extension states for profile " << profile1->GetDebugName() |
| 188 << " do not match profile " << profile2->GetDebugName(); |
| 189 return false; |
| 190 } |
| 191 ++it1; |
| 192 ++it2; |
| 193 } |
| 194 return true; |
| 195 } |
| 196 |
| 130 void LiveSyncExtensionHelper::SetupProfile(Profile* profile) { | 197 void LiveSyncExtensionHelper::SetupProfile(Profile* profile) { |
| 131 profile->InitExtensions(true); | 198 profile->InitExtensions(true); |
| 132 profile_extensions_.insert(make_pair(profile, ExtensionNameMap())); | 199 profile_extensions_.insert(make_pair(profile, ExtensionNameMap())); |
| 133 } | 200 } |
| 134 | 201 |
| 135 namespace { | 202 namespace { |
| 136 | 203 |
| 137 std::string NameToPublicKey(const std::string& name) { | 204 std::string NameToPublicKey(const std::string& name) { |
| 138 std::string public_key; | 205 std::string public_key; |
| 139 std::string pem; | 206 std::string pem; |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 if (extension->id() != expected_id) { | 299 if (extension->id() != expected_id) { |
| 233 EXPECT_EQ(expected_id, extension->id()); | 300 EXPECT_EQ(expected_id, extension->id()); |
| 234 return NULL; | 301 return NULL; |
| 235 } | 302 } |
| 236 VLOG(2) << "created extension with name = " | 303 VLOG(2) << "created extension with name = " |
| 237 << name << ", id = " << expected_id; | 304 << name << ", id = " << expected_id; |
| 238 (it->second)[name] = extension; | 305 (it->second)[name] = extension; |
| 239 id_to_name_[expected_id] = name; | 306 id_to_name_[expected_id] = name; |
| 240 return extension; | 307 return extension; |
| 241 } | 308 } |
| OLD | NEW |