2017-12-18 20:38:56 +02:00
|
|
|
unit uCEFServerHandler;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2017-12-18 20:38:56 +02:00
|
|
|
{$I cef.inc}
|
|
|
|
|
2022-02-19 19:56:41 +02:00
|
|
|
{$IFNDEF TARGET_64BITS}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
|
|
|
|
2017-12-18 20:38:56 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
|
|
|
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
|
|
|
|
|
|
|
|
type
|
|
|
|
TCEFServerHandlerOwn = class(TCefBaseRefCountedOwn, ICefServerHandler)
|
|
|
|
protected
|
|
|
|
procedure OnServerCreated(const server: ICefServer); virtual;
|
|
|
|
procedure OnServerDestroyed(const server: ICefServer); virtual;
|
|
|
|
procedure OnClientConnected(const server: ICefServer; connection_id: Integer); virtual;
|
|
|
|
procedure OnClientDisconnected(const server: ICefServer; connection_id: Integer); virtual;
|
|
|
|
procedure OnHttpRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest); virtual;
|
|
|
|
procedure OnWebSocketRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest; const callback: ICefCallback); virtual;
|
|
|
|
procedure OnWebSocketConnected(const server: ICefServer; connection_id: Integer); virtual;
|
|
|
|
procedure OnWebSocketMessage(const server: ICefServer; connection_id: Integer; const data: Pointer; data_size: NativeUInt); virtual;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TCustomServerHandler = class(TCEFServerHandlerOwn)
|
|
|
|
protected
|
2020-03-29 17:31:42 +02:00
|
|
|
FEvents : Pointer;
|
2017-12-18 20:38:56 +02:00
|
|
|
|
|
|
|
procedure OnServerCreated(const server: ICefServer); override;
|
|
|
|
procedure OnServerDestroyed(const server: ICefServer); override;
|
|
|
|
procedure OnClientConnected(const server: ICefServer; connection_id: Integer); override;
|
|
|
|
procedure OnClientDisconnected(const server: ICefServer; connection_id: Integer); override;
|
|
|
|
procedure OnHttpRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest); override;
|
|
|
|
procedure OnWebSocketRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest; const callback: ICefCallback); override;
|
|
|
|
procedure OnWebSocketConnected(const server: ICefServer; connection_id: Integer); override;
|
|
|
|
procedure OnWebSocketMessage(const server: ICefServer; connection_id: Integer; const data: Pointer; data_size: NativeUInt); override;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create(const events: IServerEvents); reintroduce;
|
|
|
|
destructor Destroy; override;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-02-03 18:52:48 +02:00
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.SysUtils,
|
|
|
|
{$ELSE}
|
|
|
|
SysUtils,
|
|
|
|
{$ENDIF}
|
2017-12-18 20:38:56 +02:00
|
|
|
uCEFMiscFunctions, uCEFLibFunctions, uCEFServer, uCEFRequest, uCEFCallback;
|
|
|
|
|
|
|
|
// **************************************************************
|
|
|
|
// ******************** TCEFServerHandlerOwn ********************
|
|
|
|
// **************************************************************
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_server_created(self : PCefServerHandler;
|
|
|
|
server : PCefServer); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnServerCreated(TCEFServerRef.UnWrap(server));
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_server_destroyed(self : PCefServerHandler;
|
|
|
|
server : PCefServer); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnServerDestroyed(TCEFServerRef.UnWrap(server));
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_client_connected(self : PCefServerHandler;
|
|
|
|
server : PCefServer;
|
|
|
|
connection_id : Integer); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnClientConnected(TCEFServerRef.UnWrap(server),
|
|
|
|
connection_id);
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_client_disconnected(self : PCefServerHandler;
|
|
|
|
server : PCefServer;
|
|
|
|
connection_id : Integer); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnClientDisconnected(TCEFServerRef.UnWrap(server),
|
|
|
|
connection_id);
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_http_request( self : PCefServerHandler;
|
|
|
|
server : PCefServer;
|
|
|
|
connection_id : Integer;
|
|
|
|
const client_address : PCefString;
|
|
|
|
request : PCefRequest); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnHttpRequest(TCEFServerRef.UnWrap(server),
|
|
|
|
connection_id,
|
|
|
|
CefString(client_address),
|
|
|
|
TCefRequestRef.UnWrap(request));
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_web_socket_request( self : PCefServerHandler;
|
|
|
|
server : PCefServer;
|
|
|
|
connection_id : Integer;
|
|
|
|
const client_address : PCefString;
|
|
|
|
request : PCefRequest;
|
|
|
|
callback : PCefCallback); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnWebSocketRequest(TCEFServerRef.UnWrap(server),
|
|
|
|
connection_id,
|
|
|
|
CefString(client_address),
|
|
|
|
TCefRequestRef.UnWrap(request),
|
|
|
|
TCefCallbackRef.UnWrap(callback));
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_web_socket_connected(self : PCefServerHandler;
|
|
|
|
server : PCefServer;
|
|
|
|
connection_id : Integer); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnWebSocketConnected(TCEFServerRef.UnWrap(server),
|
|
|
|
connection_id);
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
procedure cef_server_handler_on_web_socket_message( self : PCefServerHandler;
|
|
|
|
server : PCefServer;
|
|
|
|
connection_id : Integer;
|
|
|
|
const data : Pointer;
|
|
|
|
data_size : NativeUInt); stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-12-18 20:38:56 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCEFServerHandlerOwn) then
|
|
|
|
TCEFServerHandlerOwn(TempObject).OnWebSocketMessage(TCEFServerRef.UnWrap(server),
|
|
|
|
connection_id,
|
|
|
|
data,
|
|
|
|
data_size);
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TCEFServerHandlerOwn.Create;
|
|
|
|
begin
|
|
|
|
inherited CreateData(SizeOf(TCEFServerHandler));
|
|
|
|
|
|
|
|
with PCEFServerHandler(FData)^ do
|
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
on_server_created := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_server_created;
|
|
|
|
on_server_destroyed := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_server_destroyed;
|
|
|
|
on_client_connected := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_client_connected;
|
|
|
|
on_client_disconnected := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_client_disconnected;
|
|
|
|
on_http_request := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_http_request;
|
|
|
|
on_web_socket_request := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_web_socket_request;
|
|
|
|
on_web_socket_connected := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_web_socket_connected;
|
|
|
|
on_web_socket_message := {$IFDEF FPC}@{$ENDIF}cef_server_handler_on_web_socket_message;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnServerCreated(const server: ICefServer);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnServerDestroyed(const server: ICefServer);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnClientConnected(const server: ICefServer; connection_id: Integer);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnClientDisconnected(const server: ICefServer; connection_id: Integer);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnHttpRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnWebSocketRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest; const callback: ICefCallback);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnWebSocketConnected(const server: ICefServer; connection_id: Integer);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCEFServerHandlerOwn.OnWebSocketMessage(const server: ICefServer; connection_id: Integer; const data: Pointer; data_size: NativeUInt);
|
|
|
|
begin
|
|
|
|
//
|
|
|
|
end;
|
|
|
|
|
|
|
|
// ************************************************************************
|
|
|
|
// ************************* TCustomServerHandler *************************
|
|
|
|
// ************************************************************************
|
|
|
|
|
|
|
|
constructor TCustomServerHandler.Create(const events: IServerEvents);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
|
|
|
|
2020-03-29 17:31:42 +02:00
|
|
|
FEvents := Pointer(events);
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TCustomServerHandler.Destroy;
|
|
|
|
begin
|
|
|
|
FEvents := nil;
|
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnServerCreated(const server: ICefServer);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnServerCreated(server);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnServerCreated', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnServerDestroyed(const server: ICefServer);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnServerDestroyed(server);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnServerDestroyed', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnClientConnected(const server: ICefServer; connection_id: Integer);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnClientConnected(server, connection_id);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnClientConnected', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnClientDisconnected(const server: ICefServer; connection_id: Integer);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnClientDisconnected(server, connection_id);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnClientDisconnected', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnHttpRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnHttpRequest(server, connection_id, client_address, request);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnHttpRequest', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnWebSocketRequest(const server: ICefServer; connection_id: Integer; const client_address: ustring; const request: ICefRequest; const callback: ICefCallback);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnWebSocketRequest(server, connection_id, client_address, request, callback);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnWebSocketRequest', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnWebSocketConnected(const server: ICefServer; connection_id: Integer);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnWebSocketConnected(server, connection_id);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnWebSocketConnected', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TCustomServerHandler.OnWebSocketMessage(const server: ICefServer; connection_id: Integer; const data: Pointer; data_size: NativeUInt);
|
|
|
|
begin
|
2018-02-03 18:52:48 +02:00
|
|
|
try
|
2020-03-29 17:31:42 +02:00
|
|
|
if (FEvents <> nil) then
|
|
|
|
IServerEvents(FEvents).doOnWebSocketMessage(server, connection_id, data, data_size);
|
2018-02-03 18:52:48 +02:00
|
|
|
except
|
|
|
|
on e : exception do
|
|
|
|
if CustomExceptionHandler('TCustomServerHandler.OnWebSocketMessage', e) then raise;
|
|
|
|
end;
|
2017-12-18 20:38:56 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|