| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/browser/extensions/extension_browsertest.h" | 5 #include "chrome/browser/extensions/extension_browsertest.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/files/file_path.h" | 13 #include "base/files/file_path.h" |
| 14 #include "base/files/file_util.h" | 14 #include "base/files/file_util.h" |
| 15 #include "base/files/scoped_temp_dir.h" | 15 #include "base/files/scoped_temp_dir.h" |
| 16 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
| 18 #include "base/strings/stringprintf.h" | 18 #include "base/strings/stringprintf.h" |
| 19 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
| 20 #include "build/build_config.h" | 20 #include "build/build_config.h" |
| 21 #include "chrome/browser/extensions/browsertest_util.h" | 21 #include "chrome/browser/extensions/browsertest_util.h" |
| 22 #include "chrome/browser/extensions/chrome_test_extension_loader.h" |
| 22 #include "chrome/browser/extensions/component_loader.h" | 23 #include "chrome/browser/extensions/component_loader.h" |
| 23 #include "chrome/browser/extensions/crx_installer.h" | 24 #include "chrome/browser/extensions/crx_installer.h" |
| 24 #include "chrome/browser/extensions/extension_creator.h" | 25 #include "chrome/browser/extensions/extension_creator.h" |
| 25 #include "chrome/browser/extensions/extension_error_reporter.h" | 26 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 26 #include "chrome/browser/extensions/extension_install_prompt.h" | 27 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 27 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" | 28 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" |
| 28 #include "chrome/browser/extensions/extension_service.h" | 29 #include "chrome/browser/extensions/extension_service.h" |
| 29 #include "chrome/browser/extensions/extension_util.h" | 30 #include "chrome/browser/extensions/extension_util.h" |
| 30 #include "chrome/browser/extensions/unpacked_installer.h" | 31 #include "chrome/browser/extensions/unpacked_installer.h" |
| 31 #include "chrome/browser/extensions/updater/extension_cache_fake.h" | 32 #include "chrome/browser/extensions/updater/extension_cache_fake.h" |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 169 const base::FilePath& path) { | 170 const base::FilePath& path) { |
| 170 return LoadExtensionWithFlags(path, | 171 return LoadExtensionWithFlags(path, |
| 171 kFlagEnableFileAccess | kFlagEnableIncognito); | 172 kFlagEnableFileAccess | kFlagEnableIncognito); |
| 172 } | 173 } |
| 173 | 174 |
| 174 const Extension* ExtensionBrowserTest::LoadExtensionWithFlags( | 175 const Extension* ExtensionBrowserTest::LoadExtensionWithFlags( |
| 175 const base::FilePath& path, int flags) { | 176 const base::FilePath& path, int flags) { |
| 176 return LoadExtensionWithInstallParam(path, flags, std::string()); | 177 return LoadExtensionWithInstallParam(path, flags, std::string()); |
| 177 } | 178 } |
| 178 | 179 |
| 179 const extensions::Extension* | 180 const Extension* ExtensionBrowserTest::LoadExtensionWithInstallParam( |
| 180 ExtensionBrowserTest::LoadExtensionWithInstallParam( | |
| 181 const base::FilePath& path, | 181 const base::FilePath& path, |
| 182 int flags, | 182 int flags, |
| 183 const std::string& install_param) { | 183 const std::string& install_param) { |
| 184 ExtensionService* service = extensions::ExtensionSystem::Get( | 184 extensions::ChromeTestExtensionLoader loader(profile()); |
| 185 profile())->extension_service(); | 185 loader.set_require_modern_manifest_version( |
| 186 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | 186 (flags & kFlagAllowOldManifestVersions) == 0); |
| 187 { | 187 loader.set_ignore_manifest_warnings( |
| 188 observer_->Watch(extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | 188 (flags & kFlagIgnoreManifestWarnings) != 0); |
| 189 content::NotificationService::AllSources()); | 189 loader.set_allow_incognito_access((flags & kFlagEnableIncognito) != 0); |
| 190 | 190 loader.set_allow_file_access((flags & kFlagEnableFileAccess) != 0); |
| 191 scoped_refptr<extensions::UnpackedInstaller> installer( | 191 loader.set_install_param(install_param); |
| 192 extensions::UnpackedInstaller::Create(service)); | 192 scoped_refptr<const Extension> extension = loader.LoadExtension(path); |
| 193 installer->set_prompt_for_plugins(false); | 193 if (extension) |
| 194 installer->set_require_modern_manifest_version( | 194 observer_->set_last_loaded_extension_id(extension->id()); |
| 195 (flags & kFlagAllowOldManifestVersions) == 0); | 195 return extension.get(); |
| 196 installer->Load(path); | |
| 197 | |
| 198 observer_->Wait(); | |
| 199 } | |
| 200 | |
| 201 // Find the loaded extension by its path. See crbug.com/59531 for why | |
| 202 // we cannot just use last_loaded_extension_id(). | |
| 203 const Extension* extension = | |
| 204 GetExtensionByPath(registry->enabled_extensions(), path); | |
| 205 if (!extension) | |
| 206 return NULL; | |
| 207 | |
| 208 if (!(flags & kFlagIgnoreManifestWarnings)) { | |
| 209 const std::vector<extensions::InstallWarning>& install_warnings = | |
| 210 extension->install_warnings(); | |
| 211 if (!install_warnings.empty()) { | |
| 212 std::string install_warnings_message = base::StringPrintf( | |
| 213 "Unexpected warnings when loading test extension %s:\n", | |
| 214 path.AsUTF8Unsafe().c_str()); | |
| 215 | |
| 216 for (std::vector<extensions::InstallWarning>::const_iterator it = | |
| 217 install_warnings.begin(); it != install_warnings.end(); ++it) { | |
| 218 install_warnings_message += " " + it->message + "\n"; | |
| 219 } | |
| 220 | |
| 221 EXPECT_EQ(0u, extension->install_warnings().size()) | |
| 222 << install_warnings_message; | |
| 223 return NULL; | |
| 224 } | |
| 225 } | |
| 226 | |
| 227 const std::string extension_id = extension->id(); | |
| 228 | |
| 229 // If this is an incognito test (e.g. where the test fixture appended the | |
| 230 // --incognito flag), we need to use the original profile when we wait for | |
| 231 // notifications. | |
| 232 Profile* original_profile = profile()->GetOriginalProfile(); | |
| 233 | |
| 234 if (!install_param.empty()) { | |
| 235 extensions::ExtensionPrefs::Get(original_profile) | |
| 236 ->SetInstallParam(extension_id, install_param); | |
| 237 // Re-enable the extension if needed. | |
| 238 if (registry->enabled_extensions().Contains(extension_id)) { | |
| 239 content::WindowedNotificationObserver load_signal( | |
| 240 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
| 241 content::Source<Profile>(original_profile)); | |
| 242 // Reload the extension so that the | |
| 243 // NOTIFICATION_EXTENSION_LOADED_DEPRECATED | |
| 244 // observers may access |install_param|. | |
| 245 service->ReloadExtension(extension_id); | |
| 246 load_signal.Wait(); | |
| 247 extension = service->GetExtensionById(extension_id, false); | |
| 248 CHECK(extension) << extension_id << " not found after reloading."; | |
| 249 } | |
| 250 } | |
| 251 | |
| 252 // Toggling incognito or file access will reload the extension, so wait for | |
| 253 // the reload and grab the new extension instance. The default state is | |
| 254 // incognito disabled and file access enabled, so we don't wait in those | |
| 255 // cases. | |
| 256 { | |
| 257 content::WindowedNotificationObserver load_signal( | |
| 258 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
| 259 content::Source<Profile>(original_profile)); | |
| 260 CHECK(!extensions::util::IsIncognitoEnabled(extension_id, original_profile)) | |
| 261 << extension_id << " is enabled in incognito, but shouldn't be"; | |
| 262 | |
| 263 if (flags & kFlagEnableIncognito) { | |
| 264 extensions::util::SetIsIncognitoEnabled(extension_id, original_profile, | |
| 265 true); | |
| 266 load_signal.Wait(); | |
| 267 extension = service->GetExtensionById(extension_id, false); | |
| 268 CHECK(extension) << extension_id << " not found after reloading."; | |
| 269 } | |
| 270 } | |
| 271 | |
| 272 { | |
| 273 content::WindowedNotificationObserver load_signal( | |
| 274 extensions::NOTIFICATION_EXTENSION_LOADED_DEPRECATED, | |
| 275 content::Source<Profile>(original_profile)); | |
| 276 CHECK(extensions::util::AllowFileAccess(extension_id, original_profile)); | |
| 277 if (!(flags & kFlagEnableFileAccess)) { | |
| 278 extensions::util::SetAllowFileAccess(extension_id, original_profile, | |
| 279 false); | |
| 280 load_signal.Wait(); | |
| 281 extension = service->GetExtensionById(extension_id, false); | |
| 282 CHECK(extension) << extension_id << " not found after reloading."; | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 if (!observer_->WaitForExtensionViewsToLoad()) | |
| 287 return NULL; | |
| 288 | |
| 289 return extension; | |
| 290 } | 196 } |
| 291 | 197 |
| 292 const Extension* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest( | 198 const Extension* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest( |
| 293 const base::FilePath& path, | 199 const base::FilePath& path, |
| 294 const base::FilePath::CharType* manifest_relative_path) { | 200 const base::FilePath::CharType* manifest_relative_path) { |
| 295 ExtensionService* service = extensions::ExtensionSystem::Get( | 201 ExtensionService* service = extensions::ExtensionSystem::Get( |
| 296 profile())->extension_service(); | 202 profile())->extension_service(); |
| 297 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | 203 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
| 298 | 204 |
| 299 std::string manifest; | 205 std::string manifest; |
| (...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 return extensions::browsertest_util::ExecuteScriptInBackgroundPage( | 551 return extensions::browsertest_util::ExecuteScriptInBackgroundPage( |
| 646 profile(), extension_id, script); | 552 profile(), extension_id, script); |
| 647 } | 553 } |
| 648 | 554 |
| 649 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait( | 555 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait( |
| 650 const std::string& extension_id, | 556 const std::string& extension_id, |
| 651 const std::string& script) { | 557 const std::string& script) { |
| 652 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait( | 558 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait( |
| 653 profile(), extension_id, script); | 559 profile(), extension_id, script); |
| 654 } | 560 } |
| OLD | NEW |