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

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

Issue 2629593005: MacViews: Support -[NSWindow close] on sheets. (Closed)
Patch Set: Fix lifetime 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
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..8fee708a4a6a7a0ffc740dd1db2741018466212f 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();
}
@@ -86,9 +92,36 @@
parent_->OnWindowKeyStatusChangedTo(false);
}
+// Does nothing: Used to force |self| to be retained in a block.
Robert Sesek 2017/01/13 20:02:47 Could you instead just put another keepAlive in th
tapted 2017/01/13 20:52:51 That would take (and release) an additional refere
Robert Sesek 2017/01/13 21:46:00 Hm, I see your point. Maybe rather than doNothing,
tapted 2017/01/13 22:32:45 Done!
+- (void)doNothing {}
+
- (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 doNothing];
+ }));
+ }
+ 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') | ui/views/widget/native_widget_mac.mm » ('J')

Powered by Google App Engine
This is Rietveld 408576698