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