2016-05-04 20:00:03 +02:00
|
|
|
// Copyright (c) 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.
|
|
|
|
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/main_message_loop_external_pump.h"
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
#include <CommCtrl.h>
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include <memory>
|
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
#include "include/cef_app.h"
|
2016-11-18 00:52:42 +01:00
|
|
|
#include "tests/shared/browser/util_win.h"
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
namespace client {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Message sent to get an additional time slice for pumping (processing) another
|
|
|
|
// task (a series of such messages creates a continuous task pump).
|
|
|
|
static const int kMsgHaveWork = WM_USER + 1;
|
|
|
|
|
|
|
|
class MainMessageLoopExternalPumpWin : public MainMessageLoopExternalPump {
|
|
|
|
public:
|
|
|
|
MainMessageLoopExternalPumpWin();
|
2024-01-20 03:22:56 +01:00
|
|
|
~MainMessageLoopExternalPumpWin() override;
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// MainMessageLoopStd methods:
|
2021-06-17 21:43:06 +02:00
|
|
|
void Quit() override;
|
|
|
|
int Run() override;
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// MainMessageLoopExternalPump methods:
|
2023-06-01 16:06:15 +02:00
|
|
|
void OnScheduleMessagePumpWork(int64_t delay_ms) override;
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
protected:
|
|
|
|
// MainMessageLoopExternalPump methods:
|
2023-06-01 16:06:15 +02:00
|
|
|
void SetTimer(int64_t delay_ms) override;
|
2021-06-17 21:43:06 +02:00
|
|
|
void KillTimer() override;
|
|
|
|
bool IsTimerPending() override { return timer_pending_; }
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
private:
|
2017-05-17 11:29:28 +02:00
|
|
|
static LRESULT CALLBACK WndProc(HWND hwnd,
|
|
|
|
UINT msg,
|
|
|
|
WPARAM wparam,
|
2016-05-04 20:00:03 +02:00
|
|
|
LPARAM lparam);
|
|
|
|
|
|
|
|
// True if a timer event is currently pending.
|
2024-01-20 03:22:56 +01:00
|
|
|
bool timer_pending_ = false;
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// HWND owned by the thread that CefDoMessageLoopWork should be invoked on.
|
2024-01-20 03:22:56 +01:00
|
|
|
HWND main_thread_target_ = nullptr;
|
2016-05-04 20:00:03 +02:00
|
|
|
};
|
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
MainMessageLoopExternalPumpWin::MainMessageLoopExternalPumpWin() {
|
2021-06-17 22:08:01 +02:00
|
|
|
HINSTANCE hInstance = GetModuleHandle(nullptr);
|
2016-05-04 20:00:03 +02:00
|
|
|
const wchar_t* const kClassName = L"CEFMainTargetHWND";
|
|
|
|
|
|
|
|
WNDCLASSEX wcex = {};
|
|
|
|
wcex.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wcex.lpfnWndProc = WndProc;
|
|
|
|
wcex.hInstance = hInstance;
|
|
|
|
wcex.lpszClassName = kClassName;
|
|
|
|
RegisterClassEx(&wcex);
|
|
|
|
|
|
|
|
// Create the message handling window.
|
2017-05-17 11:29:28 +02:00
|
|
|
main_thread_target_ =
|
2021-06-17 22:08:01 +02:00
|
|
|
CreateWindowW(kClassName, nullptr, WS_OVERLAPPEDWINDOW, 0, 0, 0, 0,
|
|
|
|
HWND_MESSAGE, nullptr, hInstance, nullptr);
|
2016-05-04 20:00:03 +02:00
|
|
|
DCHECK(main_thread_target_);
|
|
|
|
SetUserDataPtr(main_thread_target_, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
MainMessageLoopExternalPumpWin::~MainMessageLoopExternalPumpWin() {
|
|
|
|
KillTimer();
|
2023-01-02 23:59:03 +01:00
|
|
|
if (main_thread_target_) {
|
2016-05-04 20:00:03 +02:00
|
|
|
DestroyWindow(main_thread_target_);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopExternalPumpWin::Quit() {
|
2021-06-17 22:08:01 +02:00
|
|
|
PostMessage(nullptr, WM_QUIT, 0, 0);
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int MainMessageLoopExternalPumpWin::Run() {
|
|
|
|
// Run the message loop.
|
|
|
|
MSG msg;
|
2021-06-17 22:08:01 +02:00
|
|
|
while (GetMessage(&msg, nullptr, 0, 0)) {
|
2016-05-04 20:00:03 +02:00
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
KillTimer();
|
|
|
|
|
|
|
|
// We need to run the message pump until it is idle. However we don't have
|
|
|
|
// that information here so we run the message loop "for a while".
|
|
|
|
for (int i = 0; i < 10; ++i) {
|
|
|
|
// Do some work.
|
|
|
|
CefDoMessageLoopWork();
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
// Sleep to allow the CEF proc to do work.
|
|
|
|
Sleep(50);
|
|
|
|
}
|
2017-05-17 11:29:28 +02:00
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void MainMessageLoopExternalPumpWin::OnScheduleMessagePumpWork(
|
|
|
|
int64_t delay_ms) {
|
2016-05-04 20:00:03 +02:00
|
|
|
// This method may be called on any thread.
|
|
|
|
PostMessage(main_thread_target_, kMsgHaveWork, 0,
|
|
|
|
static_cast<LPARAM>(delay_ms));
|
|
|
|
}
|
|
|
|
|
2023-06-01 16:06:15 +02:00
|
|
|
void MainMessageLoopExternalPumpWin::SetTimer(int64_t delay_ms) {
|
2016-05-04 20:00:03 +02:00
|
|
|
DCHECK(!timer_pending_);
|
|
|
|
DCHECK_GT(delay_ms, 0);
|
|
|
|
timer_pending_ = true;
|
2021-06-17 22:08:01 +02:00
|
|
|
::SetTimer(main_thread_target_, 1, static_cast<UINT>(delay_ms), nullptr);
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void MainMessageLoopExternalPumpWin::KillTimer() {
|
|
|
|
if (timer_pending_) {
|
|
|
|
::KillTimer(main_thread_target_, 1);
|
|
|
|
timer_pending_ = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
2017-05-17 11:29:28 +02:00
|
|
|
LRESULT CALLBACK MainMessageLoopExternalPumpWin::WndProc(HWND hwnd,
|
|
|
|
UINT msg,
|
|
|
|
WPARAM wparam,
|
|
|
|
LPARAM lparam) {
|
2016-05-04 20:00:03 +02:00
|
|
|
if (msg == WM_TIMER || msg == kMsgHaveWork) {
|
|
|
|
MainMessageLoopExternalPumpWin* message_loop =
|
|
|
|
GetUserDataPtr<MainMessageLoopExternalPumpWin*>(hwnd);
|
|
|
|
if (msg == kMsgHaveWork) {
|
|
|
|
// OnScheduleMessagePumpWork() request.
|
2023-06-01 16:06:15 +02:00
|
|
|
const int64_t delay_ms = static_cast<int64_t>(lparam);
|
2016-05-04 20:00:03 +02:00
|
|
|
message_loop->OnScheduleWork(delay_ms);
|
|
|
|
} else {
|
|
|
|
// Timer timed out.
|
|
|
|
message_loop->OnTimerTimeout();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return DefWindowProc(hwnd, msg, wparam, lparam);
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
} // namespace
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
// static
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<MainMessageLoopExternalPump>
|
|
|
|
MainMessageLoopExternalPump::Create() {
|
|
|
|
return std::make_unique<MainMessageLoopExternalPumpWin>();
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace client
|