2018-02-03 17:52:48 +01:00
|
|
|
|
// ************************************************************************
|
|
|
|
|
// ***************************** CEF4Delphi *******************************
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
//
|
|
|
|
|
// CEF4Delphi is based on DCEF3 which uses CEF3 to embed a chromium-based
|
|
|
|
|
// browser in Delphi applications.
|
|
|
|
|
//
|
|
|
|
|
// The original license of DCEF3 still applies to CEF4Delphi.
|
|
|
|
|
//
|
|
|
|
|
// For more information about CEF4Delphi visit :
|
|
|
|
|
// https://www.briskbard.com/index.php?lang=en&pageid=cef
|
|
|
|
|
//
|
|
|
|
|
// Copyright <20> 2018 Salvador D<>az Fau. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
// ************ vvvv Original license and comments below vvvv *************
|
|
|
|
|
// ************************************************************************
|
|
|
|
|
(*
|
|
|
|
|
* Delphi Chromium Embedded 3
|
|
|
|
|
*
|
|
|
|
|
* Usage allowed under the restrictions of the Lesser GNU General Public License
|
|
|
|
|
* or alternatively the restrictions of the Mozilla Public License 1.1
|
|
|
|
|
*
|
|
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
|
|
|
|
|
* the specific language governing rights and limitations under the License.
|
|
|
|
|
*
|
|
|
|
|
* Unit owner : Henri Gourvest <hgourvest@gmail.com>
|
|
|
|
|
* Web site : http://www.progdigy.com
|
|
|
|
|
* Repository : http://code.google.com/p/delphichromiumembedded/
|
|
|
|
|
* Group : http://groups.google.com/group/delphichromiumembedded
|
|
|
|
|
*
|
|
|
|
|
* Embarcadero Technologies, Inc is not permitted to use or redistribute
|
|
|
|
|
* this source code without explicit permission.
|
|
|
|
|
*
|
|
|
|
|
*)
|
|
|
|
|
|
2018-02-04 11:50:14 +01:00
|
|
|
|
unit uResponseFilterBrowser;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
|
|
|
|
|
{$I cef.inc}
|
|
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
|
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
|
|
|
|
|
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, System.SyncObjs,
|
|
|
|
|
{$ELSE}
|
|
|
|
|
Windows, Messages, SysUtils, Variants, Classes, Graphics,
|
|
|
|
|
Controls, Forms, Dialogs, StdCtrls, ExtCtrls, SyncObjs,
|
|
|
|
|
{$ENDIF}
|
|
|
|
|
uCEFChromium, uCEFWindowParent, uCEFInterfaces, uCEFConstants, uCEFTypes, uCEFResponseFilter;
|
|
|
|
|
|
|
|
|
|
const
|
|
|
|
|
STREAM_COPY_COMPLETE = WM_APP + $B00;
|
|
|
|
|
|
|
|
|
|
type
|
|
|
|
|
TResponseFilterBrowserFrm = class(TForm)
|
|
|
|
|
AddressPnl: TPanel;
|
|
|
|
|
AddressEdt: TEdit;
|
|
|
|
|
Timer1: TTimer;
|
|
|
|
|
Chromium1: TChromium;
|
|
|
|
|
CEFWindowParent1: TCEFWindowParent;
|
|
|
|
|
Splitter1: TSplitter;
|
|
|
|
|
Memo1: TMemo;
|
2018-02-08 10:49:06 +01:00
|
|
|
|
Panel1: TPanel;
|
|
|
|
|
GoBtn: TButton;
|
|
|
|
|
Label1: TLabel;
|
|
|
|
|
RscNameEdt: TEdit;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
procedure GoBtnClick(Sender: TObject);
|
|
|
|
|
procedure Timer1Timer(Sender: TObject);
|
|
|
|
|
procedure Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
|
2018-02-08 10:49:06 +01:00
|
|
|
|
procedure Chromium1GetResourceResponseFilter(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const response: ICefResponse; out Result: ICefResponseFilter);
|
2018-02-13 20:16:24 +01:00
|
|
|
|
procedure Chromium1ResourceLoadComplete(Sender: TObject; const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest; const response: ICefResponse; status: TCefUrlRequestStatus; receivedContentLength: Int64);
|
2018-02-08 10:49:06 +01:00
|
|
|
|
procedure FormShow(Sender: TObject);
|
2018-02-03 17:52:48 +01:00
|
|
|
|
procedure FormCreate(Sender: TObject);
|
|
|
|
|
procedure FormDestroy(Sender: TObject);
|
2018-02-16 18:41:13 +01:00
|
|
|
|
procedure Chromium1BeforePopup(Sender: TObject;
|
|
|
|
|
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
|
|
|
|
|
targetFrameName: ustring;
|
|
|
|
|
targetDisposition: TCefWindowOpenDisposition; userGesture: Boolean;
|
2018-03-29 20:02:04 +02:00
|
|
|
|
const popupFeatures: TCefPopupFeatures; var windowInfo: TCefWindowInfo;
|
2018-02-16 18:41:13 +01:00
|
|
|
|
var client: ICefClient; var settings: TCefBrowserSettings;
|
2018-03-29 20:02:04 +02:00
|
|
|
|
var noJavascriptAccess: Boolean; var Result: Boolean);
|
2018-02-03 17:52:48 +01:00
|
|
|
|
protected
|
2018-02-07 14:30:44 +01:00
|
|
|
|
FFilter : ICefResponseFilter; // CEF Filter interface that receives the resource contents
|
|
|
|
|
FStream : TMemoryStream; // TMemoryStream to hold the resource contents
|
|
|
|
|
FStreamCS : TCriticalSection; // Critical section used to protect the memory stream
|
|
|
|
|
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.
|
2018-02-03 17:52:48 +01:00
|
|
|
|
|
|
|
|
|
procedure WMMove(var aMessage : TWMMove); message WM_MOVE;
|
|
|
|
|
procedure WMMoving(var aMessage : TMessage); message WM_MOVING;
|
|
|
|
|
procedure WMEnterMenuLoop(var aMessage: TMessage); message WM_ENTERMENULOOP;
|
|
|
|
|
procedure WMExitMenuLoop(var aMessage: TMessage); message WM_EXITMENULOOP;
|
|
|
|
|
procedure BrowserCreatedMsg(var aMessage : TMessage); message CEF_AFTERCREATED;
|
|
|
|
|
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);
|
2018-02-08 10:49:06 +01:00
|
|
|
|
|
|
|
|
|
function IsMyResource(const aRequest : ICefRequest) : boolean;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
public
|
|
|
|
|
{ Public declarations }
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
var
|
|
|
|
|
ResponseFilterBrowserFrm: TResponseFilterBrowserFrm;
|
|
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
{$R *.dfm}
|
|
|
|
|
|
|
|
|
|
uses
|
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
|
System.Math,
|
|
|
|
|
{$ELSE}
|
|
|
|
|
Math,
|
|
|
|
|
{$ENDIF}
|
2018-02-04 11:50:14 +01:00
|
|
|
|
uCEFApplication, uCEFMiscFunctions;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
|
|
|
|
|
// This demo uses a TCustomResponseFilter to read the contents from a JavaScript file in wikipedia.org into a TMemoryStream.
|
|
|
|
|
// The stream is shown in the TMemo when it's finished.
|
|
|
|
|
|
|
|
|
|
// For more information read the CEF3 code comments here :
|
|
|
|
|
// https://github.com/chromiumembedded/cef/blob/master/include/capi/cef_response_filter_capi.h
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.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);
|
|
|
|
|
begin
|
|
|
|
|
try
|
2018-02-04 11:50:14 +01:00
|
|
|
|
try
|
|
|
|
|
// This event will be called repeatedly until the input buffer has been fully read.
|
|
|
|
|
// When there's no more data then data_in is nil and you can show the stream contents.
|
|
|
|
|
|
|
|
|
|
FStreamCS.Acquire;
|
|
|
|
|
|
|
|
|
|
if (data_in = nil) then
|
|
|
|
|
begin
|
|
|
|
|
data_in_read := 0;
|
|
|
|
|
data_out_written := 0;
|
|
|
|
|
aResult := RESPONSE_FILTER_DONE;
|
|
|
|
|
|
2018-02-07 10:36:11 +01:00
|
|
|
|
if not(FRscCompleted) then
|
|
|
|
|
FRscCompleted := PostMessage(Handle, STREAM_COPY_COMPLETE, 0, 0);
|
2018-02-04 11:50:14 +01:00
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
begin
|
|
|
|
|
if (data_out <> nil) then
|
|
|
|
|
begin
|
2018-02-07 10:02:24 +01:00
|
|
|
|
data_out_written := min(data_in_size, data_out_size);
|
|
|
|
|
|
|
|
|
|
if (data_out_written > 0) then
|
|
|
|
|
Move(data_in^, data_out^, data_out_written);
|
2018-02-04 11:50:14 +01:00
|
|
|
|
end;
|
|
|
|
|
|
2018-02-07 10:02:24 +01:00
|
|
|
|
if (data_in_size > 0) then
|
|
|
|
|
data_in_read := FStream.Write(data_in^, data_in_size);
|
|
|
|
|
|
2018-02-07 14:30:44 +01:00
|
|
|
|
// Send the STREAM_COPY_COMPLETE message only if the server sent the data size in
|
|
|
|
|
// a Content-Length header and we can compare it with the stream size
|
|
|
|
|
if not(FRscCompleted) and (FRscSize <> -1) and (FRscSize = FStream.Size) then
|
2018-02-07 10:36:11 +01:00
|
|
|
|
FRscCompleted := PostMessage(Handle, STREAM_COPY_COMPLETE, 0, 0);
|
|
|
|
|
|
2018-02-07 10:02:24 +01:00
|
|
|
|
aResult := RESPONSE_FILTER_NEED_MORE_DATA;
|
2018-02-04 11:50:14 +01:00
|
|
|
|
end;
|
|
|
|
|
except
|
|
|
|
|
on e : exception do
|
|
|
|
|
begin
|
|
|
|
|
aResult := RESPONSE_FILTER_ERROR;
|
|
|
|
|
if CustomExceptionHandler('TResponseFilterBrowserFrm.Filter_OnFilter', e) then raise;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
finally
|
|
|
|
|
FStreamCS.Release;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-02-08 10:49:06 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
|
procedure TResponseFilterBrowserFrm.FormCreate(Sender: TObject);
|
|
|
|
|
begin
|
2018-02-07 10:36:11 +01:00
|
|
|
|
FRscCompleted := False;
|
2018-02-07 14:30:44 +01:00
|
|
|
|
FRscSize := -1;
|
2018-02-07 10:36:11 +01:00
|
|
|
|
FStream := TMemoryStream.Create;
|
|
|
|
|
FStreamCS := TCriticalSection.Create;
|
|
|
|
|
FFilter := TCustomResponseFilter.Create;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
|
|
|
|
|
// This event will receive the data
|
|
|
|
|
TCustomResponseFilter(FFilter).OnFilter := Filter_OnFilter;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.FormDestroy(Sender: TObject);
|
|
|
|
|
begin
|
|
|
|
|
FFilter := nil;
|
|
|
|
|
FStream.Free;
|
|
|
|
|
FStreamCS.Free;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.FormShow(Sender: TObject);
|
|
|
|
|
begin
|
|
|
|
|
// GlobalCEFApp.GlobalContextInitialized has to be TRUE before creating any browser
|
|
|
|
|
// If it's not initialized yet, we use a simple timer to create the browser later.
|
|
|
|
|
if not(Chromium1.CreateBrowser(CEFWindowParent1)) then Timer1.Enabled := True;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.Chromium1AfterCreated(Sender: TObject; const browser: ICefBrowser);
|
|
|
|
|
begin
|
|
|
|
|
// Now the browser is fully initialized we can send a message to the main form to load the initial web page.
|
|
|
|
|
PostMessage(Handle, CEF_AFTERCREATED, 0, 0);
|
|
|
|
|
end;
|
|
|
|
|
|
2018-02-16 18:41:13 +01:00
|
|
|
|
procedure TResponseFilterBrowserFrm.Chromium1BeforePopup(Sender: TObject;
|
|
|
|
|
const browser: ICefBrowser; const frame: ICefFrame; const targetUrl,
|
|
|
|
|
targetFrameName: ustring; targetDisposition: TCefWindowOpenDisposition;
|
2018-03-29 20:02:04 +02:00
|
|
|
|
userGesture: Boolean; const popupFeatures: TCefPopupFeatures;
|
2018-02-16 18:41:13 +01:00
|
|
|
|
var windowInfo: TCefWindowInfo; var client: ICefClient;
|
|
|
|
|
var settings: TCefBrowserSettings; var noJavascriptAccess: Boolean;
|
2018-03-29 20:02:04 +02:00
|
|
|
|
var Result: Boolean);
|
2018-02-16 18:41:13 +01:00
|
|
|
|
begin
|
|
|
|
|
// For simplicity, this demo blocks all popup windows and new tabs
|
|
|
|
|
Result := (targetDisposition in [WOD_NEW_FOREGROUND_TAB, WOD_NEW_BACKGROUND_TAB, WOD_NEW_POPUP, WOD_NEW_WINDOW]);
|
|
|
|
|
end;
|
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
|
procedure TResponseFilterBrowserFrm.Chromium1GetResourceResponseFilter(Sender : TObject;
|
|
|
|
|
const browser : ICefBrowser;
|
|
|
|
|
const frame : ICefFrame;
|
|
|
|
|
const request : ICefRequest;
|
|
|
|
|
const response : ICefResponse;
|
|
|
|
|
out Result : ICefResponseFilter);
|
2018-02-07 14:30:44 +01:00
|
|
|
|
var
|
|
|
|
|
TempHeader : string;
|
|
|
|
|
TempLen : integer;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
begin
|
2018-02-08 10:49:06 +01:00
|
|
|
|
if (response <> nil) and IsMyResource(request) then
|
2018-02-07 10:36:11 +01:00
|
|
|
|
begin
|
2018-02-07 14:30:44 +01:00
|
|
|
|
Result := FFilter;
|
|
|
|
|
TempHeader := trim(response.GetHeader('Content-Length'));
|
|
|
|
|
|
|
|
|
|
if TryStrToInt(TempHeader, TempLen) and (TempLen > 0) then
|
|
|
|
|
FRscSize := TempLen
|
|
|
|
|
else
|
|
|
|
|
FRscSize := -1;
|
2018-02-07 10:36:11 +01:00
|
|
|
|
end
|
2018-02-03 17:52:48 +01:00
|
|
|
|
else
|
|
|
|
|
Result := nil;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-02-08 10:49:06 +01:00
|
|
|
|
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;
|
|
|
|
|
|
2018-02-03 17:52:48 +01:00
|
|
|
|
procedure TResponseFilterBrowserFrm.BrowserCreatedMsg(var aMessage : TMessage);
|
|
|
|
|
begin
|
|
|
|
|
Caption := 'Response Filter Browser';
|
|
|
|
|
AddressPnl.Enabled := True;
|
|
|
|
|
GoBtn.Click;
|
|
|
|
|
end;
|
|
|
|
|
|
2018-02-07 14:30:44 +01:00
|
|
|
|
// This procedure handles the stream contents after it's fully downloaded
|
2018-02-03 17:52:48 +01:00
|
|
|
|
procedure TResponseFilterBrowserFrm.StreamCopyCompleteMsg(var aMessage : TMessage);
|
|
|
|
|
begin
|
|
|
|
|
try
|
|
|
|
|
FStreamCS.Acquire;
|
2018-02-07 11:20:38 +01:00
|
|
|
|
|
2018-02-07 14:30:44 +01:00
|
|
|
|
if (FStream.Size > 0) then
|
|
|
|
|
begin
|
|
|
|
|
FStream.Seek(0, soBeginning);
|
2018-02-07 11:20:38 +01:00
|
|
|
|
|
2018-02-07 14:30:44 +01:00
|
|
|
|
Memo1.Lines.Clear;
|
|
|
|
|
Memo1.Lines.LoadFromStream(FStream);
|
2018-02-07 11:20:38 +01:00
|
|
|
|
|
2018-02-07 14:30:44 +01:00
|
|
|
|
FStream.Clear;
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
Memo1.Lines.Clear;
|
2018-02-07 11:20:38 +01:00
|
|
|
|
|
2018-02-07 14:30:44 +01:00
|
|
|
|
FRscSize := -1;
|
|
|
|
|
FRscCompleted := False;
|
2018-02-03 17:52:48 +01:00
|
|
|
|
finally
|
|
|
|
|
FStreamCS.Release;
|
|
|
|
|
end;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.GoBtnClick(Sender: TObject);
|
|
|
|
|
begin
|
|
|
|
|
Chromium1.LoadURL(AddressEdt.Text);
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.Timer1Timer(Sender: TObject);
|
|
|
|
|
begin
|
|
|
|
|
Timer1.Enabled := False;
|
|
|
|
|
if not(Chromium1.CreateBrowser(CEFWindowParent1)) and not(Chromium1.Initialized) then
|
|
|
|
|
Timer1.Enabled := True;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.WMMove(var aMessage : TWMMove);
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
|
|
if (Chromium1 <> nil) then Chromium1.NotifyMoveOrResizeStarted;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.WMMoving(var aMessage : TMessage);
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
|
|
if (Chromium1 <> nil) then Chromium1.NotifyMoveOrResizeStarted;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.WMEnterMenuLoop(var aMessage: TMessage);
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
|
|
if (aMessage.wParam = 0) and (GlobalCEFApp <> nil) then GlobalCEFApp.OsmodalLoop := True;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
procedure TResponseFilterBrowserFrm.WMExitMenuLoop(var aMessage: TMessage);
|
|
|
|
|
begin
|
|
|
|
|
inherited;
|
|
|
|
|
|
|
|
|
|
if (aMessage.wParam = 0) and (GlobalCEFApp <> nil) then GlobalCEFApp.OsmodalLoop := False;
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
|
|
end.
|