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

Unified Diff: ios/chrome/test/base/scoped_block_swizzler.mm

Issue 2551083002: [ios] Adds ScopedBlockSwizzler. (Closed)
Patch Set: Created 4 years 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 side-by-side diff with in-line comments
Download patch
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));
+}
« no previous file with comments | « ios/chrome/test/base/scoped_block_swizzler.h ('k') | ios/chrome/test/base/scoped_block_swizzler_unittest.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698