2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 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.
|
|
|
|
|
|
|
|
#ifndef CEF_LIBCEF_BROWSER_THREAD_UTIL_H_
|
|
|
|
#define CEF_LIBCEF_BROWSER_THREAD_UTIL_H_
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "base/location.h"
|
|
|
|
#include "base/logging.h"
|
2021-04-21 00:52:34 +02:00
|
|
|
#include "base/task/thread_pool.h"
|
2019-03-13 22:27:37 +01:00
|
|
|
#include "base/threading/scoped_blocking_call.h"
|
2018-03-20 21:15:08 +01:00
|
|
|
#include "base/threading/thread_restrictions.h"
|
2018-10-02 14:14:11 +02:00
|
|
|
#include "content/public/browser/browser_task_traits.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
|
|
|
#define CEF_UIT content::BrowserThread::UI
|
|
|
|
#define CEF_IOT content::BrowserThread::IO
|
|
|
|
|
|
|
|
#define CEF_CURRENTLY_ON(id) content::BrowserThread::CurrentlyOn(id)
|
|
|
|
#define CEF_CURRENTLY_ON_UIT() CEF_CURRENTLY_ON(CEF_UIT)
|
|
|
|
#define CEF_CURRENTLY_ON_IOT() CEF_CURRENTLY_ON(CEF_IOT)
|
|
|
|
|
|
|
|
#define CEF_REQUIRE(id) DCHECK(CEF_CURRENTLY_ON(id))
|
|
|
|
#define CEF_REQUIRE_UIT() CEF_REQUIRE(CEF_UIT)
|
|
|
|
#define CEF_REQUIRE_IOT() CEF_REQUIRE(CEF_IOT)
|
|
|
|
|
2023-05-08 17:07:57 +02:00
|
|
|
#define CEF_REQUIRE_RETURN(id, var) \
|
|
|
|
if (!CEF_CURRENTLY_ON(id)) { \
|
|
|
|
DCHECK(false) << "called on invalid thread"; \
|
|
|
|
return var; \
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
#define CEF_REQUIRE_UIT_RETURN(var) CEF_REQUIRE_RETURN(CEF_UIT, var)
|
|
|
|
#define CEF_REQUIRE_IOT_RETURN(var) CEF_REQUIRE_RETURN(CEF_IOT, var)
|
|
|
|
|
2023-05-08 17:07:57 +02:00
|
|
|
#define CEF_REQUIRE_RETURN_VOID(id) \
|
|
|
|
if (!CEF_CURRENTLY_ON(id)) { \
|
|
|
|
DCHECK(false) << "called on invalid thread"; \
|
|
|
|
return; \
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
#define CEF_REQUIRE_UIT_RETURN_VOID() CEF_REQUIRE_RETURN_VOID(CEF_UIT)
|
|
|
|
#define CEF_REQUIRE_IOT_RETURN_VOID() CEF_REQUIRE_RETURN_VOID(CEF_IOT)
|
|
|
|
|
2022-05-19 12:28:44 +02:00
|
|
|
template <int id, std::enable_if_t<id == CEF_UIT, bool> = true>
|
|
|
|
auto CEF_TASK_RUNNER() {
|
|
|
|
return content::GetUIThreadTaskRunner({});
|
|
|
|
}
|
|
|
|
template <int id, std::enable_if_t<id == CEF_IOT, bool> = true>
|
|
|
|
auto CEF_TASK_RUNNER() {
|
|
|
|
return content::GetIOThreadTaskRunner({});
|
|
|
|
}
|
|
|
|
|
|
|
|
#define CEF_POST_TASK(id, task) CEF_TASK_RUNNER<id>()->PostTask(FROM_HERE, task)
|
|
|
|
#define CEF_POST_DELAYED_TASK(id, task, delay_ms) \
|
|
|
|
CEF_TASK_RUNNER<id>()->PostDelayedTask(FROM_HERE, task, \
|
|
|
|
base::Milliseconds(delay_ms))
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2018-03-20 21:15:08 +01:00
|
|
|
// Post a blocking task with the specified |priority|. Tasks that have not
|
|
|
|
// started executing at shutdown will never run. However, any task that has
|
|
|
|
// already begun executing when shutdown is invoked will be allowed to continue
|
|
|
|
// and will block shutdown until completion.
|
|
|
|
// Tasks posted with this method are not guaranteed to run sequentially. Use
|
2019-10-01 15:55:16 +02:00
|
|
|
// base::CreateSequencedTaskRunner instead if sequence is important.
|
2018-03-20 21:15:08 +01:00
|
|
|
// Sequenced runners at various priorities that always execute all pending tasks
|
2020-06-28 20:29:44 +02:00
|
|
|
// before shutdown are available via CefTaskRunnerManager and exposed by the CEF
|
|
|
|
// API.
|
2021-04-21 00:52:34 +02:00
|
|
|
#define CEF_POST_BLOCKING_TASK(priority, task) \
|
|
|
|
base::ThreadPool::PostTask( \
|
|
|
|
FROM_HERE, \
|
|
|
|
{priority, base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN, \
|
|
|
|
base::MayBlock()}, \
|
2018-03-20 21:15:08 +01:00
|
|
|
task)
|
|
|
|
|
|
|
|
// Post a blocking task that affects UI or responsiveness of future user
|
|
|
|
// interactions. Do not use if an immediate response to a user interaction is
|
|
|
|
// expected.
|
|
|
|
#define CEF_POST_USER_VISIBLE_TASK(task) \
|
|
|
|
CEF_POST_BLOCKING_TASK(base::TaskPriority::USER_VISIBLE, task)
|
|
|
|
|
|
|
|
// Post a blocking task where the user won't notice if it takes an arbitrarily
|
|
|
|
// long time to complete.
|
|
|
|
#define CEF_POST_BACKGROUND_TASK(task) \
|
2018-07-31 21:21:39 +02:00
|
|
|
CEF_POST_BLOCKING_TASK(base::TaskPriority::BEST_EFFORT, task)
|
2018-03-20 21:15:08 +01:00
|
|
|
|
|
|
|
// Assert that blocking is allowed on the current thread.
|
2019-03-13 22:27:37 +01:00
|
|
|
#define CEF_REQUIRE_BLOCKING() \
|
|
|
|
base::ScopedBlockingCall scoped_blocking_call( \
|
|
|
|
FROM_HERE, base::BlockingType::WILL_BLOCK)
|
2018-03-20 21:15:08 +01:00
|
|
|
|
2016-03-16 03:55:59 +01:00
|
|
|
// Same as IMPLEMENT_REFCOUNTING() but using the specified Destructor.
|
2023-01-02 23:59:03 +01:00
|
|
|
#define IMPLEMENT_REFCOUNTING_EX(ClassName, Destructor) \
|
|
|
|
public: \
|
|
|
|
void AddRef() const override { \
|
|
|
|
ref_count_.AddRef(); \
|
|
|
|
} \
|
|
|
|
bool Release() const override { \
|
|
|
|
if (ref_count_.Release()) { \
|
|
|
|
Destructor::Destruct(this); \
|
|
|
|
return true; \
|
|
|
|
} \
|
|
|
|
return false; \
|
|
|
|
} \
|
|
|
|
bool HasOneRef() const override { \
|
|
|
|
return ref_count_.HasOneRef(); \
|
|
|
|
} \
|
|
|
|
bool HasAtLeastOneRef() const override { \
|
|
|
|
return ref_count_.HasAtLeastOneRef(); \
|
|
|
|
} \
|
|
|
|
\
|
|
|
|
private: \
|
2019-03-13 22:27:37 +01:00
|
|
|
CefRefCount ref_count_
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define IMPLEMENT_REFCOUNTING_DELETE_ON_UIT(ClassName) \
|
|
|
|
IMPLEMENT_REFCOUNTING_EX(ClassName, content::BrowserThread::DeleteOnUIThread)
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
#define IMPLEMENT_REFCOUNTING_DELETE_ON_IOT(ClassName) \
|
|
|
|
IMPLEMENT_REFCOUNTING_EX(ClassName, content::BrowserThread::DeleteOnIOThread)
|
2016-03-16 03:55:59 +01:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
#endif // CEF_LIBCEF_BROWSER_THREAD_UTIL_H_
|