| 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/fakes/crw_test_js_injection_receiver.h" | 5 #import "ios/web/public/test/fakes/crw_test_js_injection_receiver.h" |
| 6 | 6 |
| 7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
| 8 #import <WebKit/WebKit.h> | 8 #import <WebKit/WebKit.h> |
| 9 | 9 |
| 10 #import "base/ios/weak_nsobject.h" | |
| 11 #import "base/mac/scoped_nsobject.h" | |
| 12 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" | 10 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h" |
| 13 #import "ios/web/web_state/ui/web_view_js_utils.h" | 11 #import "ios/web/web_state/ui/web_view_js_utils.h" |
| 14 | 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 15 @interface CRWTestWKWebViewEvaluator : NSObject<CRWJSInjectionEvaluator> { | 17 @interface CRWTestWKWebViewEvaluator : NSObject<CRWJSInjectionEvaluator> { |
| 16 // Web view for JavaScript evaluation. | 18 // Web view for JavaScript evaluation. |
| 17 base::scoped_nsobject<WKWebView> _webView; | 19 WKWebView* _webView; |
| 18 // Set to track injected script managers. | 20 // Set to track injected script managers. |
| 19 base::scoped_nsobject<NSMutableSet> _injectedScriptManagers; | 21 NSMutableSet* _injectedScriptManagers; |
| 20 } | 22 } |
| 21 @end | 23 @end |
| 22 | 24 |
| 23 @implementation CRWTestWKWebViewEvaluator | 25 @implementation CRWTestWKWebViewEvaluator |
| 24 | 26 |
| 25 - (instancetype)init { | 27 - (instancetype)init { |
| 26 if (self = [super init]) { | 28 if (self = [super init]) { |
| 27 _webView.reset([[WKWebView alloc] init]); | 29 _webView = [[WKWebView alloc] init]; |
| 28 _injectedScriptManagers.reset([[NSMutableSet alloc] init]); | 30 _injectedScriptManagers = [[NSMutableSet alloc] init]; |
| 29 } | 31 } |
| 30 return self; | 32 return self; |
| 31 } | 33 } |
| 32 | 34 |
| 33 - (void)executeJavaScript:(NSString*)script | 35 - (void)executeJavaScript:(NSString*)script |
| 34 completionHandler:(web::JavaScriptResultBlock)completionHandler { | 36 completionHandler:(web::JavaScriptResultBlock)completionHandler { |
| 35 web::ExecuteJavaScript(_webView, script, completionHandler); | 37 web::ExecuteJavaScript(_webView, script, completionHandler); |
| 36 } | 38 } |
| 37 | 39 |
| 38 - (BOOL)scriptHasBeenInjectedForClass:(Class)injectionManagerClass { | 40 - (BOOL)scriptHasBeenInjectedForClass:(Class)injectionManagerClass { |
| 39 return [_injectedScriptManagers containsObject:injectionManagerClass]; | 41 return [_injectedScriptManagers containsObject:injectionManagerClass]; |
| 40 } | 42 } |
| 41 | 43 |
| 42 - (void)injectScript:(NSString*)script forClass:(Class)JSInjectionManagerClass { | 44 - (void)injectScript:(NSString*)script forClass:(Class)JSInjectionManagerClass { |
| 43 // Web layer guarantees that __gCrWeb object is always injected first. | 45 // Web layer guarantees that __gCrWeb object is always injected first. |
| 44 NSString* supplementedScript = | 46 NSString* supplementedScript = |
| 45 [@"window.__gCrWeb = {};" stringByAppendingString:script]; | 47 [@"window.__gCrWeb = {};" stringByAppendingString:script]; |
| 46 [_webView evaluateJavaScript:supplementedScript completionHandler:nil]; | 48 [_webView evaluateJavaScript:supplementedScript completionHandler:nil]; |
| 47 [_injectedScriptManagers addObject:JSInjectionManagerClass]; | 49 [_injectedScriptManagers addObject:JSInjectionManagerClass]; |
| 48 } | 50 } |
| 49 | 51 |
| 50 @end | 52 @end |
| 51 | 53 |
| 52 @interface CRWTestJSInjectionReceiver () { | 54 @interface CRWTestJSInjectionReceiver () { |
| 53 base::scoped_nsobject<CRWTestWKWebViewEvaluator> evaluator_; | 55 CRWTestWKWebViewEvaluator* evaluator_; |
| 54 } | 56 } |
| 55 @end | 57 @end |
| 56 | 58 |
| 57 @implementation CRWTestJSInjectionReceiver | 59 @implementation CRWTestJSInjectionReceiver |
| 58 | 60 |
| 59 - (id)init { | 61 - (id)init { |
| 60 base::scoped_nsobject<CRWTestWKWebViewEvaluator> evaluator( | 62 CRWTestWKWebViewEvaluator* evaluator = |
| 61 [[CRWTestWKWebViewEvaluator alloc] init]); | 63 [[CRWTestWKWebViewEvaluator alloc] init]; |
| 62 if (self = [super initWithEvaluator:evaluator]) | 64 if (self = [super initWithEvaluator:evaluator]) |
| 63 evaluator_.swap(evaluator); | 65 evaluator_ = evaluator; |
| 64 return self; | 66 return self; |
| 65 } | 67 } |
| 66 | 68 |
| 67 @end | 69 @end |
| OLD | NEW |