Ei kuvausta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UniWebViewMessage.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //
  2. // UniWebViewMessage.cs
  3. // Created by Wang Wei(@onevcat) on 2017-05-12.
  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.Collections.Generic;
  18. using UnityEngine;
  19. using UnityEngine.Networking;
  20. #if UNITY_2017_3_OR_NEWER
  21. using Net = UnityEngine.Networking.UnityWebRequest;
  22. #else
  23. using Net = UnityEngine.WWW;
  24. #endif
  25. /// <summary>
  26. /// A structure represents a message from webview.
  27. /// </summary>
  28. public struct UniWebViewMessage {
  29. /// <summary>
  30. /// Gets the raw message. It is the original url which initialized this message.
  31. /// </summary>
  32. public string RawMessage {get; private set;}
  33. /// <summary>
  34. /// The url scheme of this UniWebViewMessage. "uniwebview" was added to message scheme list
  35. /// by default. You can add your own scheme by using `UniWebView.AddUrlScheme`.
  36. /// </summary>
  37. public string Scheme {get; private set;}
  38. /// <summary>
  39. /// The path of this UniWebViewMessage.
  40. /// This will be the decoded value for the path of original url.
  41. /// </summary>
  42. public string Path {get; private set;}
  43. /// <summary>
  44. /// The arguments of this UniWebViewMessage.
  45. ///
  46. /// When received url "uniwebview://yourPath?param1=value1&param2=value2",
  47. /// the args is a Dictionary with: Args["param1"] = value1, Args["param2"] = value2
  48. ///
  49. /// Both the key and valud will be url decoded from the original url.
  50. /// </summary>
  51. public Dictionary<string, string> Args{get; private set;}
  52. /// <summary>
  53. /// Initializes a new instance of the `UniWebViewMessage` struct.
  54. /// </summary>
  55. /// <param name="rawMessage">Raw message which will be parsed to a UniWebViewMessage.</param>
  56. public UniWebViewMessage(string rawMessage): this() {
  57. UniWebViewLogger.Instance.Debug("Try to parse raw message: " + rawMessage);
  58. this.RawMessage = rawMessage;
  59. string[] schemeSplit = rawMessage.Split(new string[] {"://"}, System.StringSplitOptions.None);
  60. if (schemeSplit.Length == 1) {
  61. // `://` not existing. Try `:/` instead.
  62. schemeSplit = rawMessage.Split(new string[] {":/"}, System.StringSplitOptions.None);
  63. }
  64. if (schemeSplit.Length == 1) {
  65. // `:/` not existing. Try `:` instead.
  66. schemeSplit = rawMessage.Split(new string[] {":"}, System.StringSplitOptions.None);
  67. }
  68. if (schemeSplit.Length >= 2) {
  69. this.Scheme = schemeSplit[0];
  70. UniWebViewLogger.Instance.Debug("Get scheme: " + this.Scheme);
  71. string pathAndArgsString = "";
  72. int index = 1;
  73. while (index < schemeSplit.Length) {
  74. pathAndArgsString = string.Concat(pathAndArgsString, schemeSplit[index]);
  75. index++;
  76. }
  77. UniWebViewLogger.Instance.Verbose("Build path and args string: " + pathAndArgsString);
  78. string[] split = pathAndArgsString.Split("?"[0]);
  79. this.Path = Net.UnEscapeURL(split[0].TrimEnd('/'));
  80. this.Args = new Dictionary<string, string>();
  81. if (split.Length > 1) {
  82. foreach (string pair in split[1].Split("&"[0])) {
  83. string[] elems = pair.Split("="[0]);
  84. if (elems.Length > 1) {
  85. var key = Net.UnEscapeURL(elems[0]);
  86. if (Args.ContainsKey(key)) {
  87. var existingValue = Args[key];
  88. Args[key] = existingValue + "," + Net.UnEscapeURL(elems[1]);
  89. } else {
  90. Args[key] = Net.UnEscapeURL(elems[1]);
  91. }
  92. UniWebViewLogger.Instance.Debug("Get arg, key: " + key + " value: " + Args[key]);
  93. }
  94. }
  95. }
  96. } else {
  97. UniWebViewLogger.Instance.Critical("Bad url scheme. Can not be parsed to UniWebViewMessage: " + rawMessage);
  98. }
  99. }
  100. }