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.
5 #import <Carbon/Carbon.h>
7 #include "content/browser/renderer_host/popup_menu_helper_mac.h"
9 #include "base/mac/scoped_nsobject.h"
10 #import "base/mac/scoped_sending_event.h"
11 #include "base/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"
23 bool g_allow_showing_popup_menus = true;
27 PopupMenuHelper::PopupMenuHelper(RenderViewHost* render_view_host)
28 : render_view_host_(static_cast<RenderViewHostImpl*>(render_view_host)) {
29 notification_registrar_.Add(
30 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
31 Source<RenderWidgetHost>(render_view_host));
34 void PopupMenuHelper::ShowPopupMenu(
35 const gfx::Rect& bounds,
37 double item_font_size,
39 const std::vector<WebMenuItem>& items,
41 bool allow_multiple_selection) {
42 // Only single selection list boxes show a popup on Mac.
43 DCHECK(!allow_multiple_selection);
45 if (!g_allow_showing_popup_menus)
48 // 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
50 // would in turn delete me, causing a crash once the -runMenuInView
51 // call returns. That's what was happening in <http://crbug.com/33250>).
52 RenderWidgetHostViewMac* rwhvm =
53 static_cast<RenderWidgetHostViewMac*>(GetRenderWidgetHostView());
54 base::scoped_nsobject<RenderWidgetHostViewCocoa> cocoa_view(
55 [rwhvm->cocoa_view() retain]);
58 base::scoped_nsobject<WebMenuRunner> menu_runner;
59 menu_runner.reset([[WebMenuRunner alloc] initWithItems:items
60 fontSize:item_font_size
61 rightAligned:right_aligned]);
64 // Make sure events can be pumped while the menu is up.
65 base::MessageLoop::ScopedNestableTaskAllower allow(
66 base::MessageLoop::current());
68 // One of the events that could be pumped is |window.close()|.
69 // User-initiated event-tracking loops protect against this by
70 // setting flags in -[CrApplication sendEvent:], but since
71 // web-content menus are initiated by IPC message the setup has to
73 base::mac::ScopedSendingEvent sending_event_scoper;
75 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished.
76 [menu_runner runMenuInView:cocoa_view
77 withBounds:[cocoa_view flipRectToNSRect:bounds]
78 initialIndex:selected_item];
81 if (!render_view_host_) {
82 // Bad news, the RenderViewHost got deleted while we were off running the
83 // menu. Nothing to do.
87 if ([menu_runner menuItemWasChosen]) {
88 render_view_host_->DidSelectPopupMenuItem(
89 [menu_runner indexOfSelectedItem]);
91 render_view_host_->DidCancelPopupMenu();
96 void PopupMenuHelper::DontShowPopupMenuForTesting() {
97 g_allow_showing_popup_menus = false;
100 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const {
101 return static_cast<RenderWidgetHostViewMac*>(render_view_host_->GetView());
104 void PopupMenuHelper::Observe(int type,
105 const NotificationSource& source,
106 const NotificationDetails& details) {
107 DCHECK(type == NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED);
108 DCHECK(Source<RenderWidgetHost>(source).ptr() == render_view_host_);
109 render_view_host_ = NULL;
112 } // namespace content