Improve support for a host owned message pump (issue #1805)
- Add new CefSettings.external_message_pump option and CefBrowserProcessHandler::OnScheduleMessagePumpWork() callback. - Improve documentation related to CefDoMessageLoopWork(). - Pass `--external-message-pump` command-line flag to cefclient or cef_unittests to test the new mode.
This commit is contained in:
parent
52f9aacdf5
commit
1ff26aa02a
14
cef.gyp
14
cef.gyp
|
@ -365,6 +365,13 @@
|
|||
'sources': [
|
||||
'tests/cefclient/browser/client_app_browser.cc',
|
||||
'tests/cefclient/browser/client_app_browser.h',
|
||||
'tests/cefclient/browser/main_message_loop.cc',
|
||||
'tests/cefclient/browser/main_message_loop.h',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump.cc',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump.h',
|
||||
'tests/cefclient/browser/main_message_loop_std.cc',
|
||||
'tests/cefclient/browser/main_message_loop_std.h',
|
||||
'tests/cefclient/browser/resource_util.h',
|
||||
'tests/cefclient/browser/resource_util.cc',
|
||||
'tests/cefclient/browser/resource_util.h',
|
||||
'tests/cefclient/common/client_app.cc',
|
||||
|
@ -468,7 +475,10 @@
|
|||
'libcef',
|
||||
],
|
||||
'sources': [
|
||||
'tests/cefclient/browser/main_message_loop_external_pump_win.cc',
|
||||
'tests/cefclient/browser/resource_util_win.cc',
|
||||
'tests/cefclient/browser/util_win.cc',
|
||||
'tests/cefclient/browser/util_win.h',
|
||||
'tests/cefclient/resources/win/cefclient.rc',
|
||||
],
|
||||
'msvs_settings': {
|
||||
|
@ -541,6 +551,7 @@
|
|||
],
|
||||
},
|
||||
'sources': [
|
||||
'tests/cefclient/browser/main_message_loop_external_pump_mac.mm',
|
||||
'tests/cefclient/browser/resource_util_mac.mm',
|
||||
'tests/cefclient/browser/resource_util_posix.cc',
|
||||
'tests/unittests/os_rendering_unittest_mac.h',
|
||||
|
@ -553,6 +564,7 @@
|
|||
'libcef',
|
||||
],
|
||||
'sources': [
|
||||
'tests/cefclient/browser/main_message_loop_external_pump_linux.cc',
|
||||
'tests/cefclient/browser/resource_util_linux.cc',
|
||||
'tests/cefclient/browser/resource_util_posix.cc',
|
||||
],
|
||||
|
@ -1932,8 +1944,6 @@
|
|||
'tests',
|
||||
],
|
||||
'sources': [
|
||||
'tests/cefclient/browser/client_app_browser.cc',
|
||||
'tests/cefclient/browser/client_app_browser.h',
|
||||
'tests/cefclient/browser/resource_util.cc',
|
||||
'tests/cefclient/browser/resource_util.h',
|
||||
'tests/cefclient/browser/resource_util_mac.mm',
|
||||
|
|
|
@ -167,6 +167,8 @@
|
|||
'tests/cefclient/browser/main_context_impl.h',
|
||||
'tests/cefclient/browser/main_message_loop.h',
|
||||
'tests/cefclient/browser/main_message_loop.cc',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump.cc',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump.h',
|
||||
'tests/cefclient/browser/main_message_loop_std.h',
|
||||
'tests/cefclient/browser/main_message_loop_std.cc',
|
||||
'tests/cefclient/browser/osr_dragdrop_events.h',
|
||||
|
@ -247,6 +249,7 @@
|
|||
'tests/cefclient/browser/browser_window_std_win.cc',
|
||||
'tests/cefclient/browser/browser_window_std_win.h',
|
||||
'tests/cefclient/browser/main_context_impl_win.cc',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump_win.cc',
|
||||
'tests/cefclient/browser/main_message_loop_multithreaded_win.cc',
|
||||
'tests/cefclient/browser/main_message_loop_multithreaded_win.h',
|
||||
'tests/cefclient/browser/osr_dragdrop_win.cc',
|
||||
|
@ -284,6 +287,7 @@
|
|||
'tests/cefclient/browser/browser_window_std_mac.h',
|
||||
'tests/cefclient/browser/browser_window_std_mac.mm',
|
||||
'tests/cefclient/browser/main_context_impl_posix.cc',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump_mac.mm',
|
||||
'tests/cefclient/browser/resource_util_mac.mm',
|
||||
'tests/cefclient/browser/resource_util_posix.cc',
|
||||
'tests/cefclient/browser/root_window_mac.h',
|
||||
|
@ -316,6 +320,7 @@
|
|||
'tests/cefclient/browser/dialog_handler_gtk.cc',
|
||||
'tests/cefclient/browser/dialog_handler_gtk.h',
|
||||
'tests/cefclient/browser/main_context_impl_posix.cc',
|
||||
'tests/cefclient/browser/main_message_loop_external_pump_linux.cc',
|
||||
'tests/cefclient/browser/print_handler_gtk.cc',
|
||||
'tests/cefclient/browser/print_handler_gtk.h',
|
||||
'tests/cefclient/browser/resource_util_linux.cc',
|
||||
|
|
|
@ -39,6 +39,9 @@
|
|||
// Chromium uses movable types.
|
||||
#define MOVE_SCOPED_PTR(var) std::move(var)
|
||||
|
||||
// Chromium uses std types.
|
||||
#define DEFAULT_DELETER(type) std::default_delete<type>
|
||||
|
||||
#else // !USING_CHROMIUM_INCLUDES
|
||||
// The following is substantially similar to the Chromium implementation.
|
||||
// If the Chromium implementation diverges the below implementation should be
|
||||
|
@ -50,6 +53,9 @@
|
|||
// CEF does not use movable types.
|
||||
#define MOVE_SCOPED_PTR(var) var.Pass()
|
||||
|
||||
// CEF uses base types.
|
||||
#define DEFAULT_DELETER(type) struct base::DefaultDeleter<type>
|
||||
|
||||
#if !defined(arraysize)
|
||||
|
||||
// The arraysize(arr) macro returns the # of elements in an array arr.
|
||||
|
|
|
@ -144,11 +144,18 @@ CEF_EXPORT void cef_shutdown();
|
|||
|
||||
///
|
||||
// Perform a single iteration of CEF message loop processing. This function is
|
||||
// used to integrate the CEF message loop into an existing application message
|
||||
// loop. Care must be taken to balance performance against excessive CPU usage.
|
||||
// This function should only be called on the main application thread and only
|
||||
// if cef_initialize() is called with a CefSettings.multi_threaded_message_loop
|
||||
// value of false (0). This function will not block.
|
||||
// provided for cases where the CEF message loop must be integrated into an
|
||||
// existing application message loop. Use of this function is not recommended
|
||||
// for most users; use either the cef_run_message_loop() function or
|
||||
// CefSettings.multi_threaded_message_loop if possible. When using this function
|
||||
// care must be taken to balance performance against excessive CPU usage. It is
|
||||
// recommended to enable the CefSettings.external_message_pump option when using
|
||||
// this function so that
|
||||
// cef_browser_process_handler_t::on_schedule_message_pump_work() callbacks can
|
||||
// facilitate the scheduling process. This function should only be called on the
|
||||
// main application thread and only if cef_initialize() is called with a
|
||||
// CefSettings.multi_threaded_message_loop value of false (0). This function
|
||||
// will not block.
|
||||
///
|
||||
CEF_EXPORT void cef_do_message_loop_work();
|
||||
|
||||
|
|
|
@ -94,6 +94,22 @@ typedef struct _cef_browser_process_handler_t {
|
|||
///
|
||||
struct _cef_print_handler_t* (CEF_CALLBACK *get_print_handler)(
|
||||
struct _cef_browser_process_handler_t* self);
|
||||
|
||||
///
|
||||
// Called from any thread when work has been scheduled for the browser process
|
||||
// main (UI) thread. This callback is used in combination with CefSettings.
|
||||
// external_message_pump and cef_do_message_loop_work() in cases where the CEF
|
||||
// message loop must be integrated into an existing application message loop
|
||||
// (see additional comments and warnings on CefDoMessageLoopWork). This
|
||||
// callback should schedule a cef_do_message_loop_work() call to happen on the
|
||||
// main (UI) thread. |delay_ms| is the requested delay in milliseconds. If
|
||||
// |delay_ms| is <= 0 then the call should happen reasonably soon. If
|
||||
// |delay_ms| is > 0 then the call should be scheduled to happen after the
|
||||
// specified delay and any currently pending scheduled call should be
|
||||
// cancelled.
|
||||
///
|
||||
void (CEF_CALLBACK *on_schedule_message_pump_work)(
|
||||
struct _cef_browser_process_handler_t* self, int64 delay_ms);
|
||||
} cef_browser_process_handler_t;
|
||||
|
||||
|
||||
|
|
|
@ -89,11 +89,17 @@ void CefShutdown();
|
|||
|
||||
///
|
||||
// Perform a single iteration of CEF message loop processing. This function is
|
||||
// used to integrate the CEF message loop into an existing application message
|
||||
// loop. Care must be taken to balance performance against excessive CPU usage.
|
||||
// This function should only be called on the main application thread and only
|
||||
// if CefInitialize() is called with a CefSettings.multi_threaded_message_loop
|
||||
// value of false. This function will not block.
|
||||
// provided for cases where the CEF message loop must be integrated into an
|
||||
// existing application message loop. Use of this function is not recommended
|
||||
// for most users; use either the CefRunMessageLoop() function or
|
||||
// CefSettings.multi_threaded_message_loop if possible. When using this function
|
||||
// care must be taken to balance performance against excessive CPU usage. It is
|
||||
// recommended to enable the CefSettings.external_message_pump option when using
|
||||
// this function so that CefBrowserProcessHandler::OnScheduleMessagePumpWork()
|
||||
// callbacks can facilitate the scheduling process. This function should only be
|
||||
// called on the main application thread and only if CefInitialize() is called
|
||||
// with a CefSettings.multi_threaded_message_loop value of false. This function
|
||||
// will not block.
|
||||
///
|
||||
/*--cef()--*/
|
||||
void CefDoMessageLoopWork();
|
||||
|
|
|
@ -87,6 +87,22 @@ class CefBrowserProcessHandler : public virtual CefBase {
|
|||
virtual CefRefPtr<CefPrintHandler> GetPrintHandler() {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
///
|
||||
// Called from any thread when work has been scheduled for the browser process
|
||||
// main (UI) thread. This callback is used in combination with CefSettings.
|
||||
// external_message_pump and CefDoMessageLoopWork() in cases where the CEF
|
||||
// message loop must be integrated into an existing application message loop
|
||||
// (see additional comments and warnings on CefDoMessageLoopWork). This
|
||||
// callback should schedule a CefDoMessageLoopWork() call to happen on the
|
||||
// main (UI) thread. |delay_ms| is the requested delay in milliseconds. If
|
||||
// |delay_ms| is <= 0 then the call should happen reasonably soon. If
|
||||
// |delay_ms| is > 0 then the call should be scheduled to happen after the
|
||||
// specified delay and any currently pending scheduled call should be
|
||||
// cancelled.
|
||||
///
|
||||
/*--cef()--*/
|
||||
virtual void OnScheduleMessagePumpWork(int64 delay_ms) {}
|
||||
};
|
||||
|
||||
#endif // CEF_INCLUDE_CEF_BROWSER_PROCESS_HANDLER_H_
|
||||
|
|
|
@ -180,6 +180,18 @@ typedef struct _cef_settings_t {
|
|||
///
|
||||
int multi_threaded_message_loop;
|
||||
|
||||
///
|
||||
// Set to true (1) to control browser process main (UI) thread message pump
|
||||
// scheduling via the CefBrowserProcessHandler::OnScheduleMessagePumpWork()
|
||||
// callback. This option is recommended for use in combination with the
|
||||
// CefDoMessageLoopWork() function in cases where the CEF message loop must be
|
||||
// integrated into an existing application message loop (see additional
|
||||
// comments and warnings on CefDoMessageLoopWork). Enabling this option is not
|
||||
// recommended for most users; leave this option disabled and use either the
|
||||
// CefRunMessageLoop() function or multi_threaded_message_loop if possible.
|
||||
///
|
||||
int external_message_pump;
|
||||
|
||||
///
|
||||
// Set to true (1) to enable windowless (off-screen) rendering support. Do not
|
||||
// enable this value if the application does not use windowless rendering as
|
||||
|
|
|
@ -575,6 +575,7 @@ struct CefSettingsTraits {
|
|||
src->browser_subprocess_path.length,
|
||||
&target->browser_subprocess_path, copy);
|
||||
target->multi_threaded_message_loop = src->multi_threaded_message_loop;
|
||||
target->external_message_pump = src->external_message_pump;
|
||||
target->windowless_rendering_enabled = src->windowless_rendering_enabled;
|
||||
target->command_line_args_disabled = src->command_line_args_disabled;
|
||||
|
||||
|
|
|
@ -3,9 +3,107 @@
|
|||
// be found in the LICENSE file.
|
||||
|
||||
#include "libcef/browser/browser_message_loop.h"
|
||||
#include "libcef/browser/context.h"
|
||||
#include "libcef/common/content_client.h"
|
||||
|
||||
#include "base/run_loop.h"
|
||||
|
||||
CefBrowserMessageLoop::CefBrowserMessageLoop() {
|
||||
#if defined(OS_MACOSX)
|
||||
#include "base/mac/scoped_nsautorelease_pool.h"
|
||||
#endif
|
||||
|
||||
namespace {
|
||||
|
||||
// MessagePump implementation that delegates to OnScheduleMessagePumpWork() for
|
||||
// scheduling.
|
||||
class MessagePumpExternal : public base::MessagePump {
|
||||
public:
|
||||
MessagePumpExternal(float max_time_slice,
|
||||
CefRefPtr<CefBrowserProcessHandler> handler)
|
||||
: max_time_slice_(max_time_slice),
|
||||
handler_(handler) {
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void Quit() override {
|
||||
}
|
||||
|
||||
void ScheduleWork() override {
|
||||
handler_->OnScheduleMessagePumpWork(0);
|
||||
}
|
||||
|
||||
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;
|
||||
did_delayed_work = delegate->DoDelayedWork(&next_time);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
scoped_ptr<base::MessagePump> CreatePump() {
|
||||
const CefSettings& settings = CefContext::Get()->settings();
|
||||
if (settings.external_message_pump) {
|
||||
CefRefPtr<CefBrowserProcessHandler> handler = GetBrowserProcessHandler();
|
||||
if (handler)
|
||||
return make_scoped_ptr(new MessagePumpExternal(0.01f, handler));
|
||||
}
|
||||
|
||||
return base::MessageLoop::CreateMessagePumpForType(
|
||||
base::MessageLoop::TYPE_UI);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
CefBrowserMessageLoop::CefBrowserMessageLoop()
|
||||
: base::MessageLoopForUI(CreatePump()) {
|
||||
BindToCurrentThread();
|
||||
}
|
||||
|
||||
CefBrowserMessageLoop::~CefBrowserMessageLoop() {
|
||||
|
@ -14,7 +112,7 @@ CefBrowserMessageLoop::~CefBrowserMessageLoop() {
|
|||
// static
|
||||
CefBrowserMessageLoop* CefBrowserMessageLoop::current() {
|
||||
base::MessageLoop* loop = base::MessageLoop::current();
|
||||
DCHECK_EQ(base::MessageLoop::TYPE_UI, loop->type());
|
||||
DCHECK(loop->IsType(base::MessageLoop::TYPE_UI));
|
||||
return static_cast<CefBrowserMessageLoop*>(loop);
|
||||
}
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@ class CefBrowserMessageLoop : public base::MessageLoopForUI {
|
|||
CefBrowserMessageLoop();
|
||||
~CefBrowserMessageLoop() override;
|
||||
|
||||
// Returns the MessageLoopForUI of the current thread.
|
||||
// Returns the CefBrowserMessageLoop of the current thread.
|
||||
static CefBrowserMessageLoop* current();
|
||||
|
||||
// Do a single interation of the UI message loop.
|
||||
|
|
|
@ -84,6 +84,19 @@ struct _cef_print_handler_t* CEF_CALLBACK browser_process_handler_get_print_hand
|
|||
return CefPrintHandlerCppToC::Wrap(_retval);
|
||||
}
|
||||
|
||||
void CEF_CALLBACK browser_process_handler_on_schedule_message_pump_work(
|
||||
struct _cef_browser_process_handler_t* self, int64 delay_ms) {
|
||||
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
|
||||
|
||||
DCHECK(self);
|
||||
if (!self)
|
||||
return;
|
||||
|
||||
// Execute
|
||||
CefBrowserProcessHandlerCppToC::Get(self)->OnScheduleMessagePumpWork(
|
||||
delay_ms);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
|
||||
|
@ -97,6 +110,8 @@ CefBrowserProcessHandlerCppToC::CefBrowserProcessHandlerCppToC() {
|
|||
GetStruct()->on_render_process_thread_created =
|
||||
browser_process_handler_on_render_process_thread_created;
|
||||
GetStruct()->get_print_handler = browser_process_handler_get_print_handler;
|
||||
GetStruct()->on_schedule_message_pump_work =
|
||||
browser_process_handler_on_schedule_message_pump_work;
|
||||
}
|
||||
|
||||
template<> CefRefPtr<CefBrowserProcessHandler> CefCppToC<CefBrowserProcessHandlerCppToC,
|
||||
|
|
|
@ -79,6 +79,18 @@ CefRefPtr<CefPrintHandler> CefBrowserProcessHandlerCToCpp::GetPrintHandler() {
|
|||
return CefPrintHandlerCToCpp::Wrap(_retval);
|
||||
}
|
||||
|
||||
void CefBrowserProcessHandlerCToCpp::OnScheduleMessagePumpWork(int64 delay_ms) {
|
||||
cef_browser_process_handler_t* _struct = GetStruct();
|
||||
if (CEF_MEMBER_MISSING(_struct, on_schedule_message_pump_work))
|
||||
return;
|
||||
|
||||
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
|
||||
|
||||
// Execute
|
||||
_struct->on_schedule_message_pump_work(_struct,
|
||||
delay_ms);
|
||||
}
|
||||
|
||||
|
||||
// CONSTRUCTOR - Do not edit by hand.
|
||||
|
||||
|
|
|
@ -37,6 +37,7 @@ class CefBrowserProcessHandlerCToCpp
|
|||
void OnRenderProcessThreadCreated(
|
||||
CefRefPtr<CefListValue> extra_info) override;
|
||||
CefRefPtr<CefPrintHandler> GetPrintHandler() override;
|
||||
void OnScheduleMessagePumpWork(int64 delay_ms) override;
|
||||
};
|
||||
|
||||
#endif // BUILDING_CEF_SHARED
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
#include "include/base/cef_logging.h"
|
||||
#include "include/cef_cookie.h"
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
#include "cefclient/common/client_switches.h"
|
||||
|
||||
namespace client {
|
||||
|
@ -82,4 +83,12 @@ void ClientAppBrowser::OnRenderProcessThreadCreated(
|
|||
(*it)->OnRenderProcessThreadCreated(this, extra_info);
|
||||
}
|
||||
|
||||
void ClientAppBrowser::OnScheduleMessagePumpWork(int64 delay) {
|
||||
// Only used when `--external-message-pump` is passed via the command-line.
|
||||
MainMessageLoopExternalPump* message_pump =
|
||||
MainMessageLoopExternalPump::Get();
|
||||
if (message_pump)
|
||||
message_pump->OnScheduleMessagePumpWork(delay);
|
||||
}
|
||||
|
||||
} // namespace client
|
||||
|
|
|
@ -66,6 +66,7 @@ class ClientAppBrowser : public ClientApp,
|
|||
CefRefPtr<CefPrintHandler> GetPrintHandler() OVERRIDE {
|
||||
return print_handler_;
|
||||
}
|
||||
void OnScheduleMessagePumpWork(int64 delay) OVERRIDE;
|
||||
|
||||
// Set of supported Delegates.
|
||||
DelegateSet delegates_;
|
||||
|
|
|
@ -113,6 +113,11 @@ void MainContextImpl::PopulateSettings(CefSettings* settings) {
|
|||
command_line_->HasSwitch(switches::kMultiThreadedMessageLoop);
|
||||
#endif
|
||||
|
||||
if (!settings->multi_threaded_message_loop) {
|
||||
settings->external_message_pump =
|
||||
command_line_->HasSwitch(switches::kExternalMessagePump);
|
||||
}
|
||||
|
||||
CefString(&settings->cache_path) =
|
||||
command_line_->GetSwitchValue(switches::kCachePath);
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ class MainMessageLoop {
|
|||
|
||||
protected:
|
||||
// Only allow deletion via scoped_ptr.
|
||||
friend struct base::DefaultDeleter<MainMessageLoop>;
|
||||
friend DEFAULT_DELETER(MainMessageLoop);
|
||||
|
||||
MainMessageLoop();
|
||||
virtual ~MainMessageLoop();
|
||||
|
|
|
@ -0,0 +1,108 @@
|
|||
// 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.
|
||||
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
|
||||
#include <climits>
|
||||
|
||||
#include "include/cef_app.h"
|
||||
#include "include/wrapper/cef_helpers.h"
|
||||
#include "cefclient/browser/main_message_loop.h"
|
||||
|
||||
namespace client {
|
||||
|
||||
namespace {
|
||||
|
||||
// Special timer delay placeholder value. Intentionally 32-bit for Windows and
|
||||
// OS X platform API compatibility.
|
||||
const int32 kTimerDelayPlaceholder = INT_MAX;
|
||||
|
||||
// The maximum number of milliseconds we're willing to wait between calls to
|
||||
// DoWork().
|
||||
const int64 kMaxTimerDelay = 1000 / 30; // 30fps
|
||||
|
||||
client::MainMessageLoopExternalPump* g_external_message_pump = NULL;
|
||||
|
||||
} // namespace
|
||||
|
||||
MainMessageLoopExternalPump::MainMessageLoopExternalPump()
|
||||
: is_active_(false),
|
||||
reentrancy_detected_(false) {
|
||||
DCHECK(!g_external_message_pump);
|
||||
g_external_message_pump = this;
|
||||
}
|
||||
|
||||
MainMessageLoopExternalPump::~MainMessageLoopExternalPump() {
|
||||
g_external_message_pump = NULL;
|
||||
}
|
||||
|
||||
MainMessageLoopExternalPump* MainMessageLoopExternalPump::Get() {
|
||||
return g_external_message_pump;
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPump::OnScheduleWork(int64 delay_ms) {
|
||||
REQUIRE_MAIN_THREAD();
|
||||
|
||||
if (delay_ms == kTimerDelayPlaceholder && IsTimerPending()) {
|
||||
// Don't set the maximum timer requested from DoWork() if a timer event is
|
||||
// currently pending.
|
||||
return;
|
||||
}
|
||||
|
||||
KillTimer();
|
||||
|
||||
if (delay_ms <= 0) {
|
||||
// Execute the work immediately.
|
||||
DoWork();
|
||||
} else {
|
||||
// Never wait longer than the maximum allowed time.
|
||||
if (delay_ms > kMaxTimerDelay)
|
||||
delay_ms = kMaxTimerDelay;
|
||||
|
||||
// Results in call to OnTimerTimeout() after the specified delay.
|
||||
SetTimer(delay_ms);
|
||||
}
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPump::OnTimerTimeout() {
|
||||
REQUIRE_MAIN_THREAD();
|
||||
|
||||
KillTimer();
|
||||
DoWork();
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPump::DoWork() {
|
||||
const bool was_reentrant = PerformMessageLoopWork();
|
||||
if (was_reentrant) {
|
||||
// Execute the remaining work as soon as possible.
|
||||
OnScheduleMessagePumpWork(0);
|
||||
} else if (!IsTimerPending()) {
|
||||
// Schedule a timer event at the maximum allowed time. This may be dropped
|
||||
// in OnScheduleWork() if another timer event is already in-flight.
|
||||
OnScheduleMessagePumpWork(kTimerDelayPlaceholder);
|
||||
}
|
||||
}
|
||||
|
||||
bool MainMessageLoopExternalPump::PerformMessageLoopWork() {
|
||||
if (is_active_) {
|
||||
// When CefDoMessageLoopWork() is called there may be various callbacks
|
||||
// (such as paint and IPC messages) that result in additional calls to this
|
||||
// method. If re-entrancy is detected we must repost a request again to the
|
||||
// owner thread to ensure that the discarded call is executed in the future.
|
||||
reentrancy_detected_ = true;
|
||||
return false;
|
||||
}
|
||||
|
||||
reentrancy_detected_ = false;
|
||||
|
||||
is_active_ = true;
|
||||
CefDoMessageLoopWork();
|
||||
is_active_ = false;
|
||||
|
||||
// |reentrancy_detected_| may have changed due to re-entrant calls to this
|
||||
// method.
|
||||
return reentrancy_detected_;
|
||||
}
|
||||
|
||||
} // namespace client
|
|
@ -0,0 +1,70 @@
|
|||
// 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.
|
||||
|
||||
#ifndef CEF_TESTS_CEFCLIENT_BROWSER_MAIN_MESSAGE_LOOP_EXTERNAL_PUMP_H_
|
||||
#define CEF_TESTS_CEFCLIENT_BROWSER_MAIN_MESSAGE_LOOP_EXTERNAL_PUMP_H_
|
||||
#pragma once
|
||||
|
||||
#include "cefclient/browser/main_message_loop_std.h"
|
||||
|
||||
namespace client {
|
||||
|
||||
// This MessageLoop implementation simulates the embedding of CEF into an
|
||||
// existing host application that runs its own message loop. The scheduling
|
||||
// implementation provided by this class is very simplistic and does not handle
|
||||
// all cases (for example, nested message loops on Windows will not function
|
||||
// correctly). See comments in Chromium's platform-specific
|
||||
// base/message_loop/message_pump_* source files for additional guidance when
|
||||
// implementing CefBrowserProcessHandler::OnScheduleMessagePumpWork() in your
|
||||
// application. Run cefclient or cef_unittests with the
|
||||
// "--external-message-pump" command-line flag to test this mode.
|
||||
class MainMessageLoopExternalPump : public MainMessageLoopStd {
|
||||
public:
|
||||
// Creates the singleton instance of this object. Must be called on the main
|
||||
// application thread.
|
||||
static scoped_ptr<MainMessageLoopExternalPump> Create();
|
||||
|
||||
// Returns the singleton instance of this object. Safe to call from any
|
||||
// thread.
|
||||
static MainMessageLoopExternalPump* Get();
|
||||
|
||||
// Called from CefBrowserProcessHandler::OnScheduleMessagePumpWork() on any
|
||||
// thread. The platform subclass must implement this method and schedule a
|
||||
// call to OnScheduleWork() on the main application thread.
|
||||
virtual void OnScheduleMessagePumpWork(int64 delay_ms) = 0;
|
||||
|
||||
protected:
|
||||
// Only allow deletion via scoped_ptr.
|
||||
friend DEFAULT_DELETER(MainMessageLoopExternalPump);
|
||||
|
||||
// Construct and destruct this object on the main application thread.
|
||||
MainMessageLoopExternalPump();
|
||||
~MainMessageLoopExternalPump();
|
||||
|
||||
// The platform subclass calls this method on the main application thread in
|
||||
// response to the OnScheduleMessagePumpWork() call.
|
||||
void OnScheduleWork(int64 delay_ms);
|
||||
|
||||
// The platform subclass calls this method on the main application thread when
|
||||
// the pending work timer times out.
|
||||
void OnTimerTimeout();
|
||||
|
||||
// Control the pending work timer in the platform subclass. Only called on
|
||||
// the main application thread.
|
||||
virtual void SetTimer(int64 delay_ms) = 0;
|
||||
virtual void KillTimer() = 0;
|
||||
virtual bool IsTimerPending() = 0;
|
||||
|
||||
private:
|
||||
// Handle work processing.
|
||||
void DoWork();
|
||||
bool PerformMessageLoopWork();
|
||||
|
||||
bool is_active_;
|
||||
bool reentrancy_detected_;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
#endif // CEF_TESTS_CEFCLIENT_BROWSER_MAIN_MESSAGE_LOOP_EXTERNAL_PUMP_H_
|
|
@ -0,0 +1,304 @@
|
|||
// 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.
|
||||
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <math.h>
|
||||
|
||||
#include <glib.h>
|
||||
|
||||
#include "include/cef_app.h"
|
||||
|
||||
// From base/posix/eintr_wrapper.h.
|
||||
// This provides a wrapper around system calls which may be interrupted by a
|
||||
// signal and return EINTR. See man 7 signal.
|
||||
// To prevent long-lasting loops (which would likely be a bug, such as a signal
|
||||
// that should be masked) to go unnoticed, there is a limit after which the
|
||||
// caller will nonetheless see an EINTR in Debug builds.
|
||||
#if !defined(HANDLE_EINTR)
|
||||
#if defined(NDEBUG)
|
||||
|
||||
#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 // defined(NDEBUG)
|
||||
#endif // !defined(HANDLE_EINTR)
|
||||
|
||||
namespace client {
|
||||
|
||||
namespace {
|
||||
|
||||
class MainMessageLoopExternalPumpLinux : public MainMessageLoopExternalPump {
|
||||
public:
|
||||
MainMessageLoopExternalPumpLinux();
|
||||
~MainMessageLoopExternalPumpLinux();
|
||||
|
||||
// MainMessageLoopStd methods:
|
||||
void Quit() OVERRIDE;
|
||||
int Run() OVERRIDE;
|
||||
|
||||
// MainMessageLoopExternalPump methods:
|
||||
void OnScheduleMessagePumpWork(int64 delay_ms) OVERRIDE;
|
||||
|
||||
// Internal methods used for processing the pump callbacks. They are public
|
||||
// for simplicity but should not be used directly. HandlePrepare is called
|
||||
// during the prepare step of glib, and returns a timeout that will be passed
|
||||
// to the poll. HandleCheck is called after the poll has completed, and
|
||||
// returns whether or not HandleDispatch should be called. HandleDispatch is
|
||||
// called if HandleCheck returned true.
|
||||
int HandlePrepare();
|
||||
bool HandleCheck();
|
||||
void HandleDispatch();
|
||||
|
||||
protected:
|
||||
// MainMessageLoopExternalPump methods:
|
||||
void SetTimer(int64 delay_ms) OVERRIDE;
|
||||
void KillTimer() OVERRIDE;
|
||||
bool IsTimerPending() OVERRIDE;
|
||||
|
||||
private:
|
||||
// Used to flag that the Run() invocation should return ASAP.
|
||||
bool should_quit_;
|
||||
|
||||
// A GLib structure that we can add event sources to. We use the default GLib
|
||||
// context, which is the one to which all GTK events are dispatched.
|
||||
GMainContext* context_;
|
||||
|
||||
// The work source. It is destroyed when the message pump is destroyed.
|
||||
GSource* work_source_;
|
||||
|
||||
// The time when we need to do delayed work.
|
||||
CefTime delayed_work_time_;
|
||||
|
||||
// We use a wakeup pipe to make sure we'll get out of the glib polling phase
|
||||
// when another thread has scheduled us to do some work. There is a glib
|
||||
// mechanism g_main_context_wakeup, but this won't guarantee that our event's
|
||||
// Dispatch() will be called.
|
||||
int wakeup_pipe_read_;
|
||||
int wakeup_pipe_write_;
|
||||
|
||||
// Use a scoped_ptr to avoid needing the definition of GPollFD in the header.
|
||||
scoped_ptr<GPollFD> wakeup_gpollfd_;
|
||||
};
|
||||
|
||||
// Return a timeout suitable for the glib loop, -1 to block forever,
|
||||
// 0 to return right away, or a timeout in milliseconds from now.
|
||||
int GetTimeIntervalMilliseconds(const CefTime& from) {
|
||||
if (from.GetDoubleT() == 0.0)
|
||||
return -1;
|
||||
|
||||
CefTime now;
|
||||
now.Now();
|
||||
|
||||
// Be careful here. CefTime has a precision of microseconds, but we want a
|
||||
// value in milliseconds. If there are 5.5ms left, should the delay be 5 or
|
||||
// 6? It should be 6 to avoid executing delayed work too early.
|
||||
int delay = static_cast<int>(
|
||||
ceil((from.GetDoubleT() - now.GetDoubleT()) * 1000.0));
|
||||
|
||||
// If this value is negative, then we need to run delayed work soon.
|
||||
return delay < 0 ? 0 : delay;
|
||||
}
|
||||
|
||||
struct WorkSource : public GSource {
|
||||
MainMessageLoopExternalPumpLinux* pump;
|
||||
};
|
||||
|
||||
gboolean WorkSourcePrepare(GSource* source,
|
||||
gint* timeout_ms) {
|
||||
*timeout_ms = static_cast<WorkSource*>(source)->pump->HandlePrepare();
|
||||
// We always return FALSE, so that our timeout is honored. If we were
|
||||
// to return TRUE, the timeout would be considered to be 0 and the poll
|
||||
// would never block. Once the poll is finished, Check will be called.
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
gboolean WorkSourceCheck(GSource* source) {
|
||||
// Only return TRUE if Dispatch should be called.
|
||||
return static_cast<WorkSource*>(source)->pump->HandleCheck();
|
||||
}
|
||||
|
||||
gboolean WorkSourceDispatch(GSource* source,
|
||||
GSourceFunc unused_func,
|
||||
gpointer unused_data) {
|
||||
|
||||
static_cast<WorkSource*>(source)->pump->HandleDispatch();
|
||||
// Always return TRUE so our source stays registered.
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
// I wish these could be const, but g_source_new wants non-const.
|
||||
GSourceFuncs WorkSourceFuncs = {
|
||||
WorkSourcePrepare,
|
||||
WorkSourceCheck,
|
||||
WorkSourceDispatch,
|
||||
NULL
|
||||
};
|
||||
|
||||
MainMessageLoopExternalPumpLinux::MainMessageLoopExternalPumpLinux()
|
||||
: should_quit_(false),
|
||||
context_(g_main_context_default()),
|
||||
wakeup_gpollfd_(new GPollFD) {
|
||||
// Create our wakeup pipe, which is used to flag when work was scheduled.
|
||||
int fds[2];
|
||||
int ret = pipe(fds);
|
||||
DCHECK_EQ(ret, 0);
|
||||
(void)ret; // Prevent warning in release mode.
|
||||
|
||||
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());
|
||||
// Use a low priority so that we let other events in the queue go first.
|
||||
g_source_set_priority(work_source_, G_PRIORITY_DEFAULT_IDLE);
|
||||
// This is needed to allow Run calls inside Dispatch.
|
||||
g_source_set_can_recurse(work_source_, TRUE);
|
||||
g_source_attach(work_source_, context_);
|
||||
}
|
||||
|
||||
MainMessageLoopExternalPumpLinux::~MainMessageLoopExternalPumpLinux() {
|
||||
g_source_destroy(work_source_);
|
||||
g_source_unref(work_source_);
|
||||
close(wakeup_pipe_read_);
|
||||
close(wakeup_pipe_write_);
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpLinux::Quit() {
|
||||
should_quit_ = true;
|
||||
}
|
||||
|
||||
int MainMessageLoopExternalPumpLinux::Run() {
|
||||
// We really only do a single task for each iteration of the loop. If we
|
||||
// have done something, assume there is likely something more to do. This
|
||||
// will mean that we don't block on the message pump until there was nothing
|
||||
// more to do. We also set this to true to make sure not to block on the
|
||||
// first iteration of the loop.
|
||||
bool more_work_is_plausible = true;
|
||||
|
||||
// We run our own loop instead of using g_main_loop_quit in one of the
|
||||
// callbacks. This is so we only quit our own loops, and we don't quit
|
||||
// nested loops run by others.
|
||||
for (;;) {
|
||||
// Don't block if we think we have more work to do.
|
||||
bool block = !more_work_is_plausible;
|
||||
|
||||
more_work_is_plausible = g_main_context_iteration(context_, block);
|
||||
if (should_quit_)
|
||||
break;
|
||||
}
|
||||
|
||||
// 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();
|
||||
|
||||
// Sleep to allow the CEF proc to do work.
|
||||
usleep(50000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpLinux::OnScheduleMessagePumpWork(
|
||||
int64 delay_ms) {
|
||||
// This can be called on any thread, so we don't want to touch any state
|
||||
// variables as we would then need locks all over. This ensures that if we
|
||||
// are sleeping in a poll that we will wake up.
|
||||
if (HANDLE_EINTR(write(wakeup_pipe_write_, &delay_ms, sizeof(int64))) !=
|
||||
sizeof(int64)) {
|
||||
NOTREACHED() << "Could not write to the UI message loop wakeup pipe!";
|
||||
}
|
||||
}
|
||||
|
||||
// Return the timeout we want passed to poll.
|
||||
int MainMessageLoopExternalPumpLinux::HandlePrepare() {
|
||||
// We don't think we have work to do, but make sure not to block longer than
|
||||
// the next time we need to run delayed work.
|
||||
return GetTimeIntervalMilliseconds(delayed_work_time_);
|
||||
}
|
||||
|
||||
bool MainMessageLoopExternalPumpLinux::HandleCheck() {
|
||||
// We usually have a single message on the wakeup pipe, since we are only
|
||||
// signaled when the queue went from empty to non-empty, but there can be
|
||||
// two messages if a task posted a task, hence we read at most two bytes.
|
||||
// The glib poll will tell us whether there was data, so this read shouldn't
|
||||
// block.
|
||||
if (wakeup_gpollfd_->revents & G_IO_IN) {
|
||||
int64 delay_ms[2];
|
||||
const size_t num_bytes =
|
||||
HANDLE_EINTR(read(wakeup_pipe_read_, delay_ms, sizeof(int64) * 2));
|
||||
if (num_bytes < sizeof(int64)) {
|
||||
NOTREACHED() << "Error reading from the wakeup pipe.";
|
||||
}
|
||||
if (num_bytes == sizeof(int64))
|
||||
OnScheduleWork(delay_ms[0]);
|
||||
if (num_bytes == sizeof(int64) * 2)
|
||||
OnScheduleWork(delay_ms[1]);
|
||||
}
|
||||
|
||||
if (GetTimeIntervalMilliseconds(delayed_work_time_) == 0) {
|
||||
// The timer has expired. That condition will stay true until we process
|
||||
// that delayed work, so we don't need to record this differently.
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpLinux::HandleDispatch() {
|
||||
OnTimerTimeout();
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpLinux::SetTimer(int64 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 MainMessageLoopExternalPumpLinux::KillTimer() {
|
||||
delayed_work_time_ = CefTime();
|
||||
}
|
||||
|
||||
bool MainMessageLoopExternalPumpLinux::IsTimerPending() {
|
||||
return GetTimeIntervalMilliseconds(delayed_work_time_) > 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
scoped_ptr<MainMessageLoopExternalPump>
|
||||
MainMessageLoopExternalPump::Create() {
|
||||
return make_scoped_ptr<MainMessageLoopExternalPump>(
|
||||
new MainMessageLoopExternalPumpLinux());
|
||||
}
|
||||
|
||||
} // namespace client
|
|
@ -0,0 +1,171 @@
|
|||
// 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.
|
||||
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <AppKit/AppKit.h>
|
||||
|
||||
#include "include/cef_app.h"
|
||||
|
||||
@class EventHandler;
|
||||
|
||||
namespace client {
|
||||
|
||||
class MainMessageLoopExternalPumpMac : public MainMessageLoopExternalPump {
|
||||
public:
|
||||
MainMessageLoopExternalPumpMac();
|
||||
~MainMessageLoopExternalPumpMac();
|
||||
|
||||
// MainMessageLoopStd methods:
|
||||
void Quit() OVERRIDE;
|
||||
int Run() OVERRIDE;
|
||||
|
||||
// MainMessageLoopExternalPump methods:
|
||||
void OnScheduleMessagePumpWork(int64 delay_ms) OVERRIDE;
|
||||
|
||||
// Internal methods used for processing the event callbacks. They are public
|
||||
// for simplicity but should not be used directly.
|
||||
void HandleScheduleWork(int64 delay_ms);
|
||||
void HandleTimerTimeout();
|
||||
|
||||
protected:
|
||||
// MainMessageLoopExternalPump methods:
|
||||
void SetTimer(int64 delay_ms) OVERRIDE;
|
||||
void KillTimer() OVERRIDE;
|
||||
bool IsTimerPending() OVERRIDE { return timer_ != nil; }
|
||||
|
||||
private:
|
||||
// Owner thread that will run events.
|
||||
NSThread* owner_thread_;
|
||||
|
||||
// Pending work timer.
|
||||
NSTimer* timer_;
|
||||
|
||||
// Used to handle event callbacks on the owner thread.
|
||||
EventHandler* event_handler_;
|
||||
};
|
||||
|
||||
} // namespace client
|
||||
|
||||
// Object that handles event callbacks on the owner thread.
|
||||
@interface EventHandler : NSObject {
|
||||
@private
|
||||
client::MainMessageLoopExternalPumpMac* pump_;
|
||||
}
|
||||
|
||||
- (id)initWithPump:(client::MainMessageLoopExternalPumpMac*)pump;
|
||||
- (void)scheduleWork:(NSNumber*)delay_ms;
|
||||
- (void)timerTimeout:(id)obj;
|
||||
@end
|
||||
|
||||
@implementation EventHandler
|
||||
|
||||
- (id)initWithPump:(client::MainMessageLoopExternalPumpMac*)pump {
|
||||
if (self = [super init]) {
|
||||
pump_ = pump;
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)scheduleWork:(NSNumber*)delay_ms {
|
||||
pump_->HandleScheduleWork([delay_ms integerValue]);
|
||||
}
|
||||
|
||||
- (void)timerTimeout:(id)obj {
|
||||
pump_->HandleTimerTimeout();
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
namespace client {
|
||||
|
||||
MainMessageLoopExternalPumpMac::MainMessageLoopExternalPumpMac()
|
||||
: owner_thread_([[NSThread currentThread] retain]),
|
||||
timer_(nil) {
|
||||
event_handler_ = [[[EventHandler alloc] initWithPump:this] retain];
|
||||
}
|
||||
|
||||
MainMessageLoopExternalPumpMac::~MainMessageLoopExternalPumpMac() {
|
||||
KillTimer();
|
||||
[owner_thread_ release];
|
||||
[event_handler_ release];
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpMac::Quit() {
|
||||
[NSApp stop:nil];
|
||||
}
|
||||
|
||||
int MainMessageLoopExternalPumpMac::Run() {
|
||||
// Run the message loop.
|
||||
[NSApp run];
|
||||
|
||||
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) {
|
||||
// Let default runloop observers run.
|
||||
CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.001, 1);
|
||||
|
||||
// Do some work.
|
||||
CefDoMessageLoopWork();
|
||||
|
||||
// Sleep to allow the CEF proc to do work.
|
||||
[NSThread sleepForTimeInterval: 0.05];
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpMac::OnScheduleMessagePumpWork(int64 delay_ms) {
|
||||
// This method may be called on any thread.
|
||||
NSNumber* number = [NSNumber numberWithInt:static_cast<int>(delay_ms)];
|
||||
[event_handler_ performSelector:@selector(scheduleWork:)
|
||||
onThread:owner_thread_
|
||||
withObject:number
|
||||
waitUntilDone:NO];
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpMac::HandleScheduleWork(int64 delay_ms) {
|
||||
OnScheduleWork(delay_ms);
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpMac::HandleTimerTimeout() {
|
||||
OnTimerTimeout();
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpMac::SetTimer(int64 delay_ms) {
|
||||
DCHECK_GT(delay_ms, 0);
|
||||
DCHECK(!timer_);
|
||||
|
||||
const double delay_s = static_cast<double>(delay_ms) / 1000.0;
|
||||
timer_ = [[NSTimer timerWithTimeInterval: delay_s
|
||||
target: event_handler_
|
||||
selector: @selector(timerTimeout:)
|
||||
userInfo: nil
|
||||
repeats: NO] retain];
|
||||
|
||||
// Add the timer to default and tracking runloop modes.
|
||||
NSRunLoop* owner_runloop = [NSRunLoop currentRunLoop];
|
||||
[owner_runloop addTimer: timer_ forMode: NSRunLoopCommonModes];
|
||||
[owner_runloop addTimer: timer_ forMode: NSEventTrackingRunLoopMode];
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpMac::KillTimer() {
|
||||
if (timer_ != nil) {
|
||||
[timer_ invalidate];
|
||||
[timer_ release];
|
||||
timer_ = nil;
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
scoped_ptr<MainMessageLoopExternalPump>
|
||||
MainMessageLoopExternalPump::Create() {
|
||||
return make_scoped_ptr<MainMessageLoopExternalPump>(
|
||||
new MainMessageLoopExternalPumpMac());
|
||||
}
|
||||
|
||||
} // namespace client
|
|
@ -0,0 +1,149 @@
|
|||
// 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.
|
||||
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
|
||||
#include <CommCtrl.h>
|
||||
|
||||
#include "include/cef_app.h"
|
||||
#include "cefclient/browser/util_win.h"
|
||||
|
||||
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();
|
||||
~MainMessageLoopExternalPumpWin();
|
||||
|
||||
// MainMessageLoopStd methods:
|
||||
void Quit() OVERRIDE;
|
||||
int Run() OVERRIDE;
|
||||
|
||||
// MainMessageLoopExternalPump methods:
|
||||
void OnScheduleMessagePumpWork(int64 delay_ms) OVERRIDE;
|
||||
|
||||
protected:
|
||||
// MainMessageLoopExternalPump methods:
|
||||
void SetTimer(int64 delay_ms) OVERRIDE;
|
||||
void KillTimer() OVERRIDE;
|
||||
bool IsTimerPending() OVERRIDE { return timer_pending_; }
|
||||
|
||||
private:
|
||||
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam,
|
||||
LPARAM lparam);
|
||||
|
||||
// True if a timer event is currently pending.
|
||||
bool timer_pending_;
|
||||
|
||||
// HWND owned by the thread that CefDoMessageLoopWork should be invoked on.
|
||||
HWND main_thread_target_;
|
||||
};
|
||||
|
||||
MainMessageLoopExternalPumpWin::MainMessageLoopExternalPumpWin()
|
||||
: timer_pending_(false),
|
||||
main_thread_target_(NULL) {
|
||||
HINSTANCE hInstance = GetModuleHandle(NULL);
|
||||
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.
|
||||
main_thread_target_ = CreateWindowW(kClassName, NULL, WS_OVERLAPPEDWINDOW,
|
||||
0, 0, 0, 0, HWND_MESSAGE , NULL, hInstance, NULL);
|
||||
DCHECK(main_thread_target_);
|
||||
SetUserDataPtr(main_thread_target_, this);
|
||||
}
|
||||
|
||||
MainMessageLoopExternalPumpWin::~MainMessageLoopExternalPumpWin() {
|
||||
KillTimer();
|
||||
if (main_thread_target_)
|
||||
DestroyWindow(main_thread_target_);
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpWin::Quit() {
|
||||
PostMessage(NULL, WM_QUIT, 0, 0);
|
||||
}
|
||||
|
||||
int MainMessageLoopExternalPumpWin::Run() {
|
||||
// Run the message loop.
|
||||
MSG msg;
|
||||
while (GetMessage(&msg, NULL, 0, 0)) {
|
||||
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();
|
||||
|
||||
// Sleep to allow the CEF proc to do work.
|
||||
Sleep(50);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpWin::OnScheduleMessagePumpWork(int64 delay_ms) {
|
||||
// This method may be called on any thread.
|
||||
PostMessage(main_thread_target_, kMsgHaveWork, 0,
|
||||
static_cast<LPARAM>(delay_ms));
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpWin::SetTimer(int64 delay_ms) {
|
||||
DCHECK(!timer_pending_);
|
||||
DCHECK_GT(delay_ms, 0);
|
||||
timer_pending_ = true;
|
||||
::SetTimer(main_thread_target_, 1, static_cast<UINT>(delay_ms), NULL);
|
||||
}
|
||||
|
||||
void MainMessageLoopExternalPumpWin::KillTimer() {
|
||||
if (timer_pending_) {
|
||||
::KillTimer(main_thread_target_, 1);
|
||||
timer_pending_ = false;
|
||||
}
|
||||
}
|
||||
|
||||
// static
|
||||
LRESULT CALLBACK MainMessageLoopExternalPumpWin::WndProc(
|
||||
HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
|
||||
if (msg == WM_TIMER || msg == kMsgHaveWork) {
|
||||
MainMessageLoopExternalPumpWin* message_loop =
|
||||
GetUserDataPtr<MainMessageLoopExternalPumpWin*>(hwnd);
|
||||
if (msg == kMsgHaveWork) {
|
||||
// OnScheduleMessagePumpWork() request.
|
||||
const int64 delay_ms = static_cast<int64>(lparam);
|
||||
message_loop->OnScheduleWork(delay_ms);
|
||||
} else {
|
||||
// Timer timed out.
|
||||
message_loop->OnTimerTimeout();
|
||||
}
|
||||
}
|
||||
return DefWindowProc(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// static
|
||||
scoped_ptr<MainMessageLoopExternalPump>
|
||||
MainMessageLoopExternalPump::Create() {
|
||||
return make_scoped_ptr<MainMessageLoopExternalPump>(
|
||||
new MainMessageLoopExternalPumpWin());
|
||||
}
|
||||
|
||||
} // namespace client
|
|
@ -20,6 +20,7 @@
|
|||
#include "include/wrapper/cef_helpers.h"
|
||||
#include "cefclient/browser/client_app_browser.h"
|
||||
#include "cefclient/browser/main_context_impl.h"
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
#include "cefclient/browser/main_message_loop_std.h"
|
||||
#include "cefclient/browser/test_runner.h"
|
||||
#include "cefclient/common/client_app_other.h"
|
||||
|
@ -90,7 +91,11 @@ int RunMain(int argc, char* argv[]) {
|
|||
context->PopulateSettings(&settings);
|
||||
|
||||
// Create the main message loop object.
|
||||
scoped_ptr<MainMessageLoop> message_loop(new MainMessageLoopStd);
|
||||
scoped_ptr<MainMessageLoop> message_loop;
|
||||
if (settings.external_message_pump)
|
||||
message_loop = MainMessageLoopExternalPump::Create();
|
||||
else
|
||||
message_loop.reset(new MainMessageLoopStd);
|
||||
|
||||
// Initialize CEF.
|
||||
context->Initialize(main_args, settings, app, NULL);
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
#import "include/cef_application_mac.h"
|
||||
#include "cefclient/browser/client_app_browser.h"
|
||||
#include "cefclient/browser/main_context_impl.h"
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
#include "cefclient/browser/main_message_loop_std.h"
|
||||
#include "cefclient/browser/resource.h"
|
||||
#include "cefclient/browser/root_window.h"
|
||||
|
@ -216,7 +217,11 @@ int RunMain(int argc, char* argv[]) {
|
|||
context->PopulateSettings(&settings);
|
||||
|
||||
// Create the main message loop object.
|
||||
scoped_ptr<MainMessageLoop> message_loop(new MainMessageLoopStd);
|
||||
scoped_ptr<MainMessageLoop> message_loop;
|
||||
if (settings.external_message_pump)
|
||||
message_loop = MainMessageLoopExternalPump::Create();
|
||||
else
|
||||
message_loop.reset(new MainMessageLoopStd);
|
||||
|
||||
// Initialize CEF.
|
||||
context->Initialize(main_args, settings, app, NULL);
|
||||
|
|
|
@ -9,6 +9,7 @@
|
|||
#include "include/cef_sandbox_win.h"
|
||||
#include "cefclient/browser/client_app_browser.h"
|
||||
#include "cefclient/browser/main_context_impl.h"
|
||||
#include "cefclient/browser/main_message_loop_external_pump.h"
|
||||
#include "cefclient/browser/main_message_loop_multithreaded_win.h"
|
||||
#include "cefclient/browser/main_message_loop_std.h"
|
||||
#include "cefclient/browser/root_window_manager.h"
|
||||
|
@ -82,6 +83,8 @@ int RunMain(HINSTANCE hInstance, int nCmdShow) {
|
|||
scoped_ptr<MainMessageLoop> message_loop;
|
||||
if (settings.multi_threaded_message_loop)
|
||||
message_loop.reset(new MainMessageLoopMultithreadedWin);
|
||||
else if (settings.external_message_pump)
|
||||
message_loop = MainMessageLoopExternalPump::Create();
|
||||
else
|
||||
message_loop.reset(new MainMessageLoopStd);
|
||||
|
||||
|
|
|
@ -18,6 +18,7 @@ namespace switches {
|
|||
// content/public/common/content_switches.cc
|
||||
|
||||
const char kMultiThreadedMessageLoop[] = "multi-threaded-message-loop";
|
||||
const char kExternalMessagePump[] = "external-message-pump";
|
||||
const char kCachePath[] = "cache-path";
|
||||
const char kUrl[] = "url";
|
||||
const char kOffScreenRenderingEnabled[] = "off-screen-rendering-enabled";
|
||||
|
|
|
@ -12,6 +12,7 @@ namespace client {
|
|||
namespace switches {
|
||||
|
||||
extern const char kMultiThreadedMessageLoop[];
|
||||
extern const char kExternalMessagePump[];
|
||||
extern const char kCachePath[];
|
||||
extern const char kUrl[];
|
||||
extern const char kOffScreenRenderingEnabled[];
|
||||
|
|
|
@ -19,6 +19,8 @@
|
|||
#include "include/wrapper/cef_helpers.h"
|
||||
#include "include/wrapper/cef_closure_task.h"
|
||||
#include "tests/cefclient/browser/client_app_browser.h"
|
||||
#include "tests/cefclient/browser/main_message_loop_external_pump.h"
|
||||
#include "tests/cefclient/browser/main_message_loop_std.h"
|
||||
#include "tests/cefclient/common/client_app_other.h"
|
||||
#include "tests/cefclient/renderer/client_app_renderer.h"
|
||||
#include "tests/unittests/test_handler.h"
|
||||
|
@ -30,6 +32,14 @@
|
|||
|
||||
namespace {
|
||||
|
||||
void QuitMessageLoop() {
|
||||
client::MainMessageLoop* message_loop = client::MainMessageLoop::Get();
|
||||
if (message_loop)
|
||||
message_loop->Quit();
|
||||
else
|
||||
CefQuitMessageLoop();
|
||||
}
|
||||
|
||||
// Thread used to run the test suite.
|
||||
class CefTestThread : public base::Thread {
|
||||
public:
|
||||
|
@ -47,7 +57,7 @@ class CefTestThread : public base::Thread {
|
|||
base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(100));
|
||||
|
||||
// Quit the CEF message loop.
|
||||
CefPostTask(TID_UI, base::Bind(&CefQuitMessageLoop));
|
||||
CefPostTask(TID_UI, base::Bind(&QuitMessageLoop));
|
||||
}
|
||||
|
||||
int retval() { return retval_; }
|
||||
|
@ -158,6 +168,15 @@ int main(int argc, char* argv[]) {
|
|||
XSetIOErrorHandler(XIOErrorHandlerImpl);
|
||||
#endif
|
||||
|
||||
// Create the MessageLoop.
|
||||
std::unique_ptr<client::MainMessageLoop> message_loop;
|
||||
if (!settings.multi_threaded_message_loop) {
|
||||
if (settings.external_message_pump)
|
||||
message_loop = client::MainMessageLoopExternalPump::Create();
|
||||
else
|
||||
message_loop.reset(new client::MainMessageLoopStd);
|
||||
}
|
||||
|
||||
// Initialize CEF.
|
||||
CefInitialize(main_args, settings, app, windows_sandbox_info);
|
||||
|
||||
|
@ -181,7 +200,7 @@ int main(int argc, char* argv[]) {
|
|||
CefPostTask(TID_UI, base::Bind(&RunTests, thread.get()));
|
||||
|
||||
// Run the CEF message loop.
|
||||
CefRunMessageLoop();
|
||||
message_loop->Run();
|
||||
|
||||
// The test suite has completed.
|
||||
retval = thread->retval();
|
||||
|
@ -193,6 +212,9 @@ int main(int argc, char* argv[]) {
|
|||
// Shut down CEF.
|
||||
CefShutdown();
|
||||
|
||||
// Destroy the MessageLoop.
|
||||
message_loop.reset(nullptr);
|
||||
|
||||
#if defined(OS_MACOSX)
|
||||
// Platform-specific cleanup.
|
||||
extern void PlatformCleanup();
|
||||
|
|
|
@ -48,6 +48,11 @@ void CefTestSuite::GetSettings(CefSettings& settings) {
|
|||
commandline_->HasSwitch(client::switches::kMultiThreadedMessageLoop);
|
||||
#endif
|
||||
|
||||
if (!settings.multi_threaded_message_loop) {
|
||||
settings.external_message_pump =
|
||||
commandline_->HasSwitch(client::switches::kExternalMessagePump);
|
||||
}
|
||||
|
||||
CefString(&settings.cache_path) =
|
||||
commandline_->GetSwitchValueASCII(client::switches::kCachePath);
|
||||
|
||||
|
|
Loading…
Reference in New Issue