This repository has been archived by the owner on Dec 17, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
BlazorGeckoViewRenderer.cs
109 lines (89 loc) · 3.96 KB
/
BlazorGeckoViewRenderer.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
using BlazorMobile.Components;
using BlazorMobile.Droid.Handler;
using BlazorMobile.Droid.Helper;
using BlazorMobile.Droid.Renderer;
using BlazorMobile.Droid.Services;
using BlazorMobile.Services;
using Org.Mozilla.Geckoview;
using System;
using Xam.Droid.GeckoView.Forms;
using Xam.Droid.GeckoView.Forms.Droid.Handlers;
using Xam.Droid.GeckoView.Forms.Droid.Renderers;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;
[assembly: ExportRenderer(typeof(BlazorGeckoView), typeof(BlazorGeckoViewRenderer))]
namespace BlazorMobile.Droid.Renderer
{
public class BlazorGeckoViewRenderer : GeckoViewRenderer
{
internal void BlazorForwardSendNavigating(WebNavigatingEventArgs args)
{
Element.SendNavigating(args);
}
private bool _firstCall = true;
public override Tuple<GeckoSession, GeckoRuntime> CreateNewSession(bool needSessionOpening, string uri)
{
if (!_firstCall)
{
GeckoSession newSession = new GeckoSession();
GeckoRuntime newRuntime = GeckoRuntime.GetDefault(Context);
//With our scenario this should not happen, but enforcing rule just for sanity check
if (needSessionOpening)
{
newSession.Open(newRuntime);
}
return new Tuple<GeckoSession, GeckoRuntime>(newSession, newRuntime);
}
var settings = new GeckoSessionSettings.Builder()
.UsePrivateMode(true) //Use private mode in order to never cache anything at each app session
.UseTrackingProtection(true)
.UserAgentMode(GeckoSessionSettings.UserAgentModeMobile)
.SuspendMediaWhenInactive(true)
.AllowJavascript(true)
.Build();
GeckoSession _session = new GeckoSession(settings);
GeckoRuntime _runtime = GeckoRuntime.GetDefault(Context);
//Register BlazorMobile iframe listener WebExtension, as GeckoView LoadRequest does not bubble up when navigating through an iFrame.
//NOTE: Delegate for WebExtension handling seem missing from current Xamarin.GeckoView generated bindings, but the handling will be workarounded through the local BlazorMobile server
//WARNING: With our implementation, registering a WebExtension is per WebView if the same runtime object is used, not per session
WebExtensionHelper.RegisterWebExtension(Element as BlazorGeckoView, _runtime, "resource://android/assets/obj/BlazorMobile/web_extensions/iframe_listener/");
if (needSessionOpening)
{
_session.Open(_runtime);
}
_session.PromptDelegate = new BlazorGeckoViewPromptDelegate(this);
_session.ProgressDelegate = new BlazorProgressDelegate(this);
_session.ContentDelegate = new BlazorContentDelegate(this);
_session.NavigationDelegate = new BlazorNavigationDelegate(this);
if (WebApplicationFactory._debugFeatures)
{
_runtime.Settings.SetRemoteDebuggingEnabled(true);
_runtime.Settings.SetConsoleOutputEnabled(true);
}
_firstCall = false;
return Tuple.Create(_session, _runtime);
}
private KeyboardUtil keyboardHelper;
protected override void OnElementChanged(ElementChangedEventArgs<GeckoViewForms> e)
{
if (Control == null)
{
_firstCall = true;
}
base.OnElementChanged(e);
if (keyboardHelper == null)
{
keyboardHelper = new KeyboardUtil(BlazorWebViewService.GetCurrentActivity(), Control);
}
}
protected override void Dispose(bool disposing)
{
if (keyboardHelper != null)
{
keyboardHelper.Disable();
}
base.Dispose();
}
}
}