Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 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. | |
| 4 | |
| 5 #import "chrome/browser/ui/cocoa/chrome_tracking_area.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 | |
| 9 // NSTrackingArea does not retain its |owner| so CrTrackingArea wraps the real | |
| 10 // owner in this proxy, which can stop forwarding messages to the owner when | |
| 11 // it is no longer |alive_|. | |
| 12 @interface CrTrackingAreaOwnerProxy : NSProxy { | |
| 13 @private | |
| 14 // Whether or not the owner is "alive" and should forward calls to the real | |
| 15 // owner object. | |
| 16 BOOL alive_; | |
| 17 | |
| 18 // The real object for which this is a proxy. Weak. | |
| 19 id owner_; | |
| 20 } | |
| 21 | |
| 22 @property(nonatomic, assign) BOOL alive; | |
| 23 @property(nonatomic, assign) id owner; | |
| 24 | |
| 25 - (id)initWithOwner:(id)owner; | |
| 26 | |
| 27 @end | |
| 28 | |
| 29 @implementation CrTrackingAreaOwnerProxy | |
| 30 | |
| 31 @synthesize alive = alive_; | |
| 32 @synthesize owner = owner_; | |
| 33 | |
| 34 - (id)initWithOwner:(id)owner { | |
| 35 alive_ = YES; | |
| 36 owner_ = owner; | |
| 37 return self; | |
| 38 } | |
| 39 | |
| 40 - (void)forwardInvocation:(NSInvocation*)invocation { | |
| 41 if (!alive_) | |
| 42 return; | |
| 43 [invocation setTarget:owner_]; | |
| 44 [invocation invoke]; | |
| 45 } | |
| 46 | |
| 47 - (NSMethodSignature*)methodSignatureForSelector:(SEL)sel { | |
| 48 // Even if the owner is not alive, return the method signature so that the | |
| 49 // runtime does not throw an exception. |-forwardInvocation:| will block the | |
| 50 // message. | |
| 51 return [owner_ methodSignatureForSelector:sel]; | |
| 52 } | |
| 53 | |
| 54 @end | |
| 55 | |
| 56 // Private Interface /////////////////////////////////////////////////////////// | |
| 57 | |
| 58 @interface CrTrackingArea (Private) | |
| 59 - (void)windowWillClose:(NSNotification*)notif; | |
| 60 @end | |
| 61 | |
| 62 //////////////////////////////////////////////////////////////////////////////// | |
| 63 | |
| 64 @implementation CrTrackingArea | |
| 65 | |
| 66 - (id)initWithRect:(NSRect)rect | |
| 67 options:(NSTrackingAreaOptions)options | |
| 68 owner:(id)owner | |
| 69 userInfo:(NSDictionary*)userInfo { | |
| 70 ownerProxy_.reset([[CrTrackingAreaOwnerProxy alloc] initWithOwner:owner]); | |
| 71 if ((self = static_cast<id>([super initWithRect:rect | |
| 72 options:options | |
| 73 owner:ownerProxy_.get() | |
| 74 userInfo:userInfo]))) { | |
|
Scott Hess - ex-Googler
2011/02/11 01:17:12
No need for the empty if(). Actually, the static_
Robert Sesek
2011/02/11 02:30:50
if() is just for convention. The static_cast<> is
| |
| 75 } | |
| 76 return self; | |
| 77 } | |
| 78 | |
| 79 - (void)dealloc { | |
| 80 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 81 [super dealloc]; | |
| 82 } | |
| 83 | |
| 84 - (void)clearOwner { | |
| 85 [ownerProxy_ setAlive:NO]; | |
| 86 } | |
| 87 | |
| 88 - (void)clearOwnerWhenWindowWillClose:(NSWindow*)window { | |
| 89 DCHECK(window); | |
| 90 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; | |
| 91 [center addObserver:self | |
| 92 selector:@selector(windowWillClose:) | |
| 93 name:NSWindowWillCloseNotification | |
| 94 object:window]; | |
| 95 } | |
| 96 | |
| 97 - (void)windowWillClose:(NSNotification*)notif { | |
| 98 [self clearOwner]; | |
| 99 } | |
| 100 | |
| 101 @end | |
| OLD | NEW |