Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / extensions / browser / extension_registry.cc
blob769539e971931a7368dcac90882b5422f25627aa
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 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,
43 observers_,
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,
52 observers_,
53 OnExtensionUnloaded(browser_context_, extension, reason));
56 void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension* extension,
57 bool is_update,
58 bool from_ephemeral,
59 const std::string& old_name) {
60 DCHECK(is_update ==
61 GenerateInstalledExtensionsSet()->Contains(extension->id()));
62 DCHECK(is_update == !old_name.empty());
63 FOR_EACH_OBSERVER(
64 ExtensionRegistryObserver,
65 observers_,
66 OnExtensionWillBeInstalled(
67 browser_context_, extension, is_update, from_ephemeral, old_name));
70 void ExtensionRegistry::TriggerOnInstalled(const Extension* extension,
71 bool is_update) {
72 DCHECK(GenerateInstalledExtensionsSet()->Contains(extension->id()));
73 FOR_EACH_OBSERVER(ExtensionRegistryObserver,
74 observers_,
75 OnExtensionInstalled(
76 browser_context_, extension, is_update));
79 void ExtensionRegistry::TriggerOnUninstalled(const Extension* extension,
80 UninstallReason reason) {
81 DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension->id()));
82 FOR_EACH_OBSERVER(
83 ExtensionRegistryObserver,
84 observers_,
85 OnExtensionUninstalled(browser_context_, extension, reason));
88 const Extension* ExtensionRegistry::GetExtensionById(const std::string& id,
89 int include_mask) const {
90 std::string lowercase_id = base::StringToLowerASCII(id);
91 if (include_mask & ENABLED) {
92 const Extension* extension = enabled_extensions_.GetByID(lowercase_id);
93 if (extension)
94 return extension;
96 if (include_mask & DISABLED) {
97 const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
98 if (extension)
99 return extension;
101 if (include_mask & TERMINATED) {
102 const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
103 if (extension)
104 return extension;
106 if (include_mask & BLACKLISTED) {
107 const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
108 if (extension)
109 return extension;
111 return NULL;
114 bool ExtensionRegistry::AddEnabled(
115 const scoped_refptr<const Extension>& extension) {
116 return enabled_extensions_.Insert(extension);
119 bool ExtensionRegistry::RemoveEnabled(const std::string& id) {
120 return enabled_extensions_.Remove(id);
123 bool ExtensionRegistry::AddDisabled(
124 const scoped_refptr<const Extension>& extension) {
125 return disabled_extensions_.Insert(extension);
128 bool ExtensionRegistry::RemoveDisabled(const std::string& id) {
129 return disabled_extensions_.Remove(id);
132 bool ExtensionRegistry::AddTerminated(
133 const scoped_refptr<const Extension>& extension) {
134 return terminated_extensions_.Insert(extension);
137 bool ExtensionRegistry::RemoveTerminated(const std::string& id) {
138 return terminated_extensions_.Remove(id);
141 bool ExtensionRegistry::AddBlacklisted(
142 const scoped_refptr<const Extension>& extension) {
143 return blacklisted_extensions_.Insert(extension);
146 bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) {
147 return blacklisted_extensions_.Remove(id);
150 void ExtensionRegistry::ClearAll() {
151 enabled_extensions_.Clear();
152 disabled_extensions_.Clear();
153 terminated_extensions_.Clear();
154 blacklisted_extensions_.Clear();
157 void ExtensionRegistry::SetDisabledModificationCallback(
158 const ExtensionSet::ModificationCallback& callback) {
159 disabled_extensions_.set_modification_callback(callback);
162 void ExtensionRegistry::Shutdown() {
163 // Release references to all Extension objects in the sets.
164 ClearAll();
165 FOR_EACH_OBSERVER(ExtensionRegistryObserver, observers_, OnShutdown(this));
168 } // namespace extensions