Roll src/third_party/skia ce86687:b880d7f
[chromium-blink-merge.git] / extensions / browser / extension_registry.cc
blob65c8b5e9d36b6c1e81b5ae44ee591fe0129fde75
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() {}
17 // static
18 ExtensionRegistry* ExtensionRegistry::Get(content::BrowserContext* context) {
19 return ExtensionRegistryFactory::GetForBrowserContext(context);
22 scoped_ptr<ExtensionSet> ExtensionRegistry::GenerateInstalledExtensionsSet()
23 const {
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) {
52 CHECK(extension);
53 DCHECK(enabled_extensions_.Contains(extension->id()));
54 FOR_EACH_OBSERVER(ExtensionRegistryObserver,
55 observers_,
56 OnExtensionLoaded(browser_context_, extension));
59 void ExtensionRegistry::TriggerOnUnloaded(
60 const Extension* extension,
61 UnloadedExtensionInfo::Reason reason) {
62 CHECK(extension);
63 DCHECK(!enabled_extensions_.Contains(extension->id()));
64 FOR_EACH_OBSERVER(ExtensionRegistryObserver,
65 observers_,
66 OnExtensionUnloaded(browser_context_, extension, reason));
69 void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension* extension,
70 bool is_update,
71 bool from_ephemeral,
72 const std::string& old_name) {
73 CHECK(extension);
74 DCHECK_EQ(is_update,
75 GenerateInstalledExtensionsSet()->Contains(extension->id()));
76 DCHECK_EQ(is_update, !old_name.empty());
77 FOR_EACH_OBSERVER(
78 ExtensionRegistryObserver,
79 observers_,
80 OnExtensionWillBeInstalled(
81 browser_context_, extension, is_update, from_ephemeral, old_name));
84 void ExtensionRegistry::TriggerOnInstalled(const Extension* extension,
85 bool is_update) {
86 CHECK(extension);
87 DCHECK(GenerateInstalledExtensionsSet()->Contains(extension->id()));
88 FOR_EACH_OBSERVER(ExtensionRegistryObserver,
89 observers_,
90 OnExtensionInstalled(
91 browser_context_, extension, is_update));
94 void ExtensionRegistry::TriggerOnUninstalled(const Extension* extension,
95 UninstallReason reason) {
96 CHECK(extension);
97 DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension->id()));
98 FOR_EACH_OBSERVER(
99 ExtensionRegistryObserver,
100 observers_,
101 OnExtensionUninstalled(browser_context_, extension, reason));
104 const Extension* ExtensionRegistry::GetExtensionById(const std::string& id,
105 int include_mask) const {
106 std::string lowercase_id = base::StringToLowerASCII(id);
107 if (include_mask & ENABLED) {
108 const Extension* extension = enabled_extensions_.GetByID(lowercase_id);
109 if (extension)
110 return extension;
112 if (include_mask & DISABLED) {
113 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
114 if (extension)
115 return extension;
117 if (include_mask & TERMINATED) {
118 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
119 if (extension)
120 return extension;
122 if (include_mask & BLACKLISTED) {
123 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
124 if (extension)
125 return extension;
127 if (include_mask & BLOCKED) {
128 const Extension* extension = blocked_extensions_.GetByID(lowercase_id);
129 if (extension)
130 return extension;
132 return NULL;
135 bool ExtensionRegistry::AddEnabled(
136 const scoped_refptr<const Extension>& extension) {
137 return enabled_extensions_.Insert(extension);
140 bool ExtensionRegistry::RemoveEnabled(const std::string& id) {
141 return enabled_extensions_.Remove(id);
144 bool ExtensionRegistry::AddDisabled(
145 const scoped_refptr<const Extension>& extension) {
146 return disabled_extensions_.Insert(extension);
149 bool ExtensionRegistry::RemoveDisabled(const std::string& id) {
150 return disabled_extensions_.Remove(id);
153 bool ExtensionRegistry::AddTerminated(
154 const scoped_refptr<const Extension>& extension) {
155 return terminated_extensions_.Insert(extension);
158 bool ExtensionRegistry::RemoveTerminated(const std::string& id) {
159 return terminated_extensions_.Remove(id);
162 bool ExtensionRegistry::AddBlacklisted(
163 const scoped_refptr<const Extension>& extension) {
164 return blacklisted_extensions_.Insert(extension);
167 bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) {
168 return blacklisted_extensions_.Remove(id);
171 bool ExtensionRegistry::AddBlocked(
172 const scoped_refptr<const Extension>& extension) {
173 return blocked_extensions_.Insert(extension);
176 bool ExtensionRegistry::RemoveBlocked(const std::string& id) {
177 return blocked_extensions_.Remove(id);
180 void ExtensionRegistry::ClearAll() {
181 enabled_extensions_.Clear();
182 disabled_extensions_.Clear();
183 terminated_extensions_.Clear();
184 blacklisted_extensions_.Clear();
185 blocked_extensions_.Clear();
188 void ExtensionRegistry::SetDisabledModificationCallback(
189 const ExtensionSet::ModificationCallback& callback) {
190 disabled_extensions_.set_modification_callback(callback);
193 void ExtensionRegistry::Shutdown() {
194 // Release references to all Extension objects in the sets.
195 ClearAll();
196 FOR_EACH_OBSERVER(ExtensionRegistryObserver, observers_, OnShutdown(this));
199 } // namespace extensions