extension_registry.cc revision 46d4c2bc3267f3f028f39e7e311b0f89aba2e4fd
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.
4
5#include "extensions/browser/extension_registry.h"
6
7#include "base/strings/string_util.h"
8#include "extensions/browser/extension_registry_factory.h"
9#include "extensions/browser/extension_registry_observer.h"
10
11namespace extensions {
12
13ExtensionRegistry::ExtensionRegistry(content::BrowserContext* browser_context)
14    : browser_context_(browser_context) {}
15ExtensionRegistry::~ExtensionRegistry() {}
16
17// static
18ExtensionRegistry* ExtensionRegistry::Get(content::BrowserContext* context) {
19  return ExtensionRegistryFactory::GetForBrowserContext(context);
20}
21
22scoped_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();
30}
31
32void ExtensionRegistry::AddObserver(ExtensionRegistryObserver* observer) {
33  observers_.AddObserver(observer);
34}
35
36void ExtensionRegistry::RemoveObserver(ExtensionRegistryObserver* observer) {
37  observers_.RemoveObserver(observer);
38}
39
40void ExtensionRegistry::TriggerOnLoaded(const Extension* extension) {
41  DCHECK(enabled_extensions_.Contains(extension->id()));
42  FOR_EACH_OBSERVER(ExtensionRegistryObserver,
43                    observers_,
44                    OnExtensionLoaded(browser_context_, extension));
45}
46
47void 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));
54}
55
56void 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));
68}
69
70void ExtensionRegistry::TriggerOnUninstalled(const Extension* extension) {
71  DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension->id()));
72  FOR_EACH_OBSERVER(ExtensionRegistryObserver,
73                    observers_,
74                    OnExtensionUninstalled(browser_context_, extension));
75}
76
77const 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);
82    if (extension)
83      return extension;
84  }
85  if (include_mask & DISABLED) {
86    const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
87    if (extension)
88      return extension;
89  }
90  if (include_mask & TERMINATED) {
91    const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
92    if (extension)
93      return extension;
94  }
95  if (include_mask & BLACKLISTED) {
96    const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
97    if (extension)
98      return extension;
99  }
100  return NULL;
101}
102
103bool ExtensionRegistry::AddEnabled(
104    const scoped_refptr<const Extension>& extension) {
105  return enabled_extensions_.Insert(extension);
106}
107
108bool ExtensionRegistry::RemoveEnabled(const std::string& id) {
109  return enabled_extensions_.Remove(id);
110}
111
112bool ExtensionRegistry::AddDisabled(
113    const scoped_refptr<const Extension>& extension) {
114  return disabled_extensions_.Insert(extension);
115}
116
117bool ExtensionRegistry::RemoveDisabled(const std::string& id) {
118  return disabled_extensions_.Remove(id);
119}
120
121bool ExtensionRegistry::AddTerminated(
122    const scoped_refptr<const Extension>& extension) {
123  return terminated_extensions_.Insert(extension);
124}
125
126bool ExtensionRegistry::RemoveTerminated(const std::string& id) {
127  return terminated_extensions_.Remove(id);
128}
129
130bool ExtensionRegistry::AddBlacklisted(
131    const scoped_refptr<const Extension>& extension) {
132  return blacklisted_extensions_.Insert(extension);
133}
134
135bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) {
136  return blacklisted_extensions_.Remove(id);
137}
138
139void ExtensionRegistry::ClearAll() {
140  enabled_extensions_.Clear();
141  disabled_extensions_.Clear();
142  terminated_extensions_.Clear();
143  blacklisted_extensions_.Clear();
144}
145
146void ExtensionRegistry::SetDisabledModificationCallback(
147    const ExtensionSet::ModificationCallback& callback) {
148  disabled_extensions_.set_modification_callback(callback);
149}
150
151void ExtensionRegistry::Shutdown() {
152  // Release references to all Extension objects in the sets.
153  ClearAll();
154  FOR_EACH_OBSERVER(ExtensionRegistryObserver, observers_, OnShutdown(this));
155}
156
157}  // namespace extensions
158