• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #ifndef EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_
6 #define EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_
7 
8 #include <string>
9 
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"
15 
16 namespace content {
17 class BrowserContext;
18 }
19 
20 namespace extensions {
21 class Extension;
22 class ExtensionRegistryObserver;
23 
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,
37   };
38 
39   explicit ExtensionRegistry(content::BrowserContext* browser_context);
40   virtual ~ExtensionRegistry();
41 
42   // Returns the instance for the given |browser_context|.
43   static ExtensionRegistry* Get(content::BrowserContext* browser_context);
44 
45   // NOTE: These sets are *eventually* mututally exclusive, but an extension can
46   // appear in two sets for short periods of time.
enabled_extensions()47   const ExtensionSet& enabled_extensions() const {
48     return enabled_extensions_;
49   }
disabled_extensions()50   const ExtensionSet& disabled_extensions() const {
51     return disabled_extensions_;
52   }
terminated_extensions()53   const ExtensionSet& terminated_extensions() const {
54     return terminated_extensions_;
55   }
blacklisted_extensions()56   const ExtensionSet& blacklisted_extensions() const {
57     return blacklisted_extensions_;
58   }
59 
60   // Returns a set of all installed, disabled, blacklisted, and terminated
61   // extensions.
62   scoped_ptr<ExtensionSet> GenerateInstalledExtensionsSet() const;
63 
64   // The usual observer interface.
65   void AddObserver(ExtensionRegistryObserver* observer);
66   void RemoveObserver(ExtensionRegistryObserver* observer);
67 
68   // Invokes the observer method OnExtensionLoaded(). The extension must be
69   // enabled at the time of the call.
70   void TriggerOnLoaded(const Extension* extension);
71 
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);
76 
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);
88 
89   // Invokes the observer method OnExtensionInstalled(). The extension must be
90   // contained in one of the registry's extension sets.
91   void TriggerOnInstalled(const Extension* extension);
92 
93   // Invokes the observer method OnExtensionUninstalled(). The extension must
94   // not be any installed extension with |extension|'s ID.
95   void TriggerOnUninstalled(const Extension* extension);
96 
97   // Find an extension by ID using |include_mask| to pick the sets to search:
98   //  * enabled_extensions()     --> ExtensionRegistry::ENABLED
99   //  * disabled_extensions()    --> ExtensionRegistry::DISABLED
100   //  * terminated_extensions()  --> ExtensionRegistry::TERMINATED
101   //  * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
102   // Returns NULL if the extension is not found in the selected sets.
103   const Extension* GetExtensionById(const std::string& id,
104                                     int include_mask) const;
105 
106   // Adds the specified extension to the enabled set. The registry becomes an
107   // owner. Any previous extension with the same ID is removed.
108   // Returns true if there is no previous extension.
109   // NOTE: You probably want to use ExtensionService instead of calling this
110   // method directly.
111   bool AddEnabled(const scoped_refptr<const Extension>& extension);
112 
113   // Removes the specified extension from the enabled set.
114   // Returns true if the set contained the specified extension.
115   // NOTE: You probably want to use ExtensionService instead of calling this
116   // method directly.
117   bool RemoveEnabled(const std::string& id);
118 
119   // As above, but for the disabled set.
120   bool AddDisabled(const scoped_refptr<const Extension>& extension);
121   bool RemoveDisabled(const std::string& id);
122 
123   // As above, but for the terminated set.
124   bool AddTerminated(const scoped_refptr<const Extension>& extension);
125   bool RemoveTerminated(const std::string& id);
126 
127   // As above, but for the blacklisted set.
128   bool AddBlacklisted(const scoped_refptr<const Extension>& extension);
129   bool RemoveBlacklisted(const std::string& id);
130 
131   // Removes all extensions from all sets.
132   void ClearAll();
133 
134   // Sets a callback to run when the disabled extension set is modified.
135   // TODO(jamescook): This is too specific for a generic registry; find some
136   // other way to do this.
137   void SetDisabledModificationCallback(
138       const ExtensionSet::ModificationCallback& callback);
139 
140   // KeyedService implementation:
141   virtual void Shutdown() OVERRIDE;
142 
143  private:
144   // Extensions that are installed, enabled and not terminated.
145   ExtensionSet enabled_extensions_;
146 
147   // Extensions that are installed and disabled.
148   ExtensionSet disabled_extensions_;
149 
150   // Extensions that are installed and terminated.
151   ExtensionSet terminated_extensions_;
152 
153   // Extensions that are installed and blacklisted. Generally these shouldn't be
154   // considered as installed by the extension platform: we only keep them around
155   // so that if extensions are blacklisted by mistake they can easily be
156   // un-blacklisted.
157   ExtensionSet blacklisted_extensions_;
158 
159   ObserverList<ExtensionRegistryObserver> observers_;
160 
161   content::BrowserContext* const browser_context_;
162 
163   DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
164 };
165 
166 }  // namespace extensions
167 
168 #endif  // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_
169