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 #import "content/browser/web_contents/web_contents_view_mac.h"
11 #import "base/mac/mac_util.h"
12 #import "base/mac/scoped_sending_event.h"
13 #include "base/mac/sdk_forward_declarations.h"
14 #include "base/message_loop/message_loop.h"
15 #import "base/message_loop/message_pump_mac.h"
16 #include "content/browser/frame_host/popup_menu_helper_mac.h"
17 #include "content/browser/renderer_host/render_view_host_factory.h"
18 #include "content/browser/renderer_host/render_view_host_impl.h"
19 #include "content/browser/renderer_host/render_widget_host_view_mac.h"
20 #include "content/browser/web_contents/web_contents_impl.h"
21 #import "content/browser/web_contents/web_drag_dest_mac.h"
22 #import "content/browser/web_contents/web_drag_source_mac.h"
23 #include "content/common/view_messages.h"
24 #include "content/public/browser/web_contents_delegate.h"
25 #include "content/public/browser/web_contents_view_delegate.h"
26 #include "skia/ext/skia_utils_mac.h"
27 #import "third_party/mozilla/NSPasteboard+Utils.h"
28 #include "ui/base/clipboard/custom_data_helper.h"
29 #import "ui/base/cocoa/focus_tracker.h"
30 #include "ui/base/dragdrop/cocoa_dnd_util.h"
31 #include "ui/gfx/image/image_skia_util_mac.h"
33 using blink::WebDragOperation;
34 using blink::WebDragOperationsMask;
35 using content::DropData;
36 using content::PopupMenuHelper;
37 using content::RenderViewHostFactory;
38 using content::RenderWidgetHostView;
39 using content::RenderWidgetHostViewMac;
40 using content::WebContents;
41 using content::WebContentsImpl;
42 using content::WebContentsViewMac;
44 // Ensure that the blink::WebDragOperation enum values stay in sync with
45 // NSDragOperation constants, since the code below static_casts between 'em.
46 #define STATIC_ASSERT_MATCHING_ENUM(name) \
47 static_assert(int(NS##name) == int(blink::Web##name), "enum mismatch: " #name)
48 STATIC_ASSERT_MATCHING_ENUM(DragOperationNone);
49 STATIC_ASSERT_MATCHING_ENUM(DragOperationCopy);
50 STATIC_ASSERT_MATCHING_ENUM(DragOperationLink);
51 STATIC_ASSERT_MATCHING_ENUM(DragOperationGeneric);
52 STATIC_ASSERT_MATCHING_ENUM(DragOperationPrivate);
53 STATIC_ASSERT_MATCHING_ENUM(DragOperationMove);
54 STATIC_ASSERT_MATCHING_ENUM(DragOperationDelete);
55 STATIC_ASSERT_MATCHING_ENUM(DragOperationEvery);
57 @interface WebContentsViewCocoa (Private)
58 - (id)initWithWebContentsViewMac:(WebContentsViewMac*)w;
59 - (void)registerDragTypes;
60 - (void)setCurrentDragOperation:(NSDragOperation)operation;
61 - (DropData*)dropData;
62 - (void)startDragWithDropData:(const DropData&)dropData
63 dragOperationMask:(NSDragOperation)operationMask
65 offset:(NSPoint)offset;
66 - (void)cancelDeferredClose;
67 - (void)clearWebContentsView;
68 - (void)closeTabAfterEvent;
69 - (void)viewDidBecomeFirstResponder:(NSNotification*)notification;
74 WebContentsView* CreateWebContentsView(
75 WebContentsImpl* web_contents,
76 WebContentsViewDelegate* delegate,
77 RenderViewHostDelegateView** render_view_host_delegate_view) {
78 WebContentsViewMac* rv = new WebContentsViewMac(web_contents, delegate);
79 *render_view_host_delegate_view = rv;
83 WebContentsViewMac::WebContentsViewMac(WebContentsImpl* web_contents,
84 WebContentsViewDelegate* delegate)
85 : web_contents_(web_contents),
87 allow_other_views_(false) {
90 WebContentsViewMac::~WebContentsViewMac() {
91 // This handles the case where a renderer close call was deferred
92 // while the user was operating a UI control which resulted in a
93 // close. In that case, the Cocoa view outlives the
94 // WebContentsViewMac instance due to Cocoa retain count.
95 [cocoa_view_ cancelDeferredClose];
96 [cocoa_view_ clearWebContentsView];
99 gfx::NativeView WebContentsViewMac::GetNativeView() const {
100 return cocoa_view_.get();
103 gfx::NativeView WebContentsViewMac::GetContentNativeView() const {
104 RenderWidgetHostView* rwhv = web_contents_->GetRenderWidgetHostView();
107 return rwhv->GetNativeView();
110 gfx::NativeWindow WebContentsViewMac::GetTopLevelNativeWindow() const {
111 NSWindow* window = [cocoa_view_.get() window];
112 return window ? window : delegate_->GetNativeWindow();
115 void WebContentsViewMac::GetContainerBounds(gfx::Rect* out) const {
116 // Convert bounds to window coordinate space.
118 [cocoa_view_.get() convertRect:[cocoa_view_.get() bounds] toView:nil];
120 // Convert bounds to screen coordinate space.
121 NSWindow* window = [cocoa_view_.get() window];
122 bounds.origin = [window convertBaseToScreen:bounds.origin];
124 // Flip y to account for screen flip.
125 NSScreen* screen = [[NSScreen screens] objectAtIndex:0];
126 bounds.origin.y = [screen frame].size.height - bounds.origin.y
127 - bounds.size.height;
128 *out = gfx::Rect(NSRectToCGRect(bounds));
131 void WebContentsViewMac::StartDragging(
132 const DropData& drop_data,
133 WebDragOperationsMask allowed_operations,
134 const gfx::ImageSkia& image,
135 const gfx::Vector2d& image_offset,
136 const DragEventSourceInfo& event_info) {
137 // By allowing nested tasks, the code below also allows Close(),
138 // which would deallocate |this|. The same problem can occur while
139 // processing -sendEvent:, so Close() is deferred in that case.
140 // Drags from web content do not come via -sendEvent:, this sets the
141 // same flag -sendEvent: would.
142 base::mac::ScopedSendingEvent sending_event_scoper;
144 // The drag invokes a nested event loop, arrange to continue
145 // processing events.
146 base::MessageLoop::ScopedNestableTaskAllower allow(
147 base::MessageLoop::current());
148 NSDragOperation mask = static_cast<NSDragOperation>(allowed_operations);
149 NSPoint offset = NSPointFromCGPoint(
150 gfx::PointAtOffsetFromOrigin(image_offset).ToCGPoint());
151 [cocoa_view_ startDragWithDropData:drop_data
152 dragOperationMask:mask
153 image:gfx::NSImageFromImageSkia(image)
157 void WebContentsViewMac::SizeContents(const gfx::Size& size) {
158 // TODO(brettw | japhet) This is a hack and should be removed.
159 // See web_contents_view.h.
160 // Note(erikchen): This method has /never/ worked correctly. I've removed the
161 // previous implementation.
164 gfx::NativeView WebContentsViewMac::GetNativeViewForFocus() const {
165 RenderWidgetHostView* rwhv =
166 web_contents_->GetFullscreenRenderWidgetHostView();
168 rwhv = web_contents_->GetRenderWidgetHostView();
169 return rwhv ? rwhv->GetNativeView() : nil;
172 void WebContentsViewMac::Focus() {
173 gfx::NativeView native_view = GetNativeViewForFocus();
174 NSWindow* window = [native_view window];
175 [window makeFirstResponder:native_view];
176 if (![window isVisible])
178 [window makeKeyAndOrderFront:nil];
181 void WebContentsViewMac::SetInitialFocus() {
182 if (web_contents_->FocusLocationBarByDefault())
183 web_contents_->SetFocusToLocationBar(false);
188 void WebContentsViewMac::StoreFocus() {
189 gfx::NativeView native_view = GetNativeViewForFocus();
190 // We're explicitly being asked to store focus, so don't worry if there's
191 // already a view saved.
192 focus_tracker_.reset(
193 [[FocusTracker alloc] initWithWindow:[native_view window]]);
196 void WebContentsViewMac::RestoreFocus() {
197 gfx::NativeView native_view = GetNativeViewForFocus();
198 // TODO(avi): Could we be restoring a view that's no longer in the key view
200 if (!(focus_tracker_.get() &&
201 [focus_tracker_ restoreFocusInWindow:[native_view window]])) {
202 // Fall back to the default focus behavior if we could not restore focus.
203 // TODO(shess): If location-bar gets focus by default, this will
204 // select-all in the field. If there was a specific selection in
205 // the field when we navigated away from it, we should restore
210 focus_tracker_.reset(nil);
213 DropData* WebContentsViewMac::GetDropData() const {
214 return [cocoa_view_ dropData];
217 void WebContentsViewMac::UpdateDragCursor(WebDragOperation operation) {
218 [cocoa_view_ setCurrentDragOperation: operation];
221 void WebContentsViewMac::GotFocus() {
222 // This is only used in the views FocusManager stuff but it bleeds through
223 // all subclasses. http://crbug.com/21875
226 // This is called when the renderer asks us to take focus back (i.e., it has
227 // iterated past the last focusable element on the page).
228 void WebContentsViewMac::TakeFocus(bool reverse) {
230 [[cocoa_view_ window] selectPreviousKeyView:cocoa_view_.get()];
232 [[cocoa_view_ window] selectNextKeyView:cocoa_view_.get()];
236 void WebContentsViewMac::ShowContextMenu(
237 RenderFrameHost* render_frame_host,
238 const ContextMenuParams& params) {
239 // Allow delegates to handle the context menu operation first.
240 if (web_contents_->GetDelegate() &&
241 web_contents_->GetDelegate()->HandleContextMenu(params)) {
246 delegate()->ShowContextMenu(render_frame_host, params);
248 DLOG(ERROR) << "Cannot show context menus without a delegate.";
251 void WebContentsViewMac::ShowPopupMenu(
252 RenderFrameHost* render_frame_host,
253 const gfx::Rect& bounds,
255 double item_font_size,
257 const std::vector<MenuItem>& items,
259 bool allow_multiple_selection) {
260 popup_menu_helper_.reset(new PopupMenuHelper(render_frame_host));
261 popup_menu_helper_->ShowPopupMenu(bounds, item_height, item_font_size,
262 selected_item, items, right_aligned,
263 allow_multiple_selection);
264 popup_menu_helper_.reset();
267 void WebContentsViewMac::HidePopupMenu() {
268 if (popup_menu_helper_)
269 popup_menu_helper_->Hide();
272 gfx::Rect WebContentsViewMac::GetViewBounds() const {
273 // This method is not currently used on mac.
278 void WebContentsViewMac::SetAllowOtherViews(bool allow) {
279 if (allow_other_views_ == allow)
282 allow_other_views_ = allow;
283 RenderWidgetHostViewMac* view = static_cast<RenderWidgetHostViewMac*>(
284 web_contents_->GetRenderWidgetHostView());
286 view->SetAllowPauseForResizeOrRepaint(!allow_other_views_);
289 bool WebContentsViewMac::GetAllowOtherViews() const {
290 return allow_other_views_;
293 void WebContentsViewMac::CreateView(
294 const gfx::Size& initial_size, gfx::NativeView context) {
295 WebContentsViewCocoa* view =
296 [[WebContentsViewCocoa alloc] initWithWebContentsViewMac:this];
297 cocoa_view_.reset(view);
300 RenderWidgetHostViewBase* WebContentsViewMac::CreateViewForWidget(
301 RenderWidgetHost* render_widget_host, bool is_guest_view_hack) {
302 if (render_widget_host->GetView()) {
303 // During testing, the view will already be set up in most cases to the
304 // test view, so we don't want to clobber it with a real one. To verify that
305 // this actually is happening (and somebody isn't accidentally creating the
306 // view twice), we check for the RVH Factory, which will be set when we're
307 // making special ones (which go along with the special views).
308 DCHECK(RenderViewHostFactory::has_factory());
309 return static_cast<RenderWidgetHostViewBase*>(
310 render_widget_host->GetView());
313 RenderWidgetHostViewMac* view = new RenderWidgetHostViewMac(
314 render_widget_host, is_guest_view_hack);
316 base::scoped_nsobject<NSObject<RenderWidgetHostViewMacDelegate> >
318 delegate()->CreateRenderWidgetHostViewDelegate(render_widget_host));
320 view->SetDelegate(rw_delegate.get());
322 view->SetAllowPauseForResizeOrRepaint(!allow_other_views_);
324 // Fancy layout comes later; for now just make it our size and resize it
325 // with us. In case there are other siblings of the content area, we want
326 // to make sure the content area is on the bottom so other things draw over
328 NSView* view_view = view->GetNativeView();
329 [view_view setFrame:[cocoa_view_.get() bounds]];
330 [view_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
331 // Add the new view below all other views; this also keeps it below any
332 // overlay view installed.
333 [cocoa_view_.get() addSubview:view_view
334 positioned:NSWindowBelow
336 // For some reason known only to Cocoa, the autorecalculation of the key view
337 // loop set on the window doesn't set the next key view when the subview is
338 // added. On 10.6 things magically work fine; on 10.5 they fail
339 // <http://crbug.com/61493>. Digging into Cocoa key view loop code yielded
340 // madness; TODO(avi,rohit): look at this again and figure out what's really
342 [cocoa_view_.get() setNextKeyView:view_view];
346 RenderWidgetHostViewBase* WebContentsViewMac::CreateViewForPopupWidget(
347 RenderWidgetHost* render_widget_host) {
348 return new RenderWidgetHostViewMac(render_widget_host, false);
351 void WebContentsViewMac::SetPageTitle(const base::string16& title) {
352 // Meaningless on the Mac; widgets don't have a "title" attribute
356 void WebContentsViewMac::RenderViewCreated(RenderViewHost* host) {
357 // We want updates whenever the intrinsic width of the webpage changes.
358 // Put the RenderView into that mode. The preferred width is used for example
359 // when the "zoom" button in the browser window is clicked.
360 host->EnablePreferredSizeMode();
363 void WebContentsViewMac::RenderViewSwappedIn(RenderViewHost* host) {
366 void WebContentsViewMac::SetOverscrollControllerEnabled(bool enabled) {
369 bool WebContentsViewMac::IsEventTracking() const {
370 return base::MessagePumpMac::IsHandlingSendEvent();
373 // Arrange to call CloseTab() after we're back to the main event loop.
374 // The obvious way to do this would be PostNonNestableTask(), but that
375 // will fire when the event-tracking loop polls for events. So we
376 // need to bounce the message via Cocoa, instead.
377 void WebContentsViewMac::CloseTabAfterEventTracking() {
378 [cocoa_view_ cancelDeferredClose];
379 [cocoa_view_ performSelector:@selector(closeTabAfterEvent)
384 void WebContentsViewMac::CloseTab() {
385 web_contents_->Close(web_contents_->GetRenderViewHost());
388 } // namespace content
390 @implementation WebContentsViewCocoa
392 - (id)initWithWebContentsViewMac:(WebContentsViewMac*)w {
393 self = [super initWithFrame:NSZeroRect];
395 webContentsView_ = w;
397 [[WebDragDest alloc] initWithWebContentsImpl:[self webContents]]);
398 [self registerDragTypes];
400 [[NSNotificationCenter defaultCenter]
402 selector:@selector(viewDidBecomeFirstResponder:)
403 name:kViewDidBecomeFirstResponder
406 if (webContentsView_->delegate()) {
407 [dragDest_ setDragDelegate:webContentsView_->delegate()->
408 GetDragDestDelegate()];
415 // Cancel any deferred tab closes, just in case.
416 [self cancelDeferredClose];
418 // This probably isn't strictly necessary, but can't hurt.
419 [self unregisterDraggedTypes];
421 [[NSNotificationCenter defaultCenter] removeObserver:self];
426 - (BOOL)allowsVibrancy {
427 // Returning YES will allow rendering this view with vibrancy effect if it is
428 // incorporated into a view hierarchy that uses vibrancy, it will have no
430 // For details see Apple documentation on NSView and NSVisualEffectView.
434 // Registers for the view for the appropriate drag types.
435 - (void)registerDragTypes {
436 NSArray* types = [NSArray arrayWithObjects:
437 ui::kChromeDragDummyPboardType,
438 kWebURLsWithTitlesPboardType,
443 NSFilenamesPboardType,
444 ui::kWebCustomDataPboardType,
446 [self registerForDraggedTypes:types];
449 - (void)setCurrentDragOperation:(NSDragOperation)operation {
450 [dragDest_ setCurrentOperation:operation];
453 - (DropData*)dropData {
454 return [dragDest_ currentDropData];
457 - (WebContentsImpl*)webContents {
458 if (webContentsView_ == NULL)
460 return webContentsView_->web_contents();
463 - (void)mouseEvent:(NSEvent*)theEvent {
464 WebContentsImpl* webContents = [self webContents];
465 if (webContents && webContents->GetDelegate()) {
466 NSPoint location = [NSEvent mouseLocation];
467 if ([theEvent type] == NSMouseMoved)
468 webContents->GetDelegate()->ContentsMouseEvent(
469 webContents, gfx::Point(location.x, location.y), true);
470 if ([theEvent type] == NSMouseExited)
471 webContents->GetDelegate()->ContentsMouseEvent(
472 webContents, gfx::Point(location.x, location.y), false);
476 - (void)setMouseDownCanMoveWindow:(BOOL)canMove {
477 mouseDownCanMoveWindow_ = canMove;
480 - (BOOL)mouseDownCanMoveWindow {
481 // This is needed to prevent mouseDowns from moving the window
482 // around. The default implementation returns YES only for opaque
483 // views. WebContentsViewCocoa does not draw itself in any way, but
484 // its subviews do paint their entire frames. Returning NO here
485 // saves us the effort of overriding this method in every possible
487 return mouseDownCanMoveWindow_;
490 - (void)setOpaque:(BOOL)opaque {
491 RenderWidgetHostViewMac* view = static_cast<RenderWidgetHostViewMac*>(
492 webContentsView_->web_contents()->GetRenderWidgetHostView());
494 [view->cocoa_view() setOpaque:opaque];
497 - (void)pasteboard:(NSPasteboard*)sender provideDataForType:(NSString*)type {
498 [dragSource_ lazyWriteToPasteboard:sender
502 - (void)startDragWithDropData:(const DropData&)dropData
503 dragOperationMask:(NSDragOperation)operationMask
504 image:(NSImage*)image
505 offset:(NSPoint)offset {
506 dragSource_.reset([[WebDragSource alloc]
507 initWithContents:[self webContents]
512 pasteboard:[NSPasteboard pasteboardWithName:NSDragPboard]
513 dragOperationMask:operationMask]);
514 [dragSource_ startDrag];
517 // NSDraggingSource methods
519 // Returns what kind of drag operations are available. This is a required
520 // method for NSDraggingSource.
521 - (NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal {
523 return [dragSource_ draggingSourceOperationMaskForLocal:isLocal];
524 // No web drag source - this is the case for dragging a file from the
525 // downloads manager. Default to copy operation. Note: It is desirable to
526 // allow the user to either move or copy, but this requires additional
527 // plumbing to update the download item's path once its moved.
528 return NSDragOperationCopy;
531 // Called when a drag initiated in our view ends.
532 - (void)draggedImage:(NSImage*)anImage
533 endedAt:(NSPoint)screenPoint
534 operation:(NSDragOperation)operation {
535 [dragSource_ endDragAt:screenPoint operation:operation];
537 // Might as well throw out this object now.
541 // Called when a drag initiated in our view moves.
542 - (void)draggedImage:(NSImage*)draggedImage movedTo:(NSPoint)screenPoint {
545 // Called when a file drag is dropped and the promised files need to be written.
546 - (NSArray*)namesOfPromisedFilesDroppedAtDestination:(NSURL*)dropDest {
547 if (![dropDest isFileURL])
550 NSString* fileName = [dragSource_ dragPromisedFileTo:[dropDest path]];
554 return @[ fileName ];
557 // NSDraggingDestination methods
559 - (NSDragOperation)draggingEntered:(id<NSDraggingInfo>)sender {
560 return [dragDest_ draggingEntered:sender view:self];
563 - (void)draggingExited:(id<NSDraggingInfo>)sender {
564 [dragDest_ draggingExited:sender];
567 - (NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender {
568 return [dragDest_ draggingUpdated:sender view:self];
571 - (BOOL)performDragOperation:(id<NSDraggingInfo>)sender {
572 return [dragDest_ performDragOperation:sender view:self];
575 - (void)cancelDeferredClose {
576 SEL aSel = @selector(closeTabAfterEvent);
577 [NSObject cancelPreviousPerformRequestsWithTarget:self
582 - (void)clearWebContentsView {
583 webContentsView_ = NULL;
584 [dragSource_ clearWebContentsView];
587 - (void)closeTabAfterEvent {
588 webContentsView_->CloseTab();
591 - (void)viewDidBecomeFirstResponder:(NSNotification*)notification {
592 NSView* view = [notification object];
593 if (![[self subviews] containsObject:view])
596 NSSelectionDirection direction =
597 [[[notification userInfo] objectForKey:kSelectionDirection]
598 unsignedIntegerValue];
599 if (direction == NSDirectSelection)
603 FocusThroughTabTraversal(direction == NSSelectingPrevious);
606 // When the subviews require a layout, their size should be reset to the size
607 // of this view. (It is possible for the size to get out of sync as an
608 // optimization in preparation for an upcoming WebContentsView resize.
609 // http://crbug.com/264207)
610 - (void)resizeSubviewsWithOldSize:(NSSize)oldBoundsSize {
611 for (NSView* subview in self.subviews)
612 [subview setFrame:self.bounds];
615 - (void)viewWillMoveToWindow:(NSWindow*)newWindow {
616 NSWindow* oldWindow = [self window];
618 NSNotificationCenter* notificationCenter =
619 [NSNotificationCenter defaultCenter];
621 // Occlusion notification APIs are new in Mavericks.
622 bool supportsOcclusionAPIs = base::mac::IsOSMavericksOrLater();
624 if (supportsOcclusionAPIs) {
628 name:NSWindowDidChangeOcclusionStateNotification
634 selector:@selector(windowChangedOcclusionState:)
635 name:NSWindowDidChangeOcclusionStateNotification
641 - (void)windowChangedOcclusionState:(NSNotification*)notification {
642 DCHECK(base::mac::IsOSMavericksOrLater());
643 NSWindow* window = [notification object];
644 WebContentsImpl* webContents = [self webContents];
645 if (window && webContents && !webContents->IsBeingDestroyed()) {
646 if ([window occlusionState] & NSWindowOcclusionStateVisible) {
647 webContents->WasUnOccluded();
649 webContents->WasOccluded();