| 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/frame_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/frame_host/render_frame_host_impl.h" |
| 12 #include "content/browser/renderer_host/render_view_host_impl.h" | 13 #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/render_widget_host_view_mac.h" |
| 14 #include "content/browser/renderer_host/webmenurunner_mac.h" | 15 #include "content/browser/renderer_host/webmenurunner_mac.h" |
| 15 #include "content/public/browser/notification_source.h" | 16 #include "content/public/browser/notification_source.h" |
| 16 #include "content/public/browser/notification_types.h" | 17 #include "content/public/browser/notification_types.h" |
| 17 #import "ui/base/cocoa/base_view.h" | 18 #import "ui/base/cocoa/base_view.h" |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 namespace { | 22 namespace { |
| 22 | 23 |
| 23 bool g_allow_showing_popup_menus = true; | 24 bool g_allow_showing_popup_menus = true; |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 PopupMenuHelper::PopupMenuHelper(RenderViewHost* render_view_host) | 28 PopupMenuHelper::PopupMenuHelper(RenderFrameHost* render_frame_host) |
| 28 : render_view_host_(static_cast<RenderViewHostImpl*>(render_view_host)), | 29 : render_frame_host_(static_cast<RenderFrameHostImpl*>(render_frame_host)), |
| 29 menu_runner_(nil), | 30 menu_runner_(nil), |
| 30 popup_was_hidden_(false) { | 31 popup_was_hidden_(false) { |
| 31 notification_registrar_.Add( | 32 notification_registrar_.Add( |
| 32 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | 33 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, |
| 33 Source<RenderWidgetHost>(render_view_host)); | 34 Source<RenderWidgetHost>(render_frame_host->GetRenderViewHost())); |
| 34 } | 35 } |
| 35 | 36 |
| 36 void PopupMenuHelper::ShowPopupMenu( | 37 void PopupMenuHelper::ShowPopupMenu( |
| 37 const gfx::Rect& bounds, | 38 const gfx::Rect& bounds, |
| 38 int item_height, | 39 int item_height, |
| 39 double item_font_size, | 40 double item_font_size, |
| 40 int selected_item, | 41 int selected_item, |
| 41 const std::vector<MenuItem>& items, | 42 const std::vector<MenuItem>& items, |
| 42 bool right_aligned, | 43 bool right_aligned, |
| 43 bool allow_multiple_selection) { | 44 bool allow_multiple_selection) { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 72 // web-content menus are initiated by IPC message the setup has to | 73 // web-content menus are initiated by IPC message the setup has to |
| 73 // be done manually. | 74 // be done manually. |
| 74 base::mac::ScopedSendingEvent sending_event_scoper; | 75 base::mac::ScopedSendingEvent sending_event_scoper; |
| 75 | 76 |
| 76 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished. | 77 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished. |
| 77 [menu_runner_ runMenuInView:cocoa_view | 78 [menu_runner_ runMenuInView:cocoa_view |
| 78 withBounds:[cocoa_view flipRectToNSRect:bounds] | 79 withBounds:[cocoa_view flipRectToNSRect:bounds] |
| 79 initialIndex:selected_item]; | 80 initialIndex:selected_item]; |
| 80 } | 81 } |
| 81 | 82 |
| 82 if (!render_view_host_) { | 83 if (!render_frame_host_) { |
| 83 // Bad news, the RenderViewHost got deleted while we were off running the | 84 // Bad news, the RenderFrameHost got deleted while we were off running the |
| 84 // menu. Nothing to do. | 85 // menu. Nothing to do. |
| 85 [menu_runner_ release]; | 86 [menu_runner_ release]; |
| 86 menu_runner_ = nil; | 87 menu_runner_ = nil; |
| 87 return; | 88 return; |
| 88 } | 89 } |
| 89 | 90 |
| 90 if (!popup_was_hidden_) { | 91 if (!popup_was_hidden_) { |
| 91 if ([menu_runner_ menuItemWasChosen]) { | 92 if ([menu_runner_ menuItemWasChosen]) { |
| 92 render_view_host_->DidSelectPopupMenuItem( | 93 render_frame_host_->DidSelectPopupMenuItem( |
| 93 [menu_runner_ indexOfSelectedItem]); | 94 [menu_runner_ indexOfSelectedItem]); |
| 94 } else { | 95 } else { |
| 95 render_view_host_->DidCancelPopupMenu(); | 96 render_frame_host_->DidCancelPopupMenu(); |
| 96 } | 97 } |
| 97 } | 98 } |
| 98 [menu_runner_ release]; | 99 [menu_runner_ release]; |
| 99 menu_runner_ = nil; | 100 menu_runner_ = nil; |
| 100 } | 101 } |
| 101 | 102 |
| 102 void PopupMenuHelper::Hide() { | 103 void PopupMenuHelper::Hide() { |
| 103 if (menu_runner_) | 104 if (menu_runner_) |
| 104 [menu_runner_ hide]; | 105 [menu_runner_ hide]; |
| 105 popup_was_hidden_ = true; | 106 popup_was_hidden_ = true; |
| 106 } | 107 } |
| 107 | 108 |
| 108 // static | 109 // static |
| 109 void PopupMenuHelper::DontShowPopupMenuForTesting() { | 110 void PopupMenuHelper::DontShowPopupMenuForTesting() { |
| 110 g_allow_showing_popup_menus = false; | 111 g_allow_showing_popup_menus = false; |
| 111 } | 112 } |
| 112 | 113 |
| 113 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const { | 114 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const { |
| 114 return static_cast<RenderWidgetHostViewMac*>(render_view_host_->GetView()); | 115 return static_cast<RenderWidgetHostViewMac*>( |
| 116 render_frame_host_->GetRenderViewHost()->GetView()); |
| 115 } | 117 } |
| 116 | 118 |
| 117 void PopupMenuHelper::Observe(int type, | 119 void PopupMenuHelper::Observe(int type, |
| 118 const NotificationSource& source, | 120 const NotificationSource& source, |
| 119 const NotificationDetails& details) { | 121 const NotificationDetails& details) { |
| 120 DCHECK(type == NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); | 122 DCHECK(type == NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); |
| 121 DCHECK(Source<RenderWidgetHost>(source).ptr() == render_view_host_); | 123 DCHECK_EQ(Source<RenderWidgetHost>(source).ptr(), |
| 122 render_view_host_ = NULL; | 124 render_frame_host_->GetRenderViewHost()); |
| 125 render_frame_host_ = NULL; |
| 123 } | 126 } |
| 124 | 127 |
| 125 } // namespace content | 128 } // namespace content |
| OLD | NEW |