summaryrefslogtreecommitdiff
path: root/libcef/common/thread_impl.cc
blob: 7c4a0946636fabfaa609d470c70422d25a1da973 (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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2016 The Chromium Embedded Framework Authors. All rights
// reserved. Use of this source code is governed by a BSD-style license that can
// be found in the LICENSE file.

#include "libcef/common/thread_impl.h"

#include "libcef/common/task_runner_impl.h"

#include "base/functional/bind.h"
#include "base/threading/thread_restrictions.h"

namespace {

void StopAndDestroy(base::Thread* thread) {
  // Calling PlatformThread::Join() on the UI thread is otherwise disallowed.
  base::ScopedAllowBaseSyncPrimitivesForTesting scoped_allow_sync_primitives;

  // Deleting |thread| will implicitly stop and join it.
  delete thread;
}

}  // namespace

// static
CefRefPtr<CefThread> CefThread::CreateThread(
    const CefString& display_name,
    cef_thread_priority_t priority,
    cef_message_loop_type_t message_loop_type,
    bool stoppable,
    cef_com_init_mode_t com_init_mode) {
  if (!CefTaskRunnerImpl::GetCurrentTaskRunner()) {
    NOTREACHED() << "called on invalid thread";
    return nullptr;
  }

  CefRefPtr<CefThreadImpl> thread_impl = new CefThreadImpl();
  if (!thread_impl->Create(display_name, priority, message_loop_type, stoppable,
                           com_init_mode)) {
    return nullptr;
  }
  return thread_impl;
}

CefThreadImpl::CefThreadImpl() : thread_id_(kInvalidPlatformThreadId) {}

CefThreadImpl::~CefThreadImpl() {
  if (thread_.get()) {
    if (!owner_task_runner_->RunsTasksInCurrentSequence()) {
      // Delete |thread_| on the correct thread.
      owner_task_runner_->PostTask(
          FROM_HERE,
          base::BindOnce(StopAndDestroy, base::Unretained(thread_.release())));
    } else {
      StopAndDestroy(thread_.release());
    }
  }
}

bool CefThreadImpl::Create(const CefString& display_name,
                           cef_thread_priority_t priority,
                           cef_message_loop_type_t message_loop_type,
                           bool stoppable,
                           cef_com_init_mode_t com_init_mode) {
  owner_task_runner_ = CefTaskRunnerImpl::GetCurrentTaskRunner();
  DCHECK(owner_task_runner_);
  if (!owner_task_runner_) {
    return false;
  }

  thread_.reset(new base::Thread(display_name));

  base::Thread::Options options;

  switch (priority) {
    case TP_BACKGROUND:
      options.thread_type = base::ThreadType::kBackground;
      break;
    case TP_DISPLAY:
      options.thread_type = base::ThreadType::kDisplayCritical;
      break;
    case TP_REALTIME_AUDIO:
      options.thread_type = base::ThreadType::kRealtimeAudio;
      break;
    default:
      break;
  }

  switch (message_loop_type) {
    case ML_TYPE_UI:
      options.message_pump_type = base::MessagePumpType::UI;
      break;
    case ML_TYPE_IO:
      options.message_pump_type = base::MessagePumpType::IO;
      break;
    default:
      break;
  }

  options.joinable = stoppable;

#if BUILDFLAG(IS_WIN)
  if (com_init_mode != COM_INIT_MODE_NONE) {
    if (com_init_mode == COM_INIT_MODE_STA) {
      options.message_pump_type = base::MessagePumpType::UI;
    }
    thread_->init_com_with_mta(com_init_mode == COM_INIT_MODE_MTA);
  }
#endif

  if (!thread_->StartWithOptions(std::move(options))) {
    thread_.reset();
    return false;
  }

  thread_task_runner_ = new CefTaskRunnerImpl(thread_->task_runner());
  thread_id_ = thread_->GetThreadId();
  return true;
}

CefRefPtr<CefTaskRunner> CefThreadImpl::GetTaskRunner() {
  return thread_task_runner_;
}

cef_platform_thread_id_t CefThreadImpl::GetPlatformThreadId() {
  return thread_id_;
}

void CefThreadImpl::Stop() {
  if (!owner_task_runner_) {
    return;
  }
  if (!owner_task_runner_->RunsTasksInCurrentSequence()) {
    NOTREACHED() << "called on invalid thread";
    return;
  }

  if (thread_) {
    StopAndDestroy(thread_.release());
  }
}

bool CefThreadImpl::IsRunning() {
  if (!owner_task_runner_) {
    return false;
  }
  if (!owner_task_runner_->RunsTasksInCurrentSequence()) {
    NOTREACHED() << "called on invalid thread";
    return false;
  }

  return thread_ && thread_->IsRunning();
}