1 // Copyright 2015 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.
5 #import "ios/web/alloc_with_zone_interceptor.h"
7 #import <objc/runtime.h>
9 #include "base/logging.h"
13 void AddAllocWithZoneMethod(Class target, id (^impl_block)(Class, NSZone*)) {
14 // Make sure |allocWithZone:| is not already implemented in the target class.
15 Class meta_class = object_getClass(target);
17 class_getMethodImplementation(meta_class, @selector(allocWithZone:)),
18 class_getMethodImplementation(object_getClass([NSObject class]),
19 @selector(allocWithZone:)));
21 IMP new_impl = imp_implementationWithBlock(^(id self, NSZone* zone) {
22 return impl_block(self, zone);
24 class_addMethod(meta_class, @selector(allocWithZone:), new_impl, "v@:@");