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"
|
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"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
const bool has_more_work = DirectRunWork(delegate);
|
|
|
|
if (!has_more_work)
|
|
|
|
break;
|
|
|
|
|
|
|
|
const base::TimeDelta& delta = base::TimeTicks::Now() - start;
|
|
|
|
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:
|
|
|
|
bool DirectRunWork(Delegate* delegate) {
|
|
|
|
bool did_work = false;
|
|
|
|
bool did_delayed_work = false;
|
|
|
|
bool did_idle_work = false;
|
|
|
|
|
|
|
|
// Perform work & delayed work.
|
|
|
|
// If no work was found, then perform idle work.
|
|
|
|
|
|
|
|
did_work = delegate->DoWork();
|
|
|
|
|
|
|
|
// We are using an external timer, so we don't have any action based on the
|
|
|
|
// returned next delayed work time.
|
|
|
|
base::TimeTicks next_time;
|
2017-05-17 11:29:28 +02:00
|
|
|
did_delayed_work = delegate->DoDelayedWork(&next_time);
|
2016-05-04 20:00:03 +02:00
|
|
|
|
|
|
|
if (!did_work && !did_delayed_work) {
|
|
|
|
did_idle_work = delegate->DoIdleWork();
|
|
|
|
}
|
|
|
|
|
|
|
|
return did_work || did_delayed_work || did_idle_work;
|
|
|
|
}
|
|
|
|
|
|
|
|
const float max_time_slice_;
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler_;
|
|
|
|
};
|
|
|
|
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() {
|
|
|
|
CefRefPtr<CefApp> app = CefContentClient::Get()->application();
|
|
|
|
if (app)
|
|
|
|
return app->GetBrowserProcessHandler();
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2016-05-25 01:35:43 +02:00
|
|
|
std::unique_ptr<base::MessagePump> CreatePump() {
|
2016-05-04 20:00:03 +02:00
|
|
|
const CefSettings& settings = CefContext::Get()->settings();
|
|
|
|
if (settings.external_message_pump) {
|
|
|
|
CefRefPtr<CefBrowserProcessHandler> handler = GetBrowserProcessHandler();
|
|
|
|
if (handler)
|
2016-05-25 01:35:43 +02:00
|
|
|
return base::WrapUnique(new MessagePumpExternal(0.01f, handler));
|
2016-05-04 20:00:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return base::MessageLoop::CreateMessagePumpForType(
|
|
|
|
base::MessageLoop::TYPE_UI);
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefBrowserMessageLoop::CefBrowserMessageLoop()
|
2017-05-17 11:29:28 +02:00
|
|
|
: base::MessageLoopForUI(CreatePump()) {
|
2016-05-04 20:00:03 +02:00
|
|
|
BindToCurrentThread();
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
CefBrowserMessageLoop::~CefBrowserMessageLoop() {}
|