[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 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 | |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 7 | #include "base/strings/string_util.h" |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 8 | #include "extensions/browser/extension_registry_factory.h" |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 9 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 10 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 11 | namespace extensions { |
| 12 | |
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 13 | ExtensionRegistry::ExtensionRegistry(content::BrowserContext* browser_context) |
| 14 | : browser_context_(browser_context) {} |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 15 | ExtensionRegistry::~ExtensionRegistry() {} |
| 16 | |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 17 | // static |
| 18 | ExtensionRegistry* ExtensionRegistry::Get(content::BrowserContext* context) { |
| 19 | return ExtensionRegistryFactory::GetForBrowserContext(context); |
| 20 | } |
| 21 | |
[email protected] | f47f717 | 2014-03-19 19:27:10 | [diff] [blame] | 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 | |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 32 | void ExtensionRegistry::AddObserver(ExtensionRegistryObserver* observer) { |
| 33 | observers_.AddObserver(observer); |
| 34 | } |
| 35 | |
| 36 | void ExtensionRegistry::RemoveObserver(ExtensionRegistryObserver* observer) { |
| 37 | observers_.RemoveObserver(observer); |
| 38 | } |
| 39 | |
[email protected] | dcc4764 | 2014-03-26 22:03:49 | [diff] [blame] | 40 | void ExtensionRegistry::TriggerOnLoaded(const Extension* extension) { |
| 41 | DCHECK(enabled_extensions_.Contains(extension->id())); |
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 42 | FOR_EACH_OBSERVER(ExtensionRegistryObserver, |
| 43 | observers_, |
| 44 | OnExtensionLoaded(browser_context_, extension)); |
[email protected] | dcc4764 | 2014-03-26 22:03:49 | [diff] [blame] | 45 | } |
| 46 | |
[email protected] | e51232f3 | 2014-04-18 20:05:36 | [diff] [blame] | 47 | void ExtensionRegistry::TriggerOnUnloaded( |
| 48 | const Extension* extension, |
| 49 | UnloadedExtensionInfo::Reason reason) { |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 50 | DCHECK(!enabled_extensions_.Contains(extension->id())); |
[email protected] | 50703fc | 2014-04-08 04:01:06 | [diff] [blame] | 51 | FOR_EACH_OBSERVER(ExtensionRegistryObserver, |
| 52 | observers_, |
[email protected] | e51232f3 | 2014-04-18 20:05:36 | [diff] [blame] | 53 | OnExtensionUnloaded(browser_context_, extension, reason)); |
[email protected] | 45f5b7d | 2014-01-22 23:47:13 | [diff] [blame] | 54 | } |
| 55 | |
[email protected] | 17f0782 | 2014-05-22 08:45:15 | [diff] [blame^] | 56 | void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension* extension, |
| 57 | bool is_update, |
| 58 | const std::string& old_name) { |
| 59 | DCHECK(is_update == |
| 60 | GenerateInstalledExtensionsSet()->Contains(extension->id())); |
| 61 | DCHECK(is_update == !old_name.empty()); |
| 62 | FOR_EACH_OBSERVER(ExtensionRegistryObserver, |
| 63 | observers_, |
| 64 | OnExtensionWillBeInstalled( |
| 65 | browser_context_, extension, is_update, old_name)); |
| 66 | } |
| 67 | |
[email protected] | 59953980 | 2014-01-07 23:06:00 | [diff] [blame] | 68 | const Extension* ExtensionRegistry::GetExtensionById(const std::string& id, |
| 69 | int include_mask) const { |
| 70 | std::string lowercase_id = StringToLowerASCII(id); |
| 71 | if (include_mask & ENABLED) { |
| 72 | const Extension* extension = enabled_extensions_.GetByID(lowercase_id); |
| 73 | if (extension) |
| 74 | return extension; |
| 75 | } |
| 76 | if (include_mask & DISABLED) { |
| 77 | const Extension* extension = disabled_extensions_.GetByID(lowercase_id); |
| 78 | if (extension) |
| 79 | return extension; |
| 80 | } |
| 81 | if (include_mask & TERMINATED) { |
| 82 | const Extension* extension = terminated_extensions_.GetByID(lowercase_id); |
| 83 | if (extension) |
| 84 | return extension; |
| 85 | } |
| 86 | if (include_mask & BLACKLISTED) { |
| 87 | const Extension* extension = blacklisted_extensions_.GetByID(lowercase_id); |
| 88 | if (extension) |
| 89 | return extension; |
| 90 | } |
| 91 | return NULL; |
| 92 | } |
| 93 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 94 | bool ExtensionRegistry::AddEnabled( |
| 95 | const scoped_refptr<const Extension>& extension) { |
| 96 | return enabled_extensions_.Insert(extension); |
| 97 | } |
| 98 | |
| 99 | bool ExtensionRegistry::RemoveEnabled(const std::string& id) { |
| 100 | return enabled_extensions_.Remove(id); |
| 101 | } |
| 102 | |
| 103 | bool ExtensionRegistry::AddDisabled( |
| 104 | const scoped_refptr<const Extension>& extension) { |
| 105 | return disabled_extensions_.Insert(extension); |
| 106 | } |
| 107 | |
| 108 | bool ExtensionRegistry::RemoveDisabled(const std::string& id) { |
| 109 | return disabled_extensions_.Remove(id); |
| 110 | } |
| 111 | |
| 112 | bool ExtensionRegistry::AddTerminated( |
| 113 | const scoped_refptr<const Extension>& extension) { |
| 114 | return terminated_extensions_.Insert(extension); |
| 115 | } |
| 116 | |
| 117 | bool ExtensionRegistry::RemoveTerminated(const std::string& id) { |
| 118 | return terminated_extensions_.Remove(id); |
| 119 | } |
| 120 | |
| 121 | bool ExtensionRegistry::AddBlacklisted( |
| 122 | const scoped_refptr<const Extension>& extension) { |
| 123 | return blacklisted_extensions_.Insert(extension); |
| 124 | } |
| 125 | |
| 126 | bool ExtensionRegistry::RemoveBlacklisted(const std::string& id) { |
| 127 | return blacklisted_extensions_.Remove(id); |
| 128 | } |
| 129 | |
| 130 | void ExtensionRegistry::ClearAll() { |
| 131 | enabled_extensions_.Clear(); |
| 132 | disabled_extensions_.Clear(); |
| 133 | terminated_extensions_.Clear(); |
| 134 | blacklisted_extensions_.Clear(); |
| 135 | } |
| 136 | |
| 137 | void ExtensionRegistry::SetDisabledModificationCallback( |
| 138 | const ExtensionSet::ModificationCallback& callback) { |
| 139 | disabled_extensions_.set_modification_callback(callback); |
| 140 | } |
| 141 | |
[email protected] | 5fdfa56 | 2013-12-27 17:43:59 | [diff] [blame] | 142 | void ExtensionRegistry::Shutdown() { |
| 143 | // Release references to all Extension objects in the sets. |
| 144 | ClearAll(); |
| 145 | } |
| 146 | |
[email protected] | bb1bc9b3 | 2013-12-21 03:09:14 | [diff] [blame] | 147 | } // namespace extensions |