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