blob: 433bc66439dede271f3fa76cc30cd22b8413f40d (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
// Copyright (c) 2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "chrome/browser/cocoa/objc_method_swizzle.h"
#import "base/logging.h"
#import "base/scoped_nsobject.h"
#import "chrome/app/breakpad_mac.h"
namespace ObjcEvilDoers {
Method GetImplementedInstanceMethod(Class aClass, SEL aSelector) {
Method method = NULL;
unsigned int methodCount = 0;
Method* methodList = class_copyMethodList(aClass, &methodCount);
if (methodList) {
for (unsigned int i = 0; i < methodCount; ++i) {
if (method_getName(methodList[i]) == aSelector) {
method = methodList[i];
break;
}
}
free(methodList);
}
return method;
}
IMP SwizzleImplementedInstanceMethods(
Class aClass, const SEL originalSelector, const SEL alternateSelector) {
// The methods must both be implemented by the target class, not
// inherited from a superclass.
Method original = GetImplementedInstanceMethod(aClass, originalSelector);
Method alternate = GetImplementedInstanceMethod(aClass, alternateSelector);
DCHECK(original);
DCHECK(alternate);
if (!original || !alternate) {
return NULL;
}
// The argument and return types must match exactly.
const char* originalTypes = method_getTypeEncoding(original);
const char* alternateTypes = method_getTypeEncoding(alternate);
DCHECK(originalTypes);
DCHECK(alternateTypes);
DCHECK(0 == strcmp(originalTypes, alternateTypes));
if (!originalTypes || !alternateTypes ||
strcmp(originalTypes, alternateTypes)) {
return NULL;
}
IMP ret = method_getImplementation(original);
if (ret) {
method_exchangeImplementations(original, alternate);
}
return ret;
}
} // namespace ObjcEvilDoers
|