OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ios/chrome/test/base/scoped_block_swizzler.h" | 5 #include "ios/chrome/test/base/scoped_block_swizzler.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 | 8 |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 10 #error "This file requires ARC support." |
| 11 #endif |
| 12 |
9 ScopedBlockSwizzler::ScopedBlockSwizzler(Class target, SEL selector, id block) { | 13 ScopedBlockSwizzler::ScopedBlockSwizzler(Class target, SEL selector, id block) { |
10 method_ = class_getInstanceMethod(target, selector); | 14 method_ = class_getInstanceMethod(target, selector); |
11 if (!method_) { | 15 if (!method_) { |
12 // Try swizzling a class method instead. | 16 // Try swizzling a class method instead. |
13 method_ = class_getClassMethod(target, selector); | 17 method_ = class_getClassMethod(target, selector); |
14 } | 18 } |
15 DCHECK(method_); | 19 DCHECK(method_); |
16 | 20 |
17 IMP block_imp = imp_implementationWithBlock(block); | 21 IMP block_imp = imp_implementationWithBlock(block); |
18 original_imp_ = method_setImplementation(method_, block_imp); | 22 original_imp_ = method_setImplementation(method_, block_imp); |
19 } | 23 } |
20 | 24 |
21 ScopedBlockSwizzler::~ScopedBlockSwizzler() { | 25 ScopedBlockSwizzler::~ScopedBlockSwizzler() { |
22 IMP block_imp = method_setImplementation(method_, original_imp_); | 26 IMP block_imp = method_setImplementation(method_, original_imp_); |
23 DCHECK(imp_removeBlock(block_imp)); | 27 DCHECK(imp_removeBlock(block_imp)); |
24 } | 28 } |
OLD | NEW |