summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/svg/svg_tree_scope_resources.cc
blob: 5feb50931ff03205bd3b54cb59d2339295326963 (plain)
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
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/core/svg/svg_tree_scope_resources.h"

#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/tree_scope.h"
#include "third_party/blink/renderer/core/svg/svg_resource.h"
#include "third_party/blink/renderer/platform/wtf/text/atomic_string.h"

namespace blink {

SVGTreeScopeResources::SVGTreeScopeResources(TreeScope* tree_scope)
    : tree_scope_(tree_scope) {}

LocalSVGResource* SVGTreeScopeResources::ResourceForId(const AtomicString& id) {
  if (id.IsEmpty())
    return nullptr;
  auto& entry = resources_.insert(id, nullptr).stored_value->value;
  if (!entry)
    entry = MakeGarbageCollected<LocalSVGResource>(*tree_scope_, id);
  return entry;
}

LocalSVGResource* SVGTreeScopeResources::ExistingResourceForId(
    const AtomicString& id) const {
  if (id.IsEmpty())
    return nullptr;
  return resources_.at(id);
}

void SVGTreeScopeResources::ProcessCustomWeakness(const LivenessBroker& info) {
  // Unregister and remove any resources that are no longer alive.
  Vector<AtomicString> to_remove;
  for (auto& resource_entry : resources_) {
    if (info.IsHeapObjectAlive(resource_entry.value))
      continue;
    resource_entry.value->Unregister();
    to_remove.push_back(resource_entry.key);
  }
  resources_.RemoveAll(to_remove);
}

void SVGTreeScopeResources::Trace(Visitor* visitor) const {
  visitor->template RegisterWeakCallbackMethod<
      SVGTreeScopeResources, &SVGTreeScopeResources::ProcessCustomWeakness>(
      this);
  visitor->Trace(resources_);
  visitor->Trace(tree_scope_);
}

}  // namespace blink