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
3 // found in the LICENSE file.
4 
5 #ifndef IOS_WEB_JS_MESSAGING_CRW_WK_SCRIPT_MESSAGE_ROUTER_H_
6 #define IOS_WEB_JS_MESSAGING_CRW_WK_SCRIPT_MESSAGE_ROUTER_H_
7 
8 #import <WebKit/WebKit.h>
9 
10 // WKUserContentController wrapper that allows adding multiple message handlers
11 // for the same message name. CRWWKScriptMessageRouter will route the messages
12 // from the underlying user content controller to a designated receiver by
13 // matching the message's name and webView.
14 @interface CRWWKScriptMessageRouter : NSObject
15 
16 // Underlying WKUserContentController.
17 @property(weak, nonatomic, readonly)
18     WKUserContentController* userContentController;
19 
20 // Designated initializer. |userContentController| must not be nil.
21 - (instancetype)initWithUserContentController:
22     (WKUserContentController*)userContentController NS_DESIGNATED_INITIALIZER;
23 
24 - (instancetype)init NS_UNAVAILABLE;
25 
26 // Sets a script message handler. Multiple message handlers can be added for
27 // the same message name and long as |webView| are different. Setting |handler|
28 // for the same |name| and |webView| pair is an error. |handler| will be called
29 // if WKScriptMessage sent by WKUserContentController will match both the |name|
30 // and the |webView|.
31 - (void)setScriptMessageHandler:(void (^)(WKScriptMessage*))handler
32                            name:(NSString*)messageName
33                         webView:(WKWebView*)webView;
34 
35 // Removes a specific message handler.
36 - (void)removeScriptMessageHandlerForName:(NSString*)messageName
37                                   webView:(WKWebView*)webView;
38 
39 // Removes all message handlers for the given |webView|.
40 - (void)removeAllScriptMessageHandlersForWebView:(WKWebView*)webView;
41 
42 @end
43 
44 #endif  // IOS_WEB_JS_MESSAGING_CRW_WK_SCRIPT_MESSAGE_ROUTER_H_
45