Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / popup_menu_helper_mac.mm
blobd3f70636ec6e5c15edafa48535690a348eaf301c
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"
19 namespace content {
21 namespace {
23 bool g_allow_showing_popup_menus = true;
25 }  // namespace
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,
36     int item_height,
37     double item_font_size,
38     int selected_item,
39     const std::vector<WebMenuItem>& items,
40     bool right_aligned,
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)
46     return;
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]);
57   // Display the menu.
58   base::scoped_nsobject<WebMenuRunner> menu_runner;
59    menu_runner.reset([[WebMenuRunner alloc] initWithItems:items
60                                                  fontSize:item_font_size
61                                              rightAligned:right_aligned]);
63   {
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
72     // be done manually.
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];
79   }
81   if (!render_view_host_) {
82     // Bad news, the RenderViewHost got deleted while we were off running the
83     // menu. Nothing to do.
84     return;
85   }
87   if ([menu_runner menuItemWasChosen]) {
88     render_view_host_->DidSelectPopupMenuItem(
89         [menu_runner indexOfSelectedItem]);
90   } else {
91     render_view_host_->DidCancelPopupMenu();
92   }
95 // static
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