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/common/extension_set.h"
20 namespace extensions
{
22 class ExtensionRegistryObserver
;
24 // ExtensionRegistry holds sets of the installed extensions for a given
25 // BrowserContext. An incognito browser context and its master browser context
26 // share a single registry.
27 class ExtensionRegistry
: public KeyedService
{
29 // Flags to pass to GetExtensionById() to select which sets to look in.
36 EVERYTHING
= (1 << 4) - 1,
39 explicit ExtensionRegistry(content::BrowserContext
* browser_context
);
40 virtual ~ExtensionRegistry();
42 // Returns the instance for the given |browser_context|.
43 static ExtensionRegistry
* Get(content::BrowserContext
* browser_context
);
45 // NOTE: These sets are *eventually* mututally exclusive, but an extension can
46 // appear in two sets for short periods of time.
47 const ExtensionSet
& enabled_extensions() const {
48 return enabled_extensions_
;
50 const ExtensionSet
& disabled_extensions() const {
51 return disabled_extensions_
;
53 const ExtensionSet
& terminated_extensions() const {
54 return terminated_extensions_
;
56 const ExtensionSet
& blacklisted_extensions() const {
57 return blacklisted_extensions_
;
60 // Returns a set of all installed, disabled, blacklisted, and terminated
62 scoped_ptr
<ExtensionSet
> GenerateInstalledExtensionsSet() const;
64 // The usual observer interface.
65 void AddObserver(ExtensionRegistryObserver
* observer
);
66 void RemoveObserver(ExtensionRegistryObserver
* observer
);
68 // Invokes the observer method OnExtensionLoaded(). The extension must be
69 // enabled at the time of the call.
70 void TriggerOnLoaded(const Extension
* extension
);
72 // Invokes the observer method OnExtensionUnloaded(). The extension must not
73 // be enabled at the time of the call.
74 void TriggerOnUnloaded(const Extension
* extension
);
76 // Find an extension by ID using |include_mask| to pick the sets to search:
77 // * enabled_extensions() --> ExtensionRegistry::ENABLED
78 // * disabled_extensions() --> ExtensionRegistry::DISABLED
79 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
80 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
81 // Returns NULL if the extension is not found in the selected sets.
82 const Extension
* GetExtensionById(const std::string
& id
,
83 int include_mask
) const;
85 // Adds the specified extension to the enabled set. The registry becomes an
86 // owner. Any previous extension with the same ID is removed.
87 // Returns true if there is no previous extension.
88 // NOTE: You probably want to use ExtensionService instead of calling this
90 bool AddEnabled(const scoped_refptr
<const Extension
>& extension
);
92 // Removes the specified extension from the enabled set.
93 // Returns true if the set contained the specified extension.
94 // NOTE: You probably want to use ExtensionService instead of calling this
96 bool RemoveEnabled(const std::string
& id
);
98 // As above, but for the disabled set.
99 bool AddDisabled(const scoped_refptr
<const Extension
>& extension
);
100 bool RemoveDisabled(const std::string
& id
);
102 // As above, but for the terminated set.
103 bool AddTerminated(const scoped_refptr
<const Extension
>& extension
);
104 bool RemoveTerminated(const std::string
& id
);
106 // As above, but for the blacklisted set.
107 bool AddBlacklisted(const scoped_refptr
<const Extension
>& extension
);
108 bool RemoveBlacklisted(const std::string
& id
);
110 // Removes all extensions from all sets.
113 // Sets a callback to run when the disabled extension set is modified.
114 // TODO(jamescook): This is too specific for a generic registry; find some
115 // other way to do this.
116 void SetDisabledModificationCallback(
117 const ExtensionSet::ModificationCallback
& callback
);
119 // KeyedService implementation:
120 virtual void Shutdown() OVERRIDE
;
123 // Extensions that are installed, enabled and not terminated.
124 ExtensionSet enabled_extensions_
;
126 // Extensions that are installed and disabled.
127 ExtensionSet disabled_extensions_
;
129 // Extensions that are installed and terminated.
130 ExtensionSet terminated_extensions_
;
132 // Extensions that are installed and blacklisted. Generally these shouldn't be
133 // considered as installed by the extension platform: we only keep them around
134 // so that if extensions are blacklisted by mistake they can easily be
136 ExtensionSet blacklisted_extensions_
;
138 ObserverList
<ExtensionRegistryObserver
> observers_
;
140 content::BrowserContext
* const browser_context_
;
142 DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry
);
145 } // namespace extensions
147 #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_