Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(65)

Unified Diff: ui/views/cocoa/views_nswindow_delegate.mm

Issue 2629593005: MacViews: Support -[NSWindow close] on sheets. (Closed)
Patch Set: no doNothing Created 3 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | ui/views/cocoa/widget_owner_nswindow_adapter.mm » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/views/cocoa/views_nswindow_delegate.mm
diff --git a/ui/views/cocoa/views_nswindow_delegate.mm b/ui/views/cocoa/views_nswindow_delegate.mm
index 7a70d44b1e9ecc55a46a08bad7167b99d38ff05e..d0d50a0eada283b210b37428d3c2fec42178ee42 100644
--- a/ui/views/cocoa/views_nswindow_delegate.mm
+++ b/ui/views/cocoa/views_nswindow_delegate.mm
@@ -5,6 +5,8 @@
#import "ui/views/cocoa/views_nswindow_delegate.h"
#include "base/logging.h"
+#import "base/mac/bind_objc_block.h"
+#include "base/threading/thread_task_runner_handle.h"
#import "ui/views/cocoa/bridged_content_view.h"
#import "ui/views/cocoa/bridged_native_widget.h"
#include "ui/views/widget/native_widget_mac.h"
@@ -46,6 +48,10 @@
- (void)sheetDidEnd:(NSWindow*)sheet
returnCode:(NSInteger)returnCode
contextInfo:(void*)contextInfo {
+ // |parent_| will be null when triggered from the block in -windowWillClose:.
+ if (!parent_)
+ return;
+
[sheet orderOut:nil];
parent_->OnWindowWillClose();
}
@@ -87,8 +93,32 @@
}
- (void)windowWillClose:(NSNotification*)notification {
- DCHECK([parent_->ns_window() isEqual:[notification object]]);
+ // Retain |self|. |parent_| should be cleared. OnWindowWillClose() may delete
+ // |parent_|, but it may also dealloc |self| before returning. However, the
+ // observers it notifies before that need a valid |parent_| on the delegate,
+ // so it can only be cleared after OnWindowWillClose() returns.
+ base::scoped_nsobject<NSObject> keepAlive(self, base::scoped_policy::RETAIN);
+ NSWindow* window = parent_->ns_window();
+ if (NSWindow* sheetParent = [window sheetParent]) {
+ // On no! Something called -[NSWindow close] on a sheet rather than calling
+ // -[NSWindow endSheet:] on its parent. If the modal session is not ended
+ // then the parent will never be able to show another sheet. But calling
+ // -endSheet: here will block the thread with an animation, so post a task.
+ // Use a block: The argument to -endSheet: must be retained, since it's the
+ // window that is closing and -performSelector: won't retain the argument.
+ // The NSWindowDelegate (i.e. |self|) must also be explicitly retained. Even
+ // though the call to OnWindowWillClose() below will remove |self| as the
+ // NSWindow delegate, the call to -[NSApp beginSheet:] also took a weak
+ // reference to the delegate, which will be destroyed when the sheet's
+ // BridgedNativeWidget is destroyed.
+ base::ThreadTaskRunnerHandle::Get()->PostTask(FROM_HERE, base::BindBlock(^{
+ [sheetParent endSheet:window];
+ [[self retain] release]; // Force |self| to be retained for the block.
+ }));
+ }
+ DCHECK([window isEqual:[notification object]]);
parent_->OnWindowWillClose();
+ parent_ = nullptr;
}
- (void)windowDidMiniaturize:(NSNotification*)notification {
« no previous file with comments | « no previous file | ui/views/cocoa/widget_owner_nswindow_adapter.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698