blob: 69c31590d171f554c716fb8bae83324eb11e32fc [file] [log] [blame]
rdevlin.cronin11f01682016-11-28 18:15:521// 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"
Karan Bhatiaab07704b2017-08-22 22:22:5313#include "base/optional.h"
rdevlin.cronin11f01682016-11-28 18:15:5214#include "extensions/common/extension.h"
15#include "extensions/common/manifest.h"
16
17namespace base {
18class FilePath;
19}
20
21namespace content {
22class BrowserContext;
23}
24
rdevlin.cronin11f01682016-11-28 18:15:5225namespace extensions {
26class ExtensionRegistry;
Devlin Cronineea1b7a2018-05-26 02:46:2127class ExtensionService;
rdevlin.cronin11f01682016-11-28 18:15:5228class 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).
34class 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.
Karan Bhatiaab07704b2017-08-22 22:22:5394 void CheckPermissions(const Extension* extension);
rdevlin.cronin11f01682016-11-28 18:15:5295
Karan Bhatiabd4983c2017-08-22 20:02:4096 // Checks for any install warnings associated with the extension.
97 bool CheckInstallWarnings(const Extension& extension);
rdevlin.cronin11f01682016-11-28 18:15:5298
99 // Waits for the extension to finish setting up.
Devlin Cronin7c1e9162018-12-21 21:39:12100 bool WaitForExtensionReady(const Extension& extension);
rdevlin.cronin11f01682016-11-28 18:15:52101
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
Karan Bhatiaab07704b2017-08-22 22:22:53128 // The install location of the added extension. Not valid for unpacked
129 // extensions.
rdevlin.cronin11f01682016-11-28 18:15:52130 Manifest::Location location_ = Manifest::INTERNAL;
131
Karan Bhatiab794ad52017-08-22 22:13:14132 // Whether or not the extension load should fail.
rdevlin.cronin11f01682016-11-28 18:15:52133 bool should_fail_ = false;
134
135 // Whether or not to always pack the extension before loading it. Otherwise,
136 // the extension will be loaded as an unpacked extension.
137 bool pack_extension_ = false;
138
139 // Whether or not to install the extension immediately. Only used for crx
140 // installs.
141 bool install_immediately_ = true;
142
143 // Whether or not to automatically grant permissions to the installed
144 // extension. Only used for crx installs.
145 bool grant_permissions_ = true;
146
147 // Whether or not to allow file access by default to the extension.
Karan Bhatiaab07704b2017-08-22 22:22:53148 base::Optional<bool> allow_file_access_;
rdevlin.cronin11f01682016-11-28 18:15:52149
150 // Whether or not to allow incognito access by default to the extension.
151 bool allow_incognito_access_ = false;
152
153 // Whether or not to ignore manifest warnings during installation.
154 bool ignore_manifest_warnings_ = false;
155
156 // Whether or not to enforce a minimum manifest version requirement.
157 bool require_modern_manifest_version_ = true;
158
159 DISALLOW_COPY_AND_ASSIGN(ChromeTestExtensionLoader);
160};
161
162} // namespace extensions
163
164#endif // CHROME_BROWSER_EXTENSIONS_CHROME_TEST_EXTENSION_LOADER_H_