#include <errno.h>
#include <fcntl.h>
#include <glib.h>
#include <math.h>
#include <memory>
#include "external_pump.h"
#include "include/base/cef_logging.h"
#include "include/cef_app.h"
#if !defined(HANDLE_EINTR)
#if !DCHECK_IS_ON()
#define HANDLE_EINTR(x) \
({ \
decltype(x) eintr_wrapper_result; \
do { \
eintr_wrapper_result = (x); \
} while (eintr_wrapper_result == -1 && errno == EINTR); \
eintr_wrapper_result; \
})
#else
#define HANDLE_EINTR(x) \
({ \
int eintr_wrapper_counter = 0; \
decltype(x) eintr_wrapper_result; \
do { \
eintr_wrapper_result = (x); \
} while (eintr_wrapper_result == -1 && errno == EINTR && \
eintr_wrapper_counter++ < 100); \
eintr_wrapper_result; \
})
#endif #endif
class ExternalPumpLinux : public ExternalPump {
public:
ExternalPumpLinux();
~ExternalPumpLinux();
void OnScheduleMessagePumpWork(int64_t delay_ms) override;
int HandlePrepare();
bool HandleCheck();
void HandleDispatch();
protected:
void SetTimer(int64_t delay_ms) override;
void KillTimer() override;
bool IsTimerPending() override;
private:
GMainContext* context_;
GSource* work_source_;
CefTime delayed_work_time_;
int wakeup_pipe_read_;
int wakeup_pipe_write_;
std::unique_ptr<GPollFD> wakeup_gpollfd_;
};
int GetTimeIntervalMilliseconds(const CefTime& from) {
if (from.GetDoubleT() == 0.0) {
return -1;
}
CefTime now;
now.Now();
int delay =
static_cast<int>(ceil((from.GetDoubleT() - now.GetDoubleT()) * 1000.0));
return delay < 0 ? 0 : delay;
}
struct WorkSource : public GSource {
ExternalPumpLinux* pump;
};
gboolean WorkSourcePrepare(GSource* source, gint* timeout_ms) {
*timeout_ms = static_cast<WorkSource*>(source)->pump->HandlePrepare();
return FALSE;
}
gboolean WorkSourceCheck(GSource* source) {
return static_cast<WorkSource*>(source)->pump->HandleCheck();
}
gboolean WorkSourceDispatch(GSource* source, GSourceFunc unused_func,
gpointer unused_data) {
static_cast<WorkSource*>(source)->pump->HandleDispatch();
return TRUE;
}
GSourceFuncs WorkSourceFuncs = {WorkSourcePrepare, WorkSourceCheck,
WorkSourceDispatch, nullptr};
ExternalPumpLinux::ExternalPumpLinux()
: context_(g_main_context_default()), wakeup_gpollfd_(new GPollFD) {
int fds[2];
int ret = pipe(fds);
DCHECK_EQ(ret, 0);
(void)ret;
wakeup_pipe_read_ = fds[0];
wakeup_pipe_write_ = fds[1];
wakeup_gpollfd_->fd = wakeup_pipe_read_;
wakeup_gpollfd_->events = G_IO_IN;
work_source_ = g_source_new(&WorkSourceFuncs, sizeof(WorkSource));
static_cast<WorkSource*>(work_source_)->pump = this;
g_source_add_poll(work_source_, wakeup_gpollfd_.get());
g_source_set_priority(work_source_, G_PRIORITY_DEFAULT_IDLE);
g_source_set_can_recurse(work_source_, TRUE);
g_source_attach(work_source_, context_);
}
ExternalPumpLinux::~ExternalPumpLinux() {
g_source_destroy(work_source_);
g_source_unref(work_source_);
close(wakeup_pipe_read_);
close(wakeup_pipe_write_);
}
void ExternalPumpLinux::OnScheduleMessagePumpWork(int64_t delay_ms) {
if (HANDLE_EINTR(write(wakeup_pipe_write_, &delay_ms, sizeof(int64_t))) !=
sizeof(int64_t)) {
NOTREACHED() << "Could not write to the UI message loop wakeup pipe!";
}
}
int ExternalPumpLinux::HandlePrepare() {
return GetTimeIntervalMilliseconds(delayed_work_time_);
}
bool ExternalPumpLinux::HandleCheck() {
if (wakeup_gpollfd_->revents & G_IO_IN) {
int64_t delay_ms[2];
const size_t num_bytes =
HANDLE_EINTR(read(wakeup_pipe_read_, delay_ms, sizeof(int64_t) * 2));
if (num_bytes < sizeof(int64_t)) {
NOTREACHED() << "Error reading from the wakeup pipe.";
}
if (num_bytes == sizeof(int64_t)) {
OnScheduleWork(delay_ms[0]);
}
if (num_bytes == sizeof(int64_t) * 2) {
OnScheduleWork(delay_ms[1]);
}
}
if (GetTimeIntervalMilliseconds(delayed_work_time_) == 0) {
return true;
}
return false;
}
void ExternalPumpLinux::HandleDispatch() { OnTimerTimeout(); }
void ExternalPumpLinux::SetTimer(int64_t delay_ms) {
DCHECK_GT(delay_ms, 0);
CefTime now;
now.Now();
delayed_work_time_ =
CefTime(now.GetDoubleT() + static_cast<double>(delay_ms) / 1000.0);
}
void ExternalPumpLinux::KillTimer() { delayed_work_time_ = CefTime(); }
bool ExternalPumpLinux::IsTimerPending() {
return GetTimeIntervalMilliseconds(delayed_work_time_) > 0;
}
std::unique_ptr<ExternalPump> ExternalPump::Create() {
return std::make_unique<ExternalPumpLinux>();
}