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.
|
|
|
|
|
|
|
|
#include "libcef/browser/browser_message_loop.h"
|
2016-05-04 20:00:03 +02:00
|
|
|
#include "libcef/browser/context.h"
|
|
|
|
#include "libcef/common/content_client.h"
|
|
|
|
|
2016-05-25 01:35:43 +02:00
|
|
|
#include "base/memory/ptr_util.h"
|
2019-07-16 19:59:21 +02:00
|
|
|
#include "base/message_loop/message_pump.h"
|
2019-03-13 22:27:37 +01:00
|
|
|
#include "base/message_loop/message_pump_for_ui.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
#include "base/mac/scoped_nsautorelease_pool.h"
|
2019-03-13 22:27:37 +01:00
|
|
|
#include "base/message_loop/message_pump_mac.h"
|
2016-05-04 20:00:03 +02:00
|
|
|
#endif
|
|
|
|
|
2019-03-13 22:27:37 +01:00
|
|
|
#include "content/public/browser/browser_thread.h"
|
|
|
|
|
2016-05-04 20:00:03 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// MessagePump implementation that delegates to OnScheduleMessagePumpWork() for
|
|
|
|
// scheduling.
|
2019-01-11 15:40:47 +01:00
|
|
|
class MessagePumpExternal : public base::MessagePumpForUI {
|
2016-05-04 20:00:03 +02:00
|
|
|
public:
|
|
|
|
MessagePumpExternal(float max_time_slice,
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler)
|
2017-05-17 11:29:28 +02:00
|
|
|
: max_time_slice_(max_time_slice), handler_(handler) {}
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
void Run(Delegate* delegate) override {
|
|
|
|
base::TimeTicks start = base::TimeTicks::Now();
|
|
|
|
while (true) {
|
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
base::mac::ScopedNSAutoreleasePool autorelease_pool;
|
|
|
|
#endif
|
|
|
|
|
2020-04-14 21:31:00 +02:00
|
|
|
base::TimeTicks next_run_time; // is_null()
|
|
|
|
const bool has_more_work = DirectRunWork(delegate, &next_run_time);
|
2016-05-04 20:00:03 +02:00
|
|
|
if (!has_more_work)
|
|
|
|
break;
|
|
|
|
|
2020-04-14 21:31:00 +02:00
|
|
|
if (next_run_time.is_null()) {
|
|
|
|
// We have more work that should run immediately.
|
|
|
|
next_run_time = base::TimeTicks::Now();
|
|
|
|
}
|
|
|
|
|
|
|
|
const base::TimeDelta& delta = next_run_time - start;
|
2016-05-04 20:00:03 +02:00
|
|
|
if (delta.InSecondsF() > max_time_slice_)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void Quit() override {}
|
2016-05-04 20:00:03 +02:00
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
void ScheduleWork() override { handler_->OnScheduleMessagePumpWork(0); }
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
void ScheduleDelayedWork(const base::TimeTicks& delayed_work_time) override {
|
|
|
|
const base::TimeDelta& delta = delayed_work_time - base::TimeTicks::Now();
|
|
|
|
handler_->OnScheduleMessagePumpWork(delta.InMilliseconds());
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-04-14 21:31:00 +02:00
|
|
|
static bool DirectRunWork(Delegate* delegate,
|
|
|
|
base::TimeTicks* next_run_time) {
|
|
|
|
bool more_immediate_work = false;
|
|
|
|
bool more_idle_work = false;
|
|
|
|
bool more_delayed_work = false;
|
|
|
|
|
|
|
|
Delegate::NextWorkInfo next_work_info = delegate->DoWork();
|
|
|
|
|
|
|
|
// is_immediate() returns true if the next task is ready right away.
|
|
|
|
more_immediate_work = next_work_info.is_immediate();
|
|
|
|
if (!more_immediate_work) {
|
|
|
|
// DoIdleWork() returns true if idle work was all done.
|
|
|
|
more_idle_work = !delegate->DoIdleWork();
|
|
|
|
|
|
|
|
// Check the next PendingTask's |delayed_run_time|.
|
|
|
|
// is_max() returns true if there are no more immediate nor delayed tasks.
|
|
|
|
more_delayed_work = !next_work_info.delayed_run_time.is_max();
|
|
|
|
if (more_delayed_work && !more_idle_work) {
|
|
|
|
// The only remaining work that we know about is the PendingTask.
|
|
|
|
// Consider the run time for that task in the time slice calculation.
|
|
|
|
*next_run_time = next_work_info.delayed_run_time;
|
|
|
|
}
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
2020-04-14 21:31:00 +02:00
|
|
|
return more_immediate_work || more_idle_work || more_delayed_work;
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const float max_time_slice_;
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() {
|
|
|
|
CefRefPtr<CefApp> app = CefContentClient::Get()->application();
|
|
|
|
if (app)
|
|
|
|
return app->GetBrowserProcessHandler();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2019-03-13 22:27:37 +01:00
|
|
|
std::unique_ptr<base::MessagePump> MessagePumpFactoryForUI() {
|
|
|
|
if (!content::BrowserThread::IsThreadInitialized(
|
|
|
|
content::BrowserThread::UI) ||
|
|
|
|
content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
2016-05-04 20:00:03 +02:00
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler = GetBrowserProcessHandler();
|
|
|
|
if (handler)
|
2019-03-13 22:27:37 +01:00
|
|
|
return std::make_unique<MessagePumpExternal>(0.01f, handler);
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
2019-03-13 22:27:37 +01:00
|
|
|
#if defined(OS_MACOSX)
|
|
|
|
return base::MessagePumpMac::Create();
|
|
|
|
#else
|
|
|
|
return std::make_unique<base::MessagePumpForUI>();
|
|
|
|
#endif
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2019-03-13 22:27:37 +01:00
|
|
|
void InitMessagePumpFactoryForUI() {
|
|
|
|
const CefSettings& settings = CefContext::Get()->settings();
|
|
|
|
if (settings.external_message_pump) {
|
2019-07-16 19:59:21 +02:00
|
|
|
base::MessagePump::OverrideMessagePumpForUIFactory(MessagePumpFactoryForUI);
|
2019-03-13 22:27:37 +01:00
|
|
|
}
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|