renderer_extension_registry.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // Copyright 2015 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/renderer/renderer_extension_registry.h"
  5. #include "base/check.h"
  6. #include "base/lazy_instance.h"
  7. #include "content/public/renderer/render_thread.h"
  8. #include "extensions/common/manifest_handlers/background_info.h"
  9. namespace extensions {
  10. namespace {
  11. base::LazyInstance<RendererExtensionRegistry>::DestructorAtExit
  12. g_renderer_extension_registry = LAZY_INSTANCE_INITIALIZER;
  13. } // namespace
  14. RendererExtensionRegistry::RendererExtensionRegistry() {}
  15. RendererExtensionRegistry::~RendererExtensionRegistry() {}
  16. // static
  17. RendererExtensionRegistry* RendererExtensionRegistry::Get() {
  18. return g_renderer_extension_registry.Pointer();
  19. }
  20. const ExtensionSet* RendererExtensionRegistry::GetMainThreadExtensionSet()
  21. const {
  22. // This can only be modified on the RenderThread, because
  23. // GetMainThreadExtensionSet is inherently thread unsafe.
  24. // Enforcing single-thread modification at least mitigates this.
  25. // TODO(annekao): Remove this restriction once GetMainThreadExtensionSet is
  26. // fixed.
  27. DCHECK(content::RenderThread::Get());
  28. base::AutoLock lock(lock_);
  29. return &extensions_;
  30. }
  31. size_t RendererExtensionRegistry::size() const {
  32. base::AutoLock lock(lock_);
  33. return extensions_.size();
  34. }
  35. bool RendererExtensionRegistry::is_empty() const {
  36. base::AutoLock lock(lock_);
  37. return extensions_.is_empty();
  38. }
  39. bool RendererExtensionRegistry::Contains(
  40. const std::string& extension_id) const {
  41. base::AutoLock lock(lock_);
  42. return extensions_.Contains(extension_id);
  43. }
  44. bool RendererExtensionRegistry::Insert(
  45. const scoped_refptr<const Extension>& extension) {
  46. DCHECK(content::RenderThread::Get());
  47. base::AutoLock lock(lock_);
  48. return extensions_.Insert(extension);
  49. }
  50. bool RendererExtensionRegistry::Remove(const std::string& id) {
  51. DCHECK(content::RenderThread::Get());
  52. base::AutoLock lock(lock_);
  53. return extensions_.Remove(id);
  54. }
  55. std::string RendererExtensionRegistry::GetExtensionOrAppIDByURL(
  56. const GURL& url) const {
  57. base::AutoLock lock(lock_);
  58. return extensions_.GetExtensionOrAppIDByURL(url);
  59. }
  60. const Extension* RendererExtensionRegistry::GetExtensionOrAppByURL(
  61. const GURL& url,
  62. bool include_guid) const {
  63. base::AutoLock lock(lock_);
  64. return extensions_.GetExtensionOrAppByURL(url, include_guid);
  65. }
  66. const Extension* RendererExtensionRegistry::GetHostedAppByURL(
  67. const GURL& url) const {
  68. base::AutoLock lock(lock_);
  69. return extensions_.GetHostedAppByURL(url);
  70. }
  71. const Extension* RendererExtensionRegistry::GetByID(
  72. const std::string& id) const {
  73. base::AutoLock lock(lock_);
  74. return extensions_.GetByID(id);
  75. }
  76. ExtensionIdSet RendererExtensionRegistry::GetIDs() const {
  77. base::AutoLock lock(lock_);
  78. return extensions_.GetIDs();
  79. }
  80. bool RendererExtensionRegistry::ExtensionBindingsAllowed(
  81. const GURL& url) const {
  82. base::AutoLock lock(lock_);
  83. return extensions_.ExtensionBindingsAllowed(url);
  84. }
  85. void RendererExtensionRegistry::SetWorkerActivationSequence(
  86. const scoped_refptr<const Extension>& extension,
  87. ActivationSequence worker_activation_sequence) {
  88. DCHECK(content::RenderThread::Get());
  89. DCHECK(Contains(extension->id()));
  90. DCHECK(BackgroundInfo::IsServiceWorkerBased(extension.get()));
  91. base::AutoLock lock(lock_);
  92. worker_activation_sequences_[extension->id()] = worker_activation_sequence;
  93. }
  94. absl::optional<ActivationSequence>
  95. RendererExtensionRegistry::GetWorkerActivationSequence(
  96. const ExtensionId& extension_id) const {
  97. base::AutoLock lock(lock_);
  98. auto iter = worker_activation_sequences_.find(extension_id);
  99. if (iter == worker_activation_sequences_.end())
  100. return absl::nullopt;
  101. return iter->second;
  102. }
  103. } // namespace extensions