Avi Drissman | 60039d4 | 2022-09-13 21:49:05 | [diff] [blame] | 1 | // Copyright 2014 The Chromium Authors |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [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 EXTENSIONS_BROWSER_EXTENSION_SYSTEM_H_ |
| 6 | #define EXTENSIONS_BROWSER_EXTENSION_SYSTEM_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
danakj | f6d89bf | 2020-01-07 01:21:29 | [diff] [blame] | 10 | #include "base/callback.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 11 | #include "base/memory/ref_counted.h" |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 12 | #include "build/build_config.h" |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 13 | #include "components/keyed_service/core/keyed_service.h" |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 14 | #include "extensions/browser/install/crx_install_error.h" |
Scott Violet | c8240b0 | 2018-03-08 22:03:59 | [diff] [blame] | 15 | #include "extensions/buildflags/buildflags.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 16 | #include "extensions/common/extension.h" |
Anton Bikineev | 6d67847 | 2021-05-15 18:48:51 | [diff] [blame] | 17 | #include "third_party/abseil-cpp/absl/types/optional.h" |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 18 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 19 | #if !BUILDFLAG(ENABLE_EXTENSIONS) |
thestig | 44b9ce2 | 2014-10-17 03:19:28 | [diff] [blame] | 20 | #error "Extensions must be enabled" |
| 21 | #endif |
| 22 | |
Christopher Lam | ebb9020 | 2019-04-04 03:42:36 | [diff] [blame] | 23 | namespace base { |
| 24 | class OneShotEvent; |
| 25 | } |
| 26 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 27 | namespace content { |
| 28 | class BrowserContext; |
| 29 | } |
| 30 | |
Louise Brett | 88346ae1 | 2021-09-09 04:47:32 | [diff] [blame] | 31 | namespace value_store { |
| 32 | class ValueStoreFactory; |
| 33 | } |
| 34 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 35 | namespace extensions { |
| 36 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 37 | class AppSorting; |
[email protected] | fd3df778 | 2014-05-08 23:54:27 | [diff] [blame] | 38 | class ContentVerifier; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 39 | class Extension; |
Devlin Cronin | eea1b7a | 2018-05-26 02:46:21 | [diff] [blame] | 40 | class ExtensionService; |
[email protected] | f698c16 | 2014-06-13 00:46:26 | [diff] [blame] | 41 | class ExtensionSet; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 42 | class InfoMap; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 43 | class ManagementPolicy; |
[email protected] | aab2310 | 2014-02-05 18:57:55 | [diff] [blame] | 44 | class QuotaService; |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame] | 45 | class ServiceWorkerManager; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 46 | class StateStore; |
Kelvin Jiang | a115a75 | 2021-02-11 03:04:01 | [diff] [blame] | 47 | class UserScriptManager; |
Devlin Cronin | c9c4efe | 2020-01-07 21:04:05 | [diff] [blame] | 48 | enum class UnloadedExtensionReason; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 49 | |
| 50 | // ExtensionSystem manages the lifetime of many of the services used by the |
| 51 | // extensions and apps system, and it handles startup and shutdown as needed. |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 52 | // Eventually, we'd like to make more of these services into KeyedServices in |
| 53 | // their own right. |
| 54 | class ExtensionSystem : public KeyedService { |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 55 | public: |
Minh X. Nguyen | 2c17b398 | 2017-10-13 18:32:29 | [diff] [blame] | 56 | // A callback to be executed when InstallUpdate finishes. |
Minh X. Nguyen | 8803f4e | 2018-05-25 01:03:10 | [diff] [blame] | 57 | using InstallUpdateCallback = |
Anton Bikineev | 6d67847 | 2021-05-15 18:48:51 | [diff] [blame] | 58 | base::OnceCallback<void(const absl::optional<CrxInstallError>& result)>; |
Minh X. Nguyen | 2c17b398 | 2017-10-13 18:32:29 | [diff] [blame] | 59 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 60 | ExtensionSystem(); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 61 | ~ExtensionSystem() override; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 62 | |
| 63 | // Returns the instance for the given browser context, or NULL if none. |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 64 | static ExtensionSystem* Get(content::BrowserContext* context); |
| 65 | |
| 66 | // Initializes extensions machinery. |
| 67 | // Component extensions are always enabled, external and user extensions are |
Randy Smith | 64399d6 | 2017-12-20 04:33:40 | [diff] [blame] | 68 | // controlled (for both incognito and non-incognito profiles) by the |
| 69 | // |extensions_enabled| flag passed to non-incognito initialization. |
| 70 | // These calls should occur after the profile IO data is initialized, |
| 71 | // as extensions initialization depends on that. |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 72 | virtual void InitForRegularProfile(bool extensions_enabled) = 0; |
| 73 | |
Michael Giuffrida | 023e353 | 2017-09-28 23:21:21 | [diff] [blame] | 74 | // The ExtensionService is created at startup. ExtensionService is only |
| 75 | // defined in Chrome. |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 76 | virtual ExtensionService* extension_service() = 0; |
| 77 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 78 | // The class controlling whether users are permitted to perform certain |
| 79 | // actions on extensions (install, uninstall, disable, etc.). |
| 80 | // The ManagementPolicy is created at startup. |
| 81 | virtual ManagementPolicy* management_policy() = 0; |
| 82 | |
rdevlin.cronin | f5863da | 2015-09-10 19:21:45 | [diff] [blame] | 83 | // The ServiceWorkerManager is created at startup. |
| 84 | virtual ServiceWorkerManager* service_worker_manager() = 0; |
| 85 | |
Kelvin Jiang | a115a75 | 2021-02-11 03:04:01 | [diff] [blame] | 86 | // The UserScriptManager is created at startup. |
| 87 | virtual UserScriptManager* user_script_manager() = 0; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 88 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 89 | // The StateStore is created at startup. |
| 90 | virtual StateStore* state_store() = 0; |
| 91 | |
| 92 | // The rules store is created at startup. |
| 93 | virtual StateStore* rules_store() = 0; |
| 94 | |
Kelvin Jiang | 3127736 | 2021-09-24 00:41:33 | [diff] [blame] | 95 | // The dynamic user scripts store is created at startup. |
| 96 | virtual StateStore* dynamic_user_scripts_store() = 0; |
| 97 | |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 98 | // Returns the |ValueStore| factory created at startup. |
Louise Brett | 88346ae1 | 2021-09-09 04:47:32 | [diff] [blame] | 99 | virtual scoped_refptr<value_store::ValueStoreFactory> store_factory() = 0; |
cmumford | 6ae8d46 | 2016-03-24 20:35:27 | [diff] [blame] | 100 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 101 | // Returns the IO-thread-accessible extension data. |
| 102 | virtual InfoMap* info_map() = 0; |
| 103 | |
[email protected] | aab2310 | 2014-02-05 18:57:55 | [diff] [blame] | 104 | // Returns the QuotaService that limits calls to certain extension functions. |
| 105 | // Lives on the UI thread. Created at startup. |
| 106 | virtual QuotaService* quota_service() = 0; |
| 107 | |
treib | 926ee2d | 2015-08-06 10:55:42 | [diff] [blame] | 108 | // Returns the AppSorting which provides an ordering for all installed apps. |
| 109 | virtual AppSorting* app_sorting() = 0; |
| 110 | |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 111 | // Called by the ExtensionService that lives in this system. Gives the |
| 112 | // info map a chance to react to the load event before the EXTENSION_LOADED |
| 113 | // notification has fired. The purpose for handling this event first is to |
| 114 | // avoid race conditions by making sure URLRequestContexts learn about new |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 115 | // extensions before anything else needs them to know. This operation happens |
| 116 | // asynchronously. |callback| is run on the calling thread once completed. |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 117 | virtual void RegisterExtensionWithRequestContexts( |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 118 | const Extension* extension, |
danakj | f6d89bf | 2020-01-07 01:21:29 | [diff] [blame] | 119 | base::OnceClosure callback) {} |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 120 | |
| 121 | // Called by the ExtensionService that lives in this system. Lets the |
| 122 | // info map clean up its RequestContexts once all the listeners to the |
| 123 | // EXTENSION_UNLOADED notification have finished running. |
| 124 | virtual void UnregisterExtensionWithRequestContexts( |
Tal Keren | 9ba46e15 | 2022-01-25 18:13:59 | [diff] [blame] | 125 | const std::string& extension_id) {} |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 126 | |
| 127 | // Signaled when the extension system has completed its startup tasks. |
Christopher Lam | ebb9020 | 2019-04-04 03:42:36 | [diff] [blame] | 128 | virtual const base::OneShotEvent& ready() const = 0; |
[email protected] | fd3df778 | 2014-05-08 23:54:27 | [diff] [blame] | 129 | |
Ghazale Hosseinabadi | 1d810e9 | 2020-06-01 20:43:02 | [diff] [blame] | 130 | // Whether the extension system is ready. |
| 131 | virtual bool is_ready() const = 0; |
| 132 | |
[email protected] | fd3df778 | 2014-05-08 23:54:27 | [diff] [blame] | 133 | // Returns the content verifier, if any. |
| 134 | virtual ContentVerifier* content_verifier() = 0; |
[email protected] | f698c16 | 2014-06-13 00:46:26 | [diff] [blame] | 135 | |
| 136 | // Get a set of extensions that depend on the given extension. |
| 137 | // TODO(elijahtaylor): Move SharedModuleService out of chrome/browser |
| 138 | // so it can be retrieved from ExtensionSystem directly. |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 139 | virtual std::unique_ptr<ExtensionSet> GetDependentExtensions( |
[email protected] | f698c16 | 2014-06-13 00:46:26 | [diff] [blame] | 140 | const Extension* extension) = 0; |
asargent | 631a99a | 2015-10-15 21:51:48 | [diff] [blame] | 141 | |
| 142 | // Install an updated version of |extension_id| with the version given in |
Minh X. Nguyen | 039eb99 | 2018-06-26 20:12:13 | [diff] [blame] | 143 | // |unpacked_dir|. If |install_immediately| is true, the system will install |
| 144 | // the given extension immediately instead of waiting until idle. Ownership |
| 145 | // of |unpacked_dir| in the filesystem is transferred and implementors of |
| 146 | // this function are responsible for cleaning it up on errors, etc. |
asargent | 631a99a | 2015-10-15 21:51:48 | [diff] [blame] | 147 | virtual void InstallUpdate(const std::string& extension_id, |
Minh X. Nguyen | 3097534 | 2017-12-04 22:02:41 | [diff] [blame] | 148 | const std::string& public_key, |
| 149 | const base::FilePath& unpacked_dir, |
Minh X. Nguyen | 039eb99 | 2018-06-26 20:12:13 | [diff] [blame] | 150 | bool install_immediately, |
Minh X. Nguyen | 3097534 | 2017-12-04 22:02:41 | [diff] [blame] | 151 | InstallUpdateCallback install_update_callback) = 0; |
Minh X. Nguyen | b4fbf92 | 2018-01-29 18:28:10 | [diff] [blame] | 152 | |
Bettina | 45e1a94 | 2020-04-10 22:29:47 | [diff] [blame] | 153 | // Perform various actions depending on the Omaga attributes on the extension. |
| 154 | virtual void PerformActionBasedOnOmahaAttributes( |
| 155 | const std::string& extension_id, |
| 156 | const base::Value& attributes) = 0; |
| 157 | |
Minh X. Nguyen | b4fbf92 | 2018-01-29 18:28:10 | [diff] [blame] | 158 | // Attempts finishing installation of an update for an extension with the |
| 159 | // specified id, when installation of that extension was previously delayed. |
| 160 | // |install_immediately| - Install the extension should be installed if it is |
| 161 | // currently in use. |
| 162 | // Returns whether the extension installation was finished. |
| 163 | virtual bool FinishDelayedInstallationIfReady(const std::string& extension_id, |
| 164 | bool install_immediately) = 0; |
[email protected] | 59b0e60 | 2014-01-30 00:41:24 | [diff] [blame] | 165 | }; |
| 166 | |
| 167 | } // namespace extensions |
| 168 | |
| 169 | #endif // EXTENSIONS_BROWSER_EXTENSION_SYSTEM_H_ |