Aucune description
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

UniWebViewChannelMethodManager.cs 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // UniWebViewChannelMethodManager.cs
  3. // Created by Wang Wei(@onevcat) on 2023-04-21.
  4. //
  5. // This file is a part of UniWebView Project (https://uniwebview.com)
  6. // By purchasing the asset, you are allowed to use this code in as many as projects
  7. // you want, only if you publish the final products under the name of the same account
  8. // used for the purchase.
  9. //
  10. // This asset and all corresponding files (such as source code) are provided on an
  11. // “as is” basis, without warranty of any kind, express of implied, including but not
  12. // limited to the warranties of merchantability, fitness for a particular purpose, and
  13. // noninfringement. In no event shall the authors or copyright holders be liable for any
  14. // claim, damages or other liability, whether in action of contract, tort or otherwise,
  15. // arising from, out of or in connection with the software or the use of other dealing in the software.
  16. //
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using UnityEngine;
  21. enum UniWebViewChannelMethod {
  22. ShouldUniWebViewHandleRequest
  23. }
  24. class UniWebViewChannelMethodManager {
  25. private static UniWebViewChannelMethodManager instance;
  26. private Dictionary<string, Dictionary<string, Func<object, object>>> channels =
  27. new Dictionary<string, Dictionary<string, Func<object, object>>>();
  28. internal static UniWebViewChannelMethodManager Instance {
  29. get {
  30. if (instance == null) {
  31. instance = new UniWebViewChannelMethodManager();
  32. }
  33. return instance;
  34. }
  35. }
  36. internal void RegisterChannelMethod(
  37. string webViewName,
  38. UniWebViewChannelMethod method,
  39. Func<object, object> handler)
  40. {
  41. if (!HasRegisteredChannel(webViewName)) {
  42. channels[webViewName] = new Dictionary<string, Func<object, object>>();
  43. }
  44. var methodName = method.ToString();
  45. channels[webViewName][methodName] = handler;
  46. UniWebViewLogger.Instance.Info("Channel method is registered for web view: " + webViewName + ", method: " +
  47. methodName);
  48. }
  49. internal void UnregisterChannel(string webViewName) {
  50. if (!HasRegisteredChannel(webViewName)) {
  51. return;
  52. }
  53. channels.Remove(webViewName);
  54. UniWebViewLogger.Instance.Debug("All channel methods are unregistered for web view: " + webViewName);
  55. }
  56. internal void UnregisterChannelMethod(string webViewName, UniWebViewChannelMethod method) {
  57. var methodName = method.ToString();
  58. if (!HasRegisteredChannel(webViewName)) {
  59. return;
  60. }
  61. channels[webViewName].Remove(methodName);
  62. UniWebViewLogger.Instance.Debug("Channel method is unregistered for web view: " + webViewName + ", method: " +
  63. methodName);
  64. }
  65. bool HasRegisteredChannel(string webViewName) {
  66. return channels.Keys.Contains(webViewName);
  67. }
  68. bool HasRegisteredMethod(string webViewName, string methodName) {
  69. if (!HasRegisteredChannel(webViewName)) {
  70. return false;
  71. }
  72. return channels[webViewName].Keys.Contains(methodName);
  73. }
  74. internal string InvokeMethod(string webViewName, string methodName, string parameters) {
  75. if (!HasRegisteredMethod(webViewName, methodName)) {
  76. UniWebViewLogger.Instance.Info("There is no handler for the channel method. Ignoring.");
  77. return null;
  78. }
  79. var func = channels[webViewName][methodName];
  80. if (!Enum.TryParse<UniWebViewChannelMethod>(methodName, out var method)) {
  81. UniWebViewLogger.Instance.Info("Unknown method name: " + methodName + ". Please check, ignoring.");
  82. return null;
  83. }
  84. UniWebViewLogger.Instance.Verbose("Channel method invoking received for web view: " + webViewName + ", method: " +
  85. methodName + ", parameters: " + parameters);
  86. switch (method) {
  87. case UniWebViewChannelMethod.ShouldUniWebViewHandleRequest:
  88. // (UniWebViewChannelMethodHandleRequest) -> bool
  89. var input = JsonUtility.FromJson<UniWebViewChannelMethodHandleRequest>(parameters);
  90. bool Func(UniWebViewChannelMethodHandleRequest i) => (bool)func(i);
  91. var result = ResultJsonWith(Func(input));
  92. UniWebViewLogger.Instance.Debug("Channel method handler responded. Result: " + result);
  93. return result;
  94. default:
  95. return null;
  96. }
  97. }
  98. string ResultJsonWith(bool value) {
  99. return value ? "{\"result\":true}" : "{\"result\":false}";
  100. }
  101. }