| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import <Carbon/Carbon.h> | |
| 6 | |
| 7 #include "content/browser/renderer_host/popup_menu_helper_mac.h" | |
| 8 | |
| 9 #include "base/mac/scoped_nsobject.h" | |
| 10 #import "base/mac/scoped_sending_event.h" | |
| 11 #include "base/message_loop/message_loop.h" | |
| 12 #include "content/browser/renderer_host/render_view_host_impl.h" | |
| 13 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | |
| 14 #include "content/browser/renderer_host/webmenurunner_mac.h" | |
| 15 #include "content/public/browser/notification_source.h" | |
| 16 #include "content/public/browser/notification_types.h" | |
| 17 #import "ui/base/cocoa/base_view.h" | |
| 18 | |
| 19 namespace content { | |
| 20 | |
| 21 namespace { | |
| 22 | |
| 23 bool g_allow_showing_popup_menus = true; | |
| 24 | |
| 25 } // namespace | |
| 26 | |
| 27 PopupMenuHelper::PopupMenuHelper(RenderViewHost* render_view_host) | |
| 28 : render_view_host_(static_cast<RenderViewHostImpl*>(render_view_host)), | |
| 29 menu_runner_(nil), | |
| 30 popup_was_hidden_(false) { | |
| 31 notification_registrar_.Add( | |
| 32 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | |
| 33 Source<RenderWidgetHost>(render_view_host)); | |
| 34 } | |
| 35 | |
| 36 void PopupMenuHelper::ShowPopupMenu( | |
| 37 const gfx::Rect& bounds, | |
| 38 int item_height, | |
| 39 double item_font_size, | |
| 40 int selected_item, | |
| 41 const std::vector<MenuItem>& items, | |
| 42 bool right_aligned, | |
| 43 bool allow_multiple_selection) { | |
| 44 // Only single selection list boxes show a popup on Mac. | |
| 45 DCHECK(!allow_multiple_selection); | |
| 46 | |
| 47 if (!g_allow_showing_popup_menus) | |
| 48 return; | |
| 49 | |
| 50 // Retain the Cocoa view for the duration of the pop-up so that it can't be | |
| 51 // dealloced if my Destroy() method is called while the pop-up's up (which | |
| 52 // would in turn delete me, causing a crash once the -runMenuInView | |
| 53 // call returns. That's what was happening in <http://crbug.com/33250>). | |
| 54 RenderWidgetHostViewMac* rwhvm = | |
| 55 static_cast<RenderWidgetHostViewMac*>(GetRenderWidgetHostView()); | |
| 56 base::scoped_nsobject<RenderWidgetHostViewCocoa> cocoa_view( | |
| 57 [rwhvm->cocoa_view() retain]); | |
| 58 | |
| 59 // Display the menu. | |
| 60 menu_runner_ = [[WebMenuRunner alloc] initWithItems:items | |
| 61 fontSize:item_font_size | |
| 62 rightAligned:right_aligned]; | |
| 63 | |
| 64 { | |
| 65 // Make sure events can be pumped while the menu is up. | |
| 66 base::MessageLoop::ScopedNestableTaskAllower allow( | |
| 67 base::MessageLoop::current()); | |
| 68 | |
| 69 // One of the events that could be pumped is |window.close()|. | |
| 70 // User-initiated event-tracking loops protect against this by | |
| 71 // setting flags in -[CrApplication sendEvent:], but since | |
| 72 // web-content menus are initiated by IPC message the setup has to | |
| 73 // be done manually. | |
| 74 base::mac::ScopedSendingEvent sending_event_scoper; | |
| 75 | |
| 76 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished. | |
| 77 [menu_runner_ runMenuInView:cocoa_view | |
| 78 withBounds:[cocoa_view flipRectToNSRect:bounds] | |
| 79 initialIndex:selected_item]; | |
| 80 } | |
| 81 | |
| 82 if (!render_view_host_) { | |
| 83 // Bad news, the RenderViewHost got deleted while we were off running the | |
| 84 // menu. Nothing to do. | |
| 85 [menu_runner_ release]; | |
| 86 menu_runner_ = nil; | |
| 87 return; | |
| 88 } | |
| 89 | |
| 90 if (!popup_was_hidden_) { | |
| 91 if ([menu_runner_ menuItemWasChosen]) { | |
| 92 render_view_host_->DidSelectPopupMenuItem( | |
| 93 [menu_runner_ indexOfSelectedItem]); | |
| 94 } else { | |
| 95 render_view_host_->DidCancelPopupMenu(); | |
| 96 } | |
| 97 } | |
| 98 [menu_runner_ release]; | |
| 99 menu_runner_ = nil; | |
| 100 } | |
| 101 | |
| 102 void PopupMenuHelper::Hide() { | |
| 103 if (menu_runner_) | |
| 104 [menu_runner_ hide]; | |
| 105 popup_was_hidden_ = true; | |
| 106 } | |
| 107 | |
| 108 // static | |
| 109 void PopupMenuHelper::DontShowPopupMenuForTesting() { | |
| 110 g_allow_showing_popup_menus = false; | |
| 111 } | |
| 112 | |
| 113 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const { | |
| 114 return static_cast<RenderWidgetHostViewMac*>(render_view_host_->GetView()); | |
| 115 } | |
| 116 | |
| 117 void PopupMenuHelper::Observe(int type, | |
| 118 const NotificationSource& source, | |
| 119 const NotificationDetails& details) { | |
| 120 DCHECK(type == NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); | |
| 121 DCHECK(Source<RenderWidgetHost>(source).ptr() == render_view_host_); | |
| 122 render_view_host_ = NULL; | |
| 123 } | |
| 124 | |
| 125 } // namespace content | |
| OLD | NEW |