1 // Copyright 2012 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/web_state/js/crw_js_injection_receiver.h"
7 #include "base/logging.h"
8 #import "base/mac/scoped_nsobject.h"
9 #import "ios/web/public/web_state/js/crw_js_injection_evaluator.h"
10 #import "ios/web/public/web_state/js/crw_js_injection_manager.h"
12 @implementation CRWJSInjectionReceiver {
13 // Used to evaluate JavaScripts.
14 id<CRWJSInjectionEvaluator> _evaluator;
16 // Map from a CRWJSInjectionManager class to its instance created for this
18 base::scoped_nsobject<NSMutableDictionary> _managers;
26 - (id)initWithEvaluator:(id<CRWJSInjectionEvaluator>)evaluator {
30 _evaluator = evaluator;
31 _managers.reset([[NSMutableDictionary alloc] init]);
37 #pragma mark CRWJSInjectionEvaluatorMethods
39 - (void)evaluateJavaScript:(NSString*)script
40 stringResultHandler:(web::JavaScriptCompletion)handler {
41 [_evaluator evaluateJavaScript:script stringResultHandler:handler];
44 - (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass
45 presenceBeacon:(NSString*)beacon {
46 return [_evaluator scriptHasBeenInjectedForClass:jsInjectionManagerClass
47 presenceBeacon:beacon];
50 - (void)injectScript:(NSString*)script forClass:(Class)jsInjectionManagerClass {
51 [_evaluator injectScript:script forClass:jsInjectionManagerClass];
54 - (web::WebViewType)webViewType {
55 return [_evaluator webViewType];
58 - (CRWJSInjectionManager*)instanceOfClass:(Class)jsInjectionManagerClass {
60 CRWJSInjectionManager* manager =
61 [_managers objectForKey:jsInjectionManagerClass];
63 base::scoped_nsobject<CRWJSInjectionManager> newManager(
64 [[jsInjectionManagerClass alloc] initWithReceiver:self]);
65 [_managers setObject:newManager forKey:jsInjectionManagerClass];
69 for (Class depedencyClass in [manager directDependencies]) {
70 [self instanceOfClass:depedencyClass];
77 @implementation CRWJSInjectionReceiver (Testing)
78 - (NSDictionary*)managers {
79 return _managers.get();