blob: 946f671834d1c46e5030bd4603bdcb89da88c560 [file] [log] [blame]
annekao6572d5c2015-08-19 16:13:361// 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
5#include "extensions/renderer/renderer_extension_registry.h"
6
Hans Wennborg09979592020-04-27 12:34:307#include "base/check.h"
annekao6572d5c2015-08-19 16:13:368#include "base/lazy_instance.h"
annekao6572d5c2015-08-19 16:13:369#include "content/public/renderer/render_thread.h"
Istiaque Ahmedc92b1ea2019-12-31 00:32:4910#include "extensions/common/manifest_handlers/background_info.h"
annekao6572d5c2015-08-19 16:13:3611
12namespace extensions {
13
14namespace {
15
scottmg5e65e3a2017-03-08 08:48:4616base::LazyInstance<RendererExtensionRegistry>::DestructorAtExit
17 g_renderer_extension_registry = LAZY_INSTANCE_INITIALIZER;
annekao6572d5c2015-08-19 16:13:3618
19} // namespace
20
21RendererExtensionRegistry::RendererExtensionRegistry() {}
22
23RendererExtensionRegistry::~RendererExtensionRegistry() {}
24
25// static
26RendererExtensionRegistry* RendererExtensionRegistry::Get() {
27 return g_renderer_extension_registry.Pointer();
28}
29
30const ExtensionSet* RendererExtensionRegistry::GetMainThreadExtensionSet()
31 const {
32 // This can only be modified on the RenderThread, because
33 // GetMainThreadExtensionSet is inherently thread unsafe.
34 // Enforcing single-thread modification at least mitigates this.
35 // TODO(annekao): Remove this restriction once GetMainThreadExtensionSet is
36 // fixed.
37 DCHECK(content::RenderThread::Get());
38 base::AutoLock lock(lock_);
39 return &extensions_;
40}
41
42size_t RendererExtensionRegistry::size() const {
43 base::AutoLock lock(lock_);
44 return extensions_.size();
45}
46
47bool RendererExtensionRegistry::is_empty() const {
48 base::AutoLock lock(lock_);
49 return extensions_.is_empty();
50}
51
52bool RendererExtensionRegistry::Contains(
53 const std::string& extension_id) const {
54 base::AutoLock lock(lock_);
55 return extensions_.Contains(extension_id);
56}
57
58bool RendererExtensionRegistry::Insert(
59 const scoped_refptr<const Extension>& extension) {
60 DCHECK(content::RenderThread::Get());
61 base::AutoLock lock(lock_);
62 return extensions_.Insert(extension);
63}
64
65bool RendererExtensionRegistry::Remove(const std::string& id) {
66 DCHECK(content::RenderThread::Get());
67 base::AutoLock lock(lock_);
68 return extensions_.Remove(id);
69}
70
71std::string RendererExtensionRegistry::GetExtensionOrAppIDByURL(
72 const GURL& url) const {
73 base::AutoLock lock(lock_);
74 return extensions_.GetExtensionOrAppIDByURL(url);
75}
76
77const Extension* RendererExtensionRegistry::GetExtensionOrAppByURL(
78 const GURL& url) const {
79 base::AutoLock lock(lock_);
80 return extensions_.GetExtensionOrAppByURL(url);
81}
82
83const Extension* RendererExtensionRegistry::GetHostedAppByURL(
84 const GURL& url) const {
85 base::AutoLock lock(lock_);
86 return extensions_.GetHostedAppByURL(url);
87}
88
89const Extension* RendererExtensionRegistry::GetByID(
90 const std::string& id) const {
91 base::AutoLock lock(lock_);
92 return extensions_.GetByID(id);
93}
94
95ExtensionIdSet RendererExtensionRegistry::GetIDs() const {
96 base::AutoLock lock(lock_);
97 return extensions_.GetIDs();
98}
99
100bool RendererExtensionRegistry::ExtensionBindingsAllowed(
101 const GURL& url) const {
102 base::AutoLock lock(lock_);
103 return extensions_.ExtensionBindingsAllowed(url);
104}
105
Istiaque Ahmedc92b1ea2019-12-31 00:32:49106void RendererExtensionRegistry::SetWorkerActivationSequence(
107 const scoped_refptr<const Extension>& extension,
Istiaque Ahmed9987ca892020-01-09 22:47:17108 ActivationSequence worker_activation_sequence) {
Istiaque Ahmedc92b1ea2019-12-31 00:32:49109 DCHECK(content::RenderThread::Get());
110 DCHECK(Contains(extension->id()));
111 DCHECK(BackgroundInfo::IsServiceWorkerBased(extension.get()));
112
113 base::AutoLock lock(lock_);
114 worker_activation_sequences_[extension->id()] = worker_activation_sequence;
115}
116
Istiaque Ahmed9987ca892020-01-09 22:47:17117base::Optional<ActivationSequence>
118RendererExtensionRegistry::GetWorkerActivationSequence(
Istiaque Ahmedc92b1ea2019-12-31 00:32:49119 const ExtensionId& extension_id) const {
120 base::AutoLock lock(lock_);
121 auto iter = worker_activation_sequences_.find(extension_id);
122 if (iter == worker_activation_sequences_.end())
123 return base::nullopt;
124 return iter->second;
125}
126
annekao6572d5c2015-08-19 16:13:36127} // namespace extensions