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/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 { | |
|
Scott Hess - ex-Googler
2011/02/14 19:25:38
I'm a tiny bit nervous about using NSProxy. I thi
Robert Sesek
2011/02/14 20:46:35
At this point, with the three dynamic messaging me
| |
| 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 // The Class of |owner_|. When the actual object is no longer alive (and could | |
| 22 // be zombie), this allows for introspection. | |
| 23 Class ownerClass_; | |
| 24 } | |
| 25 | |
| 26 @property(nonatomic, assign) BOOL alive; | |
| 27 @property(nonatomic, assign) id owner; | |
| 28 | |
| 29 - (id)initWithOwner:(id)owner; | |
| 30 | |
| 31 @end | |
| 32 | |
| 33 @implementation CrTrackingAreaOwnerProxy | |
| 34 | |
| 35 @synthesize alive = alive_; | |
| 36 @synthesize owner = owner_; | |
| 37 | |
| 38 - (id)initWithOwner:(id)owner { | |
| 39 alive_ = YES; | |
| 40 owner_ = owner; | |
| 41 ownerClass_ = [owner class]; | |
| 42 return self; | |
| 43 } | |
| 44 | |
| 45 - (void)forwardInvocation:(NSInvocation*)invocation { | |
| 46 if (!alive_) | |
| 47 return; | |
| 48 [invocation invokeWithTarget:owner_]; | |
| 49 } | |
| 50 | |
| 51 - (NSMethodSignature*)methodSignatureForSelector:(SEL)sel { | |
| 52 // This can be called if |owner_| is not |alive_|, so use the Class to | |
| 53 // generate the signature. |-forwardInvocation:| will block the actual call. | |
| 54 return [ownerClass_ instanceMethodSignatureForSelector:sel]; | |
| 55 } | |
| 56 | |
| 57 - (BOOL)respondsToSelector:(SEL)aSelector { | |
| 58 return YES; | |
|
Scott Hess - ex-Googler
2011/02/14 19:25:38
Seems like [ownerClass_ instancesRespondToSelector
Robert Sesek
2011/02/14 20:46:35
Do'h. I was looking for that method but couldn't f
| |
| 59 } | |
| 60 | |
| 61 @end | |
| 62 | |
| 63 // Private Interface /////////////////////////////////////////////////////////// | |
| 64 | |
| 65 @interface CrTrackingArea (Private) | |
| 66 - (void)windowWillClose:(NSNotification*)notif; | |
| 67 @end | |
| 68 | |
| 69 //////////////////////////////////////////////////////////////////////////////// | |
| 70 | |
| 71 @implementation CrTrackingArea | |
| 72 | |
| 73 - (id)initWithRect:(NSRect)rect | |
| 74 options:(NSTrackingAreaOptions)options | |
| 75 owner:(id)owner | |
| 76 userInfo:(NSDictionary*)userInfo { | |
| 77 ownerProxy_.reset([[CrTrackingAreaOwnerProxy alloc] initWithOwner:owner]); | |
|
Scott Hess - ex-Googler
2011/02/14 19:25:38
You can get away with this, but I don't think you
Robert Sesek
2011/02/14 20:46:35
Done.
| |
| 78 if ((self = static_cast<id>([super initWithRect:rect | |
| 79 options:options | |
| 80 owner:ownerProxy_.get() | |
| 81 userInfo:userInfo]))) { | |
| 82 } | |
| 83 return self; | |
| 84 } | |
| 85 | |
| 86 - (void)dealloc { | |
| 87 [[NSNotificationCenter defaultCenter] removeObserver:self]; | |
| 88 [super dealloc]; | |
| 89 } | |
| 90 | |
| 91 - (void)clearOwner { | |
| 92 [ownerProxy_ setAlive:NO]; | |
| 93 } | |
| 94 | |
| 95 - (void)clearOwnerWhenWindowWillClose:(NSWindow*)window { | |
| 96 DCHECK(window); | |
| 97 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; | |
| 98 [center addObserver:self | |
| 99 selector:@selector(windowWillClose:) | |
| 100 name:NSWindowWillCloseNotification | |
| 101 object:window]; | |
| 102 } | |
| 103 | |
| 104 - (void)windowWillClose:(NSNotification*)notif { | |
| 105 [self clearOwner]; | |
| 106 } | |
| 107 | |
| 108 @end | |
| OLD | NEW |