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 scoped_ptr
<ExtensionSet
> installed_extensions(new ExtensionSet
);
25 installed_extensions
->InsertAll(enabled_extensions_
);
26 installed_extensions
->InsertAll(disabled_extensions_
);
27 installed_extensions
->InsertAll(terminated_extensions_
);
28 installed_extensions
->InsertAll(blacklisted_extensions_
);
29 return installed_extensions
.Pass();
32 void ExtensionRegistry::AddObserver(ExtensionRegistryObserver
* observer
) {
33 observers_
.AddObserver(observer
);
36 void ExtensionRegistry::RemoveObserver(ExtensionRegistryObserver
* observer
) {
37 observers_
.RemoveObserver(observer
);
40 void ExtensionRegistry::TriggerOnLoaded(const Extension
* extension
) {
41 DCHECK(enabled_extensions_
.Contains(extension
->id()));
42 FOR_EACH_OBSERVER(ExtensionRegistryObserver
,
44 OnExtensionLoaded(browser_context_
, extension
));
47 void ExtensionRegistry::TriggerOnUnloaded(
48 const Extension
* extension
,
49 UnloadedExtensionInfo::Reason reason
) {
50 DCHECK(!enabled_extensions_
.Contains(extension
->id()));
51 FOR_EACH_OBSERVER(ExtensionRegistryObserver
,
53 OnExtensionUnloaded(browser_context_
, extension
, reason
));
56 void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension
* extension
,
59 const std::string
& old_name
) {
61 GenerateInstalledExtensionsSet()->Contains(extension
->id()));
62 DCHECK(is_update
== !old_name
.empty());
64 ExtensionRegistryObserver
,
66 OnExtensionWillBeInstalled(
67 browser_context_
, extension
, is_update
, from_ephemeral
, old_name
));
70 void ExtensionRegistry::TriggerOnUninstalled(const Extension
* extension
) {
71 DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension
->id()));
72 FOR_EACH_OBSERVER(ExtensionRegistryObserver
,
74 OnExtensionUninstalled(browser_context_
, extension
));
77 const Extension
* ExtensionRegistry::GetExtensionById(const std::string
& id
,
78 int include_mask
) const {
79 std::string lowercase_id
= StringToLowerASCII(id
);
80 if (include_mask
& ENABLED
) {
81 const Extension
* extension
= enabled_extensions_
.GetByID(lowercase_id
);
85 if (include_mask
& DISABLED
) {
86 const Extension
* extension
= disabled_extensions_
.GetByID(lowercase_id
);
90 if (include_mask
& TERMINATED
) {
91 const Extension
* extension
= terminated_extensions_
.GetByID(lowercase_id
);
95 if (include_mask
& BLACKLISTED
) {
96 const Extension
* extension
= blacklisted_extensions_
.GetByID(lowercase_id
);
103 bool ExtensionRegistry::AddEnabled(
104 const scoped_refptr
<const Extension
>& extension
) {
105 return enabled_extensions_
.Insert(extension
);
108 bool ExtensionRegistry::RemoveEnabled(const std::string
& id
) {
109 return enabled_extensions_
.Remove(id
);
112 bool ExtensionRegistry::AddDisabled(
113 const scoped_refptr
<const Extension
>& extension
) {
114 return disabled_extensions_
.Insert(extension
);
117 bool ExtensionRegistry::RemoveDisabled(const std::string
& id
) {
118 return disabled_extensions_
.Remove(id
);
121 bool ExtensionRegistry::AddTerminated(
122 const scoped_refptr
<const Extension
>& extension
) {
123 return terminated_extensions_
.Insert(extension
);
126 bool ExtensionRegistry::RemoveTerminated(const std::string
& id
) {
127 return terminated_extensions_
.Remove(id
);
130 bool ExtensionRegistry::AddBlacklisted(
131 const scoped_refptr
<const Extension
>& extension
) {
132 return blacklisted_extensions_
.Insert(extension
);
135 bool ExtensionRegistry::RemoveBlacklisted(const std::string
& id
) {
136 return blacklisted_extensions_
.Remove(id
);
139 void ExtensionRegistry::ClearAll() {
140 enabled_extensions_
.Clear();
141 disabled_extensions_
.Clear();
142 terminated_extensions_
.Clear();
143 blacklisted_extensions_
.Clear();
146 void ExtensionRegistry::SetDisabledModificationCallback(
147 const ExtensionSet::ModificationCallback
& callback
) {
148 disabled_extensions_
.set_modification_callback(callback
);
151 void ExtensionRegistry::Shutdown() {
152 // Release references to all Extension objects in the sets.
156 } // namespace extensions