Index: ui/views/controls/menu/menu_message_loop_aura.cc |
diff --git a/ui/views/controls/menu/menu_message_loop_aura.cc b/ui/views/controls/menu/menu_message_loop_aura.cc |
index 3ed33281aa6f43e8b99a301acb33af9aef39515f..d6ea343b42679c902e433a455751a1a5f266447a 100644 |
--- a/ui/views/controls/menu/menu_message_loop_aura.cc |
+++ b/ui/views/controls/menu/menu_message_loop_aura.cc |
@@ -96,6 +96,9 @@ class ActivationChangeObserverImpl |
DISALLOW_COPY_AND_ASSIGN(ActivationChangeObserverImpl); |
}; |
+// The ID that has been given to the last created run_loop. |
+int g_last_run_loop_id_ = 0; |
+ |
} // namespace |
// static |
@@ -103,7 +106,9 @@ MenuMessageLoop* MenuMessageLoop::Create() { |
return new MenuMessageLoopAura; |
} |
-MenuMessageLoopAura::MenuMessageLoopAura() : owner_(NULL) { |
+MenuMessageLoopAura::MenuMessageLoopAura() |
+ : owner_(NULL), |
+ menu_event_filter_(new MenuEventFilter) { |
} |
MenuMessageLoopAura::~MenuMessageLoopAura() { |
@@ -139,6 +144,11 @@ void MenuMessageLoopAura::Run(MenuController* controller, |
base::AutoReset<base::Closure> reset_quit_closure(&message_loop_quit_, |
base::Closure()); |
+ // The run_loop is about to start, QuitNow() hasn't been called for it yet. |
+ int current_run_loop_id = ++g_last_run_loop_id_; |
+ run_loop_ids_stack_.push(current_run_loop_id); |
+ run_loops_status_[current_run_loop_id] = false; |
+ |
#if defined(OS_WIN) |
internal::MenuMessagePumpDispatcher nested_dispatcher(controller); |
if (root) { |
@@ -157,36 +167,67 @@ void MenuMessageLoopAura::Run(MenuController* controller, |
run_loop.Run(); |
} |
#else |
- internal::MenuEventDispatcher event_dispatcher(controller); |
- scoped_ptr<ui::ScopedEventDispatcher> dispatcher_override; |
- if (ui::PlatformEventSource::GetInstance()) { |
- dispatcher_override = |
- ui::PlatformEventSource::GetInstance()->OverrideDispatcher( |
- &event_dispatcher); |
- } |
+ DCHECK(menu_event_filter_.get()); |
+ |
+ scoped_ptr<ActivationChangeObserverImpl> observer; |
+ MenuEventFilter::Delegate* previous_delegate = |
+ menu_event_filter_->GetCurrentDelegate(); |
+ |
if (root) { |
- scoped_ptr<ActivationChangeObserverImpl> observer; |
if (!nested_menu) |
observer.reset(new ActivationChangeObserverImpl(controller, root)); |
- aura::client::DispatcherRunLoop run_loop( |
- aura::client::GetDispatcherClient(root), NULL); |
- message_loop_quit_ = run_loop.QuitClosure(); |
- run_loop.Run(); |
- } else { |
- base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
- base::MessageLoop::ScopedNestableTaskAllower allow(loop); |
- base::RunLoop run_loop; |
- message_loop_quit_ = run_loop.QuitClosure(); |
- run_loop.Run(); |
+ MenuEventFilter::Delegate* filter_delegate = |
+ MenuEventFilter::GetMenuEventFilterDelegate(root); |
+ if (filter_delegate) |
+ menu_event_filter_->SetDelegate(filter_delegate); |
} |
-#endif |
+ |
+ aura::Env::GetInstanceDontCreate()->PrependPreTargetHandler( |
+ menu_event_filter_.get()); |
+ |
+ base::MessageLoopForUI* loop = base::MessageLoopForUI::current(); |
+ base::MessageLoop::ScopedNestableTaskAllower allow(loop); |
+ base::RunLoop run_loop; |
+ message_loop_quit_ = run_loop.QuitClosure(); |
+ |
+ run_loop.Run(); |
+ |
+ // Restore the previous delegate in the case of nested run loops. |
+ menu_event_filter_->SetDelegate(previous_delegate); |
pkotwicz
2015/08/27 17:19:55
Can we remove the MenuEventFilter pretarget handle
afakhry
2015/08/28 01:24:41
This is an awesome idea! I don't even need to care
|
+#endif // defined(OS_WIN) |
+ |
+ // Now erase this run_loop ID to mark it as returned. |
+ run_loops_status_.erase(current_run_loop_id); |
+ run_loop_ids_stack_.pop(); |
} |
void MenuMessageLoopAura::QuitNow() { |
CHECK(!message_loop_quit_.is_null()); |
+ |
+ if (run_loop_ids_stack_.empty()) |
+ return; |
+ |
+ // This RunLoop has already returned? We should not call QuitNow() again. |
+ int current_run_loop_id = run_loop_ids_stack_.top(); |
+ auto itr = run_loops_status_.find(current_run_loop_id); |
+ if (itr == run_loops_status_.end()) |
+ return; |
+ |
+ // QuitNow() has already been called? |
+ if (itr->second) |
+ return; |
+ |
+ // Overwrite the status so that QuitNow() is marked as called for this |
+ // RunLoop. |
+ run_loops_status_[current_run_loop_id] = true; |
+ |
message_loop_quit_.Run(); |
#if !defined(OS_WIN) |
+ aura::Env::GetInstanceDontCreate()->RemovePreTargetHandler( |
+ menu_event_filter_.get()); |
+ menu_event_filter_->ClearDelegate(); |
+ |
// Ask PlatformEventSource to stop dispatching events in this message loop |
// iteration. We want our menu's loop to return before the next event. |
if (ui::PlatformEventSource::GetInstance()) |