CustomScheme.cs 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Linq;
  3. namespace Xilium.CefGlue.Common.Shared
  4. {
  5. public class CustomScheme
  6. {
  7. private const string CommandLineSchemesSeparator = ";";
  8. private const string CommandLinePropertiesSeparator = "|";
  9. /// <summary>
  10. /// Gets or sets schema Name e.g. custom
  11. /// </summary>
  12. public string SchemeName { get; set; }
  13. /// <summary>
  14. /// Optional Domain Name. An empty value for a standard scheme
  15. /// will cause the factory to match all domain names. The |domain_name| value
  16. /// will be ignored for non-standard schemes.
  17. /// </summary>
  18. public string DomainName { get; set; }
  19. /// <summary>
  20. /// If true the scheme will be treated as a standard scheme.
  21. /// Standard schemes are subject to URL canonicalization and parsing rules as
  22. /// defined in the Common Internet Scheme Syntax RFC 1738 Section 3.1 available
  23. /// at http://www.ietf.org/rfc/rfc1738.txt
  24. /// In particular, the syntax for standard scheme URLs must be of the form:
  25. /// <pre>
  26. /// [scheme]://[username]:[password]@[host]:[port]/[url-path]
  27. /// </pre>
  28. /// Standard scheme URLs must have a host component that is a fully qualified
  29. /// domain name as defined in Section 3.5 of RFC 1034 [13] and Section 2.1 of
  30. /// RFC 1123. These URLs will be canonicalized to "scheme://host/path" in the
  31. /// simplest case and "scheme://username:password@host:port/path" in the most
  32. /// explicit case. For example, "scheme:host/path" and "scheme:///host/path"
  33. /// will both be canonicalized to "scheme://host/path". The origin of a
  34. /// standard scheme URL is the combination of scheme, host and port (i.e.,
  35. /// "scheme://host:port" in the most explicit case).
  36. /// For non-standard scheme URLs only the "scheme:" component is parsed and
  37. /// canonicalized. The remainder of the URL will be passed to the handler
  38. /// as-is. For example, "scheme:///some%20text" will remain the same.
  39. /// Non-standard scheme URLs cannot be used as a target for form submission.
  40. /// </summary>
  41. public bool IsStandard { get; set; }
  42. /// <summary>
  43. /// If true the scheme will be treated with the same security
  44. /// rules as those applied to "file" URLs. Normal pages cannot link to or
  45. /// access local URLs. Also, by default, local URLs can only perform
  46. /// XMLHttpRequest calls to the same URL (origin + path) that originated the
  47. /// request. To allow XMLHttpRequest calls from a local URL to other URLs with
  48. /// the same origin set the CefSettings.file_access_from_file_urls_allowed
  49. /// value to true. To allow XMLHttpRequest calls from a local URL to all
  50. /// origins set the CefSettings.universal_access_from_file_urls_allowed value
  51. /// to true.
  52. /// </summary>
  53. public bool IsLocal { get; set; }
  54. /// <summary>
  55. /// If true the scheme can only be displayed from
  56. /// other content hosted with the same scheme. For example, pages in other
  57. /// origins cannot create iframes or hyperlinks to URLs with the scheme. For
  58. /// schemes that must be accessible from other schemes set this value to false,
  59. /// set |is_cors_enabled| to true, and use CORS "Access-Control-Allow-Origin"
  60. /// headers to further restrict access.
  61. /// </summary>
  62. public bool IsDisplayIsolated { get; set; }
  63. /// <summary>
  64. /// If true the scheme will be treated with the same security
  65. /// rules as those applied to "https" URLs. For example, loading this scheme
  66. /// from other secure schemes will not trigger mixed content warnings.
  67. /// </summary>
  68. public bool IsSecure { get; set; }
  69. /// <summary>
  70. /// If true the scheme can be sent CORS requests. This
  71. /// value should be true in most cases where |is_standard| is true.
  72. /// </summary>
  73. public bool IsCorsEnabled { get; set; }
  74. /// <summary>
  75. /// If true the scheme can bypass Content-Security-Policy
  76. /// (CSP) checks. This value should be false in most cases where |is_standard|
  77. /// is true.
  78. /// </summary>
  79. public bool IsCSPBypassing { get; set; }
  80. /// <summary>
  81. /// If ture the scheme can perform Fetch API requests.
  82. /// </summary>
  83. public bool IsFetchEnabled { get; set; }
  84. /// <summary>
  85. /// Factory class that creates <see cref="CefResourceHandler"/> instances
  86. /// for handling current scheme requests.
  87. /// </summary>
  88. public CefSchemeHandlerFactory SchemeHandlerFactory { get; set; }
  89. /// <summary>
  90. /// Creates a new CustomScheme.
  91. /// </summary>
  92. public CustomScheme()
  93. {
  94. IsStandard = true;
  95. IsLocal = false;
  96. IsDisplayIsolated = false;
  97. IsSecure = true;
  98. IsCorsEnabled = true;
  99. IsCSPBypassing = false;
  100. IsFetchEnabled = true;
  101. }
  102. public CefSchemeOptions Options
  103. {
  104. get
  105. {
  106. var options = CefSchemeOptions.None;
  107. if (IsStandard)
  108. {
  109. options |= CefSchemeOptions.Standard;
  110. }
  111. if (IsLocal)
  112. {
  113. options |= CefSchemeOptions.Local;
  114. }
  115. if (IsSecure)
  116. {
  117. options |= CefSchemeOptions.Secure;
  118. }
  119. if (IsDisplayIsolated)
  120. {
  121. options |= CefSchemeOptions.DisplayIsolated;
  122. }
  123. if (IsCorsEnabled)
  124. {
  125. options |= CefSchemeOptions.CorsEnabled;
  126. }
  127. if (IsCSPBypassing)
  128. {
  129. options |= CefSchemeOptions.CspBypassing;
  130. }
  131. if (IsFetchEnabled)
  132. {
  133. options |= CefSchemeOptions.FetchEnabled;
  134. }
  135. return options;
  136. }
  137. }
  138. private string SerializeToCommandLineValue()
  139. {
  140. return SchemeName + CommandLinePropertiesSeparator + DomainName + CommandLinePropertiesSeparator + ((int)Options).ToString();
  141. }
  142. private static CustomScheme DeserializeFromCommandLineValue(string value)
  143. {
  144. var tokens = value.Split(new string[] { CommandLinePropertiesSeparator }, StringSplitOptions.None);
  145. if (tokens.Length < 3)
  146. {
  147. return null;
  148. }
  149. Enum.TryParse<CefSchemeOptions>(tokens[2], out var properties);
  150. return new CustomScheme()
  151. {
  152. SchemeName = tokens[0],
  153. DomainName = tokens[1],
  154. IsStandard = properties.HasFlag(CefSchemeOptions.Standard),
  155. IsLocal = properties.HasFlag(CefSchemeOptions.Local),
  156. IsDisplayIsolated = properties.HasFlag(CefSchemeOptions.DisplayIsolated),
  157. IsSecure = properties.HasFlag(CefSchemeOptions.Secure),
  158. IsCorsEnabled = properties.HasFlag(CefSchemeOptions.CorsEnabled),
  159. IsCSPBypassing = properties.HasFlag(CefSchemeOptions.CspBypassing),
  160. IsFetchEnabled = properties.HasFlag(CefSchemeOptions.FetchEnabled)
  161. };
  162. }
  163. internal static string ToCommandLineValue(CustomScheme[] schemes)
  164. {
  165. return string.Join(CommandLineSchemesSeparator, schemes.Select(s => s.SerializeToCommandLineValue()));
  166. }
  167. internal static CustomScheme[] FromCommandLineValue(string value)
  168. {
  169. var schemes = value?.Split(new string[] { CommandLineSchemesSeparator }, StringSplitOptions.RemoveEmptyEntries);
  170. if (schemes == null)
  171. {
  172. return new CustomScheme[0];
  173. }
  174. return schemes.Select(DeserializeFromCommandLineValue).Where(s => s != null).ToArray();
  175. }
  176. }
  177. }