1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . IO ;
3
4
using Microsoft . Extensions . DependencyInjection ;
4
5
using Microsoft . Extensions . FileProviders ;
5
6
using Microsoft . Maui ;
6
7
using Microsoft . Maui . Dispatching ;
7
8
using Microsoft . Maui . Handlers ;
8
- using Tizen . WebView ;
9
- using TChromium = Tizen . WebView . Chromium ;
10
- using TWebView = Tizen . WebView . WebView ;
9
+ using Tizen . NUI ;
10
+ using NWebView = Tizen . NUI . BaseComponents . WebView ;
11
11
12
12
namespace Microsoft . AspNetCore . Components . WebView . Maui
13
13
{
14
14
/// <summary>
15
15
/// The Tizen <see cref="ViewHandler"/> for <see cref="BlazorWebView"/>.
16
16
/// </summary>
17
- public partial class BlazorWebViewHandler : ViewHandler < IBlazorWebView , WebViewContainer >
17
+ public partial class BlazorWebViewHandler : ViewHandler < IBlazorWebView , NWebView >
18
18
{
19
+ private const string BlazorWebViewIdentifier = "BlazorWebView:" ;
20
+ private const string UserAgentHeaderKey = "User-Agent" ;
19
21
private const string AppOrigin = "http://0.0.0.0/" ;
20
22
private const string BlazorInitScript = @"
21
23
window.__receiveMessageCallbacks = [];
@@ -42,53 +44,99 @@ public partial class BlazorWebViewHandler : ViewHandler<IBlazorWebView, WebViewC
42
44
})();
43
45
" ;
44
46
45
- private TizenWebViewManager ? _webviewManager ;
46
- private WebViewExtensions . InterceptRequestCallback ? _interceptRequestCallback ;
47
+ static private Dictionary < string , WeakReference < BlazorWebViewHandler > > s_webviewHandlerTable = new Dictionary < string , WeakReference < BlazorWebViewHandler > > ( ) ;
47
48
48
- private TWebView PlatformWebView => PlatformView . WebView ;
49
+ private TizenWebViewManager ? _webviewManager ;
49
50
50
51
private bool RequiredStartupPropertiesSet =>
51
52
//_webview != null &&
52
53
HostPage != null &&
53
54
Services != null ;
54
55
55
56
/// <inheritdoc />
56
- protected override WebViewContainer CreatePlatformView ( )
57
+ protected override NWebView CreatePlatformView ( )
57
58
{
58
- TChromium . Initialize ( ) ;
59
- MauiApplication . Current . Terminated += ( s , e ) => TChromium . Shutdown ( ) ;
60
-
61
- return new WebViewContainer ( PlatformParent ) ;
59
+ return new NWebView ( )
60
+ {
61
+ MouseEventsEnabled = true ,
62
+ KeyEventsEnabled = true ,
63
+ } ;
62
64
}
63
65
64
66
/// <inheritdoc />
65
- protected override void ConnectHandler ( WebViewContainer platformView )
67
+ protected override void ConnectHandler ( NWebView platformView )
66
68
{
67
- _interceptRequestCallback = OnRequestInterceptCallback ;
68
- PlatformWebView . LoadFinished += OnLoadFinished ;
69
- PlatformWebView . AddJavaScriptMessageHandler ( "BlazorHandler" , PostMessageFromJS ) ;
70
- PlatformWebView . SetInterceptRequestCallback ( _interceptRequestCallback ) ;
71
- PlatformWebView . GetSettings ( ) . JavaScriptEnabled = true ;
69
+ platformView . PageLoadFinished += OnLoadFinished ;
70
+ platformView . Context . RegisterHttpRequestInterceptedCallback ( OnRequestInterceptStaticCallback ) ;
71
+ platformView . AddJavaScriptMessageHandler ( "BlazorHandler" , PostMessageFromJS ) ;
72
+ platformView . UserAgent += $ " { BlazorWebViewIdentifier } { GetHashCode ( ) } " ;
73
+ s_webviewHandlerTable [ GetHashCode ( ) . ToString ( ) ] = new WeakReference < BlazorWebViewHandler > ( this ) ;
72
74
}
73
75
74
76
/// <inheritdoc />
75
- protected override void DisconnectHandler ( WebViewContainer platformView )
77
+ protected override void DisconnectHandler ( NWebView platformView )
76
78
{
77
- PlatformWebView . LoadFinished -= OnLoadFinished ;
79
+ platformView . PageLoadFinished -= OnLoadFinished ;
78
80
base . DisconnectHandler ( platformView ) ;
81
+ s_webviewHandlerTable . Remove ( GetHashCode ( ) . ToString ( ) ) ;
82
+ }
83
+
84
+
85
+ private void PostMessageFromJS ( string message )
86
+ {
87
+ _webviewManager ! . MessageReceivedInternal ( new Uri ( PlatformView . Url ) , message ) ;
79
88
}
80
89
81
- private void PostMessageFromJS ( JavaScriptMessage message )
90
+ private void OnLoadFinished ( object ? sender , WebViewPageLoadEventArgs e )
82
91
{
83
- if ( message is null )
92
+ //FocusManager.Instance.SetCurrentFocusView(NativeView);
93
+ var url = PlatformView . Url ;
94
+
95
+ if ( url == AppOrigin )
96
+ PlatformView . EvaluateJavaScript ( BlazorInitScript ) ;
97
+ }
98
+
99
+ private static void OnRequestInterceptStaticCallback ( WebHttpRequestInterceptor interceptor )
100
+ {
101
+ if ( interceptor . Headers . TryGetValue ( UserAgentHeaderKey , out var agent ) )
84
102
{
85
- throw new ArgumentNullException ( nameof ( message ) ) ;
103
+ var idx = agent . IndexOf ( BlazorWebViewIdentifier ) ;
104
+ if ( idx >= 0 )
105
+ {
106
+ var webviewKey = agent . Substring ( idx + BlazorWebViewIdentifier . Length ) ;
107
+ if ( s_webviewHandlerTable . TryGetValue ( webviewKey , out var weakHandler )
108
+ && weakHandler . TryGetTarget ( out var handler ) )
109
+ {
110
+ handler . OnRequestInterceptCallback ( interceptor ) ;
111
+ return ;
112
+ }
113
+ }
86
114
}
115
+ interceptor . Ignore ( ) ;
116
+ }
87
117
88
- if ( message . Name . Equals ( "BlazorHandler" , StringComparison . Ordinal ) )
118
+ private void OnRequestInterceptCallback ( WebHttpRequestInterceptor interceptor )
119
+ {
120
+ var url = interceptor . Url ;
121
+ if ( url . StartsWith ( AppOrigin ) )
89
122
{
90
- _webviewManager ! . MessageReceivedInternal ( new Uri ( PlatformWebView . Url ) , message . GetBodyAsString ( ) ) ;
123
+ var allowFallbackOnHostPage = url . EndsWith ( "/" ) ;
124
+ url = QueryStringHelper . RemovePossibleQueryString ( url ) ;
125
+ if ( _webviewManager ! . TryGetResponseContentInternal ( url , allowFallbackOnHostPage , out var statusCode , out var statusMessage , out var content , out var headers ) )
126
+ {
127
+ var header = $ "HTTP/1.0 200 OK\r \n ";
128
+ foreach ( var item in headers )
129
+ {
130
+ header += $ "{ item . Key } :{ item . Value } \r \n ";
131
+ }
132
+ header += "\r \n " ;
133
+ MemoryStream memstream = new MemoryStream ( ) ;
134
+ content . CopyTo ( memstream ) ;
135
+ interceptor . SetResponse ( header , memstream . ToArray ( ) ) ;
136
+ return ;
137
+ }
91
138
}
139
+ interceptor . Ignore ( ) ;
92
140
}
93
141
94
142
private void StartWebViewCoreIfPossible ( )
@@ -105,14 +153,14 @@ private void StartWebViewCoreIfPossible()
105
153
106
154
// We assume the host page is always in the root of the content directory, because it's
107
155
// unclear there's any other use case. We can add more options later if so.
108
- var contentRootDir = Path . GetDirectoryName ( HostPage ! ) ?? string . Empty ;
109
- var hostPageRelativePath = Path . GetRelativePath ( contentRootDir , HostPage ! ) ;
156
+ var contentRootDir = System . IO . Path . GetDirectoryName ( HostPage ! ) ?? string . Empty ;
157
+ var hostPageRelativePath = System . IO . Path . GetRelativePath ( contentRootDir , HostPage ! ) ;
110
158
111
159
var fileProvider = VirtualView . CreateFileProvider ( contentRootDir ) ;
112
160
113
161
_webviewManager = new TizenWebViewManager (
114
162
this ,
115
- PlatformWebView ,
163
+ PlatformView ,
116
164
Services ! ,
117
165
new MauiDispatcher ( Services ! . GetRequiredService < IDispatcher > ( ) ) ,
118
166
fileProvider ,
@@ -125,7 +173,7 @@ private void StartWebViewCoreIfPossible()
125
173
VirtualView . BlazorWebViewInitializing ( new BlazorWebViewInitializingEventArgs ( ) ) ;
126
174
VirtualView . BlazorWebViewInitialized ( new BlazorWebViewInitializedEventArgs
127
175
{
128
- WebView = PlatformWebView ,
176
+ WebView = PlatformView ,
129
177
} ) ;
130
178
131
179
if ( RootComponents != null )
@@ -139,50 +187,6 @@ private void StartWebViewCoreIfPossible()
139
187
_webviewManager . Navigate ( "/" ) ;
140
188
}
141
189
142
- private void OnRequestInterceptCallback ( IntPtr context , IntPtr request , IntPtr userdata )
143
- {
144
- if ( request == IntPtr . Zero )
145
- {
146
- return ;
147
- }
148
-
149
- var url = PlatformWebView . GetInterceptRequestUrl ( request ) ;
150
-
151
- if ( url . StartsWith ( AppOrigin ) )
152
- {
153
- var allowFallbackOnHostPage = url . EndsWith ( "/" ) ;
154
- url = QueryStringHelper . RemovePossibleQueryString ( url ) ;
155
- if ( _webviewManager ! . TryGetResponseContentInternal ( url , allowFallbackOnHostPage , out var statusCode , out var statusMessage , out var content , out var headers ) )
156
- {
157
- var header = $ "HTTP/1.0 200 OK\r \n ";
158
- foreach ( var item in headers )
159
- {
160
- header += $ "{ item . Key } :{ item . Value } \r \n ";
161
- }
162
- header += "\r \n " ;
163
-
164
- using ( MemoryStream memstream = new MemoryStream ( ) )
165
- {
166
- content . CopyTo ( memstream ) ;
167
- var body = memstream . ToArray ( ) ;
168
- PlatformWebView . SetInterceptRequestResponse ( request , header , body , ( uint ) body . Length ) ;
169
- }
170
- return ;
171
- }
172
- }
173
-
174
- PlatformWebView . IgnoreInterceptRequest ( request ) ;
175
- }
176
-
177
- private void OnLoadFinished ( object ? sender , EventArgs e )
178
- {
179
- PlatformWebView . SetFocus ( true ) ;
180
- var url = PlatformWebView . Url ;
181
-
182
- if ( url == AppOrigin )
183
- PlatformWebView . Eval ( BlazorInitScript ) ;
184
- }
185
-
186
190
internal IFileProvider CreateFileProvider ( string contentRootDir )
187
191
{
188
192
return new TizenMauiAssetFileProvider ( contentRootDir ) ;
0 commit comments