1313#import " WKJavaScriptBridgePluginAnnotation.h"
1414#import " WKBasePlugin.h"
1515#import " WKCommandProtocol.h"
16+ #import " WKPluginSecurityProtocol.h"
1617
1718static NSString * const WKJSBridge = @" WKJSBridgeMessageName" ;
1819
@@ -21,11 +22,11 @@ @interface WKJavaScriptBridge ()<WKScriptMessageHandler>
2122@property (nonatomic , strong ) WKJavaScriptBridgePluginAnnotation *annotation;// 模块注册白名单
2223@property (nonatomic , strong ) WKJavaScriptMessageDispatcher *dispatcher;// 插件模块派发
2324@property (nonatomic , strong ) WKCommandImpl *commandDelegate;// 插件模块可执行函数实现
24- @property (nonatomic , strong ) NSMutableDictionary *whiteList;// 白名单列表
25- @property (nonatomic , assign , getter =isOpenWhiteList) BOOL openWhiteList;// 是否开启白名单
2625@property (nonatomic , strong ) NSMutableDictionary *pluginsByName;// 插件模块实例对象
2726@property (nonatomic , weak , readwrite ) WKWebView *webView;
28- @property (nonatomic , strong ) NSRecursiveLock *lock;
27+ // 安全相关
28+ @property (nonatomic , strong ) id <WKPluginSecurityProtocol>securityPlugin;// 白名单插件
29+ @property (nonatomic , strong ) NSArray *secInfo;// 业务注册的安全信息
2930
3031@end
3132
@@ -52,10 +53,6 @@ + (instancetype)bindBridgeWithWebView:(WKWebView *)webView {
5253 return nil ;
5354}
5455
55- - (void )openWhiteList : (BOOL )openning {
56- self.openWhiteList = openning;
57- }
58-
5956#pragma mark - Init
6057
6158- (instancetype )initBridgeWithWebView : (WKWebView *)webView {
@@ -68,6 +65,7 @@ - (instancetype)initBridgeWithWebView:(WKWebView *)webView {
6865}
6966
7067- (void )commonInit {
68+ self.secInfo = [NSArray array ];
7169 self.dispatcher = [[WKJavaScriptMessageDispatcher alloc ] initWithBridge: self ];
7270 self.annotation = [[WKJavaScriptBridgePluginAnnotation alloc ] initWithBridge: self ];
7371 self.commandDelegate = [[WKCommandImpl alloc ] initWithBridge: self ];
@@ -123,34 +121,36 @@ - (void)pluginInstall:(WKBasePlugin *)plugin {
123121
124122#pragma mark - WKJavaScriptBridgeProtocol
125123
126- - (void )addWhiteList : (NSString *)pluginName {
127- [self .lock lock ];
128- [self .whiteList setValue: pluginName forKey: [pluginName lowercaseString ]];
129- [self .lock unlock ];
124+ - (void )registerSecurityPlugin : (NSString *)name {
125+ id <WKPluginSecurityProtocol>securityPlugin = [self getCommandInstance: name];
126+ if ([securityPlugin isKindOfClass: [WKBasePlugin class ]] &&
127+ [securityPlugin conformsToProtocol: @protocol (WKPluginSecurityProtocol)]) {
128+ self.securityPlugin = securityPlugin;
129+ }
130130}
131131
132- - (id )getCommandInstance : (NSString *)pluginName {
133- NSString * className;
134- if (self.isOpenWhiteList ) {// 校验白名单
135- [self .lock lock ];
136- className = [self .whiteList objectForKey: [pluginName lowercaseString ]];
137- [self .lock unlock ];
138- if (className == nil ) {
139- #ifdef DEBUG
140- NSLog (@" WKJavaScriptBridge Error : 模块: (%@ ) 没有注册白名单。请关闭白名单或者或者使用(@WKRegisterWhiteList)注册。" , pluginName);
141- #endif
142- return nil ;
143- }
132+ - (void )registerPluginSecurityInfomation : (NSArray <NSString *>*)secInfo {
133+ self.secInfo = secInfo;
134+ }
135+
136+ - (void )pluginSecurityVerityWithCommand : (WKMsgCommand *)command completionHandler : (void (^)(BOOL passed))completionHandler {
137+ if (self.securityPlugin && [self .securityPlugin respondsToSelector: @selector (applyAuthorityWithService:action:securityInfomation:complete: )]) {
138+ [self .securityPlugin applyAuthorityWithService: command.className action: command.methodName securityInfomation: self .secInfo complete: ^(BOOL allowed) {
139+ completionHandler (allowed);
140+ }];
144141 }else {
145- className = pluginName ;
142+ completionHandler ( YES ) ;
146143 }
144+ }
145+
146+ - (id )getCommandInstance : (NSString *)pluginName {
147147
148148 WKBasePlugin <WKPluginProtocol> *plugin = nil ;
149149 Class implClass = [self pluginImplClass: pluginName];
150150
151151 BOOL shouldCache = [self pluginShouldCache: implClass];
152152 if (shouldCache) {
153- plugin = [self .pluginsByName objectForKey: className ];
153+ plugin = [self .pluginsByName objectForKey: pluginName ];
154154 if (plugin) {
155155 return plugin;
156156 }
@@ -166,11 +166,11 @@ - (id)getCommandInstance:(NSString*)pluginName {
166166 if (plugin) {
167167 [self pluginInstall: plugin];
168168 if (shouldCache) {
169- [self .pluginsByName setObject: plugin forKey: className ];
169+ [self .pluginsByName setObject: plugin forKey: pluginName ];
170170 }
171171 }else {
172172#ifdef DEBUG
173- NSLog (@" WKJavaScriptBridge Error : 模块: (%@ ) 不存在。" , pluginName);
173+ NSLog (@" PHJavaScriptBridge Error : 模块: (%@ ) 不存在。" , pluginName);
174174#endif
175175 }
176176 return plugin;
@@ -181,11 +181,11 @@ - (id)getCommandInstance:(NSString*)pluginName {
181181 if (plugin) {
182182 [self pluginInstall: plugin];
183183 if (shouldCache) {
184- [self .pluginsByName setObject: plugin forKey: className ];
184+ [self .pluginsByName setObject: plugin forKey: pluginName ];
185185 }
186186 }else {
187187#ifdef DEBUG
188- NSLog (@" WKJavaScriptBridge Error : 模块: (%@ ) 不存在。" , pluginName);
188+ NSLog (@" PHJavaScriptBridge Error : 模块: (%@ ) 不存在。" , pluginName);
189189#endif
190190 }
191191 return plugin;
@@ -220,20 +220,6 @@ - (void)userContentController:(WKUserContentController *)userContentController d
220220
221221#pragma mark - Set or Get
222222
223- - (NSRecursiveLock *)lock {
224- if (!_lock) {
225- _lock = [[NSRecursiveLock alloc ] init ];
226- }
227- return _lock;
228- }
229-
230- - (NSMutableDictionary *)whiteList {
231- if (!_whiteList) {
232- _whiteList = [[NSMutableDictionary alloc ] initWithCapacity: 1 ];
233- }
234- return _whiteList;
235- }
236-
237223- (NSMutableDictionary *)pluginsByName {
238224 if (!_pluginsByName) {
239225 _pluginsByName = [[NSMutableDictionary alloc ] initWithCapacity: 1 ];
0 commit comments