2017-01-27 17:37:51 +02: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
|
|
|
|
//
|
2019-01-08 20:15:25 +02:00
|
|
|
// Copyright � 2019 Salvador Diaz Fau. All rights reserved.
|
2017-01-27 17:37:51 +02:00
|
|
|
//
|
|
|
|
// ************************************************************************
|
|
|
|
// ************ 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.
|
|
|
|
*
|
|
|
|
*)
|
|
|
|
|
|
|
|
unit uCEFWriteHandler;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
{$IFDEF FPC}
|
|
|
|
{$MODE OBJFPC}{$H+}
|
|
|
|
{$ENDIF}
|
|
|
|
|
2018-11-22 16:02:21 +02:00
|
|
|
{$IFNDEF CPUX64}{$ALIGN ON}{$ENDIF}
|
|
|
|
{$MINENUMSIZE 4}
|
2017-01-27 17:37:51 +02:00
|
|
|
|
2017-02-05 21:56:46 +02:00
|
|
|
{$I cef.inc}
|
|
|
|
|
2017-01-27 17:37:51 +02:00
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
WinApi.Windows,
|
|
|
|
{$ELSE}
|
|
|
|
Windows,
|
|
|
|
{$ENDIF}
|
2017-07-18 17:50:28 +02:00
|
|
|
{$ENDIF}
|
2017-03-16 20:09:42 +02:00
|
|
|
uCEFBaseRefCounted, uCEFInterfaces, uCEFTypes;
|
2017-01-27 17:37:51 +02:00
|
|
|
|
|
|
|
type
|
2017-03-16 20:09:42 +02:00
|
|
|
TCefWriteHandlerOwn = class(TCefBaseRefCountedOwn, ICefWriteHandler)
|
2017-07-18 17:50:28 +02:00
|
|
|
protected
|
|
|
|
function Write(const ptr: Pointer; size, n: NativeUInt): NativeUInt; virtual;
|
|
|
|
function Seek(offset: Int64; whence: Integer): Integer; virtual;
|
|
|
|
function Tell: Int64; virtual;
|
|
|
|
function Flush: Integer; virtual;
|
|
|
|
function MayBlock: Boolean; virtual;
|
|
|
|
public
|
|
|
|
constructor Create; virtual;
|
|
|
|
end;
|
|
|
|
|
|
|
|
TCefBytesWriteHandler = class(TCefWriteHandlerOwn)
|
|
|
|
protected
|
|
|
|
FCriticalSection : TRTLCriticalSection;
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
FGrow : NativeUInt;
|
|
|
|
FBuffer : Pointer;
|
|
|
|
FBufferSize : int64;
|
|
|
|
FOffset : int64;
|
2017-07-18 17:50:28 +02:00
|
|
|
|
|
|
|
function Grow(size : NativeUInt) : NativeUInt;
|
|
|
|
|
|
|
|
public
|
|
|
|
constructor Create(aGrow : NativeUInt); reintroduce;
|
|
|
|
destructor Destroy; override;
|
|
|
|
|
|
|
|
function Write(const ptr: Pointer; size, n: NativeUInt): NativeUInt; override;
|
|
|
|
function Seek(offset: Int64; whence: Integer): Integer; override;
|
|
|
|
function Tell: Int64; override;
|
|
|
|
function Flush: Integer; override;
|
|
|
|
function MayBlock: Boolean; override;
|
|
|
|
|
|
|
|
function GetData : pointer;
|
|
|
|
function GetDataSize : int64;
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
uses
|
2018-10-04 20:45:31 +02:00
|
|
|
{$IFDEF DELPHI16_UP}
|
|
|
|
System.Math,
|
|
|
|
{$ELSE}
|
|
|
|
Math,
|
|
|
|
{$ENDIF}
|
2017-01-27 17:37:51 +02:00
|
|
|
uCEFMiscFunctions, uCEFLibFunctions;
|
|
|
|
|
|
|
|
|
2017-07-18 17:50:28 +02:00
|
|
|
// *******************************************
|
|
|
|
// *********** TCefWriteHandlerOwn ***********
|
|
|
|
// *******************************************
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
function cef_write_handler_write( self : PCefWriteHandler;
|
|
|
|
const ptr : Pointer;
|
|
|
|
size : NativeUInt;
|
|
|
|
n : NativeUInt): NativeUInt; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 17:37:51 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
Result := 0;
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefWriteHandlerOwn) then
|
|
|
|
Result:= TCefWriteHandlerOwn(TempObject).Write(ptr,
|
|
|
|
size,
|
|
|
|
n);
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
2018-03-29 20:02:04 +02:00
|
|
|
function cef_write_handler_seek(self : PCefWriteHandler;
|
|
|
|
offset : Int64;
|
|
|
|
whence : Integer): Integer; stdcall;
|
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 17:37:51 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
Result := 0;
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefWriteHandlerOwn) then
|
|
|
|
Result:= TCefWriteHandlerOwn(TempObject).Seek(offset,
|
|
|
|
whence);
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function cef_write_handler_tell(self: PCefWriteHandler): Int64; stdcall;
|
2018-03-29 20:02:04 +02:00
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 17:37:51 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
Result := 0;
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefWriteHandlerOwn) then
|
|
|
|
Result:= TCefWriteHandlerOwn(TempObject).Tell;
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function cef_write_handler_flush(self: PCefWriteHandler): Integer; stdcall;
|
2018-03-29 20:02:04 +02:00
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 17:37:51 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
Result := 0;
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefWriteHandlerOwn) then
|
|
|
|
Result:= TCefWriteHandlerOwn(TempObject).Flush;
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function cef_write_handler_may_block(self: PCefWriteHandler): Integer; stdcall;
|
2018-03-29 20:02:04 +02:00
|
|
|
var
|
|
|
|
TempObject : TObject;
|
2017-01-27 17:37:51 +02:00
|
|
|
begin
|
2018-03-29 20:02:04 +02:00
|
|
|
Result := Ord(False);
|
|
|
|
TempObject := CefGetObject(self);
|
|
|
|
|
|
|
|
if (TempObject <> nil) and (TempObject is TCefWriteHandlerOwn) then
|
|
|
|
Result := Ord(TCefWriteHandlerOwn(TempObject).MayBlock);
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
constructor TCefWriteHandlerOwn.Create;
|
|
|
|
begin
|
|
|
|
inherited CreateData(SizeOf(TCefWriteHandler));
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2017-01-27 17:37:51 +02:00
|
|
|
with PCefWriteHandler(FData)^ do
|
2017-07-18 17:50:28 +02:00
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
write := {$IFDEF FPC}@{$ENDIF}cef_write_handler_write;
|
|
|
|
seek := {$IFDEF FPC}@{$ENDIF}cef_write_handler_seek;
|
|
|
|
tell := {$IFDEF FPC}@{$ENDIF}cef_write_handler_tell;
|
|
|
|
flush := {$IFDEF FPC}@{$ENDIF}cef_write_handler_flush;
|
|
|
|
may_block := {$IFDEF FPC}@{$ENDIF}cef_write_handler_may_block;
|
2017-07-18 17:50:28 +02:00
|
|
|
end;
|
2017-01-27 17:37:51 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefWriteHandlerOwn.Flush: Integer;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefWriteHandlerOwn.MayBlock: Boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefWriteHandlerOwn.Seek(offset: Int64; whence: Integer): Integer;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefWriteHandlerOwn.Tell: Int64;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
2017-07-18 17:50:28 +02:00
|
|
|
function TCefWriteHandlerOwn.Write(const ptr: Pointer; size, n: NativeUInt): NativeUInt;
|
2017-01-27 17:37:51 +02:00
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
2017-07-18 17:50:28 +02:00
|
|
|
|
|
|
|
// *******************************************
|
|
|
|
// ********** TCefBytesWriteHandler **********
|
|
|
|
// *******************************************
|
|
|
|
|
|
|
|
constructor TCefBytesWriteHandler.Create(aGrow : NativeUInt);
|
|
|
|
begin
|
|
|
|
inherited Create;
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
2017-07-18 17:50:28 +02:00
|
|
|
InitializeCriticalSection(FCriticalSection);
|
2019-01-08 20:15:25 +02:00
|
|
|
{$ELSE}
|
|
|
|
InitCriticalSection(FCriticalSection);
|
|
|
|
{$ENDIF}
|
2018-05-12 14:50:54 +02:00
|
|
|
FGrow := aGrow;
|
|
|
|
FBufferSize := aGrow;
|
|
|
|
FOffset := 0;
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
GetMem(FBuffer, aGrow);
|
2017-07-18 17:50:28 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TCefBytesWriteHandler.Destroy;
|
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
if (FBuffer <> nil) then FreeMem(FBuffer);
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2019-01-08 20:15:25 +02:00
|
|
|
{$IFDEF MSWINDOWS}
|
|
|
|
DeleteCriticalSection(FCriticalSection);
|
|
|
|
FCriticalSection.DebugInfo := nil;
|
|
|
|
FCriticalSection.LockCount := 0;
|
|
|
|
FCriticalSection.RecursionCount := 0;
|
|
|
|
FCriticalSection.OwningThread := 0;
|
|
|
|
FCriticalSection.LockSemaphore := 0;
|
|
|
|
{$IFNDEF FPC}
|
|
|
|
FCriticalSection.Reserved := 0;
|
|
|
|
{$ENDIF}
|
|
|
|
{$ELSE}
|
|
|
|
DoneCriticalSection(FCriticalSection);
|
2018-05-12 14:50:54 +02:00
|
|
|
{$ENDIF}
|
2017-07-18 17:50:28 +02:00
|
|
|
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.Write(const ptr: Pointer; size, n: NativeUInt): NativeUInt;
|
|
|
|
var
|
|
|
|
TempPointer : pointer;
|
2018-10-04 20:45:31 +02:00
|
|
|
TempSize : int64;
|
2017-07-18 17:50:28 +02:00
|
|
|
begin
|
|
|
|
EnterCriticalSection(FCriticalSection);
|
|
|
|
|
2018-10-04 20:45:31 +02:00
|
|
|
TempSize := size * n;
|
|
|
|
|
|
|
|
if ((FOffset + TempSize) >= FBufferSize) and (Grow(TempSize) = 0) then
|
2017-07-18 17:50:28 +02:00
|
|
|
Result := 0
|
|
|
|
else
|
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
TempPointer := Pointer(cardinal(FBuffer) + FOffset);
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2019-01-08 20:15:25 +02:00
|
|
|
Move(ptr^, TempPointer^, TempSize);
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2018-10-04 20:45:31 +02:00
|
|
|
FOffset := FOffset + TempSize;
|
2017-07-18 17:50:28 +02:00
|
|
|
Result := n;
|
|
|
|
end;
|
|
|
|
|
|
|
|
LeaveCriticalSection(FCriticalSection);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.Seek(offset: Int64; whence: Integer): Integer;
|
|
|
|
const
|
|
|
|
SEEK_SET = 0;
|
|
|
|
SEEK_CUR = 1;
|
|
|
|
SEEK_END = 2;
|
|
|
|
var
|
|
|
|
TempAbsOffset : int64;
|
|
|
|
begin
|
|
|
|
EnterCriticalSection(FCriticalSection);
|
|
|
|
|
|
|
|
Result := -1;
|
|
|
|
|
|
|
|
case whence of
|
|
|
|
SEEK_CUR :
|
2018-05-12 14:50:54 +02:00
|
|
|
if not((FOffset + offset > FBufferSize) or (FOffset + offset < 0)) then
|
2017-07-18 17:50:28 +02:00
|
|
|
begin
|
|
|
|
FOffset := FOffset + offset;
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
SEEK_END:
|
|
|
|
begin
|
|
|
|
TempAbsOffset := abs(offset);
|
|
|
|
|
2018-05-12 14:50:54 +02:00
|
|
|
if not(TempAbsOffset > FBufferSize) then
|
2017-07-18 17:50:28 +02:00
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
FOffset := FBufferSize - TempAbsOffset;
|
2017-07-18 17:50:28 +02:00
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
SEEK_SET:
|
2018-05-12 14:50:54 +02:00
|
|
|
if not((offset > FBufferSize) or (offset < 0)) then
|
2017-07-18 17:50:28 +02:00
|
|
|
begin
|
|
|
|
FOffset := offset;
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
end;
|
|
|
|
|
|
|
|
LeaveCriticalSection(FCriticalSection);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.Tell: Int64;
|
|
|
|
begin
|
|
|
|
EnterCriticalSection(FCriticalSection);
|
|
|
|
|
|
|
|
Result := FOffset;
|
|
|
|
|
|
|
|
LeaveCriticalSection(FCriticalSection);
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.Flush: Integer;
|
|
|
|
begin
|
|
|
|
Result := 0;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.MayBlock: Boolean;
|
|
|
|
begin
|
|
|
|
Result := False;
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.GetData : pointer;
|
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
Result := FBuffer;
|
2017-07-18 17:50:28 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.GetDataSize : int64;
|
|
|
|
begin
|
2018-05-12 14:50:54 +02:00
|
|
|
Result := FBufferSize;
|
2017-07-18 17:50:28 +02:00
|
|
|
end;
|
|
|
|
|
|
|
|
function TCefBytesWriteHandler.Grow(size : NativeUInt) : NativeUInt;
|
|
|
|
var
|
2018-10-04 20:45:31 +02:00
|
|
|
TempTotal : int64;
|
2017-07-18 17:50:28 +02:00
|
|
|
begin
|
2018-10-04 20:45:31 +02:00
|
|
|
try
|
|
|
|
EnterCriticalSection(FCriticalSection);
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2018-10-04 21:24:59 +02:00
|
|
|
if (size < FGrow) then
|
|
|
|
TempTotal := FGrow
|
|
|
|
else
|
|
|
|
TempTotal := size;
|
|
|
|
|
2018-10-04 20:45:31 +02:00
|
|
|
inc(TempTotal, FBufferSize);
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2018-10-04 20:45:31 +02:00
|
|
|
ReallocMem(FBuffer, TempTotal);
|
2017-07-18 17:50:28 +02:00
|
|
|
|
2018-10-04 20:45:31 +02:00
|
|
|
FBufferSize := TempTotal;
|
|
|
|
Result := FBufferSize;
|
|
|
|
finally
|
|
|
|
LeaveCriticalSection(FCriticalSection);
|
|
|
|
end;
|
2017-07-18 17:50:28 +02:00
|
|
|
end;
|
|
|
|
|
2017-01-27 17:37:51 +02:00
|
|
|
end.
|