blob: 2d2514bc0a9a3e434bb50274e8c28f2282c8bcf4 [file] [log] [blame]
[email protected]bb1bc9b32013-12-21 03:09:141// 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]5fdfa562013-12-27 17:43:5910#include "base/compiler_specific.h"
avic9cec102015-12-23 00:39:2611#include "base/macros.h"
[email protected]bb1bc9b32013-12-21 03:09:1412#include "base/memory/ref_counted.h"
[email protected]45f5b7d2014-01-22 23:47:1313#include "base/observer_list.h"
lazyboy640dc512017-05-19 21:37:0214#include "base/version.h"
[email protected]b33f0b112014-03-13 17:05:3015#include "components/keyed_service/core/keyed_service.h"
[email protected]e43c61f2014-07-20 21:46:3416#include "extensions/browser/uninstall_reason.h"
Scott Violetc8240b02018-03-08 22:03:5917#include "extensions/buildflags/buildflags.h"
Michael Giuffrida19a55982018-02-10 03:03:2618#include "extensions/common/extension_id.h"
[email protected]bb1bc9b32013-12-21 03:09:1419#include "extensions/common/extension_set.h"
20
brettw00899e62016-11-12 02:10:1721#if !BUILDFLAG(ENABLE_EXTENSIONS)
thestig44b9ce22014-10-17 03:19:2822#error "Extensions must be enabled"
23#endif
24
[email protected]5fdfa562013-12-27 17:43:5925namespace content {
26class BrowserContext;
27}
28
[email protected]bb1bc9b32013-12-21 03:09:1429namespace extensions {
Michael Giuffrida19a55982018-02-10 03:03:2630
[email protected]bb1bc9b32013-12-21 03:09:1431class Extension;
[email protected]45f5b7d2014-01-22 23:47:1332class ExtensionRegistryObserver;
Michael Giuffrida19a55982018-02-10 03:03:2633enum class UnloadedExtensionReason;
[email protected]bb1bc9b32013-12-21 03:09:1434
35// ExtensionRegistry holds sets of the installed extensions for a given
[email protected]5fdfa562013-12-27 17:43:5936// BrowserContext. An incognito browser context and its master browser context
37// share a single registry.
[email protected]b33f0b112014-03-13 17:05:3038class ExtensionRegistry : public KeyedService {
[email protected]bb1bc9b32013-12-21 03:09:1439 public:
[email protected]599539802014-01-07 23:06:0040 // Flags to pass to GetExtensionById() to select which sets to look in.
41 enum IncludeFlag {
mlerman6a37b6a42014-11-26 22:10:5342 NONE = 0,
43 ENABLED = 1 << 0,
44 DISABLED = 1 << 1,
45 TERMINATED = 1 << 2,
[email protected]599539802014-01-07 23:06:0046 BLACKLISTED = 1 << 3,
mlerman6a37b6a42014-11-26 22:10:5347 BLOCKED = 1 << 4,
48 EVERYTHING = (1 << 5) - 1,
[email protected]599539802014-01-07 23:06:0049 };
50
[email protected]50703fc2014-04-08 04:01:0651 explicit ExtensionRegistry(content::BrowserContext* browser_context);
dcheng9168b2f2014-10-21 12:38:2452 ~ExtensionRegistry() override;
[email protected]5fdfa562013-12-27 17:43:5953
54 // Returns the instance for the given |browser_context|.
55 static ExtensionRegistry* Get(content::BrowserContext* browser_context);
[email protected]bb1bc9b32013-12-21 03:09:1456
[email protected]6b54fda2014-07-22 02:13:4757 content::BrowserContext* browser_context() const { return browser_context_; }
58
skuhne3498b1d2014-10-09 23:00:0159 // NOTE: These sets are *eventually* mutually exclusive, but an extension can
[email protected]bb1bc9b32013-12-21 03:09:1460 // 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 }
mlerman6a37b6a42014-11-26 22:10:5373 const ExtensionSet& blocked_extensions() const { return blocked_extensions_; }
rockot494f0072015-07-29 17:58:0774 const ExtensionSet& ready_extensions() const { return ready_extensions_; }
[email protected]bb1bc9b32013-12-21 03:09:1475
mlerman6a37b6a42014-11-26 22:10:5376 // Returns the set of all installed extensions, regardless of state (enabled,
77 // disabled, etc). Equivalent to GenerateInstalledExtensionSet(EVERYTHING).
dchengf5d241082016-04-21 03:43:1178 std::unique_ptr<ExtensionSet> GenerateInstalledExtensionsSet() const;
[email protected]f47f7172014-03-19 19:27:1079
mlerman6a37b6a42014-11-26 22:10:5380 // 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
dchengf5d241082016-04-21 03:43:1186 std::unique_ptr<ExtensionSet> GenerateInstalledExtensionsSet(
mlerman6a37b6a42014-11-26 22:10:5387 int include_mask) const;
88
lazyboy640dc512017-05-19 21:37:0289 // 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]45f5b7d2014-01-22 23:47:1399 // The usual observer interface.
100 void AddObserver(ExtensionRegistryObserver* observer);
101 void RemoveObserver(ExtensionRegistryObserver* observer);
102
[email protected]dcc47642014-03-26 22:03:49103 // Invokes the observer method OnExtensionLoaded(). The extension must be
104 // enabled at the time of the call.
105 void TriggerOnLoaded(const Extension* extension);
106
rockot494f0072015-07-29 17:58:07107 // 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]45f5b7d2014-01-22 23:47:13112 // Invokes the observer method OnExtensionUnloaded(). The extension must not
113 // be enabled at the time of the call.
[email protected]e51232f32014-04-18 20:05:36114 void TriggerOnUnloaded(const Extension* extension,
limasdf0deef2042017-05-03 19:17:17115 UnloadedExtensionReason reason);
[email protected]45f5b7d2014-01-22 23:47:13116
[email protected]17f07822014-05-22 08:45:15117 // 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]bc44b5da2014-06-12 14:20:00125 // Invokes the observer method OnExtensionInstalled(). The extension must be
126 // contained in one of the registry's extension sets.
[email protected]38e872532014-07-16 23:27:51127 void TriggerOnInstalled(const Extension* extension,
128 bool is_update);
[email protected]bc44b5da2014-06-12 14:20:00129
[email protected]1d7f2482014-05-24 00:11:51130 // Invokes the observer method OnExtensionUninstalled(). The extension must
131 // not be any installed extension with |extension|'s ID.
[email protected]e43c61f2014-07-20 21:46:34132 void TriggerOnUninstalled(const Extension* extension, UninstallReason reason);
[email protected]1d7f2482014-05-24 00:11:51133
[email protected]599539802014-01-07 23:06:00134 // 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
mlerman6a37b6a42014-11-26 22:10:53139 // * blocked_extensions() --> ExtensionRegistry::BLOCKED
[email protected]599539802014-01-07 23:06:00140 // 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.naginenidb931782015-06-05 12:01:50144 // 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]bb1bc9b32013-12-21 03:09:14148 // 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
mlerman6a37b6a42014-11-26 22:10:53173 // As above, but for the blocked set.
174 bool AddBlocked(const scoped_refptr<const Extension>& extension);
175 bool RemoveBlocked(const std::string& id);
176
rockot494f0072015-07-29 17:58:07177 // 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]bb1bc9b32013-12-21 03:09:14181 // Removes all extensions from all sets.
182 void ClearAll();
183
[email protected]b33f0b112014-03-13 17:05:30184 // KeyedService implementation:
dcheng9168b2f2014-10-21 12:38:24185 void Shutdown() override;
[email protected]5fdfa562013-12-27 17:43:59186
[email protected]bb1bc9b32013-12-21 03:09:14187 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
mlerman6a37b6a42014-11-26 22:10:53203 // Extensions that are installed and blocked. Will never be loaded.
204 ExtensionSet blocked_extensions_;
205
rockot494f0072015-07-29 17:58:07206 // Extensions that are ready for execution. This set is a non-exclusive
207 // subset of |enabled_extensions_|.
208 ExtensionSet ready_extensions_;
209
Trent Apteda250ec3ab2018-08-19 08:52:19210 base::ObserverList<ExtensionRegistryObserver>::Unchecked observers_;
[email protected]45f5b7d2014-01-22 23:47:13211
[email protected]50703fc2014-04-08 04:01:06212 content::BrowserContext* const browser_context_;
213
[email protected]bb1bc9b32013-12-21 03:09:14214 DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
215};
216
217} // namespace extensions
218
219#endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_