rdevlin.cronin | 11f0168 | 2016-11-28 18:15:52 | [diff] [blame^] | 1 | // Copyright 2016 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 | #ifndef CHROME_BROWSER_EXTENSIONS_CHROME_TEST_EXTENSION_LOADER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_CHROME_TEST_EXTENSION_LOADER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
| 10 | #include "base/files/file_path.h" |
| 11 | #include "base/files/scoped_temp_dir.h" |
| 12 | #include "base/macros.h" |
| 13 | #include "extensions/common/extension.h" |
| 14 | #include "extensions/common/manifest.h" |
| 15 | |
| 16 | namespace base { |
| 17 | class FilePath; |
| 18 | } |
| 19 | |
| 20 | namespace content { |
| 21 | class BrowserContext; |
| 22 | } |
| 23 | |
| 24 | class ExtensionService; |
| 25 | |
| 26 | namespace extensions { |
| 27 | class ExtensionRegistry; |
| 28 | class ExtensionSystem; |
| 29 | |
| 30 | // A test class to help with loading packed or unpacked extensions. Designed to |
| 31 | // be used by both browser tests and unit tests. Note that this should be used |
| 32 | // for a single extension, and is designed to be used on the stack (rather than |
| 33 | // as a test suite member). |
| 34 | class ChromeTestExtensionLoader { |
| 35 | public: |
| 36 | explicit ChromeTestExtensionLoader(content::BrowserContext* browser_context); |
| 37 | ~ChromeTestExtensionLoader(); |
| 38 | |
| 39 | // Loads the extension specified by |file_path|. Works for both packed and |
| 40 | // unpacked extensions. |
| 41 | scoped_refptr<const Extension> LoadExtension(const base::FilePath& file_path); |
| 42 | |
| 43 | // Myriad different settings. See the member variable declarations for |
| 44 | // explanations and defaults. |
| 45 | // Prefer using these setters rather than adding n different |
| 46 | // LoadExtensionWith* variants (that's not scalable). |
| 47 | void set_expected_id(const std::string& expected_id) { |
| 48 | expected_id_ = expected_id; |
| 49 | } |
| 50 | void add_creation_flag(Extension::InitFromValueFlags flag) { |
| 51 | creation_flags_ |= flag; |
| 52 | } |
| 53 | void set_creation_flags(int flags) { creation_flags_ = flags; } |
| 54 | void set_location(Manifest::Location location) { location_ = location; } |
| 55 | void set_should_fail(bool should_fail) { should_fail_ = should_fail; } |
| 56 | void set_pack_extension(bool pack_extension) { |
| 57 | pack_extension_ = pack_extension; |
| 58 | } |
| 59 | void set_install_immediately(bool install_immediately) { |
| 60 | install_immediately_ = install_immediately; |
| 61 | } |
| 62 | void set_grant_permissions(bool grant_permissions) { |
| 63 | grant_permissions_ = grant_permissions; |
| 64 | } |
| 65 | void set_allow_file_access(bool allow_file_access) { |
| 66 | allow_file_access_ = allow_file_access; |
| 67 | } |
| 68 | void set_allow_incognito_access(bool allow_incognito_access) { |
| 69 | allow_incognito_access_ = allow_incognito_access; |
| 70 | } |
| 71 | void set_ignore_manifest_warnings(bool ignore_manifest_warnings) { |
| 72 | ignore_manifest_warnings_ = ignore_manifest_warnings; |
| 73 | } |
| 74 | void set_require_modern_manifest_version(bool require_modern_version) { |
| 75 | require_modern_manifest_version_ = require_modern_version; |
| 76 | } |
| 77 | void set_install_param(const std::string& install_param) { |
| 78 | install_param_ = install_param; |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | // Packs the extension at |unpacked_path| and returns the path to the created |
| 83 | // crx. Note that the created crx is tied to the lifetime of |this|. |
| 84 | base::FilePath PackExtension(const base::FilePath& unpacked_path); |
| 85 | |
| 86 | // Loads the crx pointed to by |crx_path|. |
| 87 | scoped_refptr<const Extension> LoadCrx(const base::FilePath& crx_path); |
| 88 | |
| 89 | // Loads the unpacked extension pointed to by |unpacked_path|. |
| 90 | scoped_refptr<const Extension> LoadUnpacked( |
| 91 | const base::FilePath& unpacked_path); |
| 92 | |
| 93 | // Checks that the permissions of the loaded extension are correct. |
| 94 | void CheckPermissions(const std::string& extension_id); |
| 95 | |
| 96 | // Checks for any errors associated with the extension. |
| 97 | bool CheckErrors(const Extension& extension); |
| 98 | |
| 99 | // Waits for the extension to finish setting up. |
| 100 | bool WaitForExtensionReady(); |
| 101 | |
| 102 | // The associated context and services. |
| 103 | content::BrowserContext* browser_context_ = nullptr; |
| 104 | ExtensionSystem* extension_system_ = nullptr; |
| 105 | ExtensionService* extension_service_ = nullptr; |
| 106 | ExtensionRegistry* extension_registry_ = nullptr; |
| 107 | |
| 108 | // A temporary directory for packing extensions. |
| 109 | base::ScopedTempDir temp_dir_; |
| 110 | |
| 111 | // The extension id of the loaded extension. |
| 112 | std::string extension_id_; |
| 113 | |
| 114 | // A provided PEM path to use. If not provided, a temporary one will be |
| 115 | // created. |
| 116 | base::FilePath pem_path_; |
| 117 | |
| 118 | // The expected extension id, if any. |
| 119 | std::string expected_id_; |
| 120 | |
| 121 | // An install param to use with the loaded extension. |
| 122 | std::string install_param_; |
| 123 | |
| 124 | // Any creation flags (see Extension::InitFromValueFlags) to use for the |
| 125 | // extension. Only used for crx installs. |
| 126 | int creation_flags_ = Extension::NO_FLAGS; |
| 127 | |
| 128 | // The install location of the added extension. |
| 129 | Manifest::Location location_ = Manifest::INTERNAL; |
| 130 | |
| 131 | // Whether or not the installation should fail. |
| 132 | bool should_fail_ = false; |
| 133 | |
| 134 | // Whether or not to always pack the extension before loading it. Otherwise, |
| 135 | // the extension will be loaded as an unpacked extension. |
| 136 | bool pack_extension_ = false; |
| 137 | |
| 138 | // Whether or not to install the extension immediately. Only used for crx |
| 139 | // installs. |
| 140 | bool install_immediately_ = true; |
| 141 | |
| 142 | // Whether or not to automatically grant permissions to the installed |
| 143 | // extension. Only used for crx installs. |
| 144 | bool grant_permissions_ = true; |
| 145 | |
| 146 | // Whether or not to allow file access by default to the extension. |
| 147 | bool allow_file_access_ = false; |
| 148 | |
| 149 | // Whether or not to allow incognito access by default to the extension. |
| 150 | bool allow_incognito_access_ = false; |
| 151 | |
| 152 | // Whether or not to ignore manifest warnings during installation. |
| 153 | bool ignore_manifest_warnings_ = false; |
| 154 | |
| 155 | // Whether or not to enforce a minimum manifest version requirement. |
| 156 | bool require_modern_manifest_version_ = true; |
| 157 | |
| 158 | DISALLOW_COPY_AND_ASSIGN(ChromeTestExtensionLoader); |
| 159 | }; |
| 160 | |
| 161 | } // namespace extensions |
| 162 | |
| 163 | #endif // CHROME_BROWSER_EXTENSIONS_CHROME_TEST_EXTENSION_LOADER_H_ |