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/renderer_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/renderer_host/render_view_host_impl.h" | 12 #include "content/browser/renderer_host/render_view_host_impl.h" |
13 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 13 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
14 #include "content/browser/renderer_host/webmenurunner_mac.h" | 14 #include "content/browser/renderer_host/webmenurunner_mac.h" |
15 #include "content/public/browser/notification_source.h" | 15 #include "content/public/browser/notification_source.h" |
16 #include "content/public/browser/notification_types.h" | 16 #include "content/public/browser/notification_types.h" |
17 #import "ui/base/cocoa/base_view.h" | 17 #import "ui/base/cocoa/base_view.h" |
18 | 18 |
19 namespace content { | 19 namespace content { |
20 | 20 |
21 namespace { | 21 namespace { |
22 | 22 |
23 bool g_allow_showing_popup_menus = true; | 23 bool g_allow_showing_popup_menus = true; |
24 | 24 |
25 } // namespace | 25 } // namespace |
26 | 26 |
27 PopupMenuHelper::PopupMenuHelper(RenderViewHost* render_view_host) | 27 PopupMenuHelper::PopupMenuHelper(RenderViewHost* render_view_host) |
28 : render_view_host_(static_cast<RenderViewHostImpl*>(render_view_host)) { | 28 : render_view_host_(static_cast<RenderViewHostImpl*>(render_view_host)), |
Robert Sesek
2014/03/20 15:06:04
menu_runner_ is uninitialized here.
tkent
2014/03/21 13:55:26
Done.
| |
29 popup_was_hidden_(false) { | |
29 notification_registrar_.Add( | 30 notification_registrar_.Add( |
30 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | 31 this, NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, |
31 Source<RenderWidgetHost>(render_view_host)); | 32 Source<RenderWidgetHost>(render_view_host)); |
32 } | 33 } |
33 | 34 |
34 void PopupMenuHelper::ShowPopupMenu( | 35 void PopupMenuHelper::ShowPopupMenu( |
35 const gfx::Rect& bounds, | 36 const gfx::Rect& bounds, |
36 int item_height, | 37 int item_height, |
37 double item_font_size, | 38 double item_font_size, |
38 int selected_item, | 39 int selected_item, |
39 const std::vector<MenuItem>& items, | 40 const std::vector<MenuItem>& items, |
40 bool right_aligned, | 41 bool right_aligned, |
41 bool allow_multiple_selection) { | 42 bool allow_multiple_selection) { |
42 // Only single selection list boxes show a popup on Mac. | 43 // Only single selection list boxes show a popup on Mac. |
43 DCHECK(!allow_multiple_selection); | 44 DCHECK(!allow_multiple_selection); |
44 | 45 |
45 if (!g_allow_showing_popup_menus) | 46 if (!g_allow_showing_popup_menus) |
46 return; | 47 return; |
47 | 48 |
48 // Retain the Cocoa view for the duration of the pop-up so that it can't be | 49 // 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 // 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 // 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 // call returns. That's what was happening in <http://crbug.com/33250>). |
52 RenderWidgetHostViewMac* rwhvm = | 53 RenderWidgetHostViewMac* rwhvm = |
53 static_cast<RenderWidgetHostViewMac*>(GetRenderWidgetHostView()); | 54 static_cast<RenderWidgetHostViewMac*>(GetRenderWidgetHostView()); |
54 base::scoped_nsobject<RenderWidgetHostViewCocoa> cocoa_view( | 55 base::scoped_nsobject<RenderWidgetHostViewCocoa> cocoa_view( |
55 [rwhvm->cocoa_view() retain]); | 56 [rwhvm->cocoa_view() retain]); |
56 | 57 |
57 // Display the menu. | 58 // Display the menu. |
58 base::scoped_nsobject<WebMenuRunner> menu_runner; | 59 menu_runner_ = [[WebMenuRunner alloc] initWithItems:items |
59 menu_runner.reset([[WebMenuRunner alloc] initWithItems:items | 60 fontSize:item_font_size |
60 fontSize:item_font_size | 61 rightAligned:right_aligned]; |
61 rightAligned:right_aligned]); | |
62 | 62 |
63 { | 63 { |
64 // Make sure events can be pumped while the menu is up. | 64 // Make sure events can be pumped while the menu is up. |
65 base::MessageLoop::ScopedNestableTaskAllower allow( | 65 base::MessageLoop::ScopedNestableTaskAllower allow( |
66 base::MessageLoop::current()); | 66 base::MessageLoop::current()); |
67 | 67 |
68 // One of the events that could be pumped is |window.close()|. | 68 // One of the events that could be pumped is |window.close()|. |
69 // User-initiated event-tracking loops protect against this by | 69 // User-initiated event-tracking loops protect against this by |
70 // setting flags in -[CrApplication sendEvent:], but since | 70 // setting flags in -[CrApplication sendEvent:], but since |
71 // web-content menus are initiated by IPC message the setup has to | 71 // web-content menus are initiated by IPC message the setup has to |
72 // be done manually. | 72 // be done manually. |
73 base::mac::ScopedSendingEvent sending_event_scoper; | 73 base::mac::ScopedSendingEvent sending_event_scoper; |
74 | 74 |
75 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished. | 75 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished. |
76 [menu_runner runMenuInView:cocoa_view | 76 [menu_runner_ runMenuInView:cocoa_view |
77 withBounds:[cocoa_view flipRectToNSRect:bounds] | 77 withBounds:[cocoa_view flipRectToNSRect:bounds] |
78 initialIndex:selected_item]; | 78 initialIndex:selected_item]; |
79 } | 79 } |
80 | 80 |
81 if (!render_view_host_) { | 81 if (!render_view_host_) { |
82 // Bad news, the RenderViewHost got deleted while we were off running the | 82 // Bad news, the RenderViewHost got deleted while we were off running the |
83 // menu. Nothing to do. | 83 // menu. Nothing to do. |
84 [menu_runner_ release]; | |
85 menu_runner_ = NULL; | |
Robert Sesek
2014/03/20 15:06:04
nil instead of NULL for ObjC objects, and on line
tkent
2014/03/21 13:55:26
Done.
| |
84 return; | 86 return; |
85 } | 87 } |
86 | 88 |
87 if ([menu_runner menuItemWasChosen]) { | 89 if (!popup_was_hidden_) { |
88 render_view_host_->DidSelectPopupMenuItem( | 90 if ([menu_runner_ menuItemWasChosen]) { |
89 [menu_runner indexOfSelectedItem]); | 91 render_view_host_->DidSelectPopupMenuItem( |
90 } else { | 92 [menu_runner_ indexOfSelectedItem]); |
91 render_view_host_->DidCancelPopupMenu(); | 93 } else { |
94 render_view_host_->DidCancelPopupMenu(); | |
95 } | |
92 } | 96 } |
97 [menu_runner_ release]; | |
98 menu_runner_ = NULL; | |
99 } | |
100 | |
101 void PopupMenuHelper::Hide() { | |
102 if (menu_runner_) | |
103 [menu_runner_ hide]; | |
104 popup_was_hidden_ = true; | |
93 } | 105 } |
94 | 106 |
95 // static | 107 // static |
96 void PopupMenuHelper::DontShowPopupMenuForTesting() { | 108 void PopupMenuHelper::DontShowPopupMenuForTesting() { |
97 g_allow_showing_popup_menus = false; | 109 g_allow_showing_popup_menus = false; |
98 } | 110 } |
99 | 111 |
100 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const { | 112 RenderWidgetHostViewMac* PopupMenuHelper::GetRenderWidgetHostView() const { |
101 return static_cast<RenderWidgetHostViewMac*>(render_view_host_->GetView()); | 113 return static_cast<RenderWidgetHostViewMac*>(render_view_host_->GetView()); |
102 } | 114 } |
103 | 115 |
104 void PopupMenuHelper::Observe(int type, | 116 void PopupMenuHelper::Observe(int type, |
105 const NotificationSource& source, | 117 const NotificationSource& source, |
106 const NotificationDetails& details) { | 118 const NotificationDetails& details) { |
107 DCHECK(type == NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); | 119 DCHECK(type == NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); |
108 DCHECK(Source<RenderWidgetHost>(source).ptr() == render_view_host_); | 120 DCHECK(Source<RenderWidgetHost>(source).ptr() == render_view_host_); |
109 render_view_host_ = NULL; | 121 render_view_host_ = NULL; |
110 } | 122 } |
111 | 123 |
112 } // namespace content | 124 } // namespace content |
OLD | NEW |