File: scoped_mojo_support.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 (82 lines) | stat: -rw-r--r-- 2,729 bytes parent folder | download | duplicates (7)
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
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "mojo/core/test/scoped_mojo_support.h"

#include "base/base_switches.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/synchronization/waitable_event.h"
#include "base/test/bind.h"
#include "base/test/test_io_thread.h"
#include "mojo/core/embedder/configuration.h"
#include "mojo/core/embedder/embedder.h"
#include "mojo/core/embedder/scoped_ipc_support.h"
#include "mojo/core/test/mojo_test_base.h"
#include "mojo/core/test/test_support_impl.h"
#include "mojo/core/test/test_switches.h"
#include "mojo/public/tests/test_support_private.h"

namespace mojo::core::test {

namespace {

// Command-line switch to disable internal Mojo Channel capability
// advertisement, used to test skew between client versions.
const char kDisableAllCapabilities[] = "disable-all-capabilities";

class TestSupportInitializer {
 public:
  TestSupportInitializer() {
    mojo::test::TestSupport::Init(new mojo::core::test::TestSupportImpl());
  }
};

}  // namespace

class ScopedMojoSupport::CoreInstance {
 public:
  CoreInstance() {
    mojo::core::Configuration mojo_config;

    // A relatively low limit to make it easier to test behavior at the limit.
    mojo_config.max_message_num_bytes =
        mojo::core::test::MojoTestBase::kMaxMessageSizeInTests;
    if (!base::CommandLine::ForCurrentProcess()->HasSwitch(
            switches::kTestChildProcess) ||
        base::CommandLine::ForCurrentProcess()->HasSwitch(
            test_switches::kMojoIsBroker)) {
      mojo_config.is_broker_process = true;
    }

    if (base::CommandLine::ForCurrentProcess()->HasSwitch(
            kDisableAllCapabilities)) {
      mojo_config.dont_advertise_capabilities = true;
    }

    mojo::core::InitFeatures();
    mojo::core::Init(mojo_config);

    static TestSupportInitializer initializer;
  }

  ~CoreInstance() { mojo::core::ShutDown(); }
};

ScopedMojoSupport::ScopedMojoSupport()
    : core_(std::make_unique<CoreInstance>()) {
  // IO thread initialization can race to modify globals which other base object
  // initializations (e.g. TaskEnvironment) might touch on the main thread as a
  // side effect of any test that might run. Ensure the thread is fully started
  // before we proceed. See https://crbug.com/1364731.
  base::WaitableEvent io_thread_initialized;
  test_io_thread_.PostTask(FROM_HERE, base::BindLambdaForTesting([&] {
                             io_thread_initialized.Signal();
                           }));
  io_thread_initialized.Wait();
}

ScopedMojoSupport::~ScopedMojoSupport() = default;

}  // namespace mojo::core::test