Use multiline attribute to check for IA2_STATE_MULTILINE.
[chromium-blink-merge.git] / content / browser / frame_host / popup_menu_helper_mac.mm
blob1ab1917d713db69be9d90f1f00e22c77e931a995
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"
21 namespace content {
23 namespace {
25 bool g_allow_showing_popup_menus = true;
27 }  // namespace
29 PopupMenuHelper::PopupMenuHelper(RenderFrameHost* render_frame_host)
30     : render_frame_host_(static_cast<RenderFrameHostImpl*>(render_frame_host)),
31       menu_runner_(nil),
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,
43     int item_height,
44     double item_font_size,
45     int selected_item,
46     const std::vector<MenuItem>& items,
47     bool right_aligned,
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)
53     return;
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]);
64   // Display the menu.
65   menu_runner_ = [[WebMenuRunner alloc] initWithItems:items
66                                              fontSize:item_font_size
67                                          rightAligned:right_aligned];
69   {
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
78     // be done manually.
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];
85   }
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];
91     menu_runner_ = nil;
92     return;
93   }
95   if (!popup_was_hidden_) {
96     if ([menu_runner_ menuItemWasChosen]) {
97       render_frame_host_->DidSelectPopupMenuItem(
98           [menu_runner_ indexOfSelectedItem]);
99     } else {
100       render_frame_host_->DidCancelPopupMenu();
101     }
102   }
103   [menu_runner_ release];
104   menu_runner_ = nil;
107 void PopupMenuHelper::Hide() {
108   if (menu_runner_)
109     [menu_runner_ hide];
110   popup_was_hidden_ = true;
113 // static
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());
128   switch (type) {
129     case NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED: {
130       render_frame_host_ = NULL;
131       break;
132     }
133     case NOTIFICATION_RENDER_WIDGET_VISIBILITY_CHANGED: {
134       bool is_visible = *Details<bool>(details).ptr();
135       if (!is_visible)
136         Hide();
137       break;
138     }
139   }
142 }  // namespace content