[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 1 | // Copyright 2013 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 EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_ |
| 6 | #define EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 12 | #include "base/memory/ref_counted.h" |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 13 | #include "base/observer_list.h" |
lazyboy | 640dc51 | 2017-05-19 21:37:02 | [diff] [blame] | 14 | #include "base/version.h" |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 15 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 16 | #include "extensions/browser/uninstall_reason.h" |
Scott Violet | c8240b0 | 2018-03-08 22:03:59 | [diff] [blame] | 17 | #include "extensions/buildflags/buildflags.h" |
Michael Giuffrida | 19a5598 | 2018-02-10 03:03:26 | [diff] [blame] | 18 | #include "extensions/common/extension_id.h" |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 19 | #include "extensions/common/extension_set.h" |
| 20 | |
brettw | 00899e6 | 2016-11-12 02:10:17 | [diff] [blame] | 21 | #if !BUILDFLAG(ENABLE_EXTENSIONS) |
thestig | 44b9ce2 | 2014-10-17 03:19:28 | [diff] [blame] | 22 | #error "Extensions must be enabled" |
| 23 | #endif |
| 24 | |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 25 | namespace content { |
| 26 | class BrowserContext; |
| 27 | } |
| 28 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 29 | namespace extensions { |
Michael Giuffrida | 19a5598 | 2018-02-10 03:03:26 | [diff] [blame] | 30 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 31 | class Extension; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 32 | class ExtensionRegistryObserver; |
Michael Giuffrida | 19a5598 | 2018-02-10 03:03:26 | [diff] [blame] | 33 | enum class UnloadedExtensionReason; |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 34 | |
| 35 | // ExtensionRegistry holds sets of the installed extensions for a given |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 36 | // BrowserContext. An incognito browser context and its master browser context |
| 37 | // share a single registry. |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 38 | class ExtensionRegistry : public KeyedService { |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 39 | public: |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 40 | // Flags to pass to GetExtensionById() to select which sets to look in. |
| 41 | enum IncludeFlag { |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 42 | NONE = 0, |
| 43 | ENABLED = 1 << 0, |
| 44 | DISABLED = 1 << 1, |
| 45 | TERMINATED = 1 << 2, |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 46 | BLACKLISTED = 1 << 3, |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 47 | BLOCKED = 1 << 4, |
| 48 | EVERYTHING = (1 << 5) - 1, |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 49 | }; |
| 50 | |
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 51 | explicit ExtensionRegistry(content::BrowserContext* browser_context); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 52 | ~ExtensionRegistry() override; |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 53 | |
| 54 | // Returns the instance for the given |browser_context|. |
| 55 | static ExtensionRegistry* Get(content::BrowserContext* browser_context); |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 56 | |
[email protected] | 6b54fda | 2014-07-22 02:13:47 | [diff] [blame] | 57 | content::BrowserContext* browser_context() const { return browser_context_; } |
| 58 | |
skuhne | 3498b1d | 2014-10-09 23:00:01 | [diff] [blame] | 59 | // NOTE: These sets are *eventually* mutually exclusive, but an extension can |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 60 | // appear in two sets for short periods of time. |
| 61 | const ExtensionSet& enabled_extensions() const { |
| 62 | return enabled_extensions_; |
| 63 | } |
| 64 | const ExtensionSet& disabled_extensions() const { |
| 65 | return disabled_extensions_; |
| 66 | } |
| 67 | const ExtensionSet& terminated_extensions() const { |
| 68 | return terminated_extensions_; |
| 69 | } |
| 70 | const ExtensionSet& blacklisted_extensions() const { |
| 71 | return blacklisted_extensions_; |
| 72 | } |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 73 | const ExtensionSet& blocked_extensions() const { return blocked_extensions_; } |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 74 | const ExtensionSet& ready_extensions() const { return ready_extensions_; } |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 75 | |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 76 | // Returns the set of all installed extensions, regardless of state (enabled, |
| 77 | // disabled, etc). Equivalent to GenerateInstalledExtensionSet(EVERYTHING). |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 78 | std::unique_ptr<ExtensionSet> GenerateInstalledExtensionsSet() const; |
[email protected] | f47f717 | 2014-03-19 19:27:10 | [diff] [blame] | 79 | |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 80 | // Returns a set of all extensions in the subsets specified by |include_mask|. |
| 81 | // * enabled_extensions() --> ExtensionRegistry::ENABLED |
| 82 | // * disabled_extensions() --> ExtensionRegistry::DISABLED |
| 83 | // * terminated_extensions() --> ExtensionRegistry::TERMINATED |
| 84 | // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED |
| 85 | // * blocked_extensions() --> ExtensionRegistry::BLOCKED |
dcheng | f5d24108 | 2016-04-21 03:43:11 | [diff] [blame] | 86 | std::unique_ptr<ExtensionSet> GenerateInstalledExtensionsSet( |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 87 | int include_mask) const; |
| 88 | |
lazyboy | 640dc51 | 2017-05-19 21:37:02 | [diff] [blame] | 89 | // Returns the current version of the extension with the given |id|, if |
| 90 | // one exists. |
| 91 | // Note: If we are currently updating the extension, this returns the |
| 92 | // version stored currently, rather than the in-progress update. |
| 93 | // |
| 94 | // TODO(lazyboy): Consider updating callers to directly retrieve version() |
| 95 | // from either GetExtensionById() or querying ExtensionSet getters of this |
| 96 | // class. |
| 97 | base::Version GetStoredVersion(const ExtensionId& id) const; |
| 98 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 99 | // The usual observer interface. |
| 100 | void AddObserver(ExtensionRegistryObserver* observer); |
| 101 | void RemoveObserver(ExtensionRegistryObserver* observer); |
| 102 | |
[email protected] | dcc4764 | 2014-03-26 22:03:49 | [diff] [blame] | 103 | // Invokes the observer method OnExtensionLoaded(). The extension must be |
| 104 | // enabled at the time of the call. |
| 105 | void TriggerOnLoaded(const Extension* extension); |
| 106 | |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 107 | // Invokes the observer method OnExtensionReady(). This always follows |
| 108 | // an OnLoaded event, but is not called until it's safe to create the |
| 109 | // extension's child process. |
| 110 | void TriggerOnReady(const Extension* extension); |
| 111 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 112 | // Invokes the observer method OnExtensionUnloaded(). The extension must not |
| 113 | // be enabled at the time of the call. |
[email protected] | e51232f3 | 2014-04-18 20:05:36 | [diff] [blame] | 114 | void TriggerOnUnloaded(const Extension* extension, |
limasdf | 0deef204 | 2017-05-03 19:17:17 | [diff] [blame] | 115 | UnloadedExtensionReason reason); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 116 | |
[email protected] | 17f0782 | 2014-05-22 08:45:15 | [diff] [blame] | 117 | // If this is a fresh install then |is_update| is false and there must not be |
| 118 | // any installed extension with |extension|'s ID. If this is an update then |
| 119 | // |is_update| is true and must be an installed extension with |extension|'s |
| 120 | // ID, and |old_name| must be non-empty. |
| 121 | void TriggerOnWillBeInstalled(const Extension* extension, |
| 122 | bool is_update, |
| 123 | const std::string& old_name); |
| 124 | |
[email protected] | bc44b5da | 2014-06-12 14:20:00 | [diff] [blame] | 125 | // Invokes the observer method OnExtensionInstalled(). The extension must be |
| 126 | // contained in one of the registry's extension sets. |
[email protected] | 38e87253 | 2014-07-16 23:27:51 | [diff] [blame] | 127 | void TriggerOnInstalled(const Extension* extension, |
| 128 | bool is_update); |
[email protected] | bc44b5da | 2014-06-12 14:20:00 | [diff] [blame] | 129 | |
[email protected] | 1d7f248 | 2014-05-24 00:11:51 | [diff] [blame] | 130 | // Invokes the observer method OnExtensionUninstalled(). The extension must |
| 131 | // not be any installed extension with |extension|'s ID. |
[email protected] | e43c61f | 2014-07-20 21:46:34 | [diff] [blame] | 132 | void TriggerOnUninstalled(const Extension* extension, UninstallReason reason); |
[email protected] | 1d7f248 | 2014-05-24 00:11:51 | [diff] [blame] | 133 | |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 134 | // Find an extension by ID using |include_mask| to pick the sets to search: |
| 135 | // * enabled_extensions() --> ExtensionRegistry::ENABLED |
| 136 | // * disabled_extensions() --> ExtensionRegistry::DISABLED |
| 137 | // * terminated_extensions() --> ExtensionRegistry::TERMINATED |
| 138 | // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 139 | // * blocked_extensions() --> ExtensionRegistry::BLOCKED |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 140 | // Returns NULL if the extension is not found in the selected sets. |
| 141 | const Extension* GetExtensionById(const std::string& id, |
| 142 | int include_mask) const; |
| 143 | |
sudarsana.nagineni | db93178 | 2015-06-05 12:01:50 | [diff] [blame] | 144 | // Looks up an extension by ID, regardless of whether it's enabled, |
| 145 | // disabled, blacklisted, or terminated. |
| 146 | const Extension* GetInstalledExtension(const std::string& id) const; |
| 147 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 148 | // Adds the specified extension to the enabled set. The registry becomes an |
| 149 | // owner. Any previous extension with the same ID is removed. |
| 150 | // Returns true if there is no previous extension. |
| 151 | // NOTE: You probably want to use ExtensionService instead of calling this |
| 152 | // method directly. |
| 153 | bool AddEnabled(const scoped_refptr<const Extension>& extension); |
| 154 | |
| 155 | // Removes the specified extension from the enabled set. |
| 156 | // Returns true if the set contained the specified extension. |
| 157 | // NOTE: You probably want to use ExtensionService instead of calling this |
| 158 | // method directly. |
| 159 | bool RemoveEnabled(const std::string& id); |
| 160 | |
| 161 | // As above, but for the disabled set. |
| 162 | bool AddDisabled(const scoped_refptr<const Extension>& extension); |
| 163 | bool RemoveDisabled(const std::string& id); |
| 164 | |
| 165 | // As above, but for the terminated set. |
| 166 | bool AddTerminated(const scoped_refptr<const Extension>& extension); |
| 167 | bool RemoveTerminated(const std::string& id); |
| 168 | |
| 169 | // As above, but for the blacklisted set. |
| 170 | bool AddBlacklisted(const scoped_refptr<const Extension>& extension); |
| 171 | bool RemoveBlacklisted(const std::string& id); |
| 172 | |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 173 | // As above, but for the blocked set. |
| 174 | bool AddBlocked(const scoped_refptr<const Extension>& extension); |
| 175 | bool RemoveBlocked(const std::string& id); |
| 176 | |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 177 | // As above, but for the ready set. |
| 178 | bool AddReady(const scoped_refptr<const Extension>& extension); |
| 179 | bool RemoveReady(const std::string& id); |
| 180 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 181 | // Removes all extensions from all sets. |
| 182 | void ClearAll(); |
| 183 | |
[email protected] | b33f0b11 | 2014-03-13 17:05:30 | [diff] [blame] | 184 | // KeyedService implementation: |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 185 | void Shutdown() override; |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 186 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 187 | private: |
| 188 | // Extensions that are installed, enabled and not terminated. |
| 189 | ExtensionSet enabled_extensions_; |
| 190 | |
| 191 | // Extensions that are installed and disabled. |
| 192 | ExtensionSet disabled_extensions_; |
| 193 | |
| 194 | // Extensions that are installed and terminated. |
| 195 | ExtensionSet terminated_extensions_; |
| 196 | |
| 197 | // Extensions that are installed and blacklisted. Generally these shouldn't be |
| 198 | // considered as installed by the extension platform: we only keep them around |
| 199 | // so that if extensions are blacklisted by mistake they can easily be |
| 200 | // un-blacklisted. |
| 201 | ExtensionSet blacklisted_extensions_; |
| 202 | |
mlerman | 6a37b6a4 | 2014-11-26 22:10:53 | [diff] [blame] | 203 | // Extensions that are installed and blocked. Will never be loaded. |
| 204 | ExtensionSet blocked_extensions_; |
| 205 | |
rockot | 494f007 | 2015-07-29 17:58:07 | [diff] [blame] | 206 | // Extensions that are ready for execution. This set is a non-exclusive |
| 207 | // subset of |enabled_extensions_|. |
| 208 | ExtensionSet ready_extensions_; |
| 209 | |
Trent Apted | a250ec3ab | 2018-08-19 08:52:19 | [diff] [blame] | 210 | base::ObserverList<ExtensionRegistryObserver>::Unchecked observers_; |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 211 | |
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 212 | content::BrowserContext* const browser_context_; |
| 213 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 214 | DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry); |
| 215 | }; |
| 216 | |
| 217 | } // namespace extensions |
| 218 | |
| 219 | #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_ |