CEF4Delphi/source/uCEFCommandLine.pas

326 lines
9.1 KiB
ObjectPascal
Raw Normal View History

2017-01-27 17:29:37 +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.
2017-01-27 17:29:37 +01: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 uCEFCommandLine;
{$IFNDEF CPUX64}
{$ALIGN ON}
{$MINENUMSIZE 4}
{$ENDIF}
2017-02-05 20:56:46 +01:00
{$I cef.inc}
2017-01-27 17:29:37 +01:00
interface
uses
2017-02-05 20:56:46 +01:00
{$IFDEF DELPHI16_UP}
2018-02-26 18:40:51 +01:00
System.Classes, System.SysUtils,
2017-02-05 20:56:46 +01:00
{$ELSE}
2018-02-26 18:40:51 +01:00
Classes, SysUtils,
2017-02-05 20:56:46 +01:00
{$ENDIF}
uCEFBaseRefCounted, uCEFTypes, uCEFInterfaces;
2017-01-27 17:29:37 +01:00
type
TCefCommandLineRef = class(TCefBaseRefCountedRef, ICefCommandLine)
2017-01-27 17:29:37 +01:00
protected
function IsValid: Boolean;
function IsReadOnly: Boolean;
function Copy: ICefCommandLine;
procedure InitFromArgv(argc: Integer; const argv: PPAnsiChar);
procedure InitFromString(const commandLine: ustring);
procedure Reset;
function GetCommandLineString: ustring;
2018-02-26 18:40:51 +01:00
procedure GetArgv(var args: TStrings);
2017-01-27 17:29:37 +01:00
function GetProgram: ustring;
procedure SetProgram(const prog: ustring);
function HasSwitches: Boolean;
function HasSwitch(const name: ustring): Boolean;
function GetSwitchValue(const name: ustring): ustring;
2018-02-26 18:40:51 +01:00
procedure GetSwitches(var switches: TStrings);
2017-01-27 17:29:37 +01:00
procedure AppendSwitch(const name: ustring);
procedure AppendSwitchWithValue(const name, value: ustring);
function HasArguments: Boolean;
2018-02-26 18:40:51 +01:00
procedure GetArguments(var arguments: TStrings);
2017-01-27 17:29:37 +01:00
procedure AppendArgument(const argument: ustring);
procedure PrependWrapper(const wrapper: ustring);
public
class function UnWrap(data: Pointer): ICefCommandLine;
class function New: ICefCommandLine;
class function Global: ICefCommandLine;
end;
implementation
uses
uCEFMiscFunctions, uCEFLibFunctions;
procedure TCefCommandLineRef.AppendArgument(const argument: ustring);
var
a: TCefString;
begin
a := CefString(argument);
PCefCommandLine(FData).append_argument(PCefCommandLine(FData), @a);
end;
procedure TCefCommandLineRef.AppendSwitch(const name: ustring);
var
n: TCefString;
begin
n := CefString(name);
PCefCommandLine(FData).append_switch(PCefCommandLine(FData), @n);
end;
procedure TCefCommandLineRef.AppendSwitchWithValue(const name, value: ustring);
var
n, v: TCefString;
begin
n := CefString(name);
v := CefString(value);
PCefCommandLine(FData).append_switch_with_value(PCefCommandLine(FData), @n, @v);
end;
function TCefCommandLineRef.Copy: ICefCommandLine;
begin
Result := UnWrap(PCefCommandLine(FData).copy(PCefCommandLine(FData)));
end;
2018-02-26 18:40:51 +01:00
procedure TCefCommandLineRef.GetArguments(var arguments : TStrings);
2017-01-27 17:29:37 +01:00
var
2018-02-26 18:40:51 +01:00
TempSL : TCefStringList;
i, j : NativeUInt;
TempString : TCefString;
2017-01-27 17:29:37 +01:00
begin
2018-02-26 18:40:51 +01:00
TempSL := nil;
2017-01-27 17:29:37 +01:00
try
2018-02-26 18:40:51 +01:00
try
if (arguments <> nil) then
begin
TempSL := cef_string_list_alloc;
PCefCommandLine(FData).get_arguments(PCefCommandLine(FData), TempSL);
i := 0;
j := cef_string_list_size(TempSL);
while (i < j) do
begin
FillChar(TempString, SizeOf(TempString), 0);
cef_string_list_value(TempSL, i, @TempString);
arguments.Add(CefStringClearAndGet(TempString));
inc(i);
end;
end;
except
on e : exception do
if CustomExceptionHandler('TCefCommandLineRef.GetArguments', e) then raise;
2017-01-27 17:29:37 +01:00
end;
finally
2018-02-26 18:40:51 +01:00
if (TempSL <> nil) then cef_string_list_free(TempSL);
2017-01-27 17:29:37 +01:00
end;
end;
2018-02-26 18:40:51 +01:00
procedure TCefCommandLineRef.GetArgv(var args: TStrings);
2017-01-27 17:29:37 +01:00
var
2018-02-26 18:40:51 +01:00
TempSL : TCefStringList;
i, j : NativeUInt;
TempString : TCefString;
2017-01-27 17:29:37 +01:00
begin
2018-02-26 18:40:51 +01:00
TempSL := nil;
2017-01-27 17:29:37 +01:00
try
2018-02-26 18:40:51 +01:00
try
if (args <> nil) then
begin
TempSL := cef_string_list_alloc;
PCefCommandLine(FData).get_argv(PCefCommandLine(FData), TempSL);
i := 0;
j := cef_string_list_size(TempSL);
while (i < j) do
begin
FillChar(TempString, SizeOf(TempString), 0);
cef_string_list_value(TempSL, i, @TempString);
args.Add(CefStringClearAndGet(TempString));
inc(i);
end;
end;
except
on e : exception do
if CustomExceptionHandler('TCefCommandLineRef.GetArgv', e) then raise;
2017-01-27 17:29:37 +01:00
end;
finally
2018-02-26 18:40:51 +01:00
if (TempSL <> nil) then cef_string_list_free(TempSL);
2017-01-27 17:29:37 +01:00
end;
end;
function TCefCommandLineRef.GetCommandLineString: ustring;
begin
Result := CefStringFreeAndGet(PCefCommandLine(FData).get_command_line_string(PCefCommandLine(FData)));
end;
function TCefCommandLineRef.GetProgram: ustring;
begin
Result := CefStringFreeAndGet(PCefCommandLine(FData).get_program(PCefCommandLine(FData)));
end;
2018-02-26 18:40:51 +01:00
procedure TCefCommandLineRef.GetSwitches(var switches: TStrings);
2017-01-27 17:29:37 +01:00
var
2018-02-26 18:40:51 +01:00
TempSL : TCefStringList;
i, j : NativeUInt;
TempString : TCefString;
2017-01-27 17:29:37 +01:00
begin
2018-02-26 18:40:51 +01:00
TempSL := nil;
2017-01-27 17:29:37 +01:00
try
2018-02-26 18:40:51 +01:00
try
if (switches <> nil) then
begin
TempSL := cef_string_list_alloc;
PCefCommandLine(FData).get_switches(PCefCommandLine(FData), TempSL);
i := 0;
j := cef_string_list_size(TempSL);
while (i < j) do
begin
FillChar(TempString, SizeOf(TempString), 0);
cef_string_list_value(TempSL, i, @TempString);
switches.Add(CefStringClearAndGet(TempString));
inc(i);
end;
end;
except
on e : exception do
if CustomExceptionHandler('TCefCommandLineRef.GetSwitches', e) then raise;
2017-01-27 17:29:37 +01:00
end;
finally
2018-02-26 18:40:51 +01:00
if (TempSL <> nil) then cef_string_list_free(TempSL);
2017-01-27 17:29:37 +01:00
end;
end;
function TCefCommandLineRef.GetSwitchValue(const name: ustring): ustring;
var
n: TCefString;
begin
n := CefString(name);
Result := CefStringFreeAndGet(PCefCommandLine(FData).get_switch_value(PCefCommandLine(FData), @n));
end;
class function TCefCommandLineRef.Global: ICefCommandLine;
begin
Result := UnWrap(cef_command_line_get_global);
end;
function TCefCommandLineRef.HasArguments: Boolean;
begin
Result := PCefCommandLine(FData).has_arguments(PCefCommandLine(FData)) <> 0;
end;
function TCefCommandLineRef.HasSwitch(const name: ustring): Boolean;
var
n: TCefString;
begin
n := CefString(name);
Result := PCefCommandLine(FData).has_switch(PCefCommandLine(FData), @n) <> 0;
end;
function TCefCommandLineRef.HasSwitches: Boolean;
begin
Result := PCefCommandLine(FData).has_switches(PCefCommandLine(FData)) <> 0;
end;
procedure TCefCommandLineRef.InitFromArgv(argc: Integer;
const argv: PPAnsiChar);
begin
PCefCommandLine(FData).init_from_argv(PCefCommandLine(FData), argc, argv);
end;
procedure TCefCommandLineRef.InitFromString(const commandLine: ustring);
var
cl: TCefString;
begin
cl := CefString(commandLine);
PCefCommandLine(FData).init_from_string(PCefCommandLine(FData), @cl);
end;
function TCefCommandLineRef.IsReadOnly: Boolean;
begin
Result := PCefCommandLine(FData).is_read_only(PCefCommandLine(FData)) <> 0;
end;
function TCefCommandLineRef.IsValid: Boolean;
begin
Result := PCefCommandLine(FData).is_valid(PCefCommandLine(FData)) <> 0;
end;
class function TCefCommandLineRef.New: ICefCommandLine;
begin
Result := UnWrap(cef_command_line_create);
end;
procedure TCefCommandLineRef.PrependWrapper(const wrapper: ustring);
var
w: TCefString;
begin
w := CefString(wrapper);
PCefCommandLine(FData).prepend_wrapper(PCefCommandLine(FData), @w);
end;
procedure TCefCommandLineRef.Reset;
begin
PCefCommandLine(FData).reset(PCefCommandLine(FData));
end;
procedure TCefCommandLineRef.SetProgram(const prog: ustring);
var
p: TCefString;
begin
p := CefString(prog);
PCefCommandLine(FData).set_program(PCefCommandLine(FData), @p);
end;
class function TCefCommandLineRef.UnWrap(data: Pointer): ICefCommandLine;
begin
if data <> nil then
Result := Create(data) as ICefCommandLine else
Result := nil;
end;
end.