Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / extensions / browser / extension_registry.cc
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
11 namespace extensions {
12
13 ExtensionRegistry::ExtensionRegistry(content::BrowserContext* browser_context)
14     : browser_context_(browser_context) {}
15 ExtensionRegistry::~ExtensionRegistry() {}
16
17 // static
18 ExtensionRegistry* ExtensionRegistry::Get(content::BrowserContext* context) {
19   return ExtensionRegistryFactory::GetForBrowserContext(context);
20 }
21
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();
30 }
31
32 void ExtensionRegistry::AddObserver(ExtensionRegistryObserver* observer) {
33   observers_.AddObserver(observer);
34 }
35
36 void ExtensionRegistry::RemoveObserver(ExtensionRegistryObserver* observer) {
37   observers_.RemoveObserver(observer);
38 }
39
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));
45 }
46
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));
54 }
55
56 const Extension* ExtensionRegistry::GetExtensionById(const std::string& id,
57                                                      int include_mask) const {
58   std::string lowercase_id = StringToLowerASCII(id);
59   if (include_mask & ENABLED) {
60     const Extension* extension = enabled_extensions_.GetByID(lowercase_id);
61     if (extension)
62       return extension;
63   }
64   if (include_mask & DISABLED) {
65     const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
66     if (extension)
67       return extension;
68   }
69   if (include_mask & TERMINATED) {
70     const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
71     if (extension)
72       return extension;
73   }
74   if (include_mask & BLACKLISTED) {
75     const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id);
76     if (extension)
77       return extension;
78   }
79   return NULL;
80 }
81
82 bool ExtensionRegistry::AddEnabled(
83     const scoped_refptr<const Extension>& extension) {
84   return enabled_extensions_.Insert(extension);
85 }
86
87 bool ExtensionRegistry::RemoveEnabled(const std::string& id) {
88   return enabled_extensions_.Remove(id);
89 }
90
91 bool ExtensionRegistry::AddDisabled(
92     const scoped_refptr<const Extension>& extension) {
93   return disabled_extensions_.Insert(extension);
94 }
95
96 bool ExtensionRegistry::RemoveDisabled(const std::string& id) {
97   return disabled_extensions_.Remove(id);
98 }
99
100 bool ExtensionRegistry::AddTerminated(
101     const scoped_refptr<const Extension>& extension) {
102   return terminated_extensions_.Insert(extension);
103 }
104
105 bool ExtensionRegistry::RemoveTerminated(const std::string& id) {
106   return terminated_extensions_.Remove(id);
107 }
108
109 bool ExtensionRegistry::AddBlacklisted(
110     const scoped_refptr<const Extension>& extension) {
111   return blacklisted_extensions_.Insert(extension);
112 }
113
114 bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) {
115   return blacklisted_extensions_.Remove(id);
116 }
117
118 void ExtensionRegistry::ClearAll() {
119   enabled_extensions_.Clear();
120   disabled_extensions_.Clear();
121   terminated_extensions_.Clear();
122   blacklisted_extensions_.Clear();
123 }
124
125 void ExtensionRegistry::SetDisabledModificationCallback(
126     const ExtensionSet::ModificationCallback& callback) {
127   disabled_extensions_.set_modification_callback(callback);
128 }
129
130 void ExtensionRegistry::Shutdown() {
131   // Release references to all Extension objects in the sets.
132   ClearAll();
133 }
134
135 }  // namespace extensions