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.
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/observer_list.h"
13 #include "components/keyed_service/core/keyed_service.h"
14 #include "extensions/browser/uninstall_reason.h"
15 #include "extensions/common/extension_set.h"
17 #if !defined(ENABLE_EXTENSIONS)
18 #error "Extensions must be enabled"
25 namespace extensions
{
27 class ExtensionRegistryObserver
;
29 // ExtensionRegistry holds sets of the installed extensions for a given
30 // BrowserContext. An incognito browser context and its master browser context
31 // share a single registry.
32 class ExtensionRegistry
: public KeyedService
{
34 // Flags to pass to GetExtensionById() to select which sets to look in.
42 EVERYTHING
= (1 << 5) - 1,
45 explicit ExtensionRegistry(content::BrowserContext
* browser_context
);
46 ~ExtensionRegistry() override
;
48 // Returns the instance for the given |browser_context|.
49 static ExtensionRegistry
* Get(content::BrowserContext
* browser_context
);
51 content::BrowserContext
* browser_context() const { return browser_context_
; }
53 // NOTE: These sets are *eventually* mutually exclusive, but an extension can
54 // appear in two sets for short periods of time.
55 const ExtensionSet
& enabled_extensions() const {
56 return enabled_extensions_
;
58 const ExtensionSet
& disabled_extensions() const {
59 return disabled_extensions_
;
61 const ExtensionSet
& terminated_extensions() const {
62 return terminated_extensions_
;
64 const ExtensionSet
& blacklisted_extensions() const {
65 return blacklisted_extensions_
;
67 const ExtensionSet
& blocked_extensions() const { return blocked_extensions_
; }
68 const ExtensionSet
& ready_extensions() const { return ready_extensions_
; }
70 // Returns the set of all installed extensions, regardless of state (enabled,
71 // disabled, etc). Equivalent to GenerateInstalledExtensionSet(EVERYTHING).
72 scoped_ptr
<ExtensionSet
> GenerateInstalledExtensionsSet() const;
74 // Returns a set of all extensions in the subsets specified by |include_mask|.
75 // * enabled_extensions() --> ExtensionRegistry::ENABLED
76 // * disabled_extensions() --> ExtensionRegistry::DISABLED
77 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
78 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
79 // * blocked_extensions() --> ExtensionRegistry::BLOCKED
80 scoped_ptr
<ExtensionSet
> GenerateInstalledExtensionsSet(
81 int include_mask
) const;
83 // The usual observer interface.
84 void AddObserver(ExtensionRegistryObserver
* observer
);
85 void RemoveObserver(ExtensionRegistryObserver
* observer
);
87 // Invokes the observer method OnExtensionLoaded(). The extension must be
88 // enabled at the time of the call.
89 void TriggerOnLoaded(const Extension
* extension
);
91 // Invokes the observer method OnExtensionReady(). This always follows
92 // an OnLoaded event, but is not called until it's safe to create the
93 // extension's child process.
94 void TriggerOnReady(const Extension
* extension
);
96 // Invokes the observer method OnExtensionUnloaded(). The extension must not
97 // be enabled at the time of the call.
98 void TriggerOnUnloaded(const Extension
* extension
,
99 UnloadedExtensionInfo::Reason reason
);
101 // If this is a fresh install then |is_update| is false and there must not be
102 // any installed extension with |extension|'s ID. If this is an update then
103 // |is_update| is true and must be an installed extension with |extension|'s
104 // ID, and |old_name| must be non-empty.
105 // If true, |from_ephemeral| indicates that the extension was previously
106 // installed ephemerally and has been promoted to a regular installed
107 // extension. |is_update| should also be true.
108 void TriggerOnWillBeInstalled(const Extension
* extension
,
111 const std::string
& old_name
);
113 // Invokes the observer method OnExtensionInstalled(). The extension must be
114 // contained in one of the registry's extension sets.
115 void TriggerOnInstalled(const Extension
* extension
,
118 // Invokes the observer method OnExtensionUninstalled(). The extension must
119 // not be any installed extension with |extension|'s ID.
120 void TriggerOnUninstalled(const Extension
* extension
, UninstallReason reason
);
122 // Find an extension by ID using |include_mask| to pick the sets to search:
123 // * enabled_extensions() --> ExtensionRegistry::ENABLED
124 // * disabled_extensions() --> ExtensionRegistry::DISABLED
125 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
126 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
127 // * blocked_extensions() --> ExtensionRegistry::BLOCKED
128 // Returns NULL if the extension is not found in the selected sets.
129 const Extension
* GetExtensionById(const std::string
& id
,
130 int include_mask
) const;
132 // Looks up an extension by ID, regardless of whether it's enabled,
133 // disabled, blacklisted, or terminated.
134 const Extension
* GetInstalledExtension(const std::string
& id
) const;
136 // Adds the specified extension to the enabled set. The registry becomes an
137 // owner. Any previous extension with the same ID is removed.
138 // Returns true if there is no previous extension.
139 // NOTE: You probably want to use ExtensionService instead of calling this
141 bool AddEnabled(const scoped_refptr
<const Extension
>& extension
);
143 // Removes the specified extension from the enabled set.
144 // Returns true if the set contained the specified extension.
145 // NOTE: You probably want to use ExtensionService instead of calling this
147 bool RemoveEnabled(const std::string
& id
);
149 // As above, but for the disabled set.
150 bool AddDisabled(const scoped_refptr
<const Extension
>& extension
);
151 bool RemoveDisabled(const std::string
& id
);
153 // As above, but for the terminated set.
154 bool AddTerminated(const scoped_refptr
<const Extension
>& extension
);
155 bool RemoveTerminated(const std::string
& id
);
157 // As above, but for the blacklisted set.
158 bool AddBlacklisted(const scoped_refptr
<const Extension
>& extension
);
159 bool RemoveBlacklisted(const std::string
& id
);
161 // As above, but for the blocked set.
162 bool AddBlocked(const scoped_refptr
<const Extension
>& extension
);
163 bool RemoveBlocked(const std::string
& id
);
165 // As above, but for the ready set.
166 bool AddReady(const scoped_refptr
<const Extension
>& extension
);
167 bool RemoveReady(const std::string
& id
);
169 // Removes all extensions from all sets.
172 // Sets a callback to run when the disabled extension set is modified.
173 // TODO(jamescook): This is too specific for a generic registry; find some
174 // other way to do this.
175 void SetDisabledModificationCallback(
176 const ExtensionSet::ModificationCallback
& callback
);
178 // KeyedService implementation:
179 void Shutdown() override
;
182 // Extensions that are installed, enabled and not terminated.
183 ExtensionSet enabled_extensions_
;
185 // Extensions that are installed and disabled.
186 ExtensionSet disabled_extensions_
;
188 // Extensions that are installed and terminated.
189 ExtensionSet terminated_extensions_
;
191 // Extensions that are installed and blacklisted. Generally these shouldn't be
192 // considered as installed by the extension platform: we only keep them around
193 // so that if extensions are blacklisted by mistake they can easily be
195 ExtensionSet blacklisted_extensions_
;
197 // Extensions that are installed and blocked. Will never be loaded.
198 ExtensionSet blocked_extensions_
;
200 // Extensions that are ready for execution. This set is a non-exclusive
201 // subset of |enabled_extensions_|.
202 ExtensionSet ready_extensions_
;
204 base::ObserverList
<ExtensionRegistryObserver
> observers_
;
206 content::BrowserContext
* const browser_context_
;
208 DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry
);
211 } // namespace extensions
213 #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_