Repository navigation
Expand file tree
/
Copy pathDefaultResourceHandlerFactory.cs
More file actions
91 lines (83 loc) · 3.42 KB
/
Copy pathDefaultResourceHandlerFactory.cs
File metadata and controls
91 lines (83 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright © 2010-2016 The CefSharp Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
namespace CefSharp
{
/// <summary>
/// Default implementation of <see cref="IResourceHandlerFactory"/> it's used
/// internally for the LoadHtml implementation - basically a resource handler is
/// registered for a specific Url.
/// </summary>
public class DefaultResourceHandlerFactory : IResourceHandlerFactory
{
/// <summary>
/// Resource handler thread safe dictionary
/// </summary>
public ConcurrentDictionary<string, IResourceHandler> Handlers { get; private set; }
/// <summary>
/// Create a new instance of DefaultResourceHandlerFactory
/// </summary>
/// <param name="comparer">string equality comparer</param>
public DefaultResourceHandlerFactory(IEqualityComparer<string> comparer = null)
{
Handlers = new ConcurrentDictionary<string, IResourceHandler>(comparer ?? StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Register handler with the specified Url
/// </summary>
/// <param name="url">url</param>
/// <param name="handler">handler</param>
/// <returns>returns true if the Url was successfully parsed into a Uri otherwise false</returns>
public virtual bool RegisterHandler(string url, IResourceHandler handler)
{
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
Handlers.AddOrUpdate(uri.ToString(), handler, (k, v) => handler);
return true;
}
return false;
}
/// <summary>
/// Unregister a handler for the specified Url
/// </summary>
/// <param name="url">Url</param>
/// <returns>returns true if successfully removed</returns>
public virtual bool UnregisterHandler(string url)
{
IResourceHandler handler;
return Handlers.TryRemove(url, out handler);
}
/// <summary>
/// Are there any <see cref="ResourceHandler"/>'s registered?
/// </summary>
public bool HasHandlers
{
get { return Handlers.Count > 0; }
}
/// <summary>
/// Called before a resource is loaded. To specify a handler for the resource return a <see cref="ResourceHandler"/> object
/// </summary>
/// <param name="browserControl">The browser UI control</param>
/// <param name="browser">the browser object</param>
/// <param name="frame">the frame object</param>
/// <param name="request">the request object - cannot be modified in this callback</param>
/// <returns>To allow the resource to load normally return NULL otherwise return an instance of ResourceHandler with a valid stream</returns>
public virtual IResourceHandler GetResourceHandler(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request)
{
try
{
IResourceHandler handler;
Handlers.TryGetValue(request.Url, out handler);
return handler;
}
finally
{
request.Dispose();
}
}
}
}