Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / cocoa / native_window_tracker_cocoa.mm
blob14afb8d9550177e1b29fd471ccc306c8085f0479
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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/cocoa/native_window_tracker_cocoa.h"
7 #import <AppKit/AppKit.h>
9 @interface BridgedNativeWindowTracker : NSObject {
10  @private
11   NSWindow* window_;
14 - (id)initWithNSWindow:(NSWindow*)window;
15 - (bool)wasNSWindowClosed;
16 - (void)onWindowWillClose:(NSNotification*)notification;
18 @end
20 @implementation BridgedNativeWindowTracker
22 - (id)initWithNSWindow:(NSWindow*)window {
23   window_ = window;
24   NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
25   [center addObserver:self
26              selector:@selector(onWindowWillClose:)
27                  name:NSWindowWillCloseNotification
28                object:window_];
29   return self;
32 - (void)dealloc {
33   [[NSNotificationCenter defaultCenter] removeObserver:self];
34   [super dealloc];
37 - (bool)wasNSWindowClosed {
38   return window_ == nil;
41 - (void)onWindowWillClose:(NSNotification*)notification {
42   [[NSNotificationCenter defaultCenter]
43       removeObserver:self
44                 name:NSWindowWillCloseNotification
45               object:window_];
46   window_ = nil;
49 @end
51 NativeWindowTrackerCocoa::NativeWindowTrackerCocoa(gfx::NativeWindow window) {
52   bridge_.reset([[BridgedNativeWindowTracker alloc] initWithNSWindow:window]);
55 NativeWindowTrackerCocoa::~NativeWindowTrackerCocoa() {
58 bool NativeWindowTrackerCocoa::WasNativeWindowClosed() const {
59   return [bridge_ wasNSWindowClosed];
62 // static
63 scoped_ptr<NativeWindowTracker> NativeWindowTracker::Create(
64     gfx::NativeWindow window) {
65   return scoped_ptr<NativeWindowTracker>(new NativeWindowTrackerCocoa(window));