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 |
| 19 @property(nonatomic, weak, readwrite) |
| 20 WKUserContentController* userContentController; |
| 21 |
16 @end | 22 @end |
17 | 23 |
18 @implementation CRWWKScriptMessageRouter { | 24 @implementation CRWWKScriptMessageRouter { |
19 // Two level map of registed message handlers. Keys are message names and | 25 // 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). | 26 // values are more maps (where keys are web views and values are handlers). |
21 base::scoped_nsobject<NSMutableDictionary> _handlers; | 27 NSMutableDictionary* _handlers; |
22 // Wrapped WKUserContentController. | |
23 base::scoped_nsobject<WKUserContentController> _userContentController; | |
24 } | 28 } |
25 | 29 |
| 30 // Wrapped WKUserContentController. |
| 31 @synthesize userContentController = _userContentController; |
| 32 |
26 #pragma mark - | 33 #pragma mark - |
27 #pragma mark Interface | 34 #pragma mark Interface |
28 | 35 |
29 - (WKUserContentController*)userContentController { | |
30 return _userContentController.get(); | |
31 } | |
32 | |
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 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 ((void (^)(WKScriptMessage*))handler)(message); | 96 ((void (^)(WKScriptMessage*))handler)(message); |
94 } | 97 } |
95 } | 98 } |
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 NS_VALID_UNTIL_END_OF_SCOPE id handler = |
| 107 [webViewToHandlerMap objectForKey:webView]; |
104 if (!handler) | 108 if (!handler) |
105 return; | 109 return; |
106 // Extend the lifetime of |handler| so removeScriptMessageHandlerForName: can | 110 |
107 // be called from inside of |handler|. | |
108 [[handler retain] autorelease]; | |
109 if (webViewToHandlerMap.count == 1) { | 111 if (webViewToHandlerMap.count == 1) { |
110 [_handlers removeObjectForKey:messageName]; | 112 [_handlers removeObjectForKey:messageName]; |
111 [_userContentController removeScriptMessageHandlerForName:messageName]; | 113 [_userContentController removeScriptMessageHandlerForName:messageName]; |
112 } else { | 114 } else { |
113 [webViewToHandlerMap removeObjectForKey:webView]; | 115 [webViewToHandlerMap removeObjectForKey:webView]; |
114 } | 116 } |
115 } | 117 } |
116 | 118 |
117 @end | 119 @end |
OLD | NEW |