| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "ui/views/cocoa/bridged_native_widget.h" | 5 #import "ui/views/cocoa/bridged_native_widget.h" |
| 6 | 6 |
| 7 #import <objc/runtime.h> | 7 #import <objc/runtime.h> |
| 8 #include <stddef.h> | 8 #include <stddef.h> |
| 9 #include <stdint.h> | 9 #include <stdint.h> |
| 10 | 10 |
| (...skipping 335 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 346 in_fullscreen_transition_(false), | 346 in_fullscreen_transition_(false), |
| 347 window_visible_(false), | 347 window_visible_(false), |
| 348 wants_to_be_visible_(false) { | 348 wants_to_be_visible_(false) { |
| 349 SetupDragEventMonitor(); | 349 SetupDragEventMonitor(); |
| 350 DCHECK(parent); | 350 DCHECK(parent); |
| 351 window_delegate_.reset( | 351 window_delegate_.reset( |
| 352 [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); | 352 [[ViewsNSWindowDelegate alloc] initWithBridgedNativeWidget:this]); |
| 353 } | 353 } |
| 354 | 354 |
| 355 BridgedNativeWidget::~BridgedNativeWidget() { | 355 BridgedNativeWidget::~BridgedNativeWidget() { |
| 356 bool close_window = false; | 356 // The delegate should be cleared already. Note this enforces the precondition |
| 357 if ([window_ delegate]) { | 357 // that -[NSWindow close] is invoked on the hosted window before the |
| 358 // If the delegate is still set on a modal dialog, it means it was not | 358 // destructor is called. |
| 359 // closed via [NSApplication endSheet:]. This is probably OK if the widget | 359 DCHECK(![window_ delegate]); |
| 360 // was never shown. But Cocoa ignores close() calls on open sheets. Calling | |
| 361 // endSheet: here would work, but it messes up assumptions elsewhere. E.g. | |
| 362 // DialogClientView assumes its delegate is alive when closing, which isn't | |
| 363 // true after endSheet: synchronously calls OnNativeWidgetDestroyed(). | |
| 364 // So ban it. Modal dialogs should be closed via Widget::Close(). | |
| 365 DCHECK(!native_widget_mac_->IsWindowModalSheet()); | |
| 366 | |
| 367 // If the delegate is still set, it means OnWindowWillClose() has not been | |
| 368 // called and the window is still open. Usually, -[NSWindow close] would | |
| 369 // synchronously call OnWindowWillClose() which removes the delegate and | |
| 370 // notifies NativeWidgetMac, which then calls this with a nil delegate. | |
| 371 // For other teardown flows (e.g. Widget::WIDGET_OWNS_NATIVE_WIDGET or | |
| 372 // Widget::CloseNow()) the delegate must first be cleared to avoid AppKit | |
| 373 // calling back into the bridge. This means OnWindowWillClose() needs to be | |
| 374 // invoked manually, which is done below. | |
| 375 // Note that if the window has children it can't be closed until the | |
| 376 // children are gone, but removing child windows calls into AppKit for the | |
| 377 // parent window, so the delegate must be cleared first. | |
| 378 [window_ setDelegate:nil]; | |
| 379 close_window = true; | |
| 380 } | |
| 381 | 360 |
| 382 RemoveOrDestroyChildren(); | 361 RemoveOrDestroyChildren(); |
| 383 DCHECK(child_windows_.empty()); | 362 DCHECK(child_windows_.empty()); |
| 384 SetFocusManager(nullptr); | 363 SetFocusManager(nullptr); |
| 385 SetRootView(nullptr); | 364 SetRootView(nullptr); |
| 386 DestroyCompositor(); | 365 DestroyCompositor(); |
| 387 | |
| 388 if (close_window) { | |
| 389 OnWindowWillClose(); | |
| 390 [window_ close]; | |
| 391 } | |
| 392 } | 366 } |
| 393 | 367 |
| 394 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window, | 368 void BridgedNativeWidget::Init(base::scoped_nsobject<NSWindow> window, |
| 395 const Widget::InitParams& params) { | 369 const Widget::InitParams& params) { |
| 396 widget_type_ = params.type; | 370 widget_type_ = params.type; |
| 397 | 371 |
| 398 DCHECK(!window_); | 372 DCHECK(!window_); |
| 399 window_.swap(window); | 373 window_.swap(window); |
| 400 [window_ setDelegate:window_delegate_]; | 374 [window_ setDelegate:window_delegate_]; |
| 401 | 375 |
| (...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 699 void* BridgedNativeWidget::GetNativeWindowProperty(const char* name) const { | 673 void* BridgedNativeWidget::GetNativeWindowProperty(const char* name) const { |
| 700 NSString* key = [NSString stringWithUTF8String:name]; | 674 NSString* key = [NSString stringWithUTF8String:name]; |
| 701 return [[GetWindowProperties() objectForKey:key] pointerValue]; | 675 return [[GetWindowProperties() objectForKey:key] pointerValue]; |
| 702 } | 676 } |
| 703 | 677 |
| 704 void BridgedNativeWidget::SetCursor(NSCursor* cursor) { | 678 void BridgedNativeWidget::SetCursor(NSCursor* cursor) { |
| 705 [window_delegate_ setCursor:cursor]; | 679 [window_delegate_ setCursor:cursor]; |
| 706 } | 680 } |
| 707 | 681 |
| 708 void BridgedNativeWidget::OnWindowWillClose() { | 682 void BridgedNativeWidget::OnWindowWillClose() { |
| 683 native_widget_mac_->GetWidget()->OnNativeWidgetDestroying(); |
| 684 |
| 709 // Ensure BridgedNativeWidget does not have capture, otherwise | 685 // Ensure BridgedNativeWidget does not have capture, otherwise |
| 710 // OnMouseCaptureLost() may reference a deleted |native_widget_mac_| when | 686 // OnMouseCaptureLost() may reference a deleted |native_widget_mac_| when |
| 711 // called via ~CocoaMouseCapture() upon the destruction of |mouse_capture_|. | 687 // called via ~CocoaMouseCapture() upon the destruction of |mouse_capture_|. |
| 712 // See crbug.com/622201. Also we do this before setting the delegate to nil, | 688 // See crbug.com/622201. Also we do this before setting the delegate to nil, |
| 713 // because this may lead to callbacks to bridge which rely on a valid | 689 // because this may lead to callbacks to bridge which rely on a valid |
| 714 // delegate. | 690 // delegate. |
| 715 ReleaseCapture(); | 691 ReleaseCapture(); |
| 716 | 692 |
| 717 if (parent_) { | 693 if (parent_) { |
| 718 parent_->RemoveChildWindow(this); | 694 parent_->RemoveChildWindow(this); |
| 719 parent_ = nullptr; | 695 parent_ = nullptr; |
| 720 } | 696 } |
| 721 [[NSNotificationCenter defaultCenter] removeObserver:window_delegate_]; | 697 [[NSNotificationCenter defaultCenter] removeObserver:window_delegate_]; |
| 722 // Note this also clears the NSWindow delegate, after informing Widget | 698 [window_ setDelegate:nil]; |
| 723 // delegates about the closure. NativeWidgetMac then deletes |this| before | 699 native_widget_mac_->OnWindowDestroyed(); |
| 724 // returning. | 700 // Note: |this| is deleted here. |
| 725 native_widget_mac_->OnWindowWillClose(); | |
| 726 } | 701 } |
| 727 | 702 |
| 728 void BridgedNativeWidget::OnFullscreenTransitionStart( | 703 void BridgedNativeWidget::OnFullscreenTransitionStart( |
| 729 bool target_fullscreen_state) { | 704 bool target_fullscreen_state) { |
| 730 // Note: This can fail for fullscreen changes started externally, but a user | 705 // Note: This can fail for fullscreen changes started externally, but a user |
| 731 // shouldn't be able to do that if the window is invisible to begin with. | 706 // shouldn't be able to do that if the window is invisible to begin with. |
| 732 DCHECK(window_visible_); | 707 DCHECK(window_visible_); |
| 733 | 708 |
| 734 DCHECK_NE(target_fullscreen_state, target_fullscreen_state_); | 709 DCHECK_NE(target_fullscreen_state, target_fullscreen_state_); |
| 735 target_fullscreen_state_ = target_fullscreen_state; | 710 target_fullscreen_state_ = target_fullscreen_state; |
| (...skipping 666 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1402 [bridged_view_ setMouseDownCanMoveWindow:draggable]; | 1377 [bridged_view_ setMouseDownCanMoveWindow:draggable]; |
| 1403 // AppKit will not update its cache of mouseDownCanMoveWindow unless something | 1378 // AppKit will not update its cache of mouseDownCanMoveWindow unless something |
| 1404 // changes. Previously we tried adding an NSView and removing it, but for some | 1379 // changes. Previously we tried adding an NSView and removing it, but for some |
| 1405 // reason it required reposting the mouse-down event, and didn't always work. | 1380 // reason it required reposting the mouse-down event, and didn't always work. |
| 1406 // Calling the below seems to be an effective solution. | 1381 // Calling the below seems to be an effective solution. |
| 1407 [window_ setMovableByWindowBackground:NO]; | 1382 [window_ setMovableByWindowBackground:NO]; |
| 1408 [window_ setMovableByWindowBackground:YES]; | 1383 [window_ setMovableByWindowBackground:YES]; |
| 1409 } | 1384 } |
| 1410 | 1385 |
| 1411 } // namespace views | 1386 } // namespace views |
| OLD | NEW |