Chromium Code Reviews| 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 "chrome/browser/tab_contents/moving_to_content/tab_contents_view_mac.h" | 7 #include "chrome/browser/tab_contents/moving_to_content/tab_contents_view_mac.h" |
| 8 | 8 |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| 11 #import "base/mac/scoped_sending_event.h" | 11 #import "base/mac/scoped_sending_event.h" |
| 12 #import "base/message_pump_mac.h" | 12 #import "base/message_pump_mac.h" |
| 13 #import "chrome/browser/renderer_host/chrome_render_widget_host_view_mac_delegat e.h" | |
| 14 #include "chrome/browser/tab_contents/render_view_context_menu_mac.h" | |
| 15 #include "chrome/browser/tab_contents/web_drag_bookmark_handler_mac.h" | |
| 16 #import "chrome/browser/ui/cocoa/view_id_util.h" | |
| 17 #include "content/browser/renderer_host/render_view_host.h" | 13 #include "content/browser/renderer_host/render_view_host.h" |
| 18 #include "content/browser/renderer_host/render_view_host_factory.h" | 14 #include "content/browser/renderer_host/render_view_host_factory.h" |
| 19 #include "content/browser/renderer_host/render_widget_host.h" | 15 #include "content/browser/renderer_host/render_widget_host.h" |
| 20 #include "content/browser/renderer_host/render_widget_host_view_mac.h" | 16 #include "content/browser/renderer_host/render_widget_host_view_mac.h" |
| 21 #include "content/browser/tab_contents/popup_menu_helper_mac.h" | 17 #include "content/browser/tab_contents/popup_menu_helper_mac.h" |
| 22 #include "content/browser/tab_contents/tab_contents.h" | 18 #include "content/browser/tab_contents/tab_contents.h" |
| 23 #import "content/browser/tab_contents/web_drag_dest_mac.h" | 19 #import "content/browser/tab_contents/web_drag_dest_mac.h" |
| 24 #import "content/browser/tab_contents/web_drag_source_mac.h" | 20 #import "content/browser/tab_contents/web_drag_source_mac.h" |
| 25 #include "content/common/view_messages.h" | 21 #include "content/common/view_messages.h" |
| 22 #include "content/public/browser/web_contents_view_mac_delegate.h" | |
| 26 #include "content/public/browser/web_contents_delegate.h" | 23 #include "content/public/browser/web_contents_delegate.h" |
| 27 #include "skia/ext/skia_utils_mac.h" | 24 #include "skia/ext/skia_utils_mac.h" |
| 28 #import "third_party/mozilla/NSPasteboard+Utils.h" | 25 #import "third_party/mozilla/NSPasteboard+Utils.h" |
| 29 #import "ui/base/cocoa/focus_tracker.h" | 26 #import "ui/base/cocoa/focus_tracker.h" |
| 30 | 27 |
| 31 using WebKit::WebDragOperation; | 28 using WebKit::WebDragOperation; |
| 32 using WebKit::WebDragOperationsMask; | 29 using WebKit::WebDragOperationsMask; |
| 33 using content::WebContents; | 30 using content::WebContents; |
| 34 | 31 |
| 35 // Ensure that the WebKit::WebDragOperation enum values stay in sync with | 32 // Ensure that the WebKit::WebDragOperation enum values stay in sync with |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 56 - (void)cancelDeferredClose; | 53 - (void)cancelDeferredClose; |
| 57 - (void)clearTabContentsView; | 54 - (void)clearTabContentsView; |
| 58 - (void)closeTabAfterEvent; | 55 - (void)closeTabAfterEvent; |
| 59 - (void)viewDidBecomeFirstResponder:(NSNotification*)notification; | 56 - (void)viewDidBecomeFirstResponder:(NSNotification*)notification; |
| 60 // Notify the RenderWidgetHost that the frame was updated so it can resize | 57 // Notify the RenderWidgetHost that the frame was updated so it can resize |
| 61 // its contents. | 58 // its contents. |
| 62 - (void)renderWidgetHostWasResized; | 59 - (void)renderWidgetHostWasResized; |
| 63 @end | 60 @end |
| 64 | 61 |
| 65 namespace tab_contents_view_mac { | 62 namespace tab_contents_view_mac { |
| 66 content::WebContentsView* CreateWebContentsView(WebContents* web_contents) { | 63 content::WebContentsView* CreateWebContentsView( |
| 67 return new TabContentsViewMac(web_contents); | 64 WebContents* web_contents, |
| 65 content::WebContentsViewMacDelegate* delegate) { | |
| 66 return new TabContentsViewMac(web_contents, delegate); | |
| 68 } | 67 } |
| 69 } | 68 } |
| 70 | 69 |
| 71 TabContentsViewMac::TabContentsViewMac(WebContents* web_contents) | 70 TabContentsViewMac::TabContentsViewMac( |
| 71 WebContents* web_contents, | |
| 72 content::WebContentsViewMacDelegate* delegate) | |
| 72 : tab_contents_(static_cast<TabContents*>(web_contents)), | 73 : tab_contents_(static_cast<TabContents*>(web_contents)), |
| 74 delegate_(delegate), | |
| 73 overlaid_view_(nil) { | 75 overlaid_view_(nil) { |
| 74 } | 76 } |
| 75 | 77 |
| 76 TabContentsViewMac::~TabContentsViewMac() { | 78 TabContentsViewMac::~TabContentsViewMac() { |
| 77 // This handles the case where a renderer close call was deferred | 79 // This handles the case where a renderer close call was deferred |
| 78 // while the user was operating a UI control which resulted in a | 80 // while the user was operating a UI control which resulted in a |
| 79 // close. In that case, the Cocoa view outlives the | 81 // close. In that case, the Cocoa view outlives the |
| 80 // TabContentsViewMac instance due to Cocoa retain count. | 82 // TabContentsViewMac instance due to Cocoa retain count. |
| 81 [cocoa_view_ cancelDeferredClose]; | 83 [cocoa_view_ cancelDeferredClose]; |
| 82 [cocoa_view_ clearTabContentsView]; | 84 [cocoa_view_ clearTabContentsView]; |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 95 // test view, so we don't want to clobber it with a real one. To verify that | 97 // test view, so we don't want to clobber it with a real one. To verify that |
| 96 // this actually is happening (and somebody isn't accidentally creating the | 98 // this actually is happening (and somebody isn't accidentally creating the |
| 97 // view twice), we check for the RVH Factory, which will be set when we're | 99 // view twice), we check for the RVH Factory, which will be set when we're |
| 98 // making special ones (which go along with the special views). | 100 // making special ones (which go along with the special views). |
| 99 DCHECK(RenderViewHostFactory::has_factory()); | 101 DCHECK(RenderViewHostFactory::has_factory()); |
| 100 return render_widget_host->view(); | 102 return render_widget_host->view(); |
| 101 } | 103 } |
| 102 | 104 |
| 103 RenderWidgetHostViewMac* view = | 105 RenderWidgetHostViewMac* view = |
| 104 new RenderWidgetHostViewMac(render_widget_host); | 106 new RenderWidgetHostViewMac(render_widget_host); |
| 105 | 107 if (delegate()) { |
| 106 // TODO(avi): Find a better place to do this. | 108 NSObject<RenderWidgetHostViewMacDelegate>* rw_delegate = |
| 107 ChromeRenderWidgetHostViewMacDelegate* rw_delegate = | 109 delegate()->CreateRenderWidgetHostViewDelegate(render_widget_host); |
| 108 [[ChromeRenderWidgetHostViewMacDelegate alloc] | 110 view->SetDelegate(rw_delegate); |
| 109 initWithRenderWidgetHost:render_widget_host]; | 111 } |
| 110 view->SetDelegate(rw_delegate); | |
| 111 | 112 |
| 112 // Fancy layout comes later; for now just make it our size and resize it | 113 // Fancy layout comes later; for now just make it our size and resize it |
| 113 // with us. In case there are other siblings of the content area, we want | 114 // with us. In case there are other siblings of the content area, we want |
| 114 // to make sure the content area is on the bottom so other things draw over | 115 // to make sure the content area is on the bottom so other things draw over |
| 115 // it. | 116 // it. |
| 116 NSView* view_view = view->GetNativeView(); | 117 NSView* view_view = view->GetNativeView(); |
| 117 [view_view setFrame:[cocoa_view_.get() bounds]]; | 118 [view_view setFrame:[cocoa_view_.get() bounds]]; |
| 118 [view_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; | 119 [view_view setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 119 // Add the new view below all other views; this also keeps it below any | 120 // Add the new view below all other views; this also keeps it below any |
| 120 // overlay view installed. | 121 // overlay view installed. |
| (...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 337 | 338 |
| 338 // A RenderWidgetHostViewMac has lifetime scoped to the view. Now that it's | 339 // A RenderWidgetHostViewMac has lifetime scoped to the view. Now that it's |
| 339 // properly embedded (or purposely ignored) we can release the retain we took | 340 // properly embedded (or purposely ignored) we can release the retain we took |
| 340 // in CreateNewFullscreenWidgetInternal(). | 341 // in CreateNewFullscreenWidgetInternal(). |
| 341 RenderWidgetHostViewMac* widget_view_mac = | 342 RenderWidgetHostViewMac* widget_view_mac = |
| 342 static_cast<RenderWidgetHostViewMac*>(widget_host_view); | 343 static_cast<RenderWidgetHostViewMac*>(widget_host_view); |
| 343 [widget_view_mac->GetNativeView() release]; | 344 [widget_view_mac->GetNativeView() release]; |
| 344 } | 345 } |
| 345 | 346 |
| 346 void TabContentsViewMac::ShowContextMenu(const ContextMenuParams& params) { | 347 void TabContentsViewMac::ShowContextMenu(const ContextMenuParams& params) { |
| 347 // The renderer may send the "show context menu" message multiple times, one | |
| 348 // for each right click mouse event it receives. Normally, this doesn't happen | |
| 349 // because mouse events are not forwarded once the context menu is showing. | |
| 350 // However, there's a race - the context menu may not yet be showing when | |
| 351 // the second mouse event arrives. In this case, |ShowContextMenu()| will | |
| 352 // get called multiple times - if so, don't create another context menu. | |
| 353 // TODO(asvitkine): Fix the renderer so that it doesn't do this. | |
| 354 RenderWidgetHostView* widget_view = tab_contents_->GetRenderWidgetHostView(); | |
| 355 if (widget_view && widget_view->showing_context_menu()) | |
| 356 return; | |
| 357 | |
| 358 // Allow delegates to handle the context menu operation first. | 348 // Allow delegates to handle the context menu operation first. |
| 359 if (tab_contents_->GetDelegate() && | 349 if (tab_contents_->GetDelegate() && |
| 360 tab_contents_->GetDelegate()->HandleContextMenu(params)) { | 350 tab_contents_->GetDelegate()->HandleContextMenu(params)) { |
| 361 return; | 351 return; |
| 362 } | 352 } |
| 363 | 353 |
| 364 context_menu_.reset(new RenderViewContextMenuMac(tab_contents(), | 354 if (delegate()) |
| 365 params, | 355 delegate()->ShowContextMenu(params); |
| 366 GetContentNativeView())); | 356 else |
| 367 context_menu_->Init(); | 357 DLOG(ERROR) << "Implement context menus without chrome/ code"; |
|
jochen (gone - plz use gerrit)
2012/02/03 00:13:33
Aren't error messages with chrome/ in it also some
Avi (use Gerrit)
2012/02/03 01:18:53
Stolen from the GTK version of this code :) Will c
| |
| 368 } | 358 } |
| 369 | 359 |
| 370 // Display a popup menu for WebKit using Cocoa widgets. | 360 // Display a popup menu for WebKit using Cocoa widgets. |
| 371 void TabContentsViewMac::ShowPopupMenu( | 361 void TabContentsViewMac::ShowPopupMenu( |
| 372 const gfx::Rect& bounds, | 362 const gfx::Rect& bounds, |
| 373 int item_height, | 363 int item_height, |
| 374 double item_font_size, | 364 double item_font_size, |
| 375 int selected_item, | 365 int selected_item, |
| 376 const std::vector<WebMenuItem>& items, | 366 const std::vector<WebMenuItem>& items, |
| 377 bool right_aligned) { | 367 bool right_aligned) { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 419 } | 409 } |
| 420 | 410 |
| 421 @implementation TabContentsViewCocoa | 411 @implementation TabContentsViewCocoa |
| 422 | 412 |
| 423 - (id)initWithTabContentsViewMac:(TabContentsViewMac*)w { | 413 - (id)initWithTabContentsViewMac:(TabContentsViewMac*)w { |
| 424 self = [super initWithFrame:NSZeroRect]; | 414 self = [super initWithFrame:NSZeroRect]; |
| 425 if (self != nil) { | 415 if (self != nil) { |
| 426 tabContentsView_ = w; | 416 tabContentsView_ = w; |
| 427 dragDest_.reset( | 417 dragDest_.reset( |
| 428 [[WebDragDest alloc] initWithTabContents:[self tabContents]]); | 418 [[WebDragDest alloc] initWithTabContents:[self tabContents]]); |
| 429 bookmarkHandler_.reset(new WebDragBookmarkHandlerMac); | |
| 430 [dragDest_ setDragDelegate: | |
| 431 static_cast<content::WebDragDestDelegate*>(bookmarkHandler_.get())]; | |
| 432 [self registerDragTypes]; | 419 [self registerDragTypes]; |
| 433 // TabContentsViewCocoa's ViewID may be changed to VIEW_ID_DEV_TOOLS_DOCKED | |
| 434 // by TabContentsController, so we can't just override -viewID method to | |
| 435 // return it. | |
| 436 view_id_util::SetID(self, VIEW_ID_TAB_CONTAINER); | |
| 437 | 420 |
| 438 [[NSNotificationCenter defaultCenter] | 421 [[NSNotificationCenter defaultCenter] |
| 439 addObserver:self | 422 addObserver:self |
| 440 selector:@selector(viewDidBecomeFirstResponder:) | 423 selector:@selector(viewDidBecomeFirstResponder:) |
| 441 name:kViewDidBecomeFirstResponder | 424 name:kViewDidBecomeFirstResponder |
| 442 object:nil]; | 425 object:nil]; |
| 426 | |
| 427 if (tabContentsView_->delegate()) { | |
| 428 [dragDest_ setDragDelegate:tabContentsView_->delegate()->DragDelegate()]; | |
| 429 tabContentsView_->delegate()->NativeViewCreated(self); | |
| 430 } | |
| 443 } | 431 } |
| 444 return self; | 432 return self; |
| 445 } | 433 } |
| 446 | 434 |
| 447 - (void)dealloc { | 435 - (void)dealloc { |
| 448 view_id_util::UnsetID(self); | 436 if (tabContentsView_ && tabContentsView_->delegate()) |
| 437 tabContentsView_->delegate()->NativeViewDestroyed(self); | |
| 449 | 438 |
| 450 // Cancel any deferred tab closes, just in case. | 439 // Cancel any deferred tab closes, just in case. |
| 451 [self cancelDeferredClose]; | 440 [self cancelDeferredClose]; |
| 452 | 441 |
| 453 // This probably isn't strictly necessary, but can't hurt. | 442 // This probably isn't strictly necessary, but can't hurt. |
| 454 [self unregisterDraggedTypes]; | 443 [self unregisterDraggedTypes]; |
| 455 | 444 |
| 456 [[NSNotificationCenter defaultCenter] removeObserver:self]; | 445 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 457 | 446 |
| 458 [super dealloc]; | 447 [super dealloc]; |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 614 [[[notification userInfo] objectForKey:kSelectionDirection] | 603 [[[notification userInfo] objectForKey:kSelectionDirection] |
| 615 unsignedIntegerValue]; | 604 unsignedIntegerValue]; |
| 616 if (direction == NSDirectSelection) | 605 if (direction == NSDirectSelection) |
| 617 return; | 606 return; |
| 618 | 607 |
| 619 [self tabContents]-> | 608 [self tabContents]-> |
| 620 FocusThroughTabTraversal(direction == NSSelectingPrevious); | 609 FocusThroughTabTraversal(direction == NSSelectingPrevious); |
| 621 } | 610 } |
| 622 | 611 |
| 623 @end | 612 @end |
| OLD | NEW |