File: v8_dom_activity_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 (127 lines) | stat: -rw-r--r-- 4,373 bytes parent folder | download | duplicates (6)
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
// Copyright 2014 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/platform/bindings/v8_dom_activity_logger.h"

#include <memory>
#include "third_party/blink/public/common/scheme_registry.h"
#include "third_party/blink/renderer/platform/bindings/v8_binding.h"
#include "third_party/blink/renderer/platform/bindings/v8_per_context_data.h"
#include "third_party/blink/renderer/platform/weborigin/kurl.h"
#include "third_party/blink/renderer/platform/weborigin/scheme_registry.h"
#include "third_party/blink/renderer/platform/wtf/hash_map.h"
#include "third_party/blink/renderer/platform/wtf/text/string_hash.h"

namespace blink {

typedef HashMap<String, std::unique_ptr<V8DOMActivityLogger>>
    DOMActivityLoggerMapForMainWorld;
typedef HashMap<int,
                std::unique_ptr<V8DOMActivityLogger>,
                IntWithZeroKeyHashTraits<int>>
    DOMActivityLoggerMapForIsolatedWorld;

static DOMActivityLoggerMapForMainWorld& DomActivityLoggersForMainWorld() {
  DCHECK(IsMainThread());
  DEFINE_STATIC_LOCAL(DOMActivityLoggerMapForMainWorld, map, ());
  return map;
}

static DOMActivityLoggerMapForIsolatedWorld&
DomActivityLoggersForIsolatedWorld() {
  DCHECK(IsMainThread());
  DEFINE_STATIC_LOCAL(DOMActivityLoggerMapForIsolatedWorld, map, ());
  return map;
}

void V8DOMActivityLogger::LogMethod(ScriptState* script_state,
                                    const char* api_name,
                                    v8::FunctionCallbackInfo<v8::Value> info) {
  v8::LocalVector<v8::Value> logger_args(info.GetIsolate());
  logger_args.reserve(info.Length());
  for (int i = 0; i < info.Length(); ++i) {
    logger_args.push_back(info[i]);
  }
  LogMethod(script_state, api_name, logger_args);
}

void V8DOMActivityLogger::SetActivityLogger(
    int world_id,
    const String& extension_id,
    std::unique_ptr<V8DOMActivityLogger> logger) {
  if (world_id)
    DomActivityLoggersForIsolatedWorld().Set(world_id, std::move(logger));
  else
    DomActivityLoggersForMainWorld().Set(extension_id, std::move(logger));
}

V8DOMActivityLogger* V8DOMActivityLogger::ActivityLogger(
    int world_id,
    const String& extension_id) {
  if (world_id) {
    DOMActivityLoggerMapForIsolatedWorld& loggers =
        DomActivityLoggersForIsolatedWorld();
    DOMActivityLoggerMapForIsolatedWorld::iterator it = loggers.find(world_id);
    return it == loggers.end() ? nullptr : it->value.get();
  }

  if (extension_id.empty())
    return nullptr;

  DOMActivityLoggerMapForMainWorld& loggers = DomActivityLoggersForMainWorld();
  DOMActivityLoggerMapForMainWorld::iterator it = loggers.find(extension_id);
  return it == loggers.end() ? nullptr : it->value.get();
}

V8DOMActivityLogger* V8DOMActivityLogger::ActivityLogger(int world_id,
                                                         const KURL& url) {
  // extension ID is ignored for worldId != 0.
  if (world_id)
    return ActivityLogger(world_id, String());

  // To find an activity logger that corresponds to the main world of an
  // extension, we need to obtain the extension ID. Extension ID is a hostname
  // of a background page's URL.
  if (!CommonSchemeRegistry::IsExtensionScheme(url.Protocol().Ascii()))
    return nullptr;

  return ActivityLogger(world_id, url.Host().ToString());
}

V8DOMActivityLogger* V8DOMActivityLogger::CurrentActivityLogger(
    v8::Isolate* isolate) {
  if (!isolate->InContext())
    return nullptr;

  v8::HandleScope handle_scope(isolate);
  V8PerContextData* context_data =
      ScriptState::ForCurrentRealm(isolate)->PerContextData();
  if (!context_data)
    return nullptr;

  return context_data->ActivityLogger();
}

V8DOMActivityLogger* V8DOMActivityLogger::CurrentActivityLoggerIfIsolatedWorld(
    v8::Isolate* isolate) {
  if (!isolate->InContext())
    return nullptr;

  ScriptState* script_state = ScriptState::ForCurrentRealm(isolate);
  if (!script_state->World().IsIsolatedWorld())
    return nullptr;

  V8PerContextData* context_data = script_state->PerContextData();
  if (!context_data)
    return nullptr;

  return context_data->ActivityLogger();
}

bool V8DOMActivityLogger::HasActivityLoggerInIsolatedWorlds() {
  DCHECK(IsMainThread());
  return !DomActivityLoggersForIsolatedWorld().empty();
}

}  // namespace blink