Nessuna descrizione
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.

ResourceRequestHandlerFactory.cs 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright © 2014 The CefSharp Authors. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  4. using System;
  5. using System.Collections.Concurrent;
  6. using System.Collections.Generic;
  7. using CefSharp.Internals;
  8. namespace CefSharp
  9. {
  10. /// <summary>
  11. /// Default implementation of <see cref="IResourceRequestHandlerFactory"/> it's used
  12. /// internally for the LoadHtml implementation - basically a resource handler is
  13. /// registered for a specific Url.
  14. /// </summary>
  15. public class ResourceRequestHandlerFactory : IResourceRequestHandlerFactory
  16. {
  17. /// <summary>
  18. /// Resource handler thread safe dictionary
  19. /// </summary>
  20. public ConcurrentDictionary<string, ResourceRequestHandlerFactoryItem> Handlers { get; private set; }
  21. /// <summary>
  22. /// Create a new instance of DefaultResourceHandlerFactory
  23. /// </summary>
  24. /// <param name="comparer">string equality comparer</param>
  25. public ResourceRequestHandlerFactory(IEqualityComparer<string> comparer = null)
  26. {
  27. Handlers = new ConcurrentDictionary<string, ResourceRequestHandlerFactoryItem>(comparer ?? StringComparer.OrdinalIgnoreCase);
  28. }
  29. /// <summary>
  30. /// Register a handler for the specified Url
  31. /// </summary>
  32. /// <param name="url">url</param>
  33. /// <param name="data">The data in byte[] format that will be used for the response</param>
  34. /// <param name="mimeType">mime type</param>
  35. /// <param name="oneTimeUse">Whether or not the handler should be used once (true) or until manually unregistered (false)</param>
  36. /// <returns>returns true if the Url was successfully parsed into a Uri otherwise false</returns>
  37. public virtual bool RegisterHandler(string url, byte[] data, string mimeType = ResourceHandler.DefaultMimeType, bool oneTimeUse = false)
  38. {
  39. Uri uri;
  40. if (Uri.TryCreate(url, UriKind.Absolute, out uri))
  41. {
  42. var entry = new ResourceRequestHandlerFactoryItem(data, mimeType, oneTimeUse);
  43. Handlers.AddOrUpdate(uri.AbsoluteUri, entry, (k, v) => entry);
  44. return true;
  45. }
  46. return false;
  47. }
  48. /// <summary>
  49. /// Unregister a handler for the specified Url
  50. /// </summary>
  51. /// <param name="url">Url</param>
  52. /// <returns>returns true if successfully removed</returns>
  53. public virtual bool UnregisterHandler(string url)
  54. {
  55. return Handlers.TryRemove(url, out _);
  56. }
  57. /// <summary>
  58. /// Are there any <see cref="ResourceHandler"/>'s registered?
  59. /// </summary>
  60. bool IResourceRequestHandlerFactory.HasHandlers
  61. {
  62. get { return Handlers.Count > 0; }
  63. }
  64. /// <inheritdoc />
  65. IResourceRequestHandler IResourceRequestHandlerFactory.GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
  66. {
  67. return GetResourceRequestHandler(chromiumWebBrowser, browser, frame, request, isNavigation, isDownload, requestInitiator, ref disableDefaultHandling);
  68. }
  69. /// <summary>
  70. /// Called on the CEF IO thread before a resource request is initiated.
  71. /// </summary>
  72. /// <param name="chromiumWebBrowser">the ChromiumWebBrowser control</param>
  73. /// <param name="browser">represent the source browser of the request</param>
  74. /// <param name="frame">represent the source frame of the request</param>
  75. /// <param name="request">represents the request contents and cannot be modified in this callback</param>
  76. /// <param name="isNavigation">will be true if the resource request is a navigation</param>
  77. /// <param name="isDownload">will be true if the resource request is a download</param>
  78. /// <param name="requestInitiator">is the origin (scheme + domain) of the page that initiated the request</param>
  79. /// <param name="disableDefaultHandling">to true to disable default handling of the request, in which case it will need to be handled via <see cref="IResourceRequestHandler.GetResourceHandler"/> or it will be canceled</param>
  80. /// <returns>To allow the resource load to proceed with default handling return null. To specify a handler for the resource return a <see cref="IResourceRequestHandler"/> object. If this callback returns null the same method will be called on the associated <see cref="IRequestContextHandler"/>, if any</returns>
  81. protected virtual IResourceRequestHandler GetResourceRequestHandler(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool isNavigation, bool isDownload, string requestInitiator, ref bool disableDefaultHandling)
  82. {
  83. try
  84. {
  85. ResourceRequestHandlerFactoryItem entry;
  86. if (Handlers.TryGetValue(request.Url, out entry))
  87. {
  88. if (entry.OneTimeUse)
  89. {
  90. Handlers.TryRemove(request.Url, out entry);
  91. }
  92. return new InMemoryResourceRequestHandler(entry.Data, entry.MimeType);
  93. }
  94. return null;
  95. }
  96. finally
  97. {
  98. request.Dispose();
  99. }
  100. }
  101. }
  102. }