Update path of checkdeps to buildtools checkout
[chromium-blink-merge.git] / extensions / browser / extension_registry.h
blob646863c224623475d3b242333fe033699ad582bd
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_
8 #include <string>
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"
16 namespace content {
17 class BrowserContext;
20 namespace extensions {
21 class Extension;
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 {
28 public:
29 // Flags to pass to GetExtensionById() to select which sets to look in.
30 enum IncludeFlag {
31 NONE = 0,
32 ENABLED = 1 << 0,
33 DISABLED = 1 << 1,
34 TERMINATED = 1 << 2,
35 BLACKLISTED = 1 << 3,
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
61 // extensions.
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,
75 UnloadedExtensionInfo::Reason reason);
77 // If this is a fresh install then |is_update| is false and there must not be
78 // any installed extension with |extension|'s ID. If this is an update then
79 // |is_update| is true and must be an installed extension with |extension|'s
80 // ID, and |old_name| must be non-empty.
81 // If true, |from_ephemeral| indicates that the extension was previously
82 // installed ephemerally and has been promoted to a regular installed
83 // extension. |is_update| should also be true.
84 void TriggerOnWillBeInstalled(const Extension* extension,
85 bool is_update,
86 bool from_ephemeral,
87 const std::string& old_name);
89 // Invokes the observer method OnExtensionUninstalled(). The extension must
90 // not be any installed extension with |extension|'s ID.
91 void TriggerOnUninstalled(const Extension* extension);
93 // Find an extension by ID using |include_mask| to pick the sets to search:
94 // * enabled_extensions() --> ExtensionRegistry::ENABLED
95 // * disabled_extensions() --> ExtensionRegistry::DISABLED
96 // * terminated_extensions() --> ExtensionRegistry::TERMINATED
97 // * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
98 // Returns NULL if the extension is not found in the selected sets.
99 const Extension* GetExtensionById(const std::string& id,
100 int include_mask) const;
102 // Adds the specified extension to the enabled set. The registry becomes an
103 // owner. Any previous extension with the same ID is removed.
104 // Returns true if there is no previous extension.
105 // NOTE: You probably want to use ExtensionService instead of calling this
106 // method directly.
107 bool AddEnabled(const scoped_refptr<const Extension>& extension);
109 // Removes the specified extension from the enabled set.
110 // Returns true if the set contained the specified extension.
111 // NOTE: You probably want to use ExtensionService instead of calling this
112 // method directly.
113 bool RemoveEnabled(const std::string& id);
115 // As above, but for the disabled set.
116 bool AddDisabled(const scoped_refptr<const Extension>& extension);
117 bool RemoveDisabled(const std::string& id);
119 // As above, but for the terminated set.
120 bool AddTerminated(const scoped_refptr<const Extension>& extension);
121 bool RemoveTerminated(const std::string& id);
123 // As above, but for the blacklisted set.
124 bool AddBlacklisted(const scoped_refptr<const Extension>& extension);
125 bool RemoveBlacklisted(const std::string& id);
127 // Removes all extensions from all sets.
128 void ClearAll();
130 // Sets a callback to run when the disabled extension set is modified.
131 // TODO(jamescook): This is too specific for a generic registry; find some
132 // other way to do this.
133 void SetDisabledModificationCallback(
134 const ExtensionSet::ModificationCallback& callback);
136 // KeyedService implementation:
137 virtual void Shutdown() OVERRIDE;
139 private:
140 // Extensions that are installed, enabled and not terminated.
141 ExtensionSet enabled_extensions_;
143 // Extensions that are installed and disabled.
144 ExtensionSet disabled_extensions_;
146 // Extensions that are installed and terminated.
147 ExtensionSet terminated_extensions_;
149 // Extensions that are installed and blacklisted. Generally these shouldn't be
150 // considered as installed by the extension platform: we only keep them around
151 // so that if extensions are blacklisted by mistake they can easily be
152 // un-blacklisted.
153 ExtensionSet blacklisted_extensions_;
155 ObserverList<ExtensionRegistryObserver> observers_;
157 content::BrowserContext* const browser_context_;
159 DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
162 } // namespace extensions
164 #endif // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_