CEFInterfaceSchemeHandler.cpp
6.0 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Engine/Source/Runtime/WebBrowser/Private/CEF/CEFSchemeHandler.cpp
#include "CEF/CEFInterfaceSchemeHandler.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
#include "IWebInterfaceBrowserSchemeHandler.h"
#if WITH_CEF3
class FHandlerHeaderSetter
: public IWebInterfaceBrowserSchemeHandler::IHeaders
{
public:
FHandlerHeaderSetter(CefRefPtr<CefResponse>& InResponse, int64& InContentLength, CefString& InRedirectUrl)
: Response(InResponse)
, ContentLength(InContentLength)
, RedirectUrl(InRedirectUrl)
, StatusCode(INDEX_NONE)
{
}
virtual ~FHandlerHeaderSetter()
{
if (Headers.size() > 0)
{
Response->SetHeaderMap(Headers);
}
if (StatusCode != INDEX_NONE)
{
Response->SetStatus(StatusCode);
}
if (MimeType.length() > 0)
{
Response->SetMimeType(MimeType);
}
}
virtual void SetMimeType(const TCHAR* InMimeType) override
{
MimeType = TCHAR_TO_WCHAR(InMimeType);
}
virtual void SetStatusCode(int32 InStatusCode) override
{
StatusCode = InStatusCode;
}
virtual void SetContentLength(int32 InContentLength) override
{
ContentLength = InContentLength;
}
virtual void SetRedirect(const TCHAR* InRedirectUrl) override
{
RedirectUrl = TCHAR_TO_WCHAR(InRedirectUrl);
}
virtual void SetHeader(const TCHAR* Key, const TCHAR* Value) override
{
Headers.insert(std::make_pair(CefString(TCHAR_TO_WCHAR(Key)), CefString(TCHAR_TO_WCHAR(Value))));
}
private:
CefRefPtr<CefResponse>& Response;
int64& ContentLength;
CefString& RedirectUrl;
CefResponse::HeaderMap Headers;
CefString MimeType;
int32 StatusCode;
};
class FCefInterfaceSchemeHandler
: public CefResourceHandler
{
public:
FCefInterfaceSchemeHandler(TUniquePtr<IWebInterfaceBrowserSchemeHandler>&& InHandlerImplementation)
: HandlerImplementation(MoveTemp(InHandlerImplementation))
{
}
virtual ~FCefInterfaceSchemeHandler()
{
}
// Begin CefResourceHandler interface.
virtual bool ProcessRequest(CefRefPtr<CefRequest> Request, CefRefPtr<CefCallback> Callback) override
{
if (HandlerImplementation.IsValid())
{
return HandlerImplementation->ProcessRequest(
WCHAR_TO_TCHAR(Request->GetMethod().ToWString().c_str()),
WCHAR_TO_TCHAR(Request->GetURL().ToWString().c_str()),
FSimpleDelegate::CreateLambda([Callback](){ Callback->Continue(); })
);
}
return false;
}
virtual void GetResponseHeaders(CefRefPtr<CefResponse> Response, int64& ResponseLength, CefString& RedirectUrl) override
{
if (ensure(HandlerImplementation.IsValid()))
{
FHandlerHeaderSetter Headers(Response, ResponseLength, RedirectUrl);
HandlerImplementation->GetResponseHeaders(Headers);
}
}
virtual bool ReadResponse(void* DataOut, int BytesToRead, int& BytesRead, CefRefPtr<CefCallback> Callback) override
{
if (ensure(HandlerImplementation.IsValid()))
{
return HandlerImplementation->ReadResponse(
(uint8*)DataOut,
BytesToRead,
BytesRead,
FSimpleDelegate::CreateLambda([Callback](){ Callback->Continue(); })
);
}
BytesRead = 0;
return false;
}
virtual void Cancel() override
{
if (HandlerImplementation.IsValid())
{
HandlerImplementation->Cancel();
}
}
// End CefResourceHandler interface.
private:
TUniquePtr<IWebInterfaceBrowserSchemeHandler> HandlerImplementation;
// Include CEF ref counting.
IMPLEMENT_REFCOUNTING(FCefInterfaceSchemeHandler);
};
class FCefInterfaceSchemeHandlerFactory
: public CefSchemeHandlerFactory
{
public:
FCefInterfaceSchemeHandlerFactory(IWebInterfaceBrowserSchemeHandlerFactory* InWebBrowserSchemeHandlerFactory)
: WebBrowserSchemeHandlerFactory(InWebBrowserSchemeHandlerFactory)
{
}
// Begin CefSchemeHandlerFactory interface.
virtual CefRefPtr<CefResourceHandler> Create(CefRefPtr<CefBrowser> Browser, CefRefPtr<CefFrame> Frame, const CefString& Scheme, CefRefPtr<CefRequest> Request) override
{
return new FCefInterfaceSchemeHandler(WebBrowserSchemeHandlerFactory->Create(
WCHAR_TO_TCHAR(Request->GetMethod().ToWString().c_str()),
WCHAR_TO_TCHAR(Request->GetURL().ToWString().c_str())));
}
// End CefSchemeHandlerFactory interface.
bool IsUsing(IWebInterfaceBrowserSchemeHandlerFactory* InWebBrowserSchemeHandlerFactory)
{
return WebBrowserSchemeHandlerFactory == InWebBrowserSchemeHandlerFactory;
}
private:
IWebInterfaceBrowserSchemeHandlerFactory* WebBrowserSchemeHandlerFactory;
// Include CEF ref counting.
IMPLEMENT_REFCOUNTING(FCefInterfaceSchemeHandlerFactory);
};
void FCefInterfaceSchemeHandlerFactories::AddSchemeHandlerFactory(FString Scheme, FString Domain, IWebInterfaceBrowserSchemeHandlerFactory* WebBrowserSchemeHandlerFactory)
{
checkf(WebBrowserSchemeHandlerFactory != nullptr, TEXT("WebBrowserSchemeHandlerFactory must be provided."));
CefRefPtr<CefSchemeHandlerFactory> Factory = new FCefInterfaceSchemeHandlerFactory(WebBrowserSchemeHandlerFactory);
CefRegisterSchemeHandlerFactory(TCHAR_TO_WCHAR(*Scheme), TCHAR_TO_WCHAR(*Domain), Factory);
SchemeHandlerFactories.Emplace(MoveTemp(Scheme), MoveTemp(Domain), MoveTemp(Factory));
}
void FCefInterfaceSchemeHandlerFactories::RemoveSchemeHandlerFactory(IWebInterfaceBrowserSchemeHandlerFactory* WebBrowserSchemeHandlerFactory)
{
checkf(WebBrowserSchemeHandlerFactory != nullptr, TEXT("WebBrowserSchemeHandlerFactory must be provided."));
SchemeHandlerFactories.RemoveAll([WebBrowserSchemeHandlerFactory](const FFactory& Element)
{
return ((FCefInterfaceSchemeHandlerFactory*)Element.Factory.get())->IsUsing(WebBrowserSchemeHandlerFactory);
});
}
void FCefInterfaceSchemeHandlerFactories::RegisterFactoriesWith(CefRefPtr<CefRequestContext>& Context)
{
if (Context)
{
for (const FFactory& SchemeHandlerFactory : SchemeHandlerFactories)
{
Context->RegisterSchemeHandlerFactory(TCHAR_TO_WCHAR(*SchemeHandlerFactory.Scheme), TCHAR_TO_WCHAR(*SchemeHandlerFactory.Domain), SchemeHandlerFactory.Factory);
}
}
}
FCefInterfaceSchemeHandlerFactories::FFactory::FFactory(FString InScheme, FString InDomain, CefRefPtr<CefSchemeHandlerFactory> InFactory)
: Scheme(MoveTemp(InScheme))
, Domain(MoveTemp(InDomain))
, Factory(MoveTemp(InFactory))
{
}
#endif