Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / extension_registry.h
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.
47   const ExtensionSet& enabled_extensions() const {
48     return enabled_extensions_;
49   }
50   const ExtensionSet& disabled_extensions() const {
51     return disabled_extensions_;
52   }
53   const ExtensionSet& terminated_extensions() const {
54     return terminated_extensions_;
55   }
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   // Find an extension by ID using |include_mask| to pick the sets to search:
78   //  * enabled_extensions()     --> ExtensionRegistry::ENABLED
79   //  * disabled_extensions()    --> ExtensionRegistry::DISABLED
80   //  * terminated_extensions()  --> ExtensionRegistry::TERMINATED
81   //  * blacklisted_extensions() --> ExtensionRegistry::BLACKLISTED
82   // Returns NULL if the extension is not found in the selected sets.
83   const Extension* GetExtensionById(const std::string& id,
84                                     int include_mask) const;
85
86   // Adds the specified extension to the enabled set. The registry becomes an
87   // owner. Any previous extension with the same ID is removed.
88   // Returns true if there is no previous extension.
89   // NOTE: You probably want to use ExtensionService instead of calling this
90   // method directly.
91   bool AddEnabled(const scoped_refptr<const Extension>& extension);
92
93   // Removes the specified extension from the enabled set.
94   // Returns true if the set contained the specified extension.
95   // NOTE: You probably want to use ExtensionService instead of calling this
96   // method directly.
97   bool RemoveEnabled(const std::string& id);
98
99   // As above, but for the disabled set.
100   bool AddDisabled(const scoped_refptr<const Extension>& extension);
101   bool RemoveDisabled(const std::string& id);
102
103   // As above, but for the terminated set.
104   bool AddTerminated(const scoped_refptr<const Extension>& extension);
105   bool RemoveTerminated(const std::string& id);
106
107   // As above, but for the blacklisted set.
108   bool AddBlacklisted(const scoped_refptr<const Extension>& extension);
109   bool RemoveBlacklisted(const std::string& id);
110
111   // Removes all extensions from all sets.
112   void ClearAll();
113
114   // Sets a callback to run when the disabled extension set is modified.
115   // TODO(jamescook): This is too specific for a generic registry; find some
116   // other way to do this.
117   void SetDisabledModificationCallback(
118       const ExtensionSet::ModificationCallback& callback);
119
120   // KeyedService implementation:
121   virtual void Shutdown() OVERRIDE;
122
123  private:
124   // Extensions that are installed, enabled and not terminated.
125   ExtensionSet enabled_extensions_;
126
127   // Extensions that are installed and disabled.
128   ExtensionSet disabled_extensions_;
129
130   // Extensions that are installed and terminated.
131   ExtensionSet terminated_extensions_;
132
133   // Extensions that are installed and blacklisted. Generally these shouldn't be
134   // considered as installed by the extension platform: we only keep them around
135   // so that if extensions are blacklisted by mistake they can easily be
136   // un-blacklisted.
137   ExtensionSet blacklisted_extensions_;
138
139   ObserverList<ExtensionRegistryObserver> observers_;
140
141   content::BrowserContext* const browser_context_;
142
143   DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
144 };
145
146 }  // namespace extensions
147
148 #endif  // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_