Commit 7c2fdfee authored by Robert Sesek's avatar Robert Sesek Committed by Commit Bot

Do not use OCMock in ScopedBundleSwizzlerMac.

OCMock prior to 3.3 is thread-hostile, and Chromium uses v3.1.5
currently. This is causing memory errors under ASan.

Bug: 997782
Change-Id: I987139e2e1bc89743d8e51956010b8738cfdce7a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1773542Reviewed-by: default avatarAvi Drissman <avi@chromium.org>
Commit-Queue: Robert Sesek <rsesek@chromium.org>
Cr-Commit-Position: refs/heads/master@{#691118}
parent 07373d4c
...@@ -272,7 +272,6 @@ static_library("test_support") { ...@@ -272,7 +272,6 @@ static_library("test_support") {
public_deps += [ public_deps += [
"//components/crash/content/app", "//components/crash/content/app",
"//third_party/breakpad", "//third_party/breakpad",
"//third_party/ocmock",
] ]
} }
......
...@@ -73,6 +73,5 @@ include_rules = [ ...@@ -73,6 +73,5 @@ include_rules = [
"+media/base", "+media/base",
"+mojo/core/embedder", "+mojo/core/embedder",
"+sandbox/win/tests", "+sandbox/win/tests",
"+third_party/ocmock",
"+third_party/webrtc", "+third_party/webrtc",
] ]
...@@ -8,37 +8,53 @@ ...@@ -8,37 +8,53 @@
#include "base/logging.h" #include "base/logging.h"
#include "base/mac/foundation_util.h" #include "base/mac/foundation_util.h"
#include "base/mac/scoped_nsobject.h"
#include "base/mac/scoped_objc_class_swizzler.h" #include "base/mac/scoped_objc_class_swizzler.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "third_party/ocmock/OCMock/OCMock.h"
#import "third_party/ocmock/OCMock/OCPartialMockObject.h"
static NSBundle* g_original_main_bundle = nil;
static id g_swizzled_main_bundle = nil; static id g_swizzled_main_bundle = nil;
// A donor class that provides a +[NSBundle mainBundle] method that can be // A donor class that provides a +[NSBundle mainBundle] method that can be
// swapped with NSBundle. // swapped with NSBundle.
@interface TestBundle : NSObject @interface TestBundle : NSProxy
- (instancetype)initWithRealBundle:(NSBundle*)bundle;
+ (NSBundle*)mainBundle; + (NSBundle*)mainBundle;
@end @end
@implementation TestBundle @implementation TestBundle {
base::scoped_nsobject<NSBundle> mainBundle_;
}
+ (NSBundle*)mainBundle { + (NSBundle*)mainBundle {
return g_swizzled_main_bundle; return g_swizzled_main_bundle;
} }
- (instancetype)initWithRealBundle:(NSBundle*)bundle {
mainBundle_.reset([bundle retain]);
return self;
}
- (NSString*)bundleIdentifier {
return base::SysUTF8ToNSString(base::mac::BaseBundleID());
}
- (void)forwardInvocation:(NSInvocation*)invocation {
invocation.target = mainBundle_.get();
[invocation invoke];
}
- (NSMethodSignature*)methodSignatureForSelector:(SEL)sel {
return [mainBundle_ methodSignatureForSelector:sel];
}
@end @end
ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() { ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() {
CHECK(!g_swizzled_main_bundle); CHECK(!g_swizzled_main_bundle);
CHECK(!g_original_main_bundle);
g_original_main_bundle = [NSBundle mainBundle]; NSBundle* original_main_bundle = [NSBundle mainBundle];
g_swizzled_main_bundle = g_swizzled_main_bundle =
[[OCPartialMockObject alloc] initWithObject:g_original_main_bundle]; [[TestBundle alloc] initWithRealBundle:original_main_bundle];
NSString* identifier = base::SysUTF8ToNSString(base::mac::BaseBundleID());
CHECK(identifier);
[[[g_swizzled_main_bundle stub] andReturn:identifier] bundleIdentifier];
class_swizzler_.reset(new base::mac::ScopedObjCClassSwizzler( class_swizzler_.reset(new base::mac::ScopedObjCClassSwizzler(
[NSBundle class], [TestBundle class], @selector(mainBundle))); [NSBundle class], [TestBundle class], @selector(mainBundle)));
...@@ -47,5 +63,4 @@ ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() { ...@@ -47,5 +63,4 @@ ScopedBundleSwizzlerMac::ScopedBundleSwizzlerMac() {
ScopedBundleSwizzlerMac::~ScopedBundleSwizzlerMac() { ScopedBundleSwizzlerMac::~ScopedBundleSwizzlerMac() {
[g_swizzled_main_bundle release]; [g_swizzled_main_bundle release];
g_swizzled_main_bundle = nil; g_swizzled_main_bundle = nil;
g_original_main_bundle = nil;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment