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

Side by Side Diff: chrome/browser/ui/cocoa/tracking_area.mm

Issue 6486002: [Mac] Create CrTrackingArea and use it in TabStripController. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 9 years, 10 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 : NSObject {
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 @property(nonatomic, assign) BOOL alive;
26 - (id)initWithOwner:(id)owner;
27 @end
28
29 @implementation CrTrackingAreaOwnerProxy
30
31 @synthesize alive = alive_;
32
33 - (id)initWithOwner:(id)owner {
Scott Hess - ex-Googler 2011/02/14 21:13:45 OK, but now you need if ((self = [super init])) {.
34 alive_ = YES;
35 owner_ = owner;
36 ownerClass_ = [owner class];
37 return self;
38 }
39
40 - (void)forwardInvocation:(NSInvocation*)invocation {
41 if (!alive_)
42 return;
43 [invocation invokeWithTarget:owner_];
44 }
45
46 - (NSMethodSignature*)methodSignatureForSelector:(SEL)sel {
47 // This can be called if |owner_| is not |alive_|, so use the Class to
48 // generate the signature. |-forwardInvocation:| will block the actual call.
49 return [ownerClass_ instanceMethodSignatureForSelector:sel];
50 }
51
52 - (BOOL)respondsToSelector:(SEL)aSelector {
53 return [ownerClass_ instancesRespondToSelector:aSelector];
54 }
55
56 @end
57
58 // Private Interface ///////////////////////////////////////////////////////////
59
60 @interface CrTrackingArea (Private)
61 - (void)windowWillClose:(NSNotification*)notif;
62 @end
63
64 ////////////////////////////////////////////////////////////////////////////////
65
66 @implementation CrTrackingArea
67
68 - (id)initWithRect:(NSRect)rect
69 options:(NSTrackingAreaOptions)options
70 owner:(id)owner
71 userInfo:(NSDictionary*)userInfo {
72 scoped_nsobject<CrTrackingAreaOwnerProxy> ownerProxy(
73 [[CrTrackingAreaOwnerProxy alloc] initWithOwner:owner]);
74 if ((self = static_cast<id>([super initWithRect:rect
75 options:options
76 owner:ownerProxy.get()
77 userInfo:userInfo]))) {
78 ownerProxy_.reset(ownerProxy.release());
Scott Hess - ex-Googler 2011/02/14 21:13:45 Suggest ownerProxy_.swap(ownerProxy);. I think yo
79 }
80 return self;
81 }
82
83 - (void)dealloc {
84 [[NSNotificationCenter defaultCenter] removeObserver:self];
85 [super dealloc];
86 }
87
88 - (void)clearOwner {
89 [ownerProxy_ setAlive:NO];
90 }
91
92 - (void)clearOwnerWhenWindowWillClose:(NSWindow*)window {
93 DCHECK(window);
94 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
95 [center addObserver:self
96 selector:@selector(windowWillClose:)
97 name:NSWindowWillCloseNotification
98 object:window];
99 }
100
101 - (void)windowWillClose:(NSNotification*)notif {
102 [self clearOwner];
103 }
104
105 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698