Chromium Code Reviews| Index: ios/web/web_state/js/crw_js_injection_receiver.mm |
| diff --git a/ios/web/web_state/js/crw_js_injection_receiver.mm b/ios/web/web_state/js/crw_js_injection_receiver.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ffc3cead48a67a7e6456857234a1f87907cd831b |
| --- /dev/null |
| +++ b/ios/web/web_state/js/crw_js_injection_receiver.mm |
| @@ -0,0 +1,77 @@ |
| +// Copyright 2012 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. |
| + |
| +#include "ios/web/public/web_state/js/crw_js_injection_receiver.h" |
| + |
| +#include "base/logging.h" |
| +#import "base/mac/scoped_nsobject.h" |
| +#include "ios/web/public/web_state/js/crw_js_injection_evaluator.h" |
|
sdefresne
2014/12/10 15:30:23
nit: those headers are Objective-C headers too, wh
|
| +#include "ios/web/public/web_state/js/crw_js_injection_manager.h" |
| + |
| +@implementation CRWJSInjectionReceiver { |
| + // Used to evaluate JavaScripts. |
| + __weak id<CRWJSInjectionEvaluator> _evaluator; |
|
sdefresne
2014/12/10 15:30:23
__weak has no meaning if not using ARC. Its presen
|
| + |
| + // Map from a CRWJSInjectionManager class to its instance created for this |
| + // receiver. |
| + base::scoped_nsobject<NSMutableDictionary> _managers; |
| +} |
| + |
| +- (id)init { |
| + NOTREACHED(); |
| + return [super init]; |
| +} |
| + |
| +- (id)initWithEvaluator:(id<CRWJSInjectionEvaluator>)evaluator { |
| + DCHECK(evaluator); |
| + self = [super init]; |
| + if (self) { |
| + _evaluator = evaluator; |
| + _managers.reset([[NSMutableDictionary alloc] init]); |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark - |
| +#pragma mark CRWJSInjectionEvaluatorMethods |
| + |
| +- (void)evaluateJavaScript:(NSString*)script |
| + stringResultHandler:(web::JavaScriptCompletion)handler { |
| + [_evaluator evaluateJavaScript:script stringResultHandler:handler]; |
| +} |
| + |
| +- (BOOL)scriptHasBeenInjectedForClass:(Class)jsInjectionManagerClass |
| + presenceBeacon:(NSString*)beacon { |
| + return [_evaluator scriptHasBeenInjectedForClass:jsInjectionManagerClass |
| + presenceBeacon:beacon]; |
| +} |
| + |
| +- (void)injectScript:(NSString*)script forClass:(Class)jsInjectionManagerClass { |
| + [_evaluator injectScript:script forClass:jsInjectionManagerClass]; |
| +} |
| + |
| +- (CRWJSInjectionManager*)instanceOfClass:(Class)jsInjectionManagerClass { |
| + DCHECK(_managers); |
| + CRWJSInjectionManager* manager = |
| + [_managers objectForKey:jsInjectionManagerClass]; |
| + if (!manager) { |
| + base::scoped_nsobject<CRWJSInjectionManager> newManager( |
| + [[jsInjectionManagerClass alloc] initWithReceiver:self]); |
| + [_managers setObject:newManager forKey:jsInjectionManagerClass]; |
| + manager = newManager; |
| + } |
| + DCHECK(manager); |
| + for (Class depedencyClass in [manager directDependencies]) { |
| + [self instanceOfClass:depedencyClass]; |
| + } |
| + return manager; |
| +} |
| + |
| +@end |
| + |
| +@implementation CRWJSInjectionReceiver (Testing) |
| +- (NSDictionary*)managers { |
| + return _managers.get(); |
| +} |
| +@end |