OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #import <Carbon/Carbon.h> | |
6 | |
7 #include "content/browser/tab_contents/popup_menu_helper_mac.h" | |
8 | |
9 #import "base/mac/scoped_sending_event.h" | |
10 #include "base/memory/scoped_nsobject.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/public/browser/notification_source.h" | |
15 #include "content/public/browser/notification_types.h" | |
16 #import "ui/base/cocoa/base_view.h" | |
17 #include "webkit/glue/webmenurunner_mac.h" | |
18 | |
19 using content::RenderViewHost; | |
20 using content::RenderViewHostImpl; | |
21 using content::RenderWidgetHost; | |
22 | |
23 PopupMenuHelper::PopupMenuHelper(RenderViewHost* render_view_host) | |
24 : render_view_host_(static_cast<RenderViewHostImpl*>(render_view_host)) { | |
25 notification_registrar_.Add( | |
26 this, content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED, | |
27 content::Source<RenderWidgetHost>(render_view_host)); | |
28 } | |
29 | |
30 void PopupMenuHelper::ShowPopupMenu( | |
31 const gfx::Rect& bounds, | |
32 int item_height, | |
33 double item_font_size, | |
34 int selected_item, | |
35 const std::vector<WebMenuItem>& items, | |
36 bool right_aligned) { | |
37 // Retain the Cocoa view for the duration of the pop-up so that it can't be | |
38 // dealloced if my Destroy() method is called while the pop-up's up (which | |
39 // would in turn delete me, causing a crash once the -runMenuInView | |
40 // call returns. That's what was happening in <http://crbug.com/33250>). | |
41 RenderWidgetHostViewMac* rwhvm = | |
42 static_cast<RenderWidgetHostViewMac*>(render_view_host_->GetView()); | |
43 scoped_nsobject<RenderWidgetHostViewCocoa> cocoa_view | |
44 ([rwhvm->cocoa_view() retain]); | |
45 | |
46 // Display the menu. | |
47 scoped_nsobject<WebMenuRunner> menu_runner; | |
48 menu_runner.reset([[WebMenuRunner alloc] initWithItems:items | |
49 fontSize:item_font_size | |
50 rightAligned:right_aligned]); | |
51 | |
52 { | |
53 // Make sure events can be pumped while the menu is up. | |
54 MessageLoop::ScopedNestableTaskAllower allow(MessageLoop::current()); | |
55 | |
56 // One of the events that could be pumped is |window.close()|. | |
57 // User-initiated event-tracking loops protect against this by | |
58 // setting flags in -[CrApplication sendEvent:], but since | |
59 // web-content menus are initiated by IPC message the setup has to | |
60 // be done manually. | |
61 base::mac::ScopedSendingEvent sending_event_scoper; | |
62 | |
63 // Now run a SYNCHRONOUS NESTED EVENT LOOP until the pop-up is finished. | |
64 [menu_runner runMenuInView:cocoa_view | |
65 withBounds:[cocoa_view flipRectToNSRect:bounds] | |
66 initialIndex:selected_item]; | |
67 } | |
68 | |
69 if (!render_view_host_) { | |
70 // Bad news, the RenderViewHost got deleted while we were off running the | |
71 // menu. Nothing to do. | |
72 return; | |
73 } | |
74 | |
75 if ([menu_runner menuItemWasChosen]) { | |
76 render_view_host_->DidSelectPopupMenuItem( | |
77 [menu_runner indexOfSelectedItem]); | |
78 } else { | |
79 render_view_host_->DidCancelPopupMenu(); | |
80 } | |
81 } | |
82 | |
83 void PopupMenuHelper::Observe( | |
84 int type, | |
85 const content::NotificationSource& source, | |
86 const content::NotificationDetails& details) { | |
87 DCHECK(type == content::NOTIFICATION_RENDER_WIDGET_HOST_DESTROYED); | |
88 DCHECK(content::Source<RenderWidgetHost>(source).ptr() == render_view_host_); | |
89 render_view_host_ = NULL; | |
90 } | |
91 | |
OLD | NEW |