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#ifndef BASE_MAC_SCOPED_OBJC_CLASS_SWIZZLER_H_ 6#define BASE_MAC_SCOPED_OBJC_CLASS_SWIZZLER_H_ 7 8#import <objc/runtime.h> 9 10#include "base/base_export.h" 11#include "base/macros.h" 12 13namespace base { 14namespace mac { 15 16// Within a given scope, swaps method implementations of a class interface, or 17// between two class interfaces. The argument and return types must match. 18class BASE_EXPORT ScopedObjCClassSwizzler { 19 public: 20 // Given two classes that each respond to |selector|, swap the implementations 21 // of those methods. 22 ScopedObjCClassSwizzler(Class target, Class source, SEL selector); 23 24 // Given two selectors on the same class interface, |target| (e.g. via 25 // inheritance or categories), swap the implementations of methods |original| 26 // and |alternate|. 27 ScopedObjCClassSwizzler(Class target, SEL original, SEL alternate); 28 29 ~ScopedObjCClassSwizzler(); 30 31 // Return a callable function pointer for the replaced method. To call this 32 // from the replacing function, the first two arguments should be |self| and 33 // |_cmd|. These are followed by the (variadic) method arguments. 34 IMP GetOriginalImplementation(); 35 36 private: 37 // Delegated constructor. 38 void Init(Class target, Class source, SEL original, SEL alternate); 39 40 Method old_selector_impl_; 41 Method new_selector_impl_; 42 43 DISALLOW_COPY_AND_ASSIGN(ScopedObjCClassSwizzler); 44}; 45 46} // namespace mac 47} // namespace base 48 49#endif // BASE_MAC_SCOPED_OBJC_CLASS_SWIZZLER_H_ 50