Chromium Code Reviews| Index: ios/web/public/test/crw_test_js_injection_receiver.mm |
| diff --git a/ios/web/public/test/crw_test_js_injection_receiver.mm b/ios/web/public/test/crw_test_js_injection_receiver.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..2492af254bb771af9b8bf27e61856b0526f5e30b |
| --- /dev/null |
| +++ b/ios/web/public/test/crw_test_js_injection_receiver.mm |
| @@ -0,0 +1,70 @@ |
| +// 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/web/public/test/crw_test_js_injection_receiver.h" |
| + |
| +#import <UIKit/UIKit.h> |
| + |
| +#include "base/mac/scoped_nsobject.h" |
| +#import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" |
| + |
| +// TODO(shreyasv): Rename this to CRWTestUIWebViewEvaluator. |
| +@interface CRWWebViewEvaluator : NSObject<CRWJSInjectionEvaluator> { |
|
sdefresne
2014/12/10 15:30:23
nit: this class is only used in this file, why not
|
| + base::scoped_nsobject<UIWebView> _webView; |
| +} |
| +@end |
| + |
| +@implementation CRWWebViewEvaluator |
| + |
| +- (id) init { |
| + if (self = [super init]) |
| + _webView.reset([[UIWebView alloc] init]); |
| + return self; |
| +} |
| + |
| +#pragma mark - |
| +#pragma mark CRWJSInjectionEvaluatorMethods |
| + |
| +- (void)evaluateJavaScript:(NSString*)script |
| + stringResultHandler:(web::JavaScriptCompletion)handler { |
| + dispatch_async(dispatch_get_main_queue(), ^{ |
| + // TODO(shreyasv): Change to weaknsobject once weaknsobject is moved to |
| + // ios/base. |
| + NSString* result = |
| + [_webView stringByEvaluatingJavaScriptFromString:script]; |
| + if (handler) |
| + handler(result, nil); |
| + }); |
| +} |
| + |
| +- (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass |
| + presenceBeacon:(NSString*)beacon { |
| + NSString* result = [_webView stringByEvaluatingJavaScriptFromString: |
| + [NSString stringWithFormat:@"typeof %@", beacon]]; |
| + return [result isEqualToString:@"object"]; |
| +} |
| + |
| +- (void)injectScript:(NSString*)script |
| + forClass:(Class)jsInjectionManagerClass { |
| + [_webView stringByEvaluatingJavaScriptFromString:script]; |
| +} |
| + |
| +@end |
| + |
| +@interface CRWTestJSInjectionReceiver () { |
| + base::scoped_nsobject<CRWWebViewEvaluator> evaluator_; |
| +} |
| +@end |
| + |
| +@implementation CRWTestJSInjectionReceiver |
| + |
| +- (id)init { |
| + base::scoped_nsobject<CRWWebViewEvaluator> evaluator( |
| + [[CRWWebViewEvaluator alloc] init]); |
| + if (self = [super initWithEvaluator:evaluator]) |
| + evaluator_.swap(evaluator); |
| + return self; |
| +} |
| + |
| +@end |