mirror of
https://github.com/salvadordf/CEF4Delphi.git
synced 2024-11-24 08:02:15 +02:00
ResponseFilterBrowser demo now also uses OnResourceLoadComplete to check if the resource is complete
This commit is contained in:
parent
0b72a3e446
commit
ace189d104
@ -44,24 +44,53 @@ object ResponseFilterBrowserFrm: TResponseFilterBrowserFrm
|
|||||||
object AddressEdt: TEdit
|
object AddressEdt: TEdit
|
||||||
Left = 5
|
Left = 5
|
||||||
Top = 5
|
Top = 5
|
||||||
Width = 997
|
Width = 755
|
||||||
Height = 20
|
Height = 20
|
||||||
Margins.Right = 5
|
Margins.Right = 5
|
||||||
Align = alClient
|
Align = alClient
|
||||||
TabOrder = 0
|
TabOrder = 0
|
||||||
Text = 'https://www.wikipedia.org'
|
Text = 'https://www.wikipedia.org'
|
||||||
|
ExplicitWidth = 716
|
||||||
ExplicitHeight = 21
|
ExplicitHeight = 21
|
||||||
end
|
end
|
||||||
object GoBtn: TButton
|
object Panel1: TPanel
|
||||||
Left = 1002
|
Left = 760
|
||||||
Top = 5
|
Top = 5
|
||||||
Width = 31
|
Width = 273
|
||||||
Height = 20
|
Height = 20
|
||||||
Margins.Left = 5
|
|
||||||
Align = alRight
|
Align = alRight
|
||||||
Caption = 'Go'
|
BevelOuter = bvNone
|
||||||
|
Padding.Left = 5
|
||||||
|
ShowCaption = False
|
||||||
TabOrder = 1
|
TabOrder = 1
|
||||||
OnClick = GoBtnClick
|
object Label1: TLabel
|
||||||
|
Left = 54
|
||||||
|
Top = 3
|
||||||
|
Width = 84
|
||||||
|
Height = 13
|
||||||
|
Caption = 'Resource name : '
|
||||||
|
end
|
||||||
|
object GoBtn: TButton
|
||||||
|
Left = 5
|
||||||
|
Top = 0
|
||||||
|
Width = 31
|
||||||
|
Height = 20
|
||||||
|
Margins.Left = 5
|
||||||
|
Align = alLeft
|
||||||
|
Caption = 'Go'
|
||||||
|
TabOrder = 0
|
||||||
|
OnClick = GoBtnClick
|
||||||
|
ExplicitLeft = 154
|
||||||
|
end
|
||||||
|
object RscNameEdt: TEdit
|
||||||
|
Left = 144
|
||||||
|
Top = 0
|
||||||
|
Width = 129
|
||||||
|
Height = 20
|
||||||
|
Align = alRight
|
||||||
|
TabOrder = 1
|
||||||
|
Text = 'index-47f5f07682.js'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
object CEFWindowParent1: TCEFWindowParent
|
object CEFWindowParent1: TCEFWindowParent
|
||||||
@ -92,6 +121,7 @@ object ResponseFilterBrowserFrm: TResponseFilterBrowserFrm
|
|||||||
object Chromium1: TChromium
|
object Chromium1: TChromium
|
||||||
OnAfterCreated = Chromium1AfterCreated
|
OnAfterCreated = Chromium1AfterCreated
|
||||||
OnGetResourceResponseFilter = Chromium1GetResourceResponseFilter
|
OnGetResourceResponseFilter = Chromium1GetResourceResponseFilter
|
||||||
|
OnResourceLoadComplete = Chromium1ResourceLoadComplete
|
||||||
Left = 56
|
Left = 56
|
||||||
Top = 160
|
Top = 160
|
||||||
end
|
end
|
||||||
|
@ -58,27 +58,30 @@ type
|
|||||||
TResponseFilterBrowserFrm = class(TForm)
|
TResponseFilterBrowserFrm = class(TForm)
|
||||||
AddressPnl: TPanel;
|
AddressPnl: TPanel;
|
||||||
AddressEdt: TEdit;
|
AddressEdt: TEdit;
|
||||||
GoBtn: TButton;
|
|
||||||
Timer1: TTimer;
|
Timer1: TTimer;
|
||||||
Chromium1: TChromium;
|
Chromium1: TChromium;
|
||||||
CEFWindowParent1: TCEFWindowParent;
|
CEFWindowParent1: TCEFWindowParent;
|
||||||
Splitter1: TSplitter;
|
Splitter1: TSplitter;
|
||||||
Memo1: TMemo;
|
Memo1: TMemo;
|
||||||
|
Panel1: TPanel;
|
||||||
|
GoBtn: TButton;
|
||||||
|
Label1: TLabel;
|
||||||
|
RscNameEdt: TEdit;
|
||||||
procedure GoBtnClick(Sender: TObject);
|
procedure GoBtnClick(Sender: TObject);
|
||||||
procedure FormShow(Sender: TObject);
|
|
||||||
procedure Timer1Timer(Sender: TObject);
|
procedure Timer1Timer(Sender: TObject);
|
||||||
procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
|
procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
|
||||||
procedure Chromium1GetResourceResponseFilter(Sender: TObject;
|
procedure Chromium1GetResourceResponseFilter(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const response: ICefResponse; out Result: ICefResponseFilter);
|
||||||
const browser: ICefBrowser; const frame: ICefFrame;
|
procedure FormShow(Sender: TObject);
|
||||||
const request: ICefRequest; const response: ICefResponse;
|
|
||||||
out Result: ICefResponseFilter);
|
|
||||||
procedure FormCreate(Sender: TObject);
|
procedure FormCreate(Sender: TObject);
|
||||||
procedure FormDestroy(Sender: TObject);
|
procedure FormDestroy(Sender: TObject);
|
||||||
|
procedure Chromium1ResourceLoadComplete(Sender: TObject;
|
||||||
|
const browser: ICefBrowser; const frame: ICefFrame;
|
||||||
|
const request: ICefRequest; const response: ICefResponse;
|
||||||
|
status: TCefUrlRequestStatus; receivedContentLength: Int64);
|
||||||
protected
|
protected
|
||||||
FFilter : ICefResponseFilter; // CEF Filter interface that receives the resource contents
|
FFilter : ICefResponseFilter; // CEF Filter interface that receives the resource contents
|
||||||
FStream : TMemoryStream; // TMemoryStream to hold the resource contents
|
FStream : TMemoryStream; // TMemoryStream to hold the resource contents
|
||||||
FStreamCS : TCriticalSection; // Critical section used to protect the memory stream
|
FStreamCS : TCriticalSection; // Critical section used to protect the memory stream
|
||||||
FRscName : string; // name of the resource that will be filtered
|
|
||||||
FRscSize : int64; // size of the resource if the server sends the Content-Length header
|
FRscSize : int64; // size of the resource if the server sends the Content-Length header
|
||||||
FRscCompleted : boolean; // This variable will be used to handle the results only once.
|
FRscCompleted : boolean; // This variable will be used to handle the results only once.
|
||||||
|
|
||||||
@ -90,6 +93,8 @@ type
|
|||||||
procedure StreamCopyCompleteMsg(var aMessage : TMessage); message STREAM_COPY_COMPLETE;
|
procedure StreamCopyCompleteMsg(var aMessage : TMessage); message STREAM_COPY_COMPLETE;
|
||||||
|
|
||||||
procedure Filter_OnFilter(Sender: TObject; data_in: Pointer; data_in_size: NativeUInt; var data_in_read: NativeUInt; data_out: Pointer; data_out_size : NativeUInt; var data_out_written: NativeUInt; var aResult : TCefResponseFilterStatus);
|
procedure Filter_OnFilter(Sender: TObject; data_in: Pointer; data_in_size: NativeUInt; var data_in_read: NativeUInt; data_out: Pointer; data_out_size : NativeUInt; var data_out_written: NativeUInt; var aResult : TCefResponseFilterStatus);
|
||||||
|
|
||||||
|
function IsMyResource(const aRequest : ICefRequest) : boolean;
|
||||||
public
|
public
|
||||||
{ Public declarations }
|
{ Public declarations }
|
||||||
end;
|
end;
|
||||||
@ -172,9 +177,20 @@ begin
|
|||||||
end;
|
end;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
function TResponseFilterBrowserFrm.IsMyResource(const aRequest : ICefRequest) : boolean;
|
||||||
|
var
|
||||||
|
TempName : string;
|
||||||
|
begin
|
||||||
|
TempName := trim(RscNameEdt.Text);
|
||||||
|
|
||||||
|
if (aRequest <> nil) and (length(TempName) > 0) then
|
||||||
|
Result := (pos(TempName, aRequest.URL) > 0)
|
||||||
|
else
|
||||||
|
Result := False;
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TResponseFilterBrowserFrm.FormCreate(Sender: TObject);
|
procedure TResponseFilterBrowserFrm.FormCreate(Sender: TObject);
|
||||||
begin
|
begin
|
||||||
FRscName := 'index-47f5f07682.js'; // JS script used at wikipedia.org
|
|
||||||
FRscCompleted := False;
|
FRscCompleted := False;
|
||||||
FRscSize := -1;
|
FRscSize := -1;
|
||||||
FStream := TMemoryStream.Create;
|
FStream := TMemoryStream.Create;
|
||||||
@ -215,7 +231,7 @@ var
|
|||||||
TempHeader : string;
|
TempHeader : string;
|
||||||
TempLen : integer;
|
TempLen : integer;
|
||||||
begin
|
begin
|
||||||
if (request <> nil) and (response <> nil) and (pos(FRscName, request.URL) > 0) then
|
if (response <> nil) and IsMyResource(request) then
|
||||||
begin
|
begin
|
||||||
Result := FFilter;
|
Result := FFilter;
|
||||||
TempHeader := trim(response.GetHeader('Content-Length'));
|
TempHeader := trim(response.GetHeader('Content-Length'));
|
||||||
@ -229,6 +245,21 @@ begin
|
|||||||
Result := nil;
|
Result := nil;
|
||||||
end;
|
end;
|
||||||
|
|
||||||
|
procedure TResponseFilterBrowserFrm.Chromium1ResourceLoadComplete(Sender : TObject;
|
||||||
|
const browser : ICefBrowser;
|
||||||
|
const frame : ICefFrame;
|
||||||
|
const request : ICefRequest;
|
||||||
|
const response : ICefResponse;
|
||||||
|
status : TCefUrlRequestStatus;
|
||||||
|
receivedContentLength : Int64);
|
||||||
|
begin
|
||||||
|
// In case the server didn't send a Content-Length header
|
||||||
|
// and CEF didn't send a data_in = nil in Filter_OnFilter
|
||||||
|
// we still can use this event to know when the resource is complete
|
||||||
|
if not(FRscCompleted) and IsMyResource(request) then
|
||||||
|
FRscCompleted := PostMessage(Handle, STREAM_COPY_COMPLETE, 0, 0);
|
||||||
|
end;
|
||||||
|
|
||||||
procedure TResponseFilterBrowserFrm.BrowserCreatedMsg(var aMessage : TMessage);
|
procedure TResponseFilterBrowserFrm.BrowserCreatedMsg(var aMessage : TMessage);
|
||||||
begin
|
begin
|
||||||
Caption := 'Response Filter Browser';
|
Caption := 'Response Filter Browser';
|
||||||
|
Loading…
Reference in New Issue
Block a user