summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/bindings/core/v8/v8_metrics.h
blob: 513796ccb1af42bb49f990a9ea9449c2c446bf13 (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
// Copyright 2020 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_BINDINGS_CORE_V8_V8_METRICS_H_
#define THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_METRICS_H_

#include "services/metrics/public/cpp/ukm_recorder.h"
#include "third_party/blink/renderer/core/core_export.h"
#include "v8/include/v8-metrics.h"

namespace blink {

class Document;

// Implements a V8 metrics recorder for gathering events generated
// within the V8 engine. This is used for some UMA and all UKM
// metrics. All event handling methods in here are run in the main
// thread, so thread-safety is not an issue and is handled by the
// V8 engine.
// For UKM events, the context is used to get the source id and the
// UKM recorder.
class CORE_EXPORT V8MetricsRecorder : public v8::metrics::Recorder {
 public:
  explicit V8MetricsRecorder(v8::Isolate* isolate) : isolate_(isolate) {}

  void AddMainThreadEvent(const v8::metrics::WasmModuleDecoded& event,
                          ContextId context_id) override;
  void AddMainThreadEvent(const v8::metrics::WasmModuleCompiled& event,
                          ContextId context_id) override;
  void AddMainThreadEvent(const v8::metrics::WasmModuleInstantiated& event,
                          ContextId context_id) override;
  void AddMainThreadEvent(const v8::metrics::WasmModuleTieredUp& event,
                          ContextId context_id) override;

  void NotifyIsolateDisposal() override;

 private:
  struct UkmRecorderAndSourceId {
    ukm::UkmRecorder* recorder;
    ukm::SourceId source_id;
    UkmRecorderAndSourceId(ukm::UkmRecorder* ukm_recorder,
                           ukm::SourceId ukm_source_id)
        : recorder(ukm_recorder), source_id(ukm_source_id) {}
  };

  Document* GetDocument(ContextId context_id);
  absl::optional<UkmRecorderAndSourceId> GetUkmRecorderAndSourceId(
      ContextId context_id);

  v8::Isolate* isolate_;
};

}  // namespace blink

#endif  // THIRD_PARTY_BLINK_RENDERER_BINDINGS_CORE_V8_V8_METRICS_H_