Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement onShouldStartLoadWithRequest and webView turboModule #250

Merged
merged 2 commits into from
Nov 8, 2024

Conversation

425765923
Copy link

@425765923 425765923 commented Nov 8, 2024

Summary

#249

安卓onShouldStartLoadWithRequest实现方案:

  1. 原生shouldOverrideUrlLoading拦截加载请求
  2. 发送onShouldStartLoadWithRequest事件
  3. 设置250ms超时保护,注释主线程去获取子线程TM中的结果
  4. js 业务侧返回拦截结果,并设置到TM中
  5. shouldOverrideUrlLoading 根据进行是否拦截
    相关代码

由于在RNOH中,emitComponentEvent是异步接口,如果按照类似实现在阻塞主线程时arkui没有发送VSync,而无法真正发送事件至js线程触发RN onShouldStartLoadWithRequest事件,因此在HarmonyOS中使用如下实现方案

HarmonyOS onShouldStartLoadWithRequest实现方案:

  1. 原生onLoadIntercept拦截加载请求
  2. 发送onShouldStartLoadWithRequest事件,设置正常加载时的回调,设置250ms超时保护超时必定加载网页
  3. onLoadIntercept必定拦截加载
  4. 待js返回结果后,在shouldStartLoadWithLockIdentifier TM中根据结果确定是否加载,并执行相关回调
  5. onLoadIntercept不在拦截

Test Plan

展示代码的稳定性。例如:用来复现场景的命令输入和结果输出、测试用例的路径地址,或者附上截图和视频。

import React, { useState } from 'react';
import { View, Button, StyleSheet } from 'react-native';
import { WebView } from 'react-native-webview';

const WebViewDemo = () => {
const [url, setUrl] = useState('https://www.example.com');
WebView.isFileUploadSupported().then(res=>{
console.log("isFileUploadSupported:",res)
})
return (

<WebView
style={{width:'100%',height:'300'}}
source={{ uri: 'https://www.baidu.com' }}
onShouldStartLoadWithRequest={(request) => {
// Only allow navigating within this website
return false
}}
onLoadEnd={()=>
console.log('end')
}
/>
<WebView
style={{width:'100%',height:'300'}}
source={{ uri: url }}
onShouldStartLoadWithRequest={(request) => {
// Only allow navigating within this website
console.log(request)
return request.url.startsWith('https://reactnative.dev');
}}
onLoadEnd={()=>
console.log('end')
}
/>
<Button title='11111' onPress={()=>{
setUrl('https://reactnative.dev')
setTimeout(() => {
setUrl('https://www.baidu.com')
}, 0);
}}>

);
};

const styles = StyleSheet.create({
container: {
width: '100%',
height: '100%'
},
webview: {
flex: 1,
marginTop: 10,
},
loading: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});

export default WebViewDemo;

Checklist

  • 已经在真机设备或模拟器上测试通过
  • 已经与 Android 或 iOS 平台做过效果/功能对比
  • 已经添加了对应 API 的测试用例(如需要)
  • 已经更新了文档(如需要)
  • 更新了 JS/TS 代码 (如有)

} else if (this.source.uri != "") {
Logger.debug(TAG, `[RNOH] newDescriptor props update uri: ` + this.source.uri);
this.controller.loadUrl(this.descriptorWrapper.rawProps.newSource.uri, this.headers)
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

都为空的时候是否需要添加判断

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

进入加载拦截的回调中都是有url 或者data的,前面加载前已经做了判断处理

Copy link

@ninjarz ninjarz Nov 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this.descriptorWrapper.rawProps.newSource.uri 导致 asset:// 转换失效了
@425765923

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果没有onShouldStartLoadWithRequest使用需求,建议0.2.33版本webview。当前版本onShouldStartLoadWithRequest实现的适用场景比较有限,完整实现需要依赖后续rnoh接口。若目前对该接口有简单需求,也可修改arkts实现代码,将rn拦截逻辑放置与原生端做规避。

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

@425765923 425765923 merged commit 524f873 into react-native-oh-library:sig Nov 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants