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.
41 EVERYTHING = (1 << 4) - 1,
44 explicit ExtensionRegistry(content::BrowserContext* browser_context);
45 ~ExtensionRegistry() override;
47 // Returns the instance for the given |browser_context|.
48 static ExtensionRegistry* Get(content::BrowserContext* browser_context);
50 content::BrowserContext* browser_context() const { return browser_context_; }
52 // NOTE: These sets are *eventually* mutually exclusive, but an extension can
53 // appear in two sets for short periods of time.
54 const ExtensionSet& enabled_extensions() const {
55 return enabled_extensions_;
57 const ExtensionSet& disabled_extensions() const {
58 return disabled_extensions_;
60 const ExtensionSet& terminated_extensions() const {
61 return terminated_extensions_;
63 const ExtensionSet& blacklisted_extensions() const {
64 return blacklisted_extensions_;
67 // Returns a set of all installed, disabled, blacklisted, and terminated
69 scoped_ptr<ExtensionSet> GenerateInstalledExtensionsSet() const;
71 // The usual observer interface.
72 void AddObserver(ExtensionRegistryObserver* observer);
73 void RemoveObserver(ExtensionRegistryObserver* observer);
75 // Invokes the observer method OnExtensionLoaded(). The extension must be
76 // enabled at the time of the call.
77 void TriggerOnLoaded(const Extension* extension);
79 // Invokes the observer method OnExtensionUnloaded(). The extension must not
80 // be enabled at the time of the call.
81 void TriggerOnUnloaded(const Extension* extension,
82 UnloadedExtensionInfo::Reason reason);
84 // If this is a fresh install then |is_update| is false and there must not be
85 // any installed extension with |extension|'s ID. If this is an update then
86 // |is_update| is true and must be an installed extension with |extension|'s
87 // ID, and |old_name| must be non-empty.
88 // If true, |from_ephemeral| indicates that the extension was previously
89 // installed ephemerally and has been promoted to a regular installed
90 // extension. |is_update| should also be true.
91 void TriggerOnWillBeInstalled(const Extension* extension,
94 const std::string& old_name);
96 // Invokes the observer method OnExtensionInstalled(). The extension must be
97 // contained in one of the registry's extension sets.
98 void TriggerOnInstalled(const Extension* extension,
101 // Invokes the observer method OnExtensionUninstalled(). The extension must
102 // not be any installed extension with |extension|'s ID.
103 void TriggerOnUninstalled(const Extension* extension, UninstallReason reason);
105 // Find an extension by ID using |include_mask| to pick the sets to search:
106 // * enabled_extensions() --> ExtensionRegistry::ENABLED
107 // * disabled_extensions() --> ExtensionRegistry::DISABLED
108 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
109 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
110 // Returns NULL if the extension is not found in the selected sets.
111 const Extension* GetExtensionById(const std::string& id,
112 int include_mask) const;
114 // Adds the specified extension to the enabled set. The registry becomes an
115 // owner. Any previous extension with the same ID is removed.
116 // Returns true if there is no previous extension.
117 // NOTE: You probably want to use ExtensionService instead of calling this
119 bool AddEnabled(const scoped_refptr<const Extension>& extension);
121 // Removes the specified extension from the enabled set.
122 // Returns true if the set contained the specified extension.
123 // NOTE: You probably want to use ExtensionService instead of calling this
125 bool RemoveEnabled(const std::string& id);
127 // As above, but for the disabled set.
128 bool AddDisabled(const scoped_refptr<const Extension>& extension);
129 bool RemoveDisabled(const std::string& id);
131 // As above, but for the terminated set.
132 bool AddTerminated(const scoped_refptr<const Extension>& extension);
133 bool RemoveTerminated(const std::string& id);
135 // As above, but for the blacklisted set.
136 bool AddBlacklisted(const scoped_refptr<const Extension>& extension);
137 bool RemoveBlacklisted(const std::string& id);
139 // Removes all extensions from all sets.
142 // Sets a callback to run when the disabled extension set is modified.
143 // TODO(jamescook): This is too specific for a generic registry; find some
144 // other way to do this.
145 void SetDisabledModificationCallback(
146 const ExtensionSet::ModificationCallback& callback);
148 // KeyedService implementation:
149 void Shutdown() override;
152 // Extensions that are installed, enabled and not terminated.
153 ExtensionSet enabled_extensions_;
155 // Extensions that are installed and disabled.
156 ExtensionSet disabled_extensions_;
158 // Extensions that are installed and terminated.
159 ExtensionSet terminated_extensions_;
161 // Extensions that are installed and blacklisted. Generally these shouldn't be
162 // considered as installed by the extension platform: we only keep them around
163 // so that if extensions are blacklisted by mistake they can easily be
165 ExtensionSet blacklisted_extensions_;
167 ObserverList<ExtensionRegistryObserver> observers_;
169 content::BrowserContext* const browser_context_;
171 DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
174 } // namespace extensions
176 #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_