2015-01-22 18:55:55 +01:00
|
|
|
// Copyright (c) 2015 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.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/main_message_loop_multithreaded_win.h"
|
2015-01-22 18:55:55 +01:00
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_callback.h"
|
2016-09-01 13:24:30 +02:00
|
|
|
#include "include/base/cef_logging.h"
|
2015-01-22 18:55:55 +01:00
|
|
|
#include "include/cef_app.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/cefclient/browser/resource.h"
|
|
|
|
#include "tests/shared/browser/util_win.h"
|
2015-01-22 18:55:55 +01:00
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const wchar_t kWndClass[] = L"Client_MessageWindow";
|
|
|
|
const wchar_t kTaskMessageName[] = L"Client_CustomTask";
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
MainMessageLoopMultithreadedWin::MainMessageLoopMultithreadedWin()
|
|
|
|
: thread_id_(base::PlatformThread::CurrentId()),
|
|
|
|
task_message_id_(RegisterWindowMessage(kTaskMessageName)),
|
2021-06-17 22:08:01 +02:00
|
|
|
dialog_hwnd_(nullptr),
|
|
|
|
message_hwnd_(nullptr) {}
|
2015-01-22 18:55:55 +01:00
|
|
|
|
|
|
|
MainMessageLoopMultithreadedWin::~MainMessageLoopMultithreadedWin() {
|
|
|
|
DCHECK(RunsTasksOnCurrentThread());
|
|
|
|
DCHECK(!message_hwnd_);
|
|
|
|
DCHECK(queued_tasks_.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
int MainMessageLoopMultithreadedWin::Run() {
|
|
|
|
DCHECK(RunsTasksOnCurrentThread());
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
HINSTANCE hInstance = ::GetModuleHandle(nullptr);
|
2015-01-22 18:55:55 +01:00
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
|
|
|
// Create the hidden window for message processing.
|
|
|
|
message_hwnd_ = CreateMessageWindow(hInstance);
|
|
|
|
CHECK(message_hwnd_);
|
|
|
|
|
|
|
|
// Store a pointer to |this| in the window's user data.
|
|
|
|
SetUserDataPtr(message_hwnd_, this);
|
|
|
|
|
|
|
|
// Execute any tasks that are currently queued.
|
|
|
|
while (!queued_tasks_.empty()) {
|
|
|
|
PostTaskInternal(queued_tasks_.front());
|
|
|
|
queued_tasks_.pop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HACCEL hAccelTable =
|
|
|
|
LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_CEFCLIENT));
|
|
|
|
|
|
|
|
MSG msg;
|
|
|
|
|
|
|
|
// Run the application message loop.
|
2021-06-17 22:08:01 +02:00
|
|
|
while (GetMessage(&msg, nullptr, 0, 0)) {
|
2015-01-22 18:55:55 +01:00
|
|
|
// Allow processing of dialog messages.
|
2023-01-02 23:59:03 +01:00
|
|
|
if (dialog_hwnd_ && IsDialogMessage(dialog_hwnd_, &msg)) {
|
2015-01-22 18:55:55 +01:00
|
|
|
continue;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2015-01-22 18:55:55 +01:00
|
|
|
|
|
|
|
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg)) {
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
|
|
|
|
// Destroy the message window.
|
|
|
|
DestroyWindow(message_hwnd_);
|
2021-06-17 22:08:01 +02:00
|
|
|
message_hwnd_ = nullptr;
|
2015-01-22 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<int>(msg.wParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopMultithreadedWin::Quit() {
|
|
|
|
// Execute PostQuitMessage(0) on the main thread.
|
2021-06-19 21:54:45 +02:00
|
|
|
PostClosure(base::BindOnce(::PostQuitMessage, 0));
|
2015-01-22 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopMultithreadedWin::PostTask(CefRefPtr<CefTask> task) {
|
|
|
|
base::AutoLock lock_scope(lock_);
|
|
|
|
PostTaskInternal(task);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MainMessageLoopMultithreadedWin::RunsTasksOnCurrentThread() const {
|
|
|
|
return (thread_id_ == base::PlatformThread::CurrentId());
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopMultithreadedWin::SetCurrentModelessDialog(
|
|
|
|
HWND hWndDialog) {
|
|
|
|
DCHECK(RunsTasksOnCurrentThread());
|
|
|
|
|
2016-09-01 13:24:30 +02:00
|
|
|
#if DCHECK_IS_ON()
|
2015-01-22 18:55:55 +01:00
|
|
|
if (hWndDialog) {
|
|
|
|
// A new dialog reference should not be set while one is currently set.
|
|
|
|
DCHECK(!dialog_hwnd_);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
dialog_hwnd_ = hWndDialog;
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
HWND MainMessageLoopMultithreadedWin::CreateMessageWindow(HINSTANCE hInstance) {
|
|
|
|
WNDCLASSEX wc = {0};
|
|
|
|
wc.cbSize = sizeof(wc);
|
|
|
|
wc.lpfnWndProc = MessageWndProc;
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.lpszClassName = kWndClass;
|
|
|
|
RegisterClassEx(&wc);
|
|
|
|
|
|
|
|
return CreateWindow(kWndClass, 0, 0, 0, 0, 0, 0, HWND_MESSAGE, 0, hInstance,
|
|
|
|
0);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-05-17 11:29:28 +02:00
|
|
|
LRESULT CALLBACK
|
|
|
|
MainMessageLoopMultithreadedWin::MessageWndProc(HWND hWnd,
|
|
|
|
UINT message,
|
|
|
|
WPARAM wParam,
|
|
|
|
LPARAM lParam) {
|
2015-01-22 18:55:55 +01:00
|
|
|
MainMessageLoopMultithreadedWin* self =
|
|
|
|
GetUserDataPtr<MainMessageLoopMultithreadedWin*>(hWnd);
|
|
|
|
|
|
|
|
if (self && message == self->task_message_id_) {
|
|
|
|
// Execute the task.
|
|
|
|
CefTask* task = reinterpret_cast<CefTask*>(wParam);
|
|
|
|
task->Execute();
|
|
|
|
|
|
|
|
// Release the reference added in PostTaskInternal. This will likely result
|
|
|
|
// in |task| being deleted.
|
|
|
|
task->Release();
|
2017-05-17 11:29:28 +02:00
|
|
|
} else {
|
|
|
|
switch (message) {
|
2017-09-01 00:10:51 +02:00
|
|
|
case WM_NCDESTROY:
|
2017-05-17 11:29:28 +02:00
|
|
|
// Clear the reference to |self|.
|
2021-06-17 22:08:01 +02:00
|
|
|
SetUserDataPtr(hWnd, nullptr);
|
2017-05-17 11:29:28 +02:00
|
|
|
break;
|
|
|
|
}
|
2015-01-22 18:55:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return DefWindowProc(hWnd, message, wParam, lParam);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopMultithreadedWin::PostTaskInternal(
|
|
|
|
CefRefPtr<CefTask> task) {
|
|
|
|
lock_.AssertAcquired();
|
|
|
|
|
|
|
|
if (!message_hwnd_) {
|
|
|
|
// Queue the task until the message loop starts running.
|
|
|
|
queued_tasks_.push(task);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a reference that will be released in MessageWndProc.
|
|
|
|
task->AddRef();
|
|
|
|
|
|
|
|
// Post the task for execution by the message window.
|
|
|
|
PostMessage(message_hwnd_, task_message_id_,
|
|
|
|
reinterpret_cast<WPARAM>(task.get()), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace client
|