OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #import "ios/web/public/test/crw_test_js_injection_receiver.h" | 5 #import "ios/web/public/test/crw_test_js_injection_receiver.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
| 9 #include "base/ios/weak_nsobject.h" |
9 #import "base/mac/scoped_nsobject.h" | 10 #import "base/mac/scoped_nsobject.h" |
10 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" | 11 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" |
11 | 12 |
12 @interface CRWTestUIWebViewEvaluator : NSObject<CRWJSInjectionEvaluator> { | 13 @interface CRWTestUIWebViewEvaluator : NSObject<CRWJSInjectionEvaluator> { |
13 base::scoped_nsobject<UIWebView> _webView; | 14 base::scoped_nsobject<UIWebView> _webView; |
14 } | 15 } |
15 @end | 16 @end |
16 | 17 |
17 @implementation CRWTestUIWebViewEvaluator | 18 @implementation CRWTestUIWebViewEvaluator |
18 | 19 |
19 - (id) init { | 20 - (id) init { |
20 if (self = [super init]) | 21 if (self = [super init]) |
21 _webView.reset([[UIWebView alloc] init]); | 22 _webView.reset([[UIWebView alloc] init]); |
22 return self; | 23 return self; |
23 } | 24 } |
24 | 25 |
25 #pragma mark - | 26 #pragma mark - |
26 #pragma mark CRWJSInjectionEvaluatorMethods | 27 #pragma mark CRWJSInjectionEvaluatorMethods |
27 | 28 |
28 - (void)evaluateJavaScript:(NSString*)script | 29 - (void)evaluateJavaScript:(NSString*)script |
29 stringResultHandler:(web::JavaScriptCompletion)handler { | 30 stringResultHandler:(web::JavaScriptCompletion)handler { |
| 31 base::WeakNSObject<CRWTestUIWebViewEvaluator> weakEvaluator(self); |
30 dispatch_async(dispatch_get_main_queue(), ^{ | 32 dispatch_async(dispatch_get_main_queue(), ^{ |
31 // TODO(shreyasv): Change to weaknsobject once weaknsobject is moved to | 33 UIWebView* webView = weakEvaluator ? weakEvaluator.get()->_webView : nil; |
32 // ios/base. | |
33 NSString* result = | 34 NSString* result = |
34 [_webView stringByEvaluatingJavaScriptFromString:script]; | 35 [webView stringByEvaluatingJavaScriptFromString:script]; |
35 if (handler) | 36 if (handler) |
36 handler(result, nil); | 37 handler(result, nil); |
37 }); | 38 }); |
38 } | 39 } |
39 | 40 |
40 - (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass | 41 - (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass |
41 presenceBeacon:(NSString*)beacon { | 42 presenceBeacon:(NSString*)beacon { |
42 NSString* result = [_webView stringByEvaluatingJavaScriptFromString: | 43 NSString* result = [_webView stringByEvaluatingJavaScriptFromString: |
43 [NSString stringWithFormat:@"typeof %@", beacon]]; | 44 [NSString stringWithFormat:@"typeof %@", beacon]]; |
44 return [result isEqualToString:@"object"]; | 45 return [result isEqualToString:@"object"]; |
(...skipping 21 matching lines...) Expand all Loading... |
66 | 67 |
67 - (id)init { | 68 - (id)init { |
68 base::scoped_nsobject<CRWTestUIWebViewEvaluator> evaluator( | 69 base::scoped_nsobject<CRWTestUIWebViewEvaluator> evaluator( |
69 [[CRWTestUIWebViewEvaluator alloc] init]); | 70 [[CRWTestUIWebViewEvaluator alloc] init]); |
70 if (self = [super initWithEvaluator:evaluator]) | 71 if (self = [super initWithEvaluator:evaluator]) |
71 evaluator_.swap(evaluator); | 72 evaluator_.swap(evaluator); |
72 return self; | 73 return self; |
73 } | 74 } |
74 | 75 |
75 @end | 76 @end |
OLD | NEW |