File: webcodecs_logger.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 (90 lines) | stat: -rw-r--r-- 3,085 bytes parent folder | download | duplicates (11)
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
// Copyright 2020 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/modules/webcodecs/webcodecs_logger.h"

#include "third_party/blink/renderer/core/inspector/console_message.h"

namespace blink {

// How frequently we check for leaks.
constexpr base::TimeDelta kTimerInterval = base::Seconds(10);

// How long we wait before stopping the timer when there is no activity.
constexpr base::TimeDelta kTimerShutdownDelay = base::Seconds(60);

void WebCodecsLogger::VideoFrameCloseAuditor::ReportUnclosedFrame() {
  were_frames_not_closed_ = true;
}

void WebCodecsLogger::VideoFrameCloseAuditor::Clear() {
  were_frames_not_closed_ = false;
}

WebCodecsLogger::WebCodecsLogger(ExecutionContext& context)
    : Supplement<ExecutionContext>(context),
      close_auditor_(base::MakeRefCounted<VideoFrameCloseAuditor>()),
      timer_(context.GetTaskRunner(TaskType::kInternalMedia),
             this,
             &WebCodecsLogger::LogCloseErrors) {}

// static
WebCodecsLogger& WebCodecsLogger::From(ExecutionContext& context) {
  WebCodecsLogger* supplement =
      Supplement<ExecutionContext>::From<WebCodecsLogger>(context);
  if (!supplement) {
    supplement = MakeGarbageCollected<WebCodecsLogger>(context);
    Supplement<ExecutionContext>::ProvideTo(context, supplement);
  }

  return *supplement;
}

scoped_refptr<WebCodecsLogger::VideoFrameCloseAuditor>
WebCodecsLogger::GetCloseAuditor() {
  // We cannot directly log close errors: they are detected during garbage
  // collection, and it would be unsafe to access GC'ed objects from a GC'ed
  // object's destructor. Instead, start a timer here to periodically poll for
  // these errors. The timer should stop itself after a period of inactivity.
  if (!timer_.IsActive())
    timer_.StartRepeating(kTimerInterval, FROM_HERE);

  last_auditor_access_ = base::TimeTicks::Now();

  return close_auditor_;
}

void WebCodecsLogger::LogCloseErrors(TimerBase*) {
  // If it's been a while since this class was used and there are not other
  // references to |leak_status_|, stop the timer.
  if (base::TimeTicks::Now() - last_auditor_access_ > kTimerShutdownDelay &&
      close_auditor_->HasOneRef()) {
    timer_.Stop();
  }

  if (!close_auditor_->were_frames_not_closed())
    return;

  auto* execution_context = GetSupplementable();
  if (!execution_context->IsContextDestroyed()) {
    execution_context->AddConsoleMessage(MakeGarbageCollected<ConsoleMessage>(
        mojom::blink::ConsoleMessageSource::kJavaScript,
        mojom::blink::ConsoleMessageLevel::kError,
        "A VideoFrame was garbage collected without being closed. "
        "Applications should call close() on frames when done with them to "
        "prevent stalls."));
  }

  close_auditor_->Clear();
}

void WebCodecsLogger::Trace(Visitor* visitor) const {
  visitor->Trace(timer_);
  Supplement<ExecutionContext>::Trace(visitor);
}

// static
const char WebCodecsLogger::kSupplementName[] = "WebCodecsLogger";

}  // namespace blink