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 namespace { |
| 20 |
| 21 std::string GetProfileName(Profile* profile) { |
| 22 const std::string& name = profile->GetPath().BaseName().MaybeAsASCII(); |
| 23 EXPECT_FALSE(name.empty()); |
| 24 return name; |
| 25 } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 LiveSyncExtensionHelper::LiveSyncExtensionHelper() {} |
| 30 |
| 31 LiveSyncExtensionHelper::~LiveSyncExtensionHelper() {} |
| 32 |
| 33 // static |
| 34 std::string LiveSyncExtensionHelper::NameToId(const std::string& name) { |
| 35 std::string id; |
| 36 EXPECT_TRUE(Extension::GenerateId(name, &id)); |
| 37 return id; |
| 38 } |
| 39 |
| 40 void LiveSyncExtensionHelper::Setup(LiveSyncTest* test) { |
| 41 for (int i = 0; i < test->num_clients(); ++i) { |
| 42 SetupProfile(test->GetProfile(i)); |
| 43 } |
| 44 SetupProfile(test->verifier()); |
| 45 } |
| 46 |
| 47 void LiveSyncExtensionHelper::InstallExtension( |
| 48 Profile* profile, const std::string& name, Extension::Type type) { |
| 49 scoped_refptr<Extension> extension = GetExtension(profile, name, type); |
| 50 ASSERT_TRUE(extension.get()) << "Could not get extension " << name |
| 51 << " (profile = " << profile << ")"; |
| 52 profile->GetExtensionService()->OnExtensionInstalled(extension); |
| 53 } |
| 54 |
| 55 bool LiveSyncExtensionHelper::IsExtensionPendingInstallForSync( |
| 56 Profile* profile, const std::string& id) const { |
| 57 const PendingExtensionManager* pending_extension_manager = |
| 58 profile->GetExtensionService()->pending_extension_manager(); |
| 59 PendingExtensionInfo info; |
| 60 if (!pending_extension_manager->GetById(id, &info)) { |
| 61 return false; |
| 62 } |
| 63 return info.is_from_sync(); |
| 64 } |
| 65 |
| 66 void LiveSyncExtensionHelper::InstallExtensionsPendingForSync( |
| 67 Profile* profile, Extension::Type type) { |
| 68 // TODO(akalin): Mock out the servers that the extensions auto-update |
| 69 // mechanism talk to so as to more closely match what actually happens. |
| 70 // Background networking will need to be re-enabled for extensions tests. |
| 71 |
| 72 // We make a copy here since InstallExtension() removes the |
| 73 // extension from the extensions service's copy. |
| 74 const PendingExtensionManager* pending_extension_manager = |
| 75 profile->GetExtensionService()->pending_extension_manager(); |
| 76 PendingExtensionManager::PendingExtensionMap pending_extensions( |
| 77 pending_extension_manager->begin(), |
| 78 pending_extension_manager->end()); |
| 79 for (PendingExtensionManager::const_iterator it = pending_extensions.begin(); |
| 80 it != pending_extensions.end(); ++it) { |
| 81 if (!it->second.is_from_sync()) { |
| 82 continue; |
| 83 } |
| 84 const std::string& id = it->first; |
| 85 StringMap::const_iterator it2 = id_to_name_.find(id); |
| 86 if (it2 == id_to_name_.end()) { |
| 87 ADD_FAILURE() << "Could not get name for id " << id |
| 88 << " (profile = " << GetProfileName(profile) << ")"; |
| 89 continue; |
| 90 } |
| 91 InstallExtension(profile, it2->second, type); |
| 92 } |
| 93 } |
| 94 |
| 95 void LiveSyncExtensionHelper::SetupProfile(Profile* profile) { |
| 96 profile->InitExtensions(true); |
| 97 profile_extensions_.insert(make_pair(profile, ExtensionNameMap())); |
| 98 } |
| 99 |
| 100 namespace { |
| 101 |
| 102 std::string NameToPublicKey(const std::string& name) { |
| 103 std::string public_key; |
| 104 std::string pem; |
| 105 EXPECT_TRUE(Extension::ProducePEM(name, &pem) && |
| 106 Extension::FormatPEMForFileOutput(pem, &public_key, |
| 107 true /* is_public */)); |
| 108 return public_key; |
| 109 } |
| 110 |
| 111 // TODO(akalin): Somehow unify this with MakeExtension() in |
| 112 // extension_util_unittest.cc. |
| 113 scoped_refptr<Extension> CreateExtension( |
| 114 const FilePath& base_dir, const std::string& name, |
| 115 Extension::Type type) { |
| 116 DictionaryValue source; |
| 117 source.SetString(extension_manifest_keys::kName, name); |
| 118 const std::string& public_key = NameToPublicKey(name); |
| 119 source.SetString(extension_manifest_keys::kPublicKey, public_key); |
| 120 source.SetString(extension_manifest_keys::kVersion, "0.0.0.0"); |
| 121 if (type == Extension::TYPE_THEME) { |
| 122 source.Set(extension_manifest_keys::kTheme, new DictionaryValue()); |
| 123 } else if (type != Extension::TYPE_EXTENSION) { |
| 124 // TODO(akalin): Handle other types. |
| 125 ADD_FAILURE(); |
| 126 return NULL; |
| 127 } |
| 128 const FilePath sub_dir = FilePath().AppendASCII(name); |
| 129 FilePath extension_dir; |
| 130 if (!file_util::PathExists(base_dir) && |
| 131 !file_util::CreateDirectory(base_dir) && |
| 132 !file_util::CreateTemporaryDirInDir( |
| 133 base_dir, sub_dir.value(), &extension_dir)) { |
| 134 ADD_FAILURE(); |
| 135 return NULL; |
| 136 } |
| 137 std::string error; |
| 138 scoped_refptr<Extension> extension = |
| 139 Extension::Create(extension_dir, Extension::INTERNAL, |
| 140 source, Extension::STRICT_ERROR_CHECKS, &error); |
| 141 if (!error.empty()) { |
| 142 ADD_FAILURE() << error; |
| 143 return NULL; |
| 144 } |
| 145 if (!extension.get()) { |
| 146 ADD_FAILURE(); |
| 147 return NULL; |
| 148 } |
| 149 if (extension->name() != name) { |
| 150 EXPECT_EQ(name, extension->name()); |
| 151 return NULL; |
| 152 } |
| 153 if (extension->GetType() != type) { |
| 154 EXPECT_EQ(type, extension->GetType()); |
| 155 return NULL; |
| 156 } |
| 157 return extension; |
| 158 } |
| 159 |
| 160 } // namespace |
| 161 |
| 162 scoped_refptr<Extension> LiveSyncExtensionHelper::GetExtension( |
| 163 Profile* profile, const std::string& name, |
| 164 Extension::Type type) { |
| 165 if (name.empty()) { |
| 166 ADD_FAILURE(); |
| 167 return NULL; |
| 168 } |
| 169 ProfileExtensionNameMap::iterator it = profile_extensions_.find(profile); |
| 170 if (it == profile_extensions_.end()) { |
| 171 ADD_FAILURE(); |
| 172 return NULL; |
| 173 } |
| 174 ExtensionNameMap::const_iterator it2 = it->second.find(name); |
| 175 if (it2 != it->second.end()) { |
| 176 return it2->second; |
| 177 } |
| 178 |
| 179 scoped_refptr<Extension> extension = |
| 180 CreateExtension(profile->GetExtensionService()->install_directory(), |
| 181 name, type); |
| 182 if (!extension.get()) { |
| 183 ADD_FAILURE(); |
| 184 return NULL; |
| 185 } |
| 186 const std::string& expected_id = NameToId(name); |
| 187 if (extension->id() != expected_id) { |
| 188 EXPECT_EQ(expected_id, extension->id()); |
| 189 return NULL; |
| 190 } |
| 191 VLOG(2) << "created extension with name = " |
| 192 << name << ", id = " << expected_id; |
| 193 (it->second)[name] = extension; |
| 194 id_to_name_[expected_id] = name; |
| 195 return extension; |
| 196 } |
OLD | NEW |