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 #include "extensions/browser/extension_registry.h"
7 #include "base/strings/string_util.h"
8 #include "extensions/browser/extension_registry_factory.h"
9 #include "extensions/browser/extension_registry_observer.h"
11 namespace extensions
{
13 ExtensionRegistry::ExtensionRegistry(content::BrowserContext
* browser_context
)
14 : browser_context_(browser_context
) {}
15 ExtensionRegistry::~ExtensionRegistry() {}
18 ExtensionRegistry
* ExtensionRegistry::Get(content::BrowserContext
* context
) {
19 return ExtensionRegistryFactory::GetForBrowserContext(context
);
22 scoped_ptr
<ExtensionSet
> ExtensionRegistry::GenerateInstalledExtensionsSet()
24 return GenerateInstalledExtensionsSet(EVERYTHING
).Pass();
27 scoped_ptr
<ExtensionSet
> ExtensionRegistry::GenerateInstalledExtensionsSet(
28 int include_mask
) const {
29 scoped_ptr
<ExtensionSet
> installed_extensions(new ExtensionSet
);
30 if (include_mask
& IncludeFlag::ENABLED
)
31 installed_extensions
->InsertAll(enabled_extensions_
);
32 if (include_mask
& IncludeFlag::DISABLED
)
33 installed_extensions
->InsertAll(disabled_extensions_
);
34 if (include_mask
& IncludeFlag::TERMINATED
)
35 installed_extensions
->InsertAll(terminated_extensions_
);
36 if (include_mask
& IncludeFlag::BLACKLISTED
)
37 installed_extensions
->InsertAll(blacklisted_extensions_
);
38 if (include_mask
& IncludeFlag::BLOCKED
)
39 installed_extensions
->InsertAll(blocked_extensions_
);
40 return installed_extensions
.Pass();
43 void ExtensionRegistry::AddObserver(ExtensionRegistryObserver
* observer
) {
44 observers_
.AddObserver(observer
);
47 void ExtensionRegistry::RemoveObserver(ExtensionRegistryObserver
* observer
) {
48 observers_
.RemoveObserver(observer
);
51 void ExtensionRegistry::TriggerOnLoaded(const Extension
* extension
) {
53 DCHECK(enabled_extensions_
.Contains(extension
->id()));
54 FOR_EACH_OBSERVER(ExtensionRegistryObserver
,
56 OnExtensionLoaded(browser_context_
, extension
));
59 void ExtensionRegistry::TriggerOnReady(const Extension
* extension
) {
61 DCHECK(enabled_extensions_
.Contains(extension
->id()));
62 FOR_EACH_OBSERVER(ExtensionRegistryObserver
, observers_
,
63 OnExtensionReady(browser_context_
, extension
));
66 void ExtensionRegistry::TriggerOnUnloaded(
67 const Extension
* extension
,
68 UnloadedExtensionInfo::Reason reason
) {
70 DCHECK(!enabled_extensions_
.Contains(extension
->id()));
71 FOR_EACH_OBSERVER(ExtensionRegistryObserver
,
73 OnExtensionUnloaded(browser_context_
, extension
, reason
));
76 void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension
* extension
,
79 const std::string
& old_name
) {
82 GenerateInstalledExtensionsSet()->Contains(extension
->id()));
83 DCHECK_EQ(is_update
, !old_name
.empty());
85 ExtensionRegistryObserver
,
87 OnExtensionWillBeInstalled(
88 browser_context_
, extension
, is_update
, from_ephemeral
, old_name
));
91 void ExtensionRegistry::TriggerOnInstalled(const Extension
* extension
,
94 DCHECK(GenerateInstalledExtensionsSet()->Contains(extension
->id()));
95 FOR_EACH_OBSERVER(ExtensionRegistryObserver
,
98 browser_context_
, extension
, is_update
));
101 void ExtensionRegistry::TriggerOnUninstalled(const Extension
* extension
,
102 UninstallReason reason
) {
104 DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension
->id()));
106 ExtensionRegistryObserver
,
108 OnExtensionUninstalled(browser_context_
, extension
, reason
));
111 const Extension
* ExtensionRegistry::GetExtensionById(const std::string
& id
,
112 int include_mask
) const {
113 std::string lowercase_id
= base::StringToLowerASCII(id
);
114 if (include_mask
& ENABLED
) {
115 const Extension
* extension
= enabled_extensions_
.GetByID(lowercase_id
);
119 if (include_mask
& DISABLED
) {
120 const Extension
* extension
= disabled_extensions_
.GetByID(lowercase_id
);
124 if (include_mask
& TERMINATED
) {
125 const Extension
* extension
= terminated_extensions_
.GetByID(lowercase_id
);
129 if (include_mask
& BLACKLISTED
) {
130 const Extension
* extension
= blacklisted_extensions_
.GetByID(lowercase_id
);
134 if (include_mask
& BLOCKED
) {
135 const Extension
* extension
= blocked_extensions_
.GetByID(lowercase_id
);
142 const Extension
* ExtensionRegistry::GetInstalledExtension(
143 const std::string
& id
) const {
144 return GetExtensionById(id
, ExtensionRegistry::EVERYTHING
);
147 bool ExtensionRegistry::AddEnabled(
148 const scoped_refptr
<const Extension
>& extension
) {
149 return enabled_extensions_
.Insert(extension
);
152 bool ExtensionRegistry::RemoveEnabled(const std::string
& id
) {
153 // Only enabled extensions can be ready, so removing an enabled extension
154 // should also remove from the ready set if possible.
155 if (ready_extensions_
.Contains(id
))
157 return enabled_extensions_
.Remove(id
);
160 bool ExtensionRegistry::AddDisabled(
161 const scoped_refptr
<const Extension
>& extension
) {
162 return disabled_extensions_
.Insert(extension
);
165 bool ExtensionRegistry::RemoveDisabled(const std::string
& id
) {
166 return disabled_extensions_
.Remove(id
);
169 bool ExtensionRegistry::AddTerminated(
170 const scoped_refptr
<const Extension
>& extension
) {
171 return terminated_extensions_
.Insert(extension
);
174 bool ExtensionRegistry::RemoveTerminated(const std::string
& id
) {
175 return terminated_extensions_
.Remove(id
);
178 bool ExtensionRegistry::AddBlacklisted(
179 const scoped_refptr
<const Extension
>& extension
) {
180 return blacklisted_extensions_
.Insert(extension
);
183 bool ExtensionRegistry::RemoveBlacklisted(const std::string
& id
) {
184 return blacklisted_extensions_
.Remove(id
);
187 bool ExtensionRegistry::AddBlocked(
188 const scoped_refptr
<const Extension
>& extension
) {
189 return blocked_extensions_
.Insert(extension
);
192 bool ExtensionRegistry::RemoveBlocked(const std::string
& id
) {
193 return blocked_extensions_
.Remove(id
);
196 bool ExtensionRegistry::AddReady(
197 const scoped_refptr
<const Extension
>& extension
) {
198 return ready_extensions_
.Insert(extension
);
201 bool ExtensionRegistry::RemoveReady(const std::string
& id
) {
202 return ready_extensions_
.Remove(id
);
205 void ExtensionRegistry::ClearAll() {
206 enabled_extensions_
.Clear();
207 disabled_extensions_
.Clear();
208 terminated_extensions_
.Clear();
209 blacklisted_extensions_
.Clear();
210 blocked_extensions_
.Clear();
211 ready_extensions_
.Clear();
214 void ExtensionRegistry::SetDisabledModificationCallback(
215 const ExtensionSet::ModificationCallback
& callback
) {
216 disabled_extensions_
.set_modification_callback(callback
);
219 void ExtensionRegistry::Shutdown() {
220 // Release references to all Extension objects in the sets.
222 FOR_EACH_OBSERVER(ExtensionRegistryObserver
, observers_
, OnShutdown(this));
225 } // namespace extensions