Index: chrome/browser/ui/cocoa/native_window_tracker_cocoa.mm |
diff --git a/chrome/browser/ui/cocoa/native_window_tracker_cocoa.mm b/chrome/browser/ui/cocoa/native_window_tracker_cocoa.mm |
new file mode 100644 |
index 0000000000000000000000000000000000000000..29911d0a83c677554623eacda1b1bdfbcc26cf78 |
--- /dev/null |
+++ b/chrome/browser/ui/cocoa/native_window_tracker_cocoa.mm |
@@ -0,0 +1,66 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/cocoa/native_window_tracker_cocoa.h" |
+ |
+#import <AppKit/AppKit.h> |
+ |
+@interface BridgedNativeWindowTracker : NSObject { |
+ @private |
+ NSWindow* window_; |
+} |
+ |
+- (id)initWithNSWindow:(NSWindow*)window; |
+- (bool)isNSWindowAlive; |
+- (void)onWindowWillClose:(NSNotification*)notification; |
+ |
+@end |
+ |
+@implementation BridgedNativeWindowTracker |
+ |
+- (id)initWithNSWindow:(NSWindow*)window { |
+ window_ = window; |
+ NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
+ [center addObserver:self |
+ selector:@selector(onWindowWillClose:) |
+ name:NSWindowWillCloseNotification |
+ object:window_]; |
+ return self; |
+} |
+ |
+- (void)dealloc { |
+ [[NSNotificationCenter defaultCenter] removeObserver:self]; |
+ [super dealloc]; |
+} |
+ |
+- (bool)isNSWindowAlive { |
+ return !!window_; |
+} |
+ |
+- (void)onWindowWillClose:(NSNotification*)notification { |
+ [[NSNotificationCenter defaultCenter] |
+ removeObserver:self |
+ name:NSWindowWillCloseNotification |
+ object:window_]; |
+ window_ = nil; |
+} |
+ |
+@end |
+ |
+NativeWindowTrackerCocoa::NativeWindowTrackerCocoa(gfx::NativeWindow window) { |
+ bridge_.reset([[BridgedNativeWindowTracker alloc] initWithNSWindow:window]); |
+} |
+ |
+NativeWindowTrackerCocoa::~NativeWindowTrackerCocoa() { |
+} |
+ |
+bool NativeWindowTrackerCocoa::IsNativeWindowAlive() const { |
+ return [bridge_ isNSWindowAlive]; |
Nico
2014/10/27 21:06:11
Not every NSWindow is automatically released when
|
+} |
+ |
+// static |
+scoped_ptr<NativeWindowTracker> NativeWindowTracker::Create( |
+ gfx::NativeWindow window) { |
+ return scoped_ptr<NativeWindowTracker>(new NativeWindowTrackerCocoa(window)); |
+} |