summaryrefslogtreecommitdiffstats
path: root/chrome/common/mac/objc_method_swizzle.mm
diff options
context:
space:
mode:
authorshess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-06 17:25:15 +0000
committershess@chromium.org <shess@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-09-06 17:25:15 +0000
commitd02437b6e66f851f1b627418db1bb2e200497e3a (patch)
treeb8db39af85ca5c8a56081538e3a36277fc9c2c65 /chrome/common/mac/objc_method_swizzle.mm
parentdceaa916f0c01a0d9f87ef77c91dc6bbce3b1979 (diff)
downloadchromium_src-d02437b6e66f851f1b627418db1bb2e200497e3a.zip
chromium_src-d02437b6e66f851f1b627418db1bb2e200497e3a.tar.gz
chromium_src-d02437b6e66f851f1b627418db1bb2e200497e3a.tar.bz2
[Mac] Enable CrZombie for all processes.
For the browser process, this will be overridden by -[BrowserCrApplication init] (which raises it). BUG=94551 TEST=Developers don't complain about zombie objects in other processes. Review URL: http://codereview.chromium.org/7826016 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@99754 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/common/mac/objc_method_swizzle.mm')
-rw-r--r--chrome/common/mac/objc_method_swizzle.mm58
1 files changed, 58 insertions, 0 deletions
diff --git a/chrome/common/mac/objc_method_swizzle.mm b/chrome/common/mac/objc_method_swizzle.mm
new file mode 100644
index 0000000..4ed4df8
--- /dev/null
+++ b/chrome/common/mac/objc_method_swizzle.mm
@@ -0,0 +1,58 @@
+// Copyright (c) 2011 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 "chrome/common/mac/objc_method_swizzle.h"
+
+#import "base/logging.h"
+#import "base/memory/scoped_nsobject.h"
+
+namespace ObjcEvilDoers {
+
+Method GetImplementedInstanceMethod(Class aClass, SEL aSelector) {
+ Method method = NULL;
+ unsigned int methodCount = 0;
+ Method* methodList = class_copyMethodList(aClass, &methodCount);
+ if (methodList) {
+ for (unsigned int i = 0; i < methodCount; ++i) {
+ if (method_getName(methodList[i]) == aSelector) {
+ method = methodList[i];
+ break;
+ }
+ }
+ free(methodList);
+ }
+ return method;
+}
+
+IMP SwizzleImplementedInstanceMethods(
+ Class aClass, const SEL originalSelector, const SEL alternateSelector) {
+ // The methods must both be implemented by the target class, not
+ // inherited from a superclass.
+ Method original = GetImplementedInstanceMethod(aClass, originalSelector);
+ Method alternate = GetImplementedInstanceMethod(aClass, alternateSelector);
+ DCHECK(original);
+ DCHECK(alternate);
+ if (!original || !alternate) {
+ return NULL;
+ }
+
+ // The argument and return types must match exactly.
+ const char* originalTypes = method_getTypeEncoding(original);
+ const char* alternateTypes = method_getTypeEncoding(alternate);
+ DCHECK(originalTypes);
+ DCHECK(alternateTypes);
+ DCHECK(0 == strcmp(originalTypes, alternateTypes));
+ if (!originalTypes || !alternateTypes ||
+ strcmp(originalTypes, alternateTypes)) {
+ return NULL;
+ }
+
+ IMP ret = method_getImplementation(original);
+ if (ret) {
+ method_exchangeImplementations(original, alternate);
+ }
+ return ret;
+}
+
+} // namespace ObjcEvilDoers