Files.app: Dispatch 'drive-connection-changed' event on initialization of VolumeManag...
[chromium-blink-merge.git] / extensions / browser / extension_registry.cc
blobc59d6356ebc8880448cf24b5efa87cd04f16351d
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 const Extension* ExtensionRegistry::GetInstalledExtension(
136 const std::string& id) const {
137 return GetExtensionById(id, ExtensionRegistry::EVERYTHING);
140 bool ExtensionRegistry::AddEnabled(
141 const scoped_refptr<const Extension>& extension) {
142 return enabled_extensions_.Insert(extension);
145 bool ExtensionRegistry::RemoveEnabled(const std::string& id) {
146 return enabled_extensions_.Remove(id);
149 bool ExtensionRegistry::AddDisabled(
150 const scoped_refptr<const Extension>& extension) {
151 return disabled_extensions_.Insert(extension);
154 bool ExtensionRegistry::RemoveDisabled(const std::string& id) {
155 return disabled_extensions_.Remove(id);
158 bool ExtensionRegistry::AddTerminated(
159 const scoped_refptr<const Extension>& extension) {
160 return terminated_extensions_.Insert(extension);
163 bool ExtensionRegistry::RemoveTerminated(const std::string& id) {
164 return terminated_extensions_.Remove(id);
167 bool ExtensionRegistry::AddBlacklisted(
168 const scoped_refptr<const Extension>& extension) {
169 return blacklisted_extensions_.Insert(extension);
172 bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) {
173 return blacklisted_extensions_.Remove(id);
176 bool ExtensionRegistry::AddBlocked(
177 const scoped_refptr<const Extension>& extension) {
178 return blocked_extensions_.Insert(extension);
181 bool ExtensionRegistry::RemoveBlocked(const std::string& id) {
182 return blocked_extensions_.Remove(id);
185 void ExtensionRegistry::ClearAll() {
186 enabled_extensions_.Clear();
187 disabled_extensions_.Clear();
188 terminated_extensions_.Clear();
189 blacklisted_extensions_.Clear();
190 blocked_extensions_.Clear();
193 void ExtensionRegistry::SetDisabledModificationCallback(
194 const ExtensionSet::ModificationCallback& callback) {
195 disabled_extensions_.set_modification_callback(callback);
198 void ExtensionRegistry::Shutdown() {
199 // Release references to all Extension objects in the sets.
200 ClearAll();
201 FOR_EACH_OBSERVER(ExtensionRegistryObserver, observers_, OnShutdown(this));
204 } // namespace extensions