| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/web_state/ui/crw_wk_script_message_router.h" | 5 #import "ios/web/web_state/ui/crw_wk_script_message_router.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 | 8 #import "base/mac/scoped_nsobject.h" |
| 9 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 10 #error "This file requires ARC support." | |
| 11 #endif | |
| 12 | 9 |
| 13 @interface CRWWKScriptMessageRouter ()<WKScriptMessageHandler> | 10 @interface CRWWKScriptMessageRouter ()<WKScriptMessageHandler> |
| 14 | 11 |
| 15 // Removes a specific message handler. Does nothing if handler does not exist. | 12 // Removes a specific message handler. Does nothing if handler does not exist. |
| 16 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName | 13 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName |
| 17 webView:(WKWebView*)webView; | 14 webView:(WKWebView*)webView; |
| 18 | 15 |
| 19 @property(nonatomic, weak, readwrite) | |
| 20 WKUserContentController* userContentController; | |
| 21 | |
| 22 @end | 16 @end |
| 23 | 17 |
| 24 @implementation CRWWKScriptMessageRouter { | 18 @implementation CRWWKScriptMessageRouter { |
| 25 // Two level map of registed message handlers. Keys are message names and | 19 // Two level map of registed message handlers. Keys are message names and |
| 26 // values are more maps (where keys are web views and values are handlers). | 20 // values are more maps (where keys are web views and values are handlers). |
| 27 NSMutableDictionary* _handlers; | 21 base::scoped_nsobject<NSMutableDictionary> _handlers; |
| 22 // Wrapped WKUserContentController. |
| 23 base::scoped_nsobject<WKUserContentController> _userContentController; |
| 28 } | 24 } |
| 29 | 25 |
| 30 // Wrapped WKUserContentController. | |
| 31 @synthesize userContentController = _userContentController; | |
| 32 | |
| 33 #pragma mark - | 26 #pragma mark - |
| 34 #pragma mark Interface | 27 #pragma mark Interface |
| 35 | 28 |
| 29 - (WKUserContentController*)userContentController { |
| 30 return _userContentController.get(); |
| 31 } |
| 32 |
| 36 - (instancetype)init { | 33 - (instancetype)init { |
| 37 NOTREACHED(); | 34 NOTREACHED(); |
| 38 return nil; | 35 return nil; |
| 39 } | 36 } |
| 40 | 37 |
| 41 - (instancetype)initWithUserContentController: | 38 - (instancetype)initWithUserContentController: |
| 42 (WKUserContentController*)userContentController { | 39 (WKUserContentController*)userContentController { |
| 43 DCHECK(userContentController); | 40 DCHECK(userContentController); |
| 44 if ((self = [super init])) { | 41 if ((self = [super init])) { |
| 45 _handlers = [[NSMutableDictionary alloc] init]; | 42 _handlers.reset([[NSMutableDictionary alloc] init]); |
| 46 _userContentController = userContentController; | 43 _userContentController.reset([userContentController retain]); |
| 47 } | 44 } |
| 48 return self; | 45 return self; |
| 49 } | 46 } |
| 50 | 47 |
| 51 - (void)setScriptMessageHandler:(void (^)(WKScriptMessage*))handler | 48 - (void)setScriptMessageHandler:(void (^)(WKScriptMessage*))handler |
| 52 name:(NSString*)messageName | 49 name:(NSString*)messageName |
| 53 webView:(WKWebView*)webView { | 50 webView:(WKWebView*)webView { |
| 54 DCHECK(handler); | 51 DCHECK(handler); |
| 55 DCHECK(messageName); | 52 DCHECK(messageName); |
| 56 DCHECK(webView); | 53 DCHECK(webView); |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 96 ((void (^)(WKScriptMessage*))handler)(message); | 93 ((void (^)(WKScriptMessage*))handler)(message); |
| 97 } | 94 } |
| 98 } | 95 } |
| 99 | 96 |
| 100 #pragma mark - | 97 #pragma mark - |
| 101 #pragma mark Implementation | 98 #pragma mark Implementation |
| 102 | 99 |
| 103 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName | 100 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName |
| 104 webView:(WKWebView*)webView { | 101 webView:(WKWebView*)webView { |
| 105 NSMapTable* webViewToHandlerMap = [_handlers objectForKey:messageName]; | 102 NSMapTable* webViewToHandlerMap = [_handlers objectForKey:messageName]; |
| 106 NS_VALID_UNTIL_END_OF_SCOPE id handler = | 103 id handler = [webViewToHandlerMap objectForKey:webView]; |
| 107 [webViewToHandlerMap objectForKey:webView]; | |
| 108 if (!handler) | 104 if (!handler) |
| 109 return; | 105 return; |
| 110 | 106 // Extend the lifetime of |handler| so removeScriptMessageHandlerForName: can |
| 107 // be called from inside of |handler|. |
| 108 [[handler retain] autorelease]; |
| 111 if (webViewToHandlerMap.count == 1) { | 109 if (webViewToHandlerMap.count == 1) { |
| 112 [_handlers removeObjectForKey:messageName]; | 110 [_handlers removeObjectForKey:messageName]; |
| 113 [_userContentController removeScriptMessageHandlerForName:messageName]; | 111 [_userContentController removeScriptMessageHandlerForName:messageName]; |
| 114 } else { | 112 } else { |
| 115 [webViewToHandlerMap removeObjectForKey:webView]; | 113 [webViewToHandlerMap removeObjectForKey:webView]; |
| 116 } | 114 } |
| 117 } | 115 } |
| 118 | 116 |
| 119 @end | 117 @end |
| OLD | NEW |