Chromium Code Reviews| Index: ios/chrome/test/base/scoped_block_swizzler.mm |
| diff --git a/ios/chrome/test/base/scoped_block_swizzler.mm b/ios/chrome/test/base/scoped_block_swizzler.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..312de59466c7db44f6b27280d89b845baf38a0cb |
| --- /dev/null |
| +++ b/ios/chrome/test/base/scoped_block_swizzler.mm |
| @@ -0,0 +1,24 @@ |
| +// Copyright 2013 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 "ios/chrome/test/base/scoped_block_swizzler.h" |
| + |
| +#include "base/logging.h" |
| + |
| +ScopedBlockSwizzler::ScopedBlockSwizzler(Class target, SEL selector, id block) { |
| + method_ = class_getInstanceMethod(target, selector); |
| + if (!method_) { |
| + // Try swizzling a class method instead. |
| + method_ = class_getClassMethod(target, selector); |
| + } |
| + DCHECK(method_); |
| + |
| + IMP block_imp = imp_implementationWithBlock(block); |
|
sdefresne
2016/12/05 15:56:00
The implementation requires that the block outlive
rohitrao (ping after 24h)
2016/12/05 15:58:11
Seems like a good idea, although then we'd potenti
|
| + original_imp_ = method_setImplementation(method_, block_imp); |
| +} |
| + |
| +ScopedBlockSwizzler::~ScopedBlockSwizzler() { |
| + IMP block_imp = method_setImplementation(method_, original_imp_); |
| + DCHECK(imp_removeBlock(block_imp)); |
| +} |