File: renderer_extension_registry.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (146 lines) | stat: -rw-r--r-- 4,877 bytes parent folder | download | duplicates (9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/renderer_extension_registry.h"

#include "base/check.h"
#include "base/containers/contains.h"
#include "base/lazy_instance.h"
#include "base/unguessable_token.h"
#include "content/public/renderer/render_thread.h"
#include "extensions/common/extension_id.h"
#include "extensions/common/manifest_handlers/background_info.h"
#include "extensions/common/manifest_handlers/incognito_info.h"
#include "extensions/renderer/extensions_renderer_client.h"

namespace extensions {

namespace {

base::LazyInstance<RendererExtensionRegistry>::DestructorAtExit
    g_renderer_extension_registry = LAZY_INSTANCE_INITIALIZER;

}  // namespace

RendererExtensionRegistry::RendererExtensionRegistry() = default;

RendererExtensionRegistry::~RendererExtensionRegistry() = default;

// static
RendererExtensionRegistry* RendererExtensionRegistry::Get() {
  return g_renderer_extension_registry.Pointer();
}

const ExtensionSet* RendererExtensionRegistry::GetMainThreadExtensionSet()
    const {
  // This can only be modified on the RenderThread, because
  // GetMainThreadExtensionSet is inherently thread unsafe.
  // Enforcing single-thread modification at least mitigates this.
  // TODO(annekao): Remove this restriction once GetMainThreadExtensionSet is
  // fixed.
  DCHECK(content::RenderThread::Get());
  base::AutoLock lock(lock_);
  return &extensions_;
}

bool RendererExtensionRegistry::Contains(
    const ExtensionId& extension_id) const {
  base::AutoLock lock(lock_);
  return extensions_.Contains(extension_id);
}

bool RendererExtensionRegistry::Insert(
    const scoped_refptr<const Extension>& extension) {
  DCHECK(content::RenderThread::Get());
  base::AutoLock lock(lock_);

  if (!BackgroundInfo::IsServiceWorkerBased(extension.get())) {
    // Non-SW based extension should never have an activation token.
    CHECK(!base::Contains(worker_activation_tokens_, extension->id()));
    return extensions_.Insert(extension);
  }

  ExtensionsRendererClient* client = ExtensionsRendererClient::Get();

  // SW based extensions should always have an activation token, except for
  // incognito processes for a spanning mode extension. The CHECK() for all
  // other worker based extension is performed in
  // Dispatcher::WillEvaluateServiceWorkerOnWorkerThread(). We can't CHECK() for
  // IsIncognitoProcess() == false here because this may be called on renderer
  // process initialization before the boolean for that has been set.
  bool is_incognito_spanning = client->IsIncognitoProcess() &&
                               IncognitoInfo::IsSpanningMode(extension.get());
  if (is_incognito_spanning) {
    CHECK(!base::Contains(worker_activation_tokens_, extension->id()));
  }

  return extensions_.Insert(extension);
}

bool RendererExtensionRegistry::Remove(const ExtensionId& id) {
  DCHECK(content::RenderThread::Get());
  base::AutoLock lock(lock_);
  worker_activation_tokens_.erase(id);
  return extensions_.Remove(id);
}

ExtensionId RendererExtensionRegistry::GetExtensionOrAppIDByURL(
    const GURL& url) const {
  base::AutoLock lock(lock_);
  return extensions_.GetExtensionOrAppIDByURL(url);
}

const Extension* RendererExtensionRegistry::GetExtensionOrAppByURL(
    const GURL& url,
    bool include_guid) const {
  base::AutoLock lock(lock_);
  return extensions_.GetExtensionOrAppByURL(url, include_guid);
}

const Extension* RendererExtensionRegistry::GetHostedAppByURL(
    const GURL& url) const {
  base::AutoLock lock(lock_);
  return extensions_.GetHostedAppByURL(url);
}

const Extension* RendererExtensionRegistry::GetByID(
    const ExtensionId& id) const {
  base::AutoLock lock(lock_);
  return extensions_.GetByID(id);
}

ExtensionIdSet RendererExtensionRegistry::GetIDs() const {
  base::AutoLock lock(lock_);
  return extensions_.GetIDs();
}

bool RendererExtensionRegistry::ExtensionBindingsAllowed(
    const GURL& url) const {
  base::AutoLock lock(lock_);
  return extensions_.ExtensionBindingsAllowed(url);
}

void RendererExtensionRegistry::SetWorkerActivationToken(
    const scoped_refptr<const Extension>& extension,
    base::UnguessableToken worker_activation_token) {
  DCHECK(content::RenderThread::Get());
  DCHECK(BackgroundInfo::IsServiceWorkerBased(extension.get()));

  base::AutoLock lock(lock_);
  worker_activation_tokens_[extension->id()] =
      std::move(worker_activation_token);
}

std::optional<base::UnguessableToken>
RendererExtensionRegistry::GetWorkerActivationToken(
    const ExtensionId& extension_id) const {
  base::AutoLock lock(lock_);
  auto iter = worker_activation_tokens_.find(extension_id);
  if (iter == worker_activation_tokens_.end()) {
    return std::nullopt;
  }
  return iter->second;
}

}  // namespace extensions