File: native_extension_bindings_system_test_base.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (130 lines) | stat: -rw-r--r-- 4,855 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
128
129
130
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "extensions/renderer/native_extension_bindings_system_test_base.h"

#include <algorithm>

#include "base/run_loop.h"
#include "content/public/test/mock_render_thread.h"
#include "extensions/common/manifest.h"
#include "extensions/common/mojom/context_type.mojom.h"
#include "extensions/common/mojom/frame.mojom.h"
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/common/utils/extension_utils.h"
#include "extensions/renderer/module_system.h"
#include "extensions/renderer/native_extension_bindings_system.h"
#include "extensions/renderer/script_context.h"
#include "extensions/renderer/script_context_set.h"
#include "extensions/renderer/test_extensions_renderer_client.h"
#include "extensions/renderer/test_v8_extension_configuration.h"

namespace extensions {

TestIPCMessageSender::TestIPCMessageSender() = default;
TestIPCMessageSender::~TestIPCMessageSender() = default;
void TestIPCMessageSender::SendRequestIPC(ScriptContext* context,
                                          mojom::RequestParamsPtr params) {
  last_params_ = std::move(params);
}

NativeExtensionBindingsSystemUnittest::NativeExtensionBindingsSystemUnittest() {
}

NativeExtensionBindingsSystemUnittest::
    ~NativeExtensionBindingsSystemUnittest() {}

v8::ExtensionConfiguration*
NativeExtensionBindingsSystemUnittest::GetV8ExtensionConfiguration() {
  return TestV8ExtensionConfiguration::GetConfiguration();
}

void NativeExtensionBindingsSystemUnittest::SetUp() {
  render_thread_ = std::make_unique<content::MockRenderThread>();
  script_context_set_ = std::make_unique<ScriptContextSet>(&extension_ids_);
  std::unique_ptr<TestIPCMessageSender> message_sender;
  if (UseStrictIPCMessageSender()) {
    message_sender =
        std::make_unique<testing::StrictMock<TestIPCMessageSender>>();
  } else {
    message_sender = std::make_unique<TestIPCMessageSender>();
  }
  ipc_message_sender_ = message_sender.get();
  bindings_system_ = std::make_unique<NativeExtensionBindingsSystem>(
      this, std::move(message_sender));
  api_provider_.AddBindingsSystemHooks(/*dispatcher=*/nullptr,
                                       bindings_system_.get());
  APIBindingTest::SetUp();
}

void NativeExtensionBindingsSystemUnittest::TearDown() {
  // Dispose all contexts now so we call WillReleaseScriptContext() on the
  // bindings system.
  DisposeAllContexts();

  // ScriptContexts are deleted asynchronously by the ScriptContextSet, so we
  // need spin here to ensure we don't leak. See also
  // ScriptContextSet::Remove().
  base::RunLoop().RunUntilIdle();

  ASSERT_TRUE(raw_script_contexts_.empty());
  script_context_set_.reset();
  bindings_system_.reset();
  render_thread_.reset();
  APIBindingTest::TearDown();
}

ScriptContext* NativeExtensionBindingsSystemUnittest::CreateScriptContext(
    v8::Local<v8::Context> v8_context,
    const Extension* extension,
    mojom::ContextType context_type) {
  auto script_context = std::make_unique<ScriptContext>(
      v8_context, nullptr, GenerateHostIdFromExtension(extension), extension,
      /*blink_isolated_world_id=*/std::nullopt, context_type, extension,
      context_type);
  script_context->SetModuleSystem(
      std::make_unique<ModuleSystem>(script_context.get(), source_map()));
  ScriptContext* raw_script_context = script_context.get();
  raw_script_contexts_.push_back(raw_script_context);
  script_context_set_->AddForTesting(std::move(script_context));
  bindings_system_->DidCreateScriptContext(raw_script_context);
  return raw_script_context;
}

void NativeExtensionBindingsSystemUnittest::OnWillDisposeContext(
    v8::Local<v8::Context> context) {
  auto iter = std::ranges::find(raw_script_contexts_, context,
                                &ScriptContext::v8_context);
  if (iter == raw_script_contexts_.end()) {
    ASSERT_TRUE(allow_unregistered_contexts_);
    return;
  }
  bindings_system_->WillReleaseScriptContext(*iter);
  script_context_set_->Remove(*iter);
  raw_script_contexts_.erase(iter);
}

std::unique_ptr<TestJSRunner::Scope>
NativeExtensionBindingsSystemUnittest::CreateTestJSRunner() {
  // The NativeExtensionBindingsSystem handles setting the JSRunner for each
  // context, so we don't need to fake one.
  return nullptr;
}

void NativeExtensionBindingsSystemUnittest::RegisterExtension(
    scoped_refptr<const Extension> extension) {
  extension_ids_.insert(extension->id());
  RendererExtensionRegistry::Get()->Insert(extension);
}

bool NativeExtensionBindingsSystemUnittest::UseStrictIPCMessageSender() {
  return false;
}

ScriptContextSetIterable*
NativeExtensionBindingsSystemUnittest::GetScriptContextSet() {
  return script_context_set_.get();
}

}  // namespace extensions