Update To 11.40.268.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/browser/uninstall_reason.h"
15 #include "extensions/common/extension_set.h"
16
17 #if !defined(ENABLE_EXTENSIONS)
18 #error "Extensions must be enabled"
19 #endif
20
21 namespace content {
22 class BrowserContext;
23 }
24
25 namespace extensions {
26 class Extension;
27 class ExtensionRegistryObserver;
28
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 {
33  public:
34   // Flags to pass to GetExtensionById() to select which sets to look in.
35   enum IncludeFlag {
36     NONE        = 0,
37     ENABLED     = 1 << 0,
38     DISABLED    = 1 << 1,
39     TERMINATED  = 1 << 2,
40     BLACKLISTED = 1 << 3,
41     EVERYTHING = (1 << 4) - 1,
42   };
43
44   explicit ExtensionRegistry(content::BrowserContext* browser_context);
45   ~ExtensionRegistry() override;
46
47   // Returns the instance for the given |browser_context|.
48   static ExtensionRegistry* Get(content::BrowserContext* browser_context);
49
50   content::BrowserContext* browser_context() const { return browser_context_; }
51
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_;
56   }
57   const ExtensionSet& disabled_extensions() const {
58     return disabled_extensions_;
59   }
60   const ExtensionSet& terminated_extensions() const {
61     return terminated_extensions_;
62   }
63   const ExtensionSet& blacklisted_extensions() const {
64     return blacklisted_extensions_;
65   }
66
67   // Returns a set of all installed, disabled, blacklisted, and terminated
68   // extensions.
69   scoped_ptr<ExtensionSet> GenerateInstalledExtensionsSet() const;
70
71   // The usual observer interface.
72   void AddObserver(ExtensionRegistryObserver* observer);
73   void RemoveObserver(ExtensionRegistryObserver* observer);
74
75   // Invokes the observer method OnExtensionLoaded(). The extension must be
76   // enabled at the time of the call.
77   void TriggerOnLoaded(const Extension* extension);
78
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);
83
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,
92                                 bool is_update,
93                                 bool from_ephemeral,
94                                 const std::string& old_name);
95
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,
99                           bool is_update);
100
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);
104
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;
113
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
118   // method directly.
119   bool AddEnabled(const scoped_refptr<const Extension>& extension);
120
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
124   // method directly.
125   bool RemoveEnabled(const std::string& id);
126
127   // As above, but for the disabled set.
128   bool AddDisabled(const scoped_refptr<const Extension>& extension);
129   bool RemoveDisabled(const std::string& id);
130
131   // As above, but for the terminated set.
132   bool AddTerminated(const scoped_refptr<const Extension>& extension);
133   bool RemoveTerminated(const std::string& id);
134
135   // As above, but for the blacklisted set.
136   bool AddBlacklisted(const scoped_refptr<const Extension>& extension);
137   bool RemoveBlacklisted(const std::string& id);
138
139   // Removes all extensions from all sets.
140   void ClearAll();
141
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);
147
148   // KeyedService implementation:
149   void Shutdown() override;
150
151  private:
152   // Extensions that are installed, enabled and not terminated.
153   ExtensionSet enabled_extensions_;
154
155   // Extensions that are installed and disabled.
156   ExtensionSet disabled_extensions_;
157
158   // Extensions that are installed and terminated.
159   ExtensionSet terminated_extensions_;
160
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
164   // un-blacklisted.
165   ExtensionSet blacklisted_extensions_;
166
167   ObserverList<ExtensionRegistryObserver> observers_;
168
169   content::BrowserContext* const browser_context_;
170
171   DISALLOW_COPY_AND_ASSIGN(ExtensionRegistry);
172 };
173
174 }  // namespace extensions
175
176 #endif  // EXTENSIONS_BROWSER_EXTENSION_REGISTRY_H_