File: svg_resource.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (382 lines) | stat: -rw-r--r-- 13,380 bytes parent folder | download | duplicates (4)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// Copyright 2018 The Chromium Authors
// 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_resource.h"

#include "services/network/public/mojom/content_security_policy.mojom-blink.h"
#include "third_party/blink/renderer/core/dom/document.h"
#include "third_party/blink/renderer/core/dom/element.h"
#include "third_party/blink/renderer/core/dom/id_target_observer.h"
#include "third_party/blink/renderer/core/dom/tree_scope.h"
#include "third_party/blink/renderer/core/execution_context/execution_context.h"
#include "third_party/blink/renderer/core/layout/svg/layout_svg_resource_container.h"
#include "third_party/blink/renderer/core/loader/resource/image_resource_content.h"
#include "third_party/blink/renderer/core/svg/graphics/svg_image.h"
#include "third_party/blink/renderer/core/svg/svg_resource_client.h"
#include "third_party/blink/renderer/core/svg/svg_resource_document_content.h"
#include "third_party/blink/renderer/core/svg/svg_uri_reference.h"
#include "third_party/blink/renderer/platform/bindings/script_forbidden_scope.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_initiator_type_names.h"
#include "third_party/blink/renderer/platform/loader/fetch/fetch_parameters.h"
#include "third_party/blink/renderer/platform/loader/fetch/resource.h"

namespace blink {

SVGResource::SVGResource() = default;

SVGResource::~SVGResource() = default;

void SVGResource::Trace(Visitor* visitor) const {
  visitor->Trace(target_);
  visitor->Trace(clients_);
  visitor->Trace(observer_wrappers_);
}

void SVGResource::AddClient(SVGResourceClient& client) {
  auto& entry = clients_.insert(&client, ClientEntry()).stored_value->value;
  entry.count++;
  entry.cached_cycle_check = kNeedCheck;
  if (LayoutSVGResourceContainer* container = ResourceContainerNoCycleCheck())
    container->ClearInvalidationMask();
}

void SVGResource::RemoveClient(SVGResourceClient& client) {
  auto it = clients_.find(&client);
  CHECK_NE(it, clients_.end());
  it->value.count--;
  if (it->value.count)
    return;
  clients_.erase(it);
  // The last instance of |client| was removed. Clear its entry in
  // resource's cache.
  if (LayoutSVGResourceContainer* container = ResourceContainerNoCycleCheck())
    container->RemoveClientFromCache(client);
}

class SVGResource::ImageResourceObserverWrapper
    : public GarbageCollected<SVGResource::ImageResourceObserverWrapper>,
      public SVGResourceClient {
 public:
  explicit ImageResourceObserverWrapper(ImageResourceObserver& observer)
      : observer_(observer) {}

  void IncRef() { count_++; }
  bool DecRef() {
    --count_;
    return count_ == 0;
  }

  void Trace(Visitor* visitor) const override {
    visitor->Trace(observer_);
    SVGResourceClient::Trace(visitor);
  }

 private:
  void ResourceContentChanged(SVGResource* resource) override {
    observer_->ImageChanged(static_cast<WrappedImagePtr>(resource),
                            ImageResourceObserver::CanDeferInvalidation::kNo);
  }

  Member<ImageResourceObserver> observer_;
  int count_ = 0;
};

void SVGResource::AddObserver(ImageResourceObserver& observer) {
  auto& wrapper =
      observer_wrappers_.insert(&observer, nullptr).stored_value->value;
  if (!wrapper) {
    wrapper = MakeGarbageCollected<ImageResourceObserverWrapper>(observer);
    AddClient(*wrapper);
  }
  wrapper->IncRef();
}

void SVGResource::RemoveObserver(ImageResourceObserver& observer) {
  auto it = observer_wrappers_.find(&observer);
  CHECK_NE(it, observer_wrappers_.end());
  if (it->value->DecRef()) {
    RemoveClient(*it->value);
    observer_wrappers_.erase(it);
  }
}

SVGResourceClient* SVGResource::GetObserverResourceClient(
    ImageResourceObserver& observer) {
  auto it = observer_wrappers_.find(&observer);
  return it != observer_wrappers_.end() ? it->value : nullptr;
}

void SVGResource::InvalidateCycleCache() {
  for (auto& client_entry : clients_.Values())
    client_entry.cached_cycle_check = kNeedCheck;
}

void SVGResource::NotifyContentChanged() {
  InvalidateCycleCache();

  HeapVector<Member<SVGResourceClient>> clients(clients_.Keys());
  for (SVGResourceClient* client : clients)
    client->ResourceContentChanged(this);
}

LayoutSVGResourceContainer* SVGResource::ResourceContainerNoCycleCheck() const {
  if (!target_)
    return nullptr;
  return DynamicTo<LayoutSVGResourceContainer>(target_->GetLayoutObject());
}

LayoutSVGResourceContainer* SVGResource::ResourceContainer(
    SVGResourceClient& client) const {
  auto it = clients_.find(&client);
  if (it == clients_.end())
    return nullptr;
  auto* container = ResourceContainerNoCycleCheck();
  if (!container)
    return nullptr;
  ClientEntry& entry = it->value;
  if (entry.cached_cycle_check == kNeedCheck) {
    entry.cached_cycle_check = kPerformingCheck;
    bool has_cycle = container->FindCycle();
    DCHECK_EQ(entry.cached_cycle_check, kPerformingCheck);
    entry.cached_cycle_check = has_cycle ? kHasCycle : kNoCycle;
  }
  if (entry.cached_cycle_check == kHasCycle)
    return nullptr;
  DCHECK_EQ(entry.cached_cycle_check, kNoCycle);
  return container;
}

bool SVGResource::FindCycle(SVGResourceClient& client) const {
  auto it = clients_.find(&client);
  if (it == clients_.end())
    return false;
  auto* container = ResourceContainerNoCycleCheck();
  if (!container)
    return false;
  ClientEntry& entry = it->value;
  switch (entry.cached_cycle_check) {
    case kNeedCheck: {
      entry.cached_cycle_check = kPerformingCheck;
      bool has_cycle = container->FindCycle();
      DCHECK_EQ(entry.cached_cycle_check, kPerformingCheck);
      // Update our cached state based on the result of FindCycle(), but don't
      // signal a cycle since ResourceContainer() will consider the resource
      // invalid if one is present, thus we break the cycle at this resource.
      entry.cached_cycle_check = has_cycle ? kHasCycle : kNoCycle;
      return false;
    }
    case kNoCycle: {
      entry.cached_cycle_check = kPerformingCheck;
      bool has_cycle = container->FindCycle();
      DCHECK_EQ(entry.cached_cycle_check, kPerformingCheck);
      entry.cached_cycle_check = kNoCycle;
      return has_cycle;
    }
    case kPerformingCheck:
      // If we're on the current checking path, signal a cycle.
      return true;
    case kHasCycle:
      // We have a cached result, but don't signal a cycle since
      // ResourceContainer() will consider the resource invalid if one is
      // present.
      return false;
  }
}

LocalSVGResource::LocalSVGResource(TreeScope& tree_scope,
                                   const AtomicString& id)
    : tree_scope_(tree_scope) {
  target_ = SVGURIReference::ObserveTarget(
      id_observer_, tree_scope, id,
      WTF::BindRepeating(&LocalSVGResource::TargetChanged,
                         WrapWeakPersistent(this), id));
}

void LocalSVGResource::Unregister() {
  SVGURIReference::UnobserveTarget(id_observer_);
}

void LocalSVGResource::NotifyFilterPrimitiveChanged(
    SVGFilterPrimitiveStandardAttributes& primitive,
    const QualifiedName& attribute) {
  HeapVector<Member<SVGResourceClient>> clients(clients_.Keys());
  for (SVGResourceClient* client : clients)
    client->FilterPrimitiveChanged(this, primitive, attribute);
}

void LocalSVGResource::TargetChanged(const AtomicString& id) {
  Element* new_target = tree_scope_->getElementById(id);
  if (new_target == target_)
    return;
  // Clear out caches on the old resource, and then notify clients about the
  // change.
  LayoutSVGResourceContainer* old_resource = ResourceContainerNoCycleCheck();
  if (old_resource)
    old_resource->RemoveAllClientsFromCache();
  target_ = new_target;
  NotifyContentChanged();
}

void LocalSVGResource::Trace(Visitor* visitor) const {
  visitor->Trace(tree_scope_);
  visitor->Trace(id_observer_);
  SVGResource::Trace(visitor);
}

ExternalSVGResourceDocumentContent::ExternalSVGResourceDocumentContent(
    const KURL& url)
    : url_(url) {}

void ExternalSVGResourceDocumentContent::Load(
    Document& document,
    CrossOriginAttributeValue cross_origin) {
  if (document_content_)
    return;
  // Loading SVG resources should not trigger script, see
  // https://crbug.com/1196853 This could be allowed if DOMContentLoaded and
  // other checkpoints were asynchronous per https://crbug.com/961428
  ScriptForbiddenScope forbid_script;
  ExecutionContext* execution_context = document.GetExecutionContext();
  ResourceLoaderOptions options(execution_context->GetCurrentWorld());
  options.initiator_info.name = fetch_initiator_type_names::kCSS;
  FetchParameters params(ResourceRequest(url_), options);
  if (cross_origin == kCrossOriginAttributeNotSet) {
    params.MutableResourceRequest().SetMode(
        network::mojom::blink::RequestMode::kSameOrigin);
  } else {
    params.SetCrossOriginAccessControl(execution_context->GetSecurityOrigin(),
                                       cross_origin);
  }
  document_content_ = SVGResourceDocumentContent::Fetch(params, document);
  if (!document_content_) {
    return;
  }
  document_content_->AddObserver(this);
  target_ = ResolveTarget();
}

void ExternalSVGResourceDocumentContent::LoadWithoutCSP(Document& document) {
  if (document_content_)
    return;
  // Loading SVG resources should not trigger script, see
  // https://crbug.com/1196853 This could be allowed if DOMContentLoaded and
  // other checkpoints were asynchronous per https://crbug.com/961428
  ScriptForbiddenScope forbid_script;
  ExecutionContext* execution_context = document.GetExecutionContext();
  ResourceLoaderOptions options(execution_context->GetCurrentWorld());
  options.initiator_info.name = fetch_initiator_type_names::kCSS;
  FetchParameters params(ResourceRequest(url_), options);
  params.SetContentSecurityCheck(
      network::mojom::blink::CSPDisposition::DO_NOT_CHECK);
  params.MutableResourceRequest().SetMode(
      network::mojom::blink::RequestMode::kSameOrigin);
  document_content_ = SVGResourceDocumentContent::Fetch(params, document);
  if (!document_content_) {
    return;
  }
  document_content_->AddObserver(this);
  target_ = ResolveTarget();
}

void ExternalSVGResourceDocumentContent::ResourceNotifyFinished(
    SVGResourceDocumentContent* document_content) {
  DCHECK_EQ(document_content_, document_content);
  Element* new_target = ResolveTarget();
  // If no target was found when resolving in Load(), we want to notify clients
  // regardless of if a target was found or not, to be able to update rendering
  // based on loading state.
  if (target_ && new_target == target_) {
    return;
  }
  target_ = new_target;
  NotifyContentChanged();
}

void ExternalSVGResourceDocumentContent::ResourceContentChanged(
    SVGResourceDocumentContent* document_content) {
  DCHECK_EQ(document_content_, document_content);
  if (!target_) {
    return;
  }
  NotifyContentChanged();
}

bool ExternalSVGResourceDocumentContent::IsLoading() const {
  return !document_content_ || document_content_->IsLoading();
}

Element* ExternalSVGResourceDocumentContent::ResolveTarget() {
  if (!document_content_)
    return nullptr;
  if (!url_.HasFragmentIdentifier())
    return nullptr;
  Document* external_document = document_content_->GetDocument();
  if (!external_document)
    return nullptr;
  AtomicString decoded_fragment(DecodeURLEscapeSequences(
      url_.FragmentIdentifier(), DecodeURLMode::kUTF8OrIsomorphic));
  return external_document->getElementById(decoded_fragment);
}

void ExternalSVGResourceDocumentContent::Trace(Visitor* visitor) const {
  visitor->Trace(document_content_);
  SVGResource::Trace(visitor);
}

ExternalSVGResourceImageContent::ExternalSVGResourceImageContent(
    ImageResourceContent* image_content,
    const AtomicString& fragment)
    : image_content_(image_content), fragment_(fragment) {
  image_content_->AddObserver(this);
}

void ExternalSVGResourceImageContent::Prefinalize() {
  image_content_->DidRemoveObserver();
  image_content_ = nullptr;
}

bool ExternalSVGResourceImageContent::IsLoading() const {
  return image_content_->IsLoading();
}

Element* ExternalSVGResourceImageContent::ResolveTarget() {
  if (!image_content_->IsLoaded() || image_content_->ErrorOccurred()) {
    return nullptr;
  }
  if (!fragment_) {
    return nullptr;
  }
  auto* svg_image = DynamicTo<SVGImage>(image_content_->GetImage());
  if (!svg_image) {
    return nullptr;
  }
  AtomicString decoded_fragment(
      DecodeURLEscapeSequences(fragment_, DecodeURLMode::kUTF8OrIsomorphic));
  return svg_image->GetResourceElement(decoded_fragment);
}

void ExternalSVGResourceImageContent::ImageNotifyFinished(
    ImageResourceContent*) {
  Element* new_target = ResolveTarget();
  // If no target was found when resolving in Load(), we want to notify clients
  // regardless of if a target was found or not, to be able to update rendering
  // based on loading state.
  if (target_ && new_target == target_) {
    return;
  }
  target_ = new_target;
  NotifyContentChanged();
}

String ExternalSVGResourceImageContent::DebugName() const {
  return "ExternalSVGResourceImageContent";
}

void ExternalSVGResourceImageContent::Trace(Visitor* visitor) const {
  visitor->Trace(image_content_);
  SVGResource::Trace(visitor);
  ImageResourceObserver::Trace(visitor);
}

}  // namespace blink