summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/core/loader/worker_resource_timing_notifier_impl.h
blob: 8ab07a2018026c18a5d59cb2059d2fa46669b8f3 (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
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
// Copyright 2019 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.

#ifndef THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_WORKER_RESOURCE_TIMING_NOTIFIER_IMPL_H_
#define THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_WORKER_RESOURCE_TIMING_NOTIFIER_IMPL_H_

#include "base/memory/scoped_refptr.h"
#include "base/single_thread_task_runner.h"
#include "third_party/blink/public/mojom/timing/worker_timing_container.mojom-blink.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "third_party/blink/renderer/platform/heap/handle.h"
#include "third_party/blink/renderer/platform/heap/persistent.h"
#include "third_party/blink/renderer/platform/loader/fetch/worker_resource_timing_notifier.h"

namespace blink {

class ExecutionContext;

// The implementation of WorkerResourceTimingNotifier that dispatches resource
// timing info to an execution context which is associated with the instance of
// this class. Thread safety: For the ctor and dtor, these must be called on the
// sequence of the execution context. For AddResourceTiming(), it can be called
// on a different sequence from the sequence of the execution context. In that
// case, this creates a copy of the given resource timing and passes it to the
// execution context's sequence via PostCrossThreadTask.
class CORE_EXPORT WorkerResourceTimingNotifierImpl final
    : public WorkerResourceTimingNotifier {
 public:
  static WorkerResourceTimingNotifierImpl* CreateForInsideResourceFetcher(
      ExecutionContext&);
  static WorkerResourceTimingNotifierImpl* CreateForOutsideResourceFetcher(
      ExecutionContext&);

  // Do not call this. Use static creation function instead. This is public
  // only for MakeGarbageCollected.
  explicit WorkerResourceTimingNotifierImpl(
      scoped_refptr<base::SingleThreadTaskRunner> task_runner);
  ~WorkerResourceTimingNotifierImpl() override = default;

  void AddResourceTiming(
      const WebResourceTimingInfo&,
      const AtomicString& initiator_type,
      mojo::PendingReceiver<mojom::blink::WorkerTimingContainer>
          worker_timing_receiver) override;

  void Trace(Visitor*) override;

 private:
  void AddCrossThreadResourceTiming(
      const WebResourceTimingInfo&,
      const String& initiator_type,
      mojo::PendingReceiver<mojom::blink::WorkerTimingContainer>
          worker_timing_receiver);

  scoped_refptr<base::SingleThreadTaskRunner> task_runner_;

  // Used when the execution context lives on the same sequence of this
  // notifier.
  // Note that using CrossThreadWeakPersistent should be fine to hold a
  // reference to an object that lives on the same sequence. Theoretically we
  // don't need to use Member<ExecutionContext> here, but we've seen
  // mysterious crashes when we do so.
  // TODO(crbug.com/959508): Merge |inside_execution_context_| and
  // |outside_execution_context_|.
  Member<ExecutionContext> inside_execution_context_;
  // Used when the execution context lives on a different sequence of this
  // notifier.
  CrossThreadWeakPersistent<ExecutionContext> outside_execution_context_;

  DISALLOW_COPY_AND_ASSIGN(WorkerResourceTimingNotifierImpl);
};

// NullWorkerResourceTimingNotifier does nothing when AddResourceTiming() is
// called. This is used for toplevel shared/service worker script fetch.
class CORE_EXPORT NullWorkerResourceTimingNotifier final
    : public WorkerResourceTimingNotifier {
 public:
  NullWorkerResourceTimingNotifier() = default;
  ~NullWorkerResourceTimingNotifier() override = default;

  void AddResourceTiming(
      const WebResourceTimingInfo&,
      const AtomicString& initiator_type,
      mojo::PendingReceiver<mojom::blink::WorkerTimingContainer>
          worker_timing_receiver) override {}

 private:
  DISALLOW_COPY_AND_ASSIGN(NullWorkerResourceTimingNotifier);
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_CORE_LOADER_WORKER_RESOURCE_TIMING_NOTIFIER_IMPL_H_