Skip to content

Commit

Permalink
Added debug logging properties
Browse files Browse the repository at this point in the history
Added the GlobalWebView2Loader.DebugLog property
Added the GlobalWebView2Loader.DebugLogLevel property
  • Loading branch information
salvadordf committed Feb 9, 2022
1 parent aac3645 commit a27211c
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 13 deletions.
3 changes: 3 additions & 0 deletions demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,9 @@ procedure TMiniBrowserFrm.WMMoving(var aMessage : TMessage);
initialization
GlobalWebView2Loader := TWVLoader.Create(nil);
GlobalWebView2Loader.UserDataFolder := ExtractFileDir(Application.ExeName) + '\CustomCache';
// Uncomment these lines to enable the debug log in 'CustomCache\EBWebView\chrome_debug.log'
//GlobalWebView2Loader.DebugLog := TWV2DebugLog.dlEnabled;
//GlobalWebView2Loader.DebugLogLevel := TWV2DebugLogLevel.dllInfo;
GlobalWebView2Loader.StartWebView2;

end.
4 changes: 2 additions & 2 deletions demos/Lazarus/MiniBrowser/MiniBrowser.lps
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
<ResourceBaseClass Value="Form"/>
<IsVisibleTab Value="True"/>
<EditorIndex Value="1"/>
<TopLine Value="259"/>
<CursorPos X="49" Y="273"/>
<TopLine Value="726"/>
<CursorPos X="66" Y="753"/>
<UsageCount Value="22"/>
<Bookmarks Count="1">
<Item0 X="106" Y="301" ID="1"/>
Expand Down
3 changes: 3 additions & 0 deletions demos/Lazarus/MiniBrowser/uMiniBrowser.pas
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,9 @@ procedure TMiniBrowserFrm.WMMoving(var aMessage : TMessage);
initialization
GlobalWebView2Loader := TWVLoader.Create(nil);
GlobalWebView2Loader.UserDataFolder := UTF8Decode(ExtractFileDir(Application.ExeName) + '\CustomCache');
// Uncomment these lines to enable the debug log in 'CustomCache\EBWebView\chrome_debug.log'
//GlobalWebView2Loader.DebugLog := TWV2DebugLog.dlEnabled;
//GlobalWebView2Loader.DebugLogLevel := TWV2DebugLogLevel.dllInfo;
GlobalWebView2Loader.StartWebView2;

end.
29 changes: 19 additions & 10 deletions source/uWVLoader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ TWVLoader = class(TComponent, IWVLoaderEvents)
FAllowRunningInsecureContent : boolean;
FDisableBackgroundNetworking : boolean;
FRemoteDebuggingPort : integer;
FDebugLog : TWV2DebugLog;
FDebugLogLevel : TWV2DebugLogLevel;

function GetAvailableBrowserVersion : wvstring;
function GetInitialized : boolean;
Expand All @@ -82,7 +84,6 @@ TWVLoader = class(TComponent, IWVLoaderEvents)
function CreateEnvironment : boolean;
procedure DestroyEnvironment;

function GetModulePath : wvstring;
function GetDLLVersion(const aDLLFile : wvstring; var aVersionInfo : TFileVersionInfo) : boolean;
function GetExtendedFileVersion(const aFileName : wvstring) : uint64;
function LoadLibProcedures : boolean;
Expand Down Expand Up @@ -169,6 +170,8 @@ TWVLoader = class(TComponent, IWVLoaderEvents)
property DisableBackgroundNetworking : boolean read FDisableBackgroundNetworking write FDisableBackgroundNetworking; // --disable-background-networking
property ForcedDeviceScaleFactor : single read FForcedDeviceScaleFactor write FForcedDeviceScaleFactor; // --force-device-scale-factor
property RemoteDebuggingPort : integer read FRemoteDebuggingPort write FRemoteDebuggingPort; // --remote-debugging-port
property DebugLog : TWV2DebugLog read FDebugLog write FDebugLog; // --enable-logging
property DebugLogLevel : TWV2DebugLogLevel read FDebugLogLevel write FDebugLogLevel; // --log-level

// ICoreWebView2Environment8 properties
property ProcessInfos : ICoreWebView2ProcessInfoCollection read GetProcessInfos;
Expand Down Expand Up @@ -278,6 +281,8 @@ constructor TWVLoader.Create(AOwner: TComponent);
FAllowFileAccessFromFiles := False;
FAllowRunningInsecureContent := False;
FDisableBackgroundNetworking := False;
FDebugLog := TWV2DebugLog.dlDisabled;
FDebugLogLevel := TWV2DebugLogLevel.dllDefault;

FProxySettings := TWVProxySettings.Create;

Expand Down Expand Up @@ -335,15 +340,6 @@ procedure TWVLoader.doProcessInfosChangedEvent(const sender: ICoreWebView2Enviro
FOnProcessInfosChanged(self, sender);
end;

function TWVLoader.GetModulePath : wvstring;
begin
{$IFDEF FPC}
Result := UTF8Decode(IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleName(HINSTANCE))));
{$ELSE}
Result := IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleName(HINSTANCE)));
{$ENDIF}
end;

function TWVLoader.GetExtendedFileVersion(const aFileName : wvstring) : uint64;
var
TempSize : DWORD;
Expand Down Expand Up @@ -931,6 +927,19 @@ function TWVLoader.GetCustomCommandLineSwitches : wvstring;
if (FRemoteDebuggingPort > 0) then
Result := Result + '--remote-debugging-port=' + inttostr(FRemoteDebuggingPort) + ' ';

case FDebugLog of
TWV2DebugLog.dlEnabled : Result := Result + '--enable-logging ';
TWV2DebugLog.dlEnabledStdOut : Result := Result + '--enable-logging=stdout ';
TWV2DebugLog.dlEnabledStdErr : Result := Result + '--enable-logging=stderr ';
end;

case FDebugLogLevel of
TWV2DebugLogLevel.dllInfo : Result := Result + '--log-level=0 ';
TWV2DebugLogLevel.dllWarning : Result := Result + '--log-level=1 ';
TWV2DebugLogLevel.dllError : Result := Result + '--log-level=2 ';
TWV2DebugLogLevel.dllFatal : Result := Result + '--log-level=3 ';
end;

if (length(FAdditionalBrowserArguments) > 0) then
Result := Result + FAdditionalBrowserArguments
else
Expand Down
104 changes: 104 additions & 0 deletions source/uWVMiscFunctions.pas
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface
{$ENDIF}
uWVConstants, uWVTypeLibrary, uWVTypes;

const
SHLWAPIDLL = 'shlwapi.dll';

function AllocCoTaskMemStr(const aString : wvstring): PWideChar;
function LowestChromiumVersion : wvstring;
function LowestLoaderDLLVersion : wvstring;
Expand All @@ -33,6 +36,23 @@ function TryIso8601BasicToDate(const Str: string; out Date: TDateTime): Boolean;
function JSONUnescape(const Source: wvstring): wvstring;
function JSONEscape(const Source: wvstring): wvstring;

function CustomPathIsRelative(const aPath : wvstring) : boolean;
function CustomPathCanonicalize(const aOriginalPath : wvstring; var aCanonicalPath : wvstring) : boolean;
function CustomAbsolutePath(const aPath : wvstring) : wvstring;
function CustomPathIsURL(const aPath : wvstring) : boolean;
function CustomPathIsUNC(const aPath : wvstring) : boolean;
function GetModulePath : wvstring;
function EscapeCommandLineParameter(const aParameter : wvstring) : wvstring;

function PathIsRelativeAnsi(pszPath: LPCSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsRelativeA';
function PathIsRelativeUnicode(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsRelativeW';
function PathCanonicalizeAnsi(pszBuf: LPSTR; pszPath: LPCSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathCanonicalizeA';
function PathCanonicalizeUnicode(pszBuf: LPWSTR; pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathCanonicalizeW';
function PathIsUNCAnsi(pszPath: LPCSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsUNCA';
function PathIsUNCUnicode(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsUNCW';
function PathIsURLAnsi(pszPath: LPCSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsURLA';
function PathIsURLUnicode(pszPath: LPCWSTR): BOOL; stdcall; external SHLWAPIDLL name 'PathIsURLW';

implementation

uses
Expand Down Expand Up @@ -445,4 +465,88 @@ function DeleteDirContents(const aDirectory : string; const aExcludeFiles : TStr
end;
end;

function CustomPathIsRelative(const aPath : wvstring) : boolean;
begin
Result := PathIsRelativeUnicode(PWideChar(aPath));
end;

function CustomPathIsURL(const aPath : wvstring) : boolean;
begin
Result := PathIsURLUnicode(PWideChar(aPath + #0));
end;

function CustomPathIsUNC(const aPath : wvstring) : boolean;
begin
Result := PathIsUNCUnicode(PWideChar(aPath + #0));
end;

function CustomPathCanonicalize(const aOriginalPath : wvstring; var aCanonicalPath : wvstring) : boolean;
var
TempBuffer: array [0..pred(MAX_PATH)] of WideChar;
begin
Result := False;
aCanonicalPath := '';

if (length(aOriginalPath) > MAX_PATH) or
(Copy(aOriginalPath, 1, 4) = '\\?\') or
CustomPathIsUNC(aOriginalPath) then
exit;

FillChar(TempBuffer, MAX_PATH * SizeOf(WideChar), 0);

if PathCanonicalizeUnicode(@TempBuffer[0], PWideChar(aOriginalPath + #0)) then
begin
aCanonicalPath := TempBuffer;
Result := True;
end;
end;

function CustomAbsolutePath(const aPath : wvstring) : wvstring;
var
TempNewPath, TempOldPath : wvstring;
begin
if (length(aPath) > 0) then
begin
if CustomPathIsRelative(aPath) then
TempOldPath := GetModulePath + aPath
else
TempOldPath := aPath;

if not(CustomPathCanonicalize(TempOldPath, TempNewPath)) then
TempNewPath := TempOldPath;

Result := TempNewPath;
end
else
Result := '';
end;

function GetModulePath : wvstring;
begin
{$IFDEF FPC}
Result := UTF8Decode(IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleName(HINSTANCE))));
{$ELSE}
Result := IncludeTrailingPathDelimiter(ExtractFileDir(GetModuleName(HINSTANCE)));
{$ENDIF}
end;

function EscapeCommandLineParameter(const aParameter : wvstring) : wvstring;
const
RESERVEDCHARS = ['`', '~', '!', '#', '$', '&', '*', '(', ')', #9, '{', '[', '|', '\', ';', #39, '"', #10, '<', '>', '?', ' '];
var
i : integer;
begin
i := 1;

while (i <= length(aParameter)) do
begin
if CharInSet(aParameter[i], RESERVEDCHARS) then
Result := Result + '\' + aParameter[i]
else
Result := Result + aParameter[i];

inc(i);
end;
end;

end.
9 changes: 9 additions & 0 deletions source/uWVTypes.pas
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ interface
ketRawKeyDown,
ketChar);

TWV2DebugLog = (dlDisabled, dlEnabled, dlEnabledStdOut, dlEnabledStdErr);

// Debug log level used when the logging is enabled
TWV2DebugLogLevel = (dllDefault,
dllInfo,
dllWarning,
dllError,
dllFatal);

// Blink editing commands used by the "Input.dispatchKeyEvent" DevTools method.
// https://chromedevtools.github.io/devtools-protocol/1-3/Input/#method-dispatchKeyEvent
// https://source.chromium.org/chromium/chromium/src/+/master:third_party/blink/renderer/core/editing/commands/editor_command_names.h
Expand Down
2 changes: 1 addition & 1 deletion update_CEF4Delphi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"UpdateLazPackages" : [
{
"ForceNotify" : true,
"InternalVersion" : 30,
"InternalVersion" : 31,
"Name" : "webview4delphi.lpk",
"Version" : "1.0.1108.44"
}
Expand Down

0 comments on commit a27211c

Please sign in to comment.