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

Side by Side Diff: base/mac/scoped_objc_class_swizzler.mm

Issue 345243007: Add ScopedObjCClassSwizzler in base/mac, absorbs objc_method_swizzle and ScopedClassSwizzler (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix EventsMacTest.ButtonEvents Created 6 years, 5 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 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.
4
5 #import "base/mac/scoped_objc_class_swizzler.h"
6
7 #include <string.h>
8
9 #include "base/logging.h"
10
11 namespace base {
12 namespace mac {
13
14 ScopedObjCClassSwizzler::ScopedObjCClassSwizzler(Class target,
15 Class source,
16 SEL selector) {
17 old_selector_impl_ = class_getInstanceMethod(target, selector);
18 new_selector_impl_ = class_getInstanceMethod(source, selector);
19 if (!old_selector_impl_ && !new_selector_impl_) {
20 // Try class methods.
21 old_selector_impl_ = class_getClassMethod(target, selector);
22 new_selector_impl_ = class_getClassMethod(source, selector);
23 }
24
25 DCHECK(old_selector_impl_);
26 DCHECK(new_selector_impl_);
27 if (!old_selector_impl_ || !new_selector_impl_)
28 return;
29
30 // The argument and return types must match exactly.
31 const char* old_types = method_getTypeEncoding(old_selector_impl_);
32 const char* new_types = method_getTypeEncoding(new_selector_impl_);
33 DCHECK(old_types);
34 DCHECK(new_types);
35 DCHECK_EQ(0, strcmp(old_types, new_types));
36 if (!old_types || !new_types || strcmp(old_types, new_types)) {
37 old_selector_impl_ = new_selector_impl_ = NULL;
38 return;
39 }
40
41 method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
42 }
43
44 ScopedObjCClassSwizzler::~ScopedObjCClassSwizzler() {
45 if (old_selector_impl_ && new_selector_impl_)
46 method_exchangeImplementations(old_selector_impl_, new_selector_impl_);
47 }
48
49 IMP ScopedObjCClassSwizzler::GetOriginalImplementation() {
50 // Note that while the swizzle is in effect the "new" method is actually
51 // pointing to the original implementation, since they have been swapped.
52 return method_getImplementation(new_selector_impl_);
53 }
54
55 } // namespace mac
56 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698