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 #import "base/mac/scoped_nsobject.h" | 8 |
9 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
10 #error "This file requires ARC support." | |
11 #endif | |
9 | 12 |
10 @interface CRWWKScriptMessageRouter ()<WKScriptMessageHandler> | 13 @interface CRWWKScriptMessageRouter ()<WKScriptMessageHandler> |
11 | 14 |
12 // Removes a specific message handler. Does nothing if handler does not exist. | 15 // Removes a specific message handler. Does nothing if handler does not exist. |
13 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName | 16 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName |
14 webView:(WKWebView*)webView; | 17 webView:(WKWebView*)webView; |
15 | 18 |
16 @end | 19 @end |
17 | 20 |
18 @implementation CRWWKScriptMessageRouter { | 21 @implementation CRWWKScriptMessageRouter { |
19 // Two level map of registed message handlers. Keys are message names and | 22 // Two level map of registed message handlers. Keys are message names and |
20 // values are more maps (where keys are web views and values are handlers). | 23 // values are more maps (where keys are web views and values are handlers). |
21 base::scoped_nsobject<NSMutableDictionary> _handlers; | 24 NSMutableDictionary* _handlers; |
22 // Wrapped WKUserContentController. | 25 // Wrapped WKUserContentController. |
23 base::scoped_nsobject<WKUserContentController> _userContentController; | 26 WKUserContentController* _userContentController; |
24 } | 27 } |
25 | 28 |
26 #pragma mark - | 29 #pragma mark - |
27 #pragma mark Interface | 30 #pragma mark Interface |
28 | 31 |
29 - (WKUserContentController*)userContentController { | 32 - (WKUserContentController*)userContentController { |
30 return _userContentController.get(); | 33 return _userContentController; |
31 } | 34 } |
32 | 35 |
33 - (instancetype)init { | 36 - (instancetype)init { |
34 NOTREACHED(); | 37 NOTREACHED(); |
35 return nil; | 38 return nil; |
36 } | 39 } |
37 | 40 |
38 - (instancetype)initWithUserContentController: | 41 - (instancetype)initWithUserContentController: |
39 (WKUserContentController*)userContentController { | 42 (WKUserContentController*)userContentController { |
40 DCHECK(userContentController); | 43 DCHECK(userContentController); |
41 if ((self = [super init])) { | 44 if ((self = [super init])) { |
42 _handlers.reset([[NSMutableDictionary alloc] init]); | 45 _handlers = [[NSMutableDictionary alloc] init]; |
43 _userContentController.reset([userContentController retain]); | 46 _userContentController = userContentController; |
44 } | 47 } |
45 return self; | 48 return self; |
46 } | 49 } |
47 | 50 |
48 - (void)setScriptMessageHandler:(void (^)(WKScriptMessage*))handler | 51 - (void)setScriptMessageHandler:(void (^)(WKScriptMessage*))handler |
49 name:(NSString*)messageName | 52 name:(NSString*)messageName |
50 webView:(WKWebView*)webView { | 53 webView:(WKWebView*)webView { |
51 DCHECK(handler); | 54 DCHECK(handler); |
52 DCHECK(messageName); | 55 DCHECK(messageName); |
53 DCHECK(webView); | 56 DCHECK(webView); |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
96 | 99 |
97 #pragma mark - | 100 #pragma mark - |
98 #pragma mark Implementation | 101 #pragma mark Implementation |
99 | 102 |
100 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName | 103 - (void)tryRemoveScriptMessageHandlerForName:(NSString*)messageName |
101 webView:(WKWebView*)webView { | 104 webView:(WKWebView*)webView { |
102 NSMapTable* webViewToHandlerMap = [_handlers objectForKey:messageName]; | 105 NSMapTable* webViewToHandlerMap = [_handlers objectForKey:messageName]; |
103 id handler = [webViewToHandlerMap objectForKey:webView]; | 106 id handler = [webViewToHandlerMap objectForKey:webView]; |
104 if (!handler) | 107 if (!handler) |
105 return; | 108 return; |
106 // Extend the lifetime of |handler| so removeScriptMessageHandlerForName: can | 109 // ARC TODO: Discuss the lifecycle of handler here in ARC. |
PL
2017/06/01 01:56:21
This is something that needs discussion.
We were
| |
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 |