| 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 "base/task_scheduler/post_task.h" | 20 #include "base/task_scheduler/post_task.h" |
| 21 #include "base/threading/thread_restrictions.h" |
| 21 #include "build/build_config.h" | 22 #include "build/build_config.h" |
| 22 #include "chrome/browser/extensions/browsertest_util.h" | 23 #include "chrome/browser/extensions/browsertest_util.h" |
| 23 #include "chrome/browser/extensions/chrome_test_extension_loader.h" | 24 #include "chrome/browser/extensions/chrome_test_extension_loader.h" |
| 24 #include "chrome/browser/extensions/component_loader.h" | 25 #include "chrome/browser/extensions/component_loader.h" |
| 25 #include "chrome/browser/extensions/crx_installer.h" | 26 #include "chrome/browser/extensions/crx_installer.h" |
| 26 #include "chrome/browser/extensions/extension_creator.h" | 27 #include "chrome/browser/extensions/extension_creator.h" |
| 27 #include "chrome/browser/extensions/extension_error_reporter.h" | 28 #include "chrome/browser/extensions/extension_error_reporter.h" |
| 28 #include "chrome/browser/extensions/extension_install_prompt.h" | 29 #include "chrome/browser/extensions/extension_install_prompt.h" |
| 29 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" | 30 #include "chrome/browser/extensions/extension_install_prompt_show_params.h" |
| 30 #include "chrome/browser/extensions/extension_service.h" | 31 #include "chrome/browser/extensions/extension_service.h" |
| (...skipping 111 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 else | 143 else |
| 143 profile_ = ProfileManager::GetActiveUserProfile(); | 144 profile_ = ProfileManager::GetActiveUserProfile(); |
| 144 } | 145 } |
| 145 return profile_; | 146 return profile_; |
| 146 } | 147 } |
| 147 | 148 |
| 148 // static | 149 // static |
| 149 const Extension* ExtensionBrowserTest::GetExtensionByPath( | 150 const Extension* ExtensionBrowserTest::GetExtensionByPath( |
| 150 const extensions::ExtensionSet& extensions, | 151 const extensions::ExtensionSet& extensions, |
| 151 const base::FilePath& path) { | 152 const base::FilePath& path) { |
| 153 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 152 base::FilePath extension_path = base::MakeAbsoluteFilePath(path); | 154 base::FilePath extension_path = base::MakeAbsoluteFilePath(path); |
| 153 EXPECT_TRUE(!extension_path.empty()); | 155 EXPECT_TRUE(!extension_path.empty()); |
| 154 for (const scoped_refptr<const Extension>& extension : extensions) { | 156 for (const scoped_refptr<const Extension>& extension : extensions) { |
| 155 if (extension->path() == extension_path) { | 157 if (extension->path() == extension_path) { |
| 156 return extension.get(); | 158 return extension.get(); |
| 157 } | 159 } |
| 158 } | 160 } |
| 159 return NULL; | 161 return NULL; |
| 160 } | 162 } |
| 161 | 163 |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 return extension.get(); | 247 return extension.get(); |
| 246 } | 248 } |
| 247 | 249 |
| 248 const Extension* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest( | 250 const Extension* ExtensionBrowserTest::LoadExtensionAsComponentWithManifest( |
| 249 const base::FilePath& path, | 251 const base::FilePath& path, |
| 250 const base::FilePath::CharType* manifest_relative_path) { | 252 const base::FilePath::CharType* manifest_relative_path) { |
| 251 ExtensionService* service = extensions::ExtensionSystem::Get( | 253 ExtensionService* service = extensions::ExtensionSystem::Get( |
| 252 profile())->extension_service(); | 254 profile())->extension_service(); |
| 253 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); | 255 ExtensionRegistry* registry = ExtensionRegistry::Get(profile()); |
| 254 | 256 |
| 257 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 255 std::string manifest; | 258 std::string manifest; |
| 256 if (!base::ReadFileToString(path.Append(manifest_relative_path), &manifest)) { | 259 if (!base::ReadFileToString(path.Append(manifest_relative_path), &manifest)) { |
| 257 return NULL; | 260 return NULL; |
| 258 } | 261 } |
| 259 | 262 |
| 260 service->component_loader()->set_ignore_whitelist_for_testing(true); | 263 service->component_loader()->set_ignore_whitelist_for_testing(true); |
| 261 std::string extension_id = service->component_loader()->Add(manifest, path); | 264 std::string extension_id = service->component_loader()->Add(manifest, path); |
| 262 const Extension* extension = | 265 const Extension* extension = |
| 263 registry->enabled_extensions().GetByID(extension_id); | 266 registry->enabled_extensions().GetByID(extension_id); |
| 264 if (!extension) | 267 if (!extension) |
| (...skipping 20 matching lines...) Expand all Loading... |
| 285 extensions::SOURCE_TEST); | 288 extensions::SOURCE_TEST); |
| 286 params.command_line = *base::CommandLine::ForCurrentProcess(); | 289 params.command_line = *base::CommandLine::ForCurrentProcess(); |
| 287 OpenApplication(params); | 290 OpenApplication(params); |
| 288 app_loaded_observer.Wait(); | 291 app_loaded_observer.Wait(); |
| 289 | 292 |
| 290 return app; | 293 return app; |
| 291 } | 294 } |
| 292 | 295 |
| 293 base::FilePath ExtensionBrowserTest::PackExtension( | 296 base::FilePath ExtensionBrowserTest::PackExtension( |
| 294 const base::FilePath& dir_path) { | 297 const base::FilePath& dir_path) { |
| 298 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 295 base::FilePath crx_path = temp_dir_.GetPath().AppendASCII("temp.crx"); | 299 base::FilePath crx_path = temp_dir_.GetPath().AppendASCII("temp.crx"); |
| 296 if (!base::DeleteFile(crx_path, false)) { | 300 if (!base::DeleteFile(crx_path, false)) { |
| 297 ADD_FAILURE() << "Failed to delete crx: " << crx_path.value(); | 301 ADD_FAILURE() << "Failed to delete crx: " << crx_path.value(); |
| 298 return base::FilePath(); | 302 return base::FilePath(); |
| 299 } | 303 } |
| 300 | 304 |
| 301 // Look for PEM files with the same name as the directory. | 305 // Look for PEM files with the same name as the directory. |
| 302 base::FilePath pem_path = | 306 base::FilePath pem_path = |
| 303 dir_path.ReplaceExtension(FILE_PATH_LITERAL(".pem")); | 307 dir_path.ReplaceExtension(FILE_PATH_LITERAL(".pem")); |
| 304 base::FilePath pem_path_out; | 308 base::FilePath pem_path_out; |
| 305 | 309 |
| 306 if (!base::PathExists(pem_path)) { | 310 if (!base::PathExists(pem_path)) { |
| 307 pem_path = base::FilePath(); | 311 pem_path = base::FilePath(); |
| 308 pem_path_out = crx_path.DirName().AppendASCII("temp.pem"); | 312 pem_path_out = crx_path.DirName().AppendASCII("temp.pem"); |
| 309 if (!base::DeleteFile(pem_path_out, false)) { | 313 if (!base::DeleteFile(pem_path_out, false)) { |
| 310 ADD_FAILURE() << "Failed to delete pem: " << pem_path_out.value(); | 314 ADD_FAILURE() << "Failed to delete pem: " << pem_path_out.value(); |
| 311 return base::FilePath(); | 315 return base::FilePath(); |
| 312 } | 316 } |
| 313 } | 317 } |
| 314 | 318 |
| 315 return PackExtensionWithOptions(dir_path, crx_path, pem_path, pem_path_out); | 319 return PackExtensionWithOptions(dir_path, crx_path, pem_path, pem_path_out); |
| 316 } | 320 } |
| 317 | 321 |
| 318 base::FilePath ExtensionBrowserTest::PackExtensionWithOptions( | 322 base::FilePath ExtensionBrowserTest::PackExtensionWithOptions( |
| 319 const base::FilePath& dir_path, | 323 const base::FilePath& dir_path, |
| 320 const base::FilePath& crx_path, | 324 const base::FilePath& crx_path, |
| 321 const base::FilePath& pem_path, | 325 const base::FilePath& pem_path, |
| 322 const base::FilePath& pem_out_path) { | 326 const base::FilePath& pem_out_path) { |
| 327 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 323 if (!base::PathExists(dir_path)) { | 328 if (!base::PathExists(dir_path)) { |
| 324 ADD_FAILURE() << "Extension dir not found: " << dir_path.value(); | 329 ADD_FAILURE() << "Extension dir not found: " << dir_path.value(); |
| 325 return base::FilePath(); | 330 return base::FilePath(); |
| 326 } | 331 } |
| 327 | 332 |
| 328 if (!base::PathExists(pem_path) && pem_out_path.empty()) { | 333 if (!base::PathExists(pem_path) && pem_out_path.empty()) { |
| 329 ADD_FAILURE() << "Must specify a PEM file or PEM output path"; | 334 ADD_FAILURE() << "Must specify a PEM file or PEM output path"; |
| 330 return base::FilePath(); | 335 return base::FilePath(); |
| 331 } | 336 } |
| 332 | 337 |
| (...skipping 274 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 607 return extensions::browsertest_util::ExecuteScriptInBackgroundPage( | 612 return extensions::browsertest_util::ExecuteScriptInBackgroundPage( |
| 608 profile(), extension_id, script); | 613 profile(), extension_id, script); |
| 609 } | 614 } |
| 610 | 615 |
| 611 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait( | 616 bool ExtensionBrowserTest::ExecuteScriptInBackgroundPageNoWait( |
| 612 const std::string& extension_id, | 617 const std::string& extension_id, |
| 613 const std::string& script) { | 618 const std::string& script) { |
| 614 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait( | 619 return extensions::browsertest_util::ExecuteScriptInBackgroundPageNoWait( |
| 615 profile(), extension_id, script); | 620 profile(), extension_id, script); |
| 616 } | 621 } |
| OLD | NEW |