extension_registry.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. #include "extensions/browser/extension_registry.h"
  5. #include "base/observer_list.h"
  6. #include "base/strings/string_util.h"
  7. #include "extensions/browser/extension_registry_factory.h"
  8. #include "extensions/browser/extension_registry_observer.h"
  9. namespace extensions {
  10. ExtensionRegistry::ExtensionRegistry(content::BrowserContext* browser_context)
  11. : browser_context_(browser_context) {}
  12. ExtensionRegistry::~ExtensionRegistry() {}
  13. // static
  14. ExtensionRegistry* ExtensionRegistry::Get(content::BrowserContext* context) {
  15. return ExtensionRegistryFactory::GetForBrowserContext(context);
  16. }
  17. std::unique_ptr<ExtensionSet>
  18. ExtensionRegistry::GenerateInstalledExtensionsSet() const {
  19. return GenerateInstalledExtensionsSet(EVERYTHING);
  20. }
  21. std::unique_ptr<ExtensionSet> ExtensionRegistry::GenerateInstalledExtensionsSet(
  22. int include_mask) const {
  23. std::unique_ptr<ExtensionSet> installed_extensions(new ExtensionSet);
  24. if (include_mask & IncludeFlag::ENABLED)
  25. installed_extensions->InsertAll(enabled_extensions_);
  26. if (include_mask & IncludeFlag::DISABLED)
  27. installed_extensions->InsertAll(disabled_extensions_);
  28. if (include_mask & IncludeFlag::TERMINATED)
  29. installed_extensions->InsertAll(terminated_extensions_);
  30. if (include_mask & IncludeFlag::BLOCKLISTED)
  31. installed_extensions->InsertAll(blocklisted_extensions_);
  32. if (include_mask & IncludeFlag::BLOCKED)
  33. installed_extensions->InsertAll(blocked_extensions_);
  34. return installed_extensions;
  35. }
  36. base::Version ExtensionRegistry::GetStoredVersion(const ExtensionId& id) const {
  37. int include_mask = ExtensionRegistry::ENABLED | ExtensionRegistry::DISABLED |
  38. ExtensionRegistry::TERMINATED |
  39. ExtensionRegistry::BLOCKLISTED |
  40. ExtensionRegistry::BLOCKED;
  41. const Extension* registry_extension = GetExtensionById(id, include_mask);
  42. return registry_extension ? registry_extension->version() : base::Version();
  43. }
  44. void ExtensionRegistry::AddObserver(ExtensionRegistryObserver* observer) {
  45. observers_.AddObserver(observer);
  46. }
  47. void ExtensionRegistry::RemoveObserver(ExtensionRegistryObserver* observer) {
  48. observers_.RemoveObserver(observer);
  49. }
  50. void ExtensionRegistry::TriggerOnLoaded(const Extension* extension) {
  51. CHECK(extension);
  52. DCHECK(enabled_extensions_.Contains(extension->id()));
  53. for (auto& observer : observers_)
  54. observer.OnExtensionLoaded(browser_context_, extension);
  55. }
  56. void ExtensionRegistry::TriggerOnReady(const Extension* extension) {
  57. CHECK(extension);
  58. DCHECK(enabled_extensions_.Contains(extension->id()));
  59. for (auto& observer : observers_)
  60. observer.OnExtensionReady(browser_context_, extension);
  61. }
  62. void ExtensionRegistry::TriggerOnUnloaded(const Extension* extension,
  63. UnloadedExtensionReason reason) {
  64. CHECK(extension);
  65. DCHECK(!enabled_extensions_.Contains(extension->id()));
  66. for (auto& observer : observers_)
  67. observer.OnExtensionUnloaded(browser_context_, extension, reason);
  68. }
  69. void ExtensionRegistry::TriggerOnWillBeInstalled(const Extension* extension,
  70. bool is_update,
  71. const std::string& old_name) {
  72. CHECK(extension);
  73. DCHECK_EQ(is_update,
  74. GenerateInstalledExtensionsSet()->Contains(extension->id()));
  75. DCHECK_EQ(is_update, !old_name.empty());
  76. for (auto& observer : observers_)
  77. observer.OnExtensionWillBeInstalled(browser_context_, extension, is_update,
  78. old_name);
  79. }
  80. void ExtensionRegistry::TriggerOnInstalled(const Extension* extension,
  81. bool is_update) {
  82. CHECK(extension);
  83. DCHECK(GenerateInstalledExtensionsSet()->Contains(extension->id()));
  84. for (auto& observer : observers_)
  85. observer.OnExtensionInstalled(browser_context_, extension, is_update);
  86. }
  87. void ExtensionRegistry::TriggerOnUninstalled(const Extension* extension,
  88. UninstallReason reason) {
  89. CHECK(extension);
  90. DCHECK(!GenerateInstalledExtensionsSet()->Contains(extension->id()));
  91. for (auto& observer : observers_)
  92. observer.OnExtensionUninstalled(browser_context_, extension, reason);
  93. }
  94. void ExtensionRegistry::TriggerOnUninstallationDenied(
  95. const Extension* extension) {
  96. CHECK(extension);
  97. DCHECK(GenerateInstalledExtensionsSet()->Contains(extension->id()));
  98. for (auto& observer : observers_)
  99. observer.OnExtensionUninstallationDenied(browser_context_, extension);
  100. }
  101. const Extension* ExtensionRegistry::GetExtensionById(const std::string& id,
  102. int include_mask) const {
  103. std::string lowercase_id = base::ToLowerASCII(id);
  104. if (include_mask & ENABLED) {
  105. const Extension* extension = enabled_extensions_.GetByID(lowercase_id);
  106. if (extension)
  107. return extension;
  108. }
  109. if (include_mask & DISABLED) {
  110. const Extension* extension = disabled_extensions_.GetByID(lowercase_id);
  111. if (extension)
  112. return extension;
  113. }
  114. if (include_mask & TERMINATED) {
  115. const Extension* extension = terminated_extensions_.GetByID(lowercase_id);
  116. if (extension)
  117. return extension;
  118. }
  119. if (include_mask & BLOCKLISTED) {
  120. const Extension* extension = blocklisted_extensions_.GetByID(lowercase_id);
  121. if (extension)
  122. return extension;
  123. }
  124. if (include_mask & BLOCKED) {
  125. const Extension* extension = blocked_extensions_.GetByID(lowercase_id);
  126. if (extension)
  127. return extension;
  128. }
  129. return NULL;
  130. }
  131. const Extension* ExtensionRegistry::GetInstalledExtension(
  132. const std::string& id) const {
  133. return GetExtensionById(id, ExtensionRegistry::EVERYTHING);
  134. }
  135. bool ExtensionRegistry::AddEnabled(
  136. const scoped_refptr<const Extension>& extension) {
  137. return enabled_extensions_.Insert(extension);
  138. }
  139. bool ExtensionRegistry::RemoveEnabled(const std::string& id) {
  140. // Only enabled extensions can be ready, so removing an enabled extension
  141. // should also remove from the ready set if possible.
  142. if (ready_extensions_.Contains(id))
  143. RemoveReady(id);
  144. return enabled_extensions_.Remove(id);
  145. }
  146. bool ExtensionRegistry::AddDisabled(
  147. const scoped_refptr<const Extension>& extension) {
  148. return disabled_extensions_.Insert(extension);
  149. }
  150. bool ExtensionRegistry::RemoveDisabled(const std::string& id) {
  151. return disabled_extensions_.Remove(id);
  152. }
  153. bool ExtensionRegistry::AddTerminated(
  154. const scoped_refptr<const Extension>& extension) {
  155. return terminated_extensions_.Insert(extension);
  156. }
  157. bool ExtensionRegistry::RemoveTerminated(const std::string& id) {
  158. return terminated_extensions_.Remove(id);
  159. }
  160. bool ExtensionRegistry::AddBlocklisted(
  161. const scoped_refptr<const Extension>& extension) {
  162. return blocklisted_extensions_.Insert(extension);
  163. }
  164. bool ExtensionRegistry::RemoveBlocklisted(const std::string& id) {
  165. return blocklisted_extensions_.Remove(id);
  166. }
  167. bool ExtensionRegistry::AddBlocked(
  168. const scoped_refptr<const Extension>& extension) {
  169. return blocked_extensions_.Insert(extension);
  170. }
  171. bool ExtensionRegistry::RemoveBlocked(const std::string& id) {
  172. return blocked_extensions_.Remove(id);
  173. }
  174. bool ExtensionRegistry::AddReady(
  175. const scoped_refptr<const Extension>& extension) {
  176. return ready_extensions_.Insert(extension);
  177. }
  178. bool ExtensionRegistry::RemoveReady(const std::string& id) {
  179. return ready_extensions_.Remove(id);
  180. }
  181. void ExtensionRegistry::ClearAll() {
  182. enabled_extensions_.Clear();
  183. disabled_extensions_.Clear();
  184. terminated_extensions_.Clear();
  185. blocklisted_extensions_.Clear();
  186. blocked_extensions_.Clear();
  187. ready_extensions_.Clear();
  188. }
  189. void ExtensionRegistry::Shutdown() {
  190. // Release references to all Extension objects in the sets.
  191. ClearAll();
  192. for (auto& observer : observers_)
  193. observer.OnShutdown(this);
  194. }
  195. } // namespace extensions