1 // Copyright 2013 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/public/test/crw_test_js_injection_receiver.h"
7 #import <UIKit/UIKit.h>
9 #import "base/mac/scoped_nsobject.h"
10 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
12 @interface CRWTestUIWebViewEvaluator : NSObject<CRWJSInjectionEvaluator> {
13 base::scoped_nsobject<UIWebView> _webView;
17 @implementation CRWTestUIWebViewEvaluator
20 if (self = [super init])
21 _webView.reset([[UIWebView alloc] init]);
26 #pragma mark CRWJSInjectionEvaluatorMethods
28 - (void)evaluateJavaScript:(NSString*)script
29 stringResultHandler:(web::JavaScriptCompletion)handler {
30 dispatch_async(dispatch_get_main_queue(), ^{
31 // TODO(shreyasv): Change to weaknsobject once weaknsobject is moved to
34 [_webView stringByEvaluatingJavaScriptFromString:script];
40 - (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass
41 presenceBeacon:(NSString*)beacon {
42 NSString* result = [_webView stringByEvaluatingJavaScriptFromString:
43 [NSString stringWithFormat:@"typeof %@", beacon]];
44 return [result isEqualToString:@"object"];
47 - (void)injectScript:(NSString*)script
48 forClass:(Class)jsInjectionManagerClass {
49 // Web layer guarantees that __gCrWeb object is always injected first.
50 [_webView stringByEvaluatingJavaScriptFromString:@"window.__gCrWeb = {};"];
51 [_webView stringByEvaluatingJavaScriptFromString:script];
54 - (web::WebViewType)webViewType {
55 return web::UI_WEB_VIEW_TYPE;
60 @interface CRWTestJSInjectionReceiver () {
61 base::scoped_nsobject<CRWTestUIWebViewEvaluator> evaluator_;
65 @implementation CRWTestJSInjectionReceiver
68 base::scoped_nsobject<CRWTestUIWebViewEvaluator> evaluator(
69 [[CRWTestUIWebViewEvaluator alloc] init]);
70 if (self = [super initWithEvaluator:evaluator])
71 evaluator_.swap(evaluator);