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