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_
; }
69 // Returns the set of all installed extensions, regardless of state (enabled,
70 // disabled, etc). Equivalent to GenerateInstalledExtensionSet(EVERYTHING).
71 scoped_ptr
<ExtensionSet
> GenerateInstalledExtensionsSet() const;
73 // Returns a set of all extensions in the subsets specified by |include_mask|.
74 // * enabled_extensions() --> ExtensionRegistry::ENABLED
75 // * disabled_extensions() --> ExtensionRegistry::DISABLED
76 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
77 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
78 // * blocked_extensions() --> ExtensionRegistry::BLOCKED
79 scoped_ptr
<ExtensionSet
> GenerateInstalledExtensionsSet(
80 int include_mask
) const;
82 // The usual observer interface.
83 void AddObserver(ExtensionRegistryObserver
* observer
);
84 void RemoveObserver(ExtensionRegistryObserver
* observer
);
86 // Invokes the observer method OnExtensionLoaded(). The extension must be
87 // enabled at the time of the call.
88 void TriggerOnLoaded(const Extension
* extension
);
90 // Invokes the observer method OnExtensionUnloaded(). The extension must not
91 // be enabled at the time of the call.
92 void TriggerOnUnloaded(const Extension
* extension
,
93 UnloadedExtensionInfo::Reason reason
);
95 // If this is a fresh install then |is_update| is false and there must not be
96 // any installed extension with |extension|'s ID. If this is an update then
97 // |is_update| is true and must be an installed extension with |extension|'s
98 // ID, and |old_name| must be non-empty.
99 // If true, |from_ephemeral| indicates that the extension was previously
100 // installed ephemerally and has been promoted to a regular installed
101 // extension. |is_update| should also be true.
102 void TriggerOnWillBeInstalled(const Extension
* extension
,
105 const std::string
& old_name
);
107 // Invokes the observer method OnExtensionInstalled(). The extension must be
108 // contained in one of the registry's extension sets.
109 void TriggerOnInstalled(const Extension
* extension
,
112 // Invokes the observer method OnExtensionUninstalled(). The extension must
113 // not be any installed extension with |extension|'s ID.
114 void TriggerOnUninstalled(const Extension
* extension
, UninstallReason reason
);
116 // Find an extension by ID using |include_mask| to pick the sets to search:
117 // * enabled_extensions() --> ExtensionRegistry::ENABLED
118 // * disabled_extensions() --> ExtensionRegistry::DISABLED
119 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
120 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
121 // * blocked_extensions() --> ExtensionRegistry::BLOCKED
122 // Returns NULL if the extension is not found in the selected sets.
123 const Extension
* GetExtensionById(const std::string
& id
,
124 int include_mask
) const;
126 // Looks up an extension by ID, regardless of whether it's enabled,
127 // disabled, blacklisted, or terminated.
128 const Extension
* GetInstalledExtension(const std::string
& id
) const;
130 // Adds the specified extension to the enabled set. The registry becomes an
131 // owner. Any previous extension with the same ID is removed.
132 // Returns true if there is no previous extension.
133 // NOTE: You probably want to use ExtensionService instead of calling this
135 bool AddEnabled(const scoped_refptr
<const Extension
>& extension
);
137 // Removes the specified extension from the enabled set.
138 // Returns true if the set contained the specified extension.
139 // NOTE: You probably want to use ExtensionService instead of calling this
141 bool RemoveEnabled(const std::string
& id
);
143 // As above, but for the disabled set.
144 bool AddDisabled(const scoped_refptr
<const Extension
>& extension
);
145 bool RemoveDisabled(const std::string
& id
);
147 // As above, but for the terminated set.
148 bool AddTerminated(const scoped_refptr
<const Extension
>& extension
);
149 bool RemoveTerminated(const std::string
& id
);
151 // As above, but for the blacklisted set.
152 bool AddBlacklisted(const scoped_refptr
<const Extension
>& extension
);
153 bool RemoveBlacklisted(const std::string
& id
);
155 // As above, but for the blocked set.
156 bool AddBlocked(const scoped_refptr
<const Extension
>& extension
);
157 bool RemoveBlocked(const std::string
& id
);
159 // Removes all extensions from all sets.
162 // Sets a callback to run when the disabled extension set is modified.
163 // TODO(jamescook): This is too specific for a generic registry; find some
164 // other way to do this.
165 void SetDisabledModificationCallback(
166 const ExtensionSet::ModificationCallback
& callback
);
168 // KeyedService implementation:
169 void Shutdown() override
;
172 // Extensions that are installed, enabled and not terminated.
173 ExtensionSet enabled_extensions_
;
175 // Extensions that are installed and disabled.
176 ExtensionSet disabled_extensions_
;
178 // Extensions that are installed and terminated.
179 ExtensionSet terminated_extensions_
;
181 // Extensions that are installed and blacklisted. Generally these shouldn't be
182 // considered as installed by the extension platform: we only keep them around
183 // so that if extensions are blacklisted by mistake they can easily be
185 ExtensionSet blacklisted_extensions_
;
187 // Extensions that are installed and blocked. Will never be loaded.
188 ExtensionSet blocked_extensions_
;
190 base::ObserverList
<ExtensionRegistryObserver
> observers_
;
192 content::BrowserContext
* const browser_context_
;
194 DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry
);
197 } // namespace extensions
199 #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_