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.
 
 
 
 

105 regels
4.6 KiB

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