[email protected] | 8e7b2cf4 | 2012-04-18 14:26:58 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 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_UNPACKED_INSTALLER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 7 | |
dcheng | c963c714 | 2016-04-08 03:55:22 | [diff] [blame] | 8 | #include <memory> |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 9 | #include <string> |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 10 | #include <vector> |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 11 | |
[email protected] | 500c7bb | 2014-04-26 22:44:33 | [diff] [blame] | 12 | #include "base/bind.h" |
[email protected] | 5799981 | 2013-02-24 05:40:52 | [diff] [blame] | 13 | #include "base/files/file_path.h" |
avi | a2f4804a | 2015-12-24 23:11:13 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 15 | #include "base/memory/ref_counted.h" |
| 16 | #include "base/memory/weak_ptr.h" |
Karandeep Bhatia | a893065 | 2017-10-11 17:41:12 | [diff] [blame^] | 17 | #include "base/optional.h" |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 18 | #include "extensions/browser/preload_check.h" |
Karan Bhatia | fe12281a | 2017-09-16 02:38:18 | [diff] [blame] | 19 | #include "extensions/common/manifest.h" |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 20 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 21 | class ExtensionService; |
dgozman | d741d25d | 2017-03-23 00:35:34 | [diff] [blame] | 22 | class Profile; |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 23 | |
| 24 | namespace extensions { |
| 25 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 26 | class Extension; |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 27 | class PreloadCheckGroup; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 28 | |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 29 | // Installs and loads an unpacked extension. Because internal state needs to be |
| 30 | // held about the instalation process, only one call to Load*() should be made |
| 31 | // per UnpackedInstaller. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 32 | // TODO(erikkay): It might be useful to be able to load a packed extension |
| 33 | // (presumably into memory) without installing it. |
| 34 | class UnpackedInstaller |
| 35 | : public base::RefCountedThreadSafe<UnpackedInstaller> { |
| 36 | public: |
rdevlin.cronin | f6ea63a | 2015-03-04 17:51:04 | [diff] [blame] | 37 | using CompletionCallback = base::Callback<void(const Extension* extension, |
| 38 | const base::FilePath&, |
| 39 | const std::string&)>; |
[email protected] | 500c7bb | 2014-04-26 22:44:33 | [diff] [blame] | 40 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 41 | static scoped_refptr<UnpackedInstaller> Create( |
| 42 | ExtensionService* extension_service); |
| 43 | |
| 44 | // Loads the extension from the directory |extension_path|, which is |
| 45 | // the top directory of a specific extension where its manifest file lives. |
| 46 | // Errors are reported through ExtensionErrorReporter. On success, |
| 47 | // ExtensionService::AddExtension() is called. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 48 | void Load(const base::FilePath& extension_path); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 49 | |
| 50 | // Loads the extension from the directory |extension_path|; |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 51 | // for use with command line switch --load-extension=path or |
| 52 | // --load-and-launch-app=path. |
| 53 | // This is equivalent to Load, except that it reads the extension from |
| 54 | // |extension_path| synchronously. |
| 55 | // The return value indicates whether the installation has begun successfully. |
| 56 | // The id of the extension being loaded is returned in |extension_id|. |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 57 | // |only_allow_apps| is used to avoid side-loading of non-app extensions. |
[email protected] | 2a69b94 | 2013-05-31 09:37:53 | [diff] [blame] | 58 | bool LoadFromCommandLine(const base::FilePath& extension_path, |
proberge | 80a37f3 | 2016-08-04 19:44:55 | [diff] [blame] | 59 | std::string* extension_id, |
| 60 | bool only_allow_apps); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 61 | |
| 62 | // Allows prompting for plugins to be disabled; intended for testing only. |
| 63 | bool prompt_for_plugins() { return prompt_for_plugins_; } |
| 64 | void set_prompt_for_plugins(bool val) { prompt_for_plugins_ = val; } |
| 65 | |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 66 | // Allows overriding of whether modern manifest versions are required; |
| 67 | // intended for testing. |
| 68 | bool require_modern_manifest_version() const { |
| 69 | return require_modern_manifest_version_; |
| 70 | } |
| 71 | void set_require_modern_manifest_version(bool val) { |
| 72 | require_modern_manifest_version_ = val; |
| 73 | } |
| 74 | |
[email protected] | 1f072244 | 2014-05-01 17:26:02 | [diff] [blame] | 75 | void set_be_noisy_on_failure(bool be_noisy_on_failure) { |
| 76 | be_noisy_on_failure_ = be_noisy_on_failure; |
| 77 | } |
| 78 | |
rdevlin.cronin | f6ea63a | 2015-03-04 17:51:04 | [diff] [blame] | 79 | void set_completion_callback(const CompletionCallback& callback) { |
| 80 | callback_ = callback; |
| 81 | } |
| 82 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 83 | private: |
| 84 | friend class base::RefCountedThreadSafe<UnpackedInstaller>; |
| 85 | |
| 86 | explicit UnpackedInstaller(ExtensionService* extension_service); |
| 87 | virtual ~UnpackedInstaller(); |
| 88 | |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 89 | // Must be called from the UI thread. |
[email protected] | 849749d | 2013-05-06 17:30:45 | [diff] [blame] | 90 | void ShowInstallPrompt(); |
| 91 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 92 | // Begin management policy and requirements checks. |
| 93 | void StartInstallChecks(); |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 94 | |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 95 | // Callback from PreloadCheckGroup. |
Istiaque Ahmed | 400c83a | 2017-10-11 02:39:35 | [diff] [blame] | 96 | void OnInstallChecksComplete(const PreloadCheck::Errors& errors); |
[email protected] | 9827043 | 2012-09-11 20:51:24 | [diff] [blame] | 97 | |
[email protected] | 8e7b2cf4 | 2012-04-18 14:26:58 | [diff] [blame] | 98 | // Verifies if loading unpacked extensions is allowed. |
| 99 | bool IsLoadingUnpackedAllowed() const; |
| 100 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 101 | // We change the input extension path to an absolute path, on the file thread. |
| 102 | // Then we need to check the file access preference, which needs |
| 103 | // to happen back on the UI thread, so it posts CheckExtensionFileAccess on |
| 104 | // the UI thread. In turn, once that gets the pref, it goes back to the |
| 105 | // file thread with LoadWithFileAccess. |
| 106 | // TODO(yoz): It would be nice to remove this ping-pong, but we need to know |
[email protected] | 85df9d1 | 2014-04-15 17:02:14 | [diff] [blame] | 107 | // what file access flags to pass to file_util::LoadExtension. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 108 | void GetAbsolutePath(); |
| 109 | void CheckExtensionFileAccess(); |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 110 | void LoadWithFileAccess(int flags); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 111 | |
[email protected] | add57db7 | 2014-04-01 23:25:22 | [diff] [blame] | 112 | // Notify the frontend that an attempt to retry will not be necessary. |
| 113 | void UnregisterLoadRetryListener(); |
| 114 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 115 | // Notify the frontend that there was an error loading an extension. |
| 116 | void ReportExtensionLoadError(const std::string& error); |
| 117 | |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 118 | // Passes the extension onto extension service. |
| 119 | void InstallExtension(); |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 120 | |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 121 | // Helper to get the Extension::CreateFlags for the installing extension. |
| 122 | int GetFlags(); |
| 123 | |
Karan Bhatia | fe12281a | 2017-09-16 02:38:18 | [diff] [blame] | 124 | // Helper to load an extension. Should be called on a sequence where file IO |
Karan Bhatia | 328e31a | 2017-09-18 21:46:16 | [diff] [blame] | 125 | // is allowed. Loads the extension, validates extension locales and persists |
| 126 | // the ruleset for the Declarative Net Request API, if needed. In case of an |
| 127 | // error, returns false and populates |error|. |
Karan Bhatia | fe12281a | 2017-09-16 02:38:18 | [diff] [blame] | 128 | bool LoadExtension(Manifest::Location location, |
| 129 | int flags, |
| 130 | std::string* error); |
| 131 | |
Karan Bhatia | 328e31a | 2017-09-18 21:46:16 | [diff] [blame] | 132 | // Reads the Declarative Net Request JSON ruleset for the extension, if it |
| 133 | // provided one, and persists the indexed ruleset. Returns false and populates |
| 134 | // |error| in case of an error. Should be called on a sequence where file IO |
| 135 | // is allowed. |
| 136 | bool IndexAndPersistRulesIfNeeded(std::string* error); |
| 137 | |
dgozman | d741d25d | 2017-03-23 00:35:34 | [diff] [blame] | 138 | const Extension* extension() { return extension_.get(); } |
[email protected] | 253fc2bb | 2014-07-10 04:21:18 | [diff] [blame] | 139 | |
[email protected] | 849749d | 2013-05-06 17:30:45 | [diff] [blame] | 140 | // The service we will report results back to. |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 141 | base::WeakPtr<ExtensionService> service_weak_; |
| 142 | |
dgozman | d741d25d | 2017-03-23 00:35:34 | [diff] [blame] | 143 | // The Profile the extension is being installed in. |
| 144 | Profile* profile_; |
| 145 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 146 | // The pathname of the directory to load from, which is an absolute path |
| 147 | // after GetAbsolutePath has been called. |
[email protected] | 650b2d5 | 2013-02-10 03:41:45 | [diff] [blame] | 148 | base::FilePath extension_path_; |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 149 | |
dgozman | d741d25d | 2017-03-23 00:35:34 | [diff] [blame] | 150 | // The extension being installed. |
Karan Bhatia | 328e31a | 2017-09-18 21:46:16 | [diff] [blame] | 151 | scoped_refptr<Extension> extension_; |
dgozman | d741d25d | 2017-03-23 00:35:34 | [diff] [blame] | 152 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 153 | // If true and the extension contains plugins, we prompt the user before |
| 154 | // loading. |
| 155 | bool prompt_for_plugins_; |
| 156 | |
[email protected] | b7462f3 | 2012-09-02 15:18:12 | [diff] [blame] | 157 | // Whether to require the extension installed to have a modern manifest |
| 158 | // version. |
| 159 | bool require_modern_manifest_version_; |
| 160 | |
[email protected] | 1f072244 | 2014-05-01 17:26:02 | [diff] [blame] | 161 | // Whether or not to be noisy (show a dialog) on failure. Defaults to true. |
| 162 | bool be_noisy_on_failure_; |
| 163 | |
michaelpg | 6a4874f | 2017-04-13 20:41:33 | [diff] [blame] | 164 | // Checks to run before the extension can be installed. |
| 165 | std::unique_ptr<PreloadCheck> policy_check_; |
| 166 | std::unique_ptr<PreloadCheck> requirements_check_; |
| 167 | |
| 168 | // Runs the above checks. |
| 169 | std::unique_ptr<PreloadCheckGroup> check_group_; |
[email protected] | 849749d | 2013-05-06 17:30:45 | [diff] [blame] | 170 | |
Karandeep Bhatia | a893065 | 2017-10-11 17:41:12 | [diff] [blame^] | 171 | // The checksum for the indexed ruleset corresponding to the Declarative Net |
| 172 | // Request API. |
| 173 | base::Optional<int> dnr_ruleset_checksum_; |
| 174 | |
rdevlin.cronin | f6ea63a | 2015-03-04 17:51:04 | [diff] [blame] | 175 | CompletionCallback callback_; |
| 176 | |
[email protected] | d8c8f25f | 2011-11-02 18:18:01 | [diff] [blame] | 177 | DISALLOW_COPY_AND_ASSIGN(UnpackedInstaller); |
| 178 | }; |
| 179 | |
| 180 | } // namespace extensions |
| 181 | |
| 182 | #endif // CHROME_BROWSER_EXTENSIONS_UNPACKED_INSTALLER_H_ |