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/frame_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/message_loop.h"
12 #include "content/browser/frame_host/render_frame_host_impl.h"
13 #include "content/browser/renderer_host/render_view_host_impl.h"
14 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
15 #include "content/browser/renderer_host/webmenurunner_mac.h"
16 #include "content/public/browser/notification_details.h"
17 #include "content/public/browser/notification_source.h"
18 #include "content/public/browser/notification_types.h"
19 #import "ui/base/cocoa/base_view.h"
25 bool g_allow_showing_popup_menus = true;
29 PopupMenuHelper::PopupMenuHelper(RenderFrameHost* render_frame_host)
30 : render_frame_host_(static_cast<RenderFrameHostImpl*>(render_frame_host)),
32 popup_was_hidden_(false) {
33 notification_registrar_.Add(
34 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED,
35 Source<RenderWidgetHost>(render_frame_host->GetRenderViewHost()));
36 notification_registrar_.Add(
37 this, NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED,
38 Source<RenderWidgetHost>(render_frame_host->GetRenderViewHost()));
41 void PopupMenuHelper::ShowPopupMenu(
42 const gfx::Rect& bounds,
44 double item_font_size,
46 const std::vector<MenuItem>& items,
48 bool allow_multiple_selection) {
49 // Only single selection list boxes show a popup on Mac.
50 DCHECK(!allow_multiple_selection);
52 if (!g_allow_showing_popup_menus)
55 // Retain the Cocoa view for the duration of the pop-up so that it can't be
56 // dealloced if my Destroy() method is called while the pop-up's up (which
57 // would in turn delete me, causing a crash once the -runMenuInView
58 // call returns. That's what was happening in <http://crbug.com/33250>).
59 RenderWidgetHostViewMac* rwhvm =
60 static_cast<RenderWidgetHostViewMac*>(GetRenderWidgetHostView());
61 base::scoped_nsobject<RenderWidgetHostViewCocoa> cocoa_view(
62 [rwhvm->cocoa_view() retain]);
65 menu_runner_ = [[WebMenuRunner alloc] initWithItems:items
66 fontSize:item_font_size
67 rightAligned:right_aligned];
70 // Make sure events can be pumped while the menu is up.
71 base::MessageLoop::ScopedNestableTaskAllower allow(
72 base::MessageLoop::current());
74 // One of the events that could be pumped is |window.close()|.
75 // User-initiated event-tracking loops protect against this by
76 // setting flags in -[CrApplication sendEvent:], but since
77 // web-content menus are initiated by IPC message the setup has to
79 base::mac::ScopedSendingEvent sending_event_scoper;
81 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished.
82 [menu_runner_ runMenuInView:cocoa_view
83 withBounds:[cocoa_view flipRectToNSRect:bounds]
84 initialIndex:selected_item];
87 if (!render_frame_host_) {
88 // Bad news, the RenderFrameHost got deleted while we were off running the
89 // menu. Nothing to do.
90 [menu_runner_ release];
95 if (!popup_was_hidden_) {
96 if ([menu_runner_ menuItemWasChosen]) {
97 render_frame_host_->DidSelectPopupMenuItem(
98 [menu_runner_ indexOfSelectedItem]);
100 render_frame_host_->DidCancelPopupMenu();
103 [menu_runner_ release];
107 void PopupMenuHelper::Hide() {
110 popup_was_hidden_ = true;
114 void PopupMenuHelper::DontShowPopupMenuForTesting() {
115 g_allow_showing_popup_menus = false;
118 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const {
119 return static_cast<RenderWidgetHostViewMac*>(
120 render_frame_host_->GetRenderViewHost()->GetView());
123 void PopupMenuHelper::Observe(int type,
124 const NotificationSource& source,
125 const NotificationDetails& details) {
126 DCHECK_EQ(Source<RenderWidgetHost>(source).ptr(),
127 render_frame_host_->GetRenderViewHost());
129 case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
130 render_frame_host_ = NULL;
133 case NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: {
134 bool is_visible = *Details<bool>(details).ptr();
142 } // namespace content