diff --git base/message_loop/message_loop.cc base/message_loop/message_loop.cc index ae58f829393d..453368e55bb7 100644 --- base/message_loop/message_loop.cc +++ base/message_loop/message_loop.cc @@ -163,6 +163,9 @@ MessageLoopBase* MessageLoop::GetMessageLoopBase() { //------------------------------------------------------------------------------ +MessageLoopForUI::MessageLoopForUI(std::unique_ptr pump) + : MessageLoop(TYPE_UI, std::move(pump)) {} + // static std::unique_ptr MessageLoop::CreateUnbound(Type type) { return WrapUnique(new MessageLoop(type, nullptr)); diff --git base/message_loop/message_loop.h base/message_loop/message_loop.h index 46782b49a7f0..78ebaf71cbdf 100644 --- base/message_loop/message_loop.h +++ base/message_loop/message_loop.h @@ -377,6 +377,7 @@ class BASE_EXPORT MessageLoop { class BASE_EXPORT MessageLoopForUI : public MessageLoop { public: explicit MessageLoopForUI(Type type = TYPE_UI); + explicit MessageLoopForUI(std::unique_ptr pump); #if defined(OS_IOS) // On iOS, the main message loop cannot be Run(). Instead call Attach(), diff --git base/message_loop/message_loop_current.cc base/message_loop/message_loop_current.cc index 7dcab95f6838..4a6ec38e051a 100644 --- base/message_loop/message_loop_current.cc +++ base/message_loop/message_loop_current.cc @@ -46,6 +46,8 @@ void MessageLoopCurrent::AddDestructionObserver( void MessageLoopCurrent::RemoveDestructionObserver( DestructionObserver* destruction_observer) { + if (!current_) + return; DCHECK(current_->IsBoundToCurrentThread()); current_->RemoveDestructionObserver(destruction_observer); } diff --git base/message_loop/message_loop_current.h base/message_loop/message_loop_current.h index 1809f870d188..d0e8e645e3a7 100644 --- base/message_loop/message_loop_current.h +++ base/message_loop/message_loop_current.h @@ -133,6 +133,12 @@ class BASE_EXPORT MessageLoopCurrent { // posted tasks. void SetAddQueueTimeToTasks(bool enable); +#if defined(OS_WIN) + void set_os_modal_loop(bool os_modal_loop) { os_modal_loop_ = os_modal_loop; } + + bool os_modal_loop() const { return os_modal_loop_; } +#endif // OS_WIN + // Enables or disables the recursive task processing. This happens in the case // of recursive message loops. Some unwanted message loops may occur when // using common controls or printer functions. By default, recursive task @@ -200,6 +206,13 @@ class BASE_EXPORT MessageLoopCurrent { friend class web::TestWebThreadBundle; MessageLoopBase* current_; + +#if defined(OS_WIN) + private: + // Should be set to true before calling Windows APIs like TrackPopupMenu, etc. + // which enter a modal message loop. + bool os_modal_loop_ = false; +#endif }; #if !defined(OS_NACL) diff --git base/message_loop/message_pump_win.cc base/message_loop/message_pump_win.cc index 3e9562cc4cc5..cf4a700364d3 100644 --- base/message_loop/message_pump_win.cc +++ base/message_loop/message_pump_win.cc @@ -2,6 +2,7 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include "base/message_loop/message_loop_current.h" #include "base/message_loop/message_pump_win.h" #include @@ -493,10 +494,18 @@ bool MessagePumpForUI::ProcessPumpReplacementMessage() { // asynchronous to this thread!! MSG msg; - const bool have_message = - ::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE) != FALSE; + bool have_message = false; + // We should not process all window messages if we are in the context of an + // OS modal loop, i.e. in the context of a windows API call like MessageBox. + // This is to ensure that these messages are peeked out by the OS modal loop. + if (MessageLoopCurrent::Get()->os_modal_loop()) { + // We only peek out WM_PAINT and WM_TIMER here for reasons mentioned above. + have_message = PeekMessage(&msg, NULL, WM_PAINT, WM_PAINT, PM_REMOVE) || + PeekMessage(&msg, NULL, WM_TIMER, WM_TIMER, PM_REMOVE); + } else { + have_message = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != FALSE; + } - // Expect no message or a message different than kMsgHaveWork. DCHECK(!have_message || kMsgHaveWork != msg.message || msg.hwnd != message_window_.hwnd());