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

content / child / sandboxed_process_thread_type_handler.cc [blame]

// 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 "content/child/sandboxed_process_thread_type_handler.h"

#include "content/child/child_thread_impl.h"
#include "content/common/thread_type_switcher.mojom.h"

namespace content {

namespace {

SandboxedProcessThreadTypeHandler* g_sandboxed_process_thread_type_handler =
    nullptr;

}  // namespace

SandboxedProcessThreadTypeHandler::SandboxedProcessThreadTypeHandler() {
  // `thread_type_switcher_receiver_` will be bound when ChildThreadImpl is
  // available.
  thread_type_switcher_receiver_ =
      thread_type_switcher_.BindNewPipeAndPassReceiver();
  base::PlatformThread::SetThreadTypeDelegate(this);
}

SandboxedProcessThreadTypeHandler::~SandboxedProcessThreadTypeHandler() {
  base::PlatformThread::SetThreadTypeDelegate(nullptr);
}

// static
void SandboxedProcessThreadTypeHandler::Create() {
  DCHECK(!g_sandboxed_process_thread_type_handler);

  g_sandboxed_process_thread_type_handler =
      new SandboxedProcessThreadTypeHandler();
}

// static
void SandboxedProcessThreadTypeHandler::NotifyMainChildThreadCreated() {
  if (g_sandboxed_process_thread_type_handler) {
    g_sandboxed_process_thread_type_handler->ConnectThreadTypeSwitcher();
  }
}

// static
SandboxedProcessThreadTypeHandler* SandboxedProcessThreadTypeHandler::Get() {
  return g_sandboxed_process_thread_type_handler;
}

bool SandboxedProcessThreadTypeHandler::HandleThreadTypeChange(
    base::PlatformThreadId thread_id,
    base::ThreadType thread_type) {
  thread_type_switcher_->SetThreadType(thread_id, thread_type);
  return true;
}

void SandboxedProcessThreadTypeHandler::ConnectThreadTypeSwitcher() {
  ChildThreadImpl* main_thread = ChildThreadImpl::current();
  DCHECK(main_thread);
  DCHECK(main_thread->main_thread_runner()->RunsTasksInCurrentSequence());

  main_thread->BindHostReceiver(std::move(thread_type_switcher_receiver_));
}

}  // namespace content