Skip to content

Commit 1531ca4

Browse files
committed
refactor: windows web browser client interface
1 parent 2f1647f commit 1531ca4

File tree

12 files changed

+225
-86
lines changed

12 files changed

+225
-86
lines changed

sample/Assets/Scripts/SelectAuthMethodScript.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ private async void InitialisePassport(string redirectUri = null, string logoutRe
7979
}
8080
catch (Exception ex)
8181
{
82-
Debug.Log(ex);
8382
ShowOutput($"Initialise Passport error: {ex.Message}");
8483
}
8584
}

src/Packages/Passport/Runtime/Resources/index.html

Lines changed: 19 additions & 19 deletions
Large diffs are not rendered by default.

src/Packages/Passport/Runtime/Scripts/Public/Passport.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public static UniTask<Passport> Init(
6969
string redirectUri = null,
7070
string logoutRedirectUri = null,
7171
int engineStartupTimeoutMs = 30000,
72-
WindowsWebBrowserClient webBrowserClient = null
72+
IWindowsWebBrowserClient windowsWebBrowserClient = null
7373
#else
7474
string clientId,
7575
string environment,
@@ -86,7 +86,7 @@ public static UniTask<Passport> Init(
8686
// Start initialisation process
8787
return Instance.Initialise(
8888
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
89-
engineStartupTimeoutMs, webBrowserClient
89+
engineStartupTimeoutMs, windowsWebBrowserClient
9090
#endif
9191
)
9292
.ContinueWith(async () =>
@@ -125,19 +125,19 @@ public static UniTask<Passport> Init(
125125
/// <param name="webBrowserClient">(Windows only) Custom Windows browser to use instead of the default browser in the SDK.</param>
126126
private async UniTask Initialise(
127127
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
128-
int engineStartupTimeoutMs, WindowsWebBrowserClient webBrowserClient
128+
int engineStartupTimeoutMs, IWindowsWebBrowserClient windowsWebBrowserClient
129129
#endif
130130
)
131131
{
132132
try
133133
{
134134
// Initialise the web browser client
135135
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
136-
if (webBrowserClient != null)
136+
if (windowsWebBrowserClient != null)
137137
{
138138
// Use the provided custom Windows browser client
139-
this.webBrowserClient = webBrowserClient;
140-
await ((WindowsWebBrowserClient)this.webBrowserClient).Init();
139+
this.webBrowserClient = new WindowsWebBrowserClientAdapter(windowsWebBrowserClient);
140+
await ((WindowsWebBrowserClientAdapter)this.webBrowserClient).Init();
141141
}
142142
else
143143
{

src/Packages/Passport/Runtime/ThirdParty/Gree/Assets/Plugins/GreeBrowserClient.cs

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,8 @@ public GreeBrowserClient()
3131
auth: InvokeOnAuthPostMessage,
3232
log: InvokeOnLogMessage
3333
);
34-
#if UNITY_ANDROID && !UNITY_EDITOR
35-
string filePath = Constants.SCHEME_FILE + ANDROID_DATA_DIRECTORY + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
36-
#elif UNITY_EDITOR_OSX
37-
string filePath = Constants.SCHEME_FILE + Path.GetFullPath(MAC_EDITOR_RESOURCES_DIRECTORY) + Constants.PASSPORT_HTML_FILE_NAME;
38-
#elif UNITY_STANDALONE_OSX
39-
string filePath = Constants.SCHEME_FILE + Path.GetFullPath(Application.dataPath) + MAC_DATA_DIRECTORY + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
40-
filePath = filePath.Replace(" ", "%20");
41-
#elif UNITY_IPHONE
42-
string filePath = Path.GetFullPath(Application.dataPath) + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
43-
#else
44-
string filePath = Constants.SCHEME_FILE + Path.GetFullPath(Application.dataPath) + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
45-
#endif
46-
webViewObject.LoadURL(filePath);
34+
35+
webViewObject.LoadURL(GameBridge.GetFilePath());
4736
}
4837

4938
private void InvokeOnPostMessageError(string id, string message)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.IO;
2+
using UnityEngine;
3+
4+
namespace Immutable.Browser.Core
5+
{
6+
public static class GameBridge
7+
{
8+
public static string GetFilePath()
9+
{
10+
string filePath = "";
11+
#if UNITY_ANDROID && !UNITY_EDITOR
12+
// Android device
13+
filePath = Constants.SCHEME_FILE + ANDROID_DATA_DIRECTORY + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
14+
#elif UNITY_EDITOR_OSX
15+
// macOS editor
16+
filePath = Constants.SCHEME_FILE + Path.GetFullPath(MAC_EDITOR_RESOURCES_DIRECTORY) + Constants.PASSPORT_HTML_FILE_NAME;
17+
#elif UNITY_STANDALONE_OSX
18+
// macOS
19+
filePath = Constants.SCHEME_FILE + Path.GetFullPath(Application.dataPath) + MAC_DATA_DIRECTORY + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
20+
filePath = filePath.Replace(" ", "%20");
21+
#elif UNITY_IPHONE
22+
// iOS device
23+
filePath = Path.GetFullPath(Application.dataPath) + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
24+
#elif UNITY_EDITOR_WIN
25+
// Windows editor
26+
filePath = Constants.SCHEME_FILE + Path.GetFullPath($"{Constants.PASSPORT_PACKAGE_RESOURCES_DIRECTORY}{Constants.PASSPORT_HTML_FILE_NAME}");
27+
#else
28+
filePath = Constants.SCHEME_FILE + Path.GetFullPath(Application.dataPath) + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
29+
#endif
30+
return filePath;
31+
}
32+
}
33+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
2+
3+
using System.IO;
4+
using UnityEngine;
5+
using Immutable.Browser.Core;
6+
using Cysharp.Threading.Tasks;
7+
8+
namespace Immutable.Browser.Core
9+
{
10+
/// <summary>
11+
/// Interface for custom Windows Web Browser Client
12+
/// </summary>
13+
public interface IWindowsWebBrowserClient
14+
{
15+
/// <summary>
16+
/// Event triggered when the JavaScript API defined in <see cref="GetPostMessageApiCall"/> posts a message
17+
/// to the Unity application.
18+
/// </summary>
19+
/// <example>
20+
/// <code>
21+
/// // Example implementation for Vuplex Windows WebView:
22+
/// public event OnUnityPostMessageDelegate OnUnityPostMessage;
23+
///
24+
/// vuplexWebView.MessageEmitted += (sender, eventArgs) =>
25+
/// {
26+
/// OnUnityPostMessage?.Invoke(eventArgs.Value);
27+
/// };
28+
/// </code>
29+
/// </example>
30+
event OnUnityPostMessageDelegate OnUnityPostMessage;
31+
32+
/// <summary>
33+
/// Initialises the Windows Web Browser Client
34+
/// </summary>
35+
/// <returns></returns>
36+
UniTask Init();
37+
38+
/// <summary>
39+
/// Loads the specified URL.
40+
/// </summary>
41+
/// <param name="url">The URL to load.</param>
42+
void LoadUrl(string url);
43+
44+
/// <summary>
45+
/// Executes the specified JavaScript code in the web browser.
46+
/// </summary>
47+
/// <param name="javaScript">The JavaScript code to execute.</param>
48+
void ExecuteJavaScript(string javaScript);
49+
50+
/// <summary>
51+
/// Returns the JavaScript API call for sending messages from the web page to the Unity application.
52+
/// </summary>
53+
/// <remarks>
54+
/// This API call must take exactly one argument of type string.
55+
/// Implementers should provide the appropriate JavaScript function for their web browser client.
56+
/// </remarks>
57+
/// <returns>
58+
/// A string representing the JavaScript API call for sending messages from the web page to the Unity application.
59+
/// </returns>
60+
/// <example>
61+
/// <code>
62+
/// // Example implementation for Vuplex Windows WebView:
63+
/// public string GetPostMessageApiCall() {
64+
/// return "window.vuplex.postMessage";
65+
/// }
66+
/// </code>
67+
/// </example>
68+
string GetPostMessageApiCall();
69+
70+
/// <summary>
71+
/// Destroys the web browser client and releases all of its resources.
72+
/// </summary>
73+
void Dispose();
74+
}
75+
}
76+
77+
#endif

src/Packages/Passport/Runtime/ThirdParty/ImmutableBrowserCore/IWindowsWebBrowserClient.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/Passport/Runtime/ThirdParty/ImmutableBrowserCore/WindowsWebBrowserClient.cs

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#if UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
2+
3+
using System.IO;
4+
using UnityEngine;
5+
using Immutable.Browser.Core;
6+
using Cysharp.Threading.Tasks;
7+
8+
namespace Immutable.Browser.Core
9+
{
10+
public class WindowsWebBrowserClientAdapter : IWebBrowserClient
11+
{
12+
public event OnUnityPostMessageDelegate OnUnityPostMessage;
13+
14+
private readonly IWindowsWebBrowserClient webBrowserClient;
15+
16+
public WindowsWebBrowserClientAdapter(IWindowsWebBrowserClient windowsWebBrowserClient)
17+
{
18+
webBrowserClient = windowsWebBrowserClient;
19+
20+
// Listen to messages from the web browser client
21+
windowsWebBrowserClient.OnUnityPostMessage += (message) =>
22+
{
23+
OnUnityPostMessage?.Invoke(message);
24+
};
25+
}
26+
27+
/// <summary>
28+
/// Initialises the web browser client, loads the game bridge file, and sets up the UnityPostMessage function.
29+
/// </summary>
30+
public async UniTask Init()
31+
{
32+
// Initialise the web browser client asynchronously
33+
await webBrowserClient.Init();
34+
35+
// Load the game bridge file into the web browser client
36+
webBrowserClient.LoadUrl(GameBridge.GetFilePath());
37+
38+
// Get the JavaScript API call for posting messages from the web page to the Unity application
39+
string postMessageApiCall = webBrowserClient.GetPostMessageApiCall();
40+
41+
// Inject a JavaScript function named UnityPostMessage into the web page
42+
// This function takes a message as an argument and calls the post message API
43+
webBrowserClient.ExecuteJavaScript($"function UnityPostMessage(message) {{ {postMessageApiCall}(message); }}");
44+
}
45+
46+
47+
public void ExecuteJs(string js)
48+
{
49+
webBrowserClient.ExecuteJavaScript(js);
50+
}
51+
52+
public void LaunchAuthURL(string url, string? redirectUri)
53+
{
54+
Application.OpenURL(url);
55+
}
56+
57+
public void Dispose()
58+
{
59+
webBrowserClient.Dispose();
60+
}
61+
}
62+
}
63+
64+
#endif

src/Packages/Passport/Runtime/ThirdParty/ImmutableBrowserCore/WindowsWebBrowserClientAdapter.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/Passport/Runtime/ThirdParty/UnityWebBrowser/Runtime/Core/WebBrowserClient.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -316,13 +316,7 @@ public async UniTask Init(int engineStartupTimeout = 30000)
316316
WebBrowserArgsBuilder argsBuilder = new();
317317

318318
//Initial URL
319-
string filePath = "";
320-
#if UNITY_EDITOR
321-
filePath = Constants.SCHEME_FILE + Path.GetFullPath($"{Constants.PASSPORT_PACKAGE_RESOURCES_DIRECTORY}{Constants.PASSPORT_HTML_FILE_NAME}");
322-
#elif UNITY_STANDALONE_WIN || (UNITY_ANDROID && UNITY_EDITOR_WIN) || (UNITY_IPHONE && UNITY_EDITOR_WIN)
323-
filePath = Constants.SCHEME_FILE + Path.GetFullPath(Application.dataPath) + Constants.PASSPORT_DATA_DIRECTORY_NAME + Constants.PASSPORT_HTML_FILE_NAME;
324-
#endif
325-
initialUrl = filePath;
319+
initialUrl = GameBridge.GetFilePath();
326320
argsBuilder.AppendArgument("initial-url", initialUrl, true);
327321

328322
//Width & Height

0 commit comments

Comments
 (0)