forked from academiadocodigo/TBGWebCharts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chromium.CustomResourceHandler.pas
99 lines (82 loc) · 3.18 KB
/
Chromium.CustomResourceHandler.pas
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
unit Chromium.CustomResourceHandler;
{$I cef.inc}
interface
uses
Classes, Windows, SysUtils,
uCEFInterfaces, uCEFTypes, uCEFResourceHandler;
type
TCustomResourceHandler = class(TCefResourceHandlerOwn)
private
FStream : TMemoryStream;
FMimeType : string;
FStatusText : string;
FStatus : Integer;
protected
function ProcessRequest(const request: ICefRequest; const callback: ICefCallback): Boolean; override;
function ReadResponse(const dataOut: Pointer; bytesToRead: Integer; var bytesRead: Integer; const callback: ICefCallback): Boolean; override;
procedure GetResponseHeaders(const response: ICefResponse; out responseLength: Int64; out redirectUrl: ustring); override;
public
constructor Create(const browser: ICefBrowser; const frame: ICefFrame; const schemeName: ustring; const request: ICefRequest; const aStream : TStream; const aMimeType : ustring); reintroduce;
destructor Destroy; override;
end;
implementation
constructor TCustomResourceHandler.Create(const browser : ICefBrowser;
const frame : ICefFrame;
const schemeName : ustring;
const request : ICefRequest;
const aStream : TStream;
const aMimeType : ustring);
begin
inherited Create(browser, frame, schemeName, request);
FStream := TMemoryStream.Create;
FStatus := 0;
FMimeType := aMimeType;
FStatusText := '';
if (FStream <> nil) and (aStream <> nil) then
begin
FStream.LoadFromStream(aStream);
FStream.Seek(0, soFromBeginning);
end;
end;
destructor TCustomResourceHandler.Destroy;
begin
if (FStream <> nil) then FreeAndNil(FStream);
inherited Destroy;
end;
procedure TCustomResourceHandler.GetResponseHeaders(const response : ICefResponse;
out responseLength : Int64;
out redirectUrl : ustring);
begin
if (response <> nil) then
begin
response.Status := FStatus;
response.StatusText := FStatusText;
response.MimeType := FMimeType;
end;
if (FStream <> nil) then
responseLength := FStream.Size
else
responseLength := 0;
end;
function TCustomResourceHandler.ProcessRequest(const request : ICefRequest; const callback : ICefCallback): Boolean;
begin
Result := True;
FStatus := 200;
FStatusText := 'OK';
if (FStream <> nil) then FStream.Seek(0, soFromBeginning);
if (callback <> nil) then callback.Cont;
end;
function TCustomResourceHandler.ReadResponse(const dataOut : Pointer;
bytesToRead : Integer;
var bytesRead : Integer;
const callback : ICefCallback): Boolean;
begin
if (FStream <> nil) and (DataOut <> nil) then
begin
BytesRead := FStream.Read(DataOut^, BytesToRead);
Result := (BytesRead > 0);
end
else
Result := False;
end;
end.