From 6e936cf07c7329a57ae5b5ac512b6106c7087214 Mon Sep 17 00:00:00 2001 From: Salvador Diaz Fau Date: Wed, 30 Sep 2020 12:43:48 +0200 Subject: [PATCH] Fixed issue #303 - Added TChromiumCore.ReplaceMisspelling - Added TChromiumCore.AddWordToDictionary - Added TChromiumCore.OpenerWindowHandle - Added TChromiumCore.MouseCursorChangeDisabled - Removed the 'Print to PDF stream...' menu option from the MiniBrowser demo because it's only available in headless chrome. --- demos/Delphi_VCL/MiniBrowser/uMiniBrowser.dfm | 4 - demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas | 84 +++---------------- source/uCEFChromiumCore.pas | 62 ++++++++++++-- source/uCEFMiscFunctions.pas | 2 +- update_CEF4Delphi.json | 2 +- 5 files changed, 66 insertions(+), 88 deletions(-) diff --git a/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.dfm b/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.dfm index 45e5967c..9d001f48 100644 --- a/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.dfm +++ b/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.dfm @@ -327,10 +327,6 @@ object MiniBrowserFrm: TMiniBrowserFrm Caption = 'Print to PDF file...' OnClick = PrintinPDF1Click end - object PrinttoPDFstream1: TMenuItem - Caption = 'Print to PDF stream...' - OnClick = PrinttoPDFstream1Click - end object N3: TMenuItem Caption = '-' end diff --git a/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas b/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas index 781dca0d..ea6177e1 100644 --- a/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas +++ b/demos/Delphi_VCL/MiniBrowser/uMiniBrowser.pas @@ -138,7 +138,6 @@ type akescreenshot1: TMenuItem; Useragent1: TMenuItem; ClearallstorageforcurrentURL1: TMenuItem; - PrinttoPDFstream1: TMenuItem; procedure FormShow(Sender: TObject); procedure FormCreate(Sender: TObject); @@ -207,13 +206,10 @@ type procedure akescreenshot1Click(Sender: TObject); procedure Useragent1Click(Sender: TObject); procedure ClearallstorageforcurrentURL1Click(Sender: TObject); - procedure PrinttoPDFstream1Click(Sender: TObject); protected - FDevToolsMsgID : integer; - FPrintToPDFMsgID : integer; - FScreenshotMsgID : integer; - + FDevToolsMsgID : integer; + FScreenshotMsgID : integer; FDevToolsMsgValue : ustring; FResponse : TStringList; @@ -224,7 +220,6 @@ type FClosing : boolean; // Set to True in the CloseQuery event. procedure AddURL(const aURL : string); - procedure PrintToPDFStream(aUseChromiumPDFSettings : boolean = False; const aPageRanges : string = ''; const aHeaderTemplate : string = ''; const aFooterTemplate : string = ''; aIgnoreInvalidPageRanges : boolean = False; aPreferCSSPageSize : boolean = False); procedure ShowDevTools(aPoint : TPoint); overload; procedure ShowDevTools; overload; @@ -587,59 +582,6 @@ begin showmessage('There was a problem generating the PDF file.'); end; -// This procedure uses the "Page.printToPDF" method from the DevTools : -// https://chromedevtools.github.io/devtools-protocol/tot/Page/#method-printToPDF -procedure TMiniBrowserFrm.PrintToPDFStream( aUseChromiumPDFSettings : boolean; - const aPageRanges, aHeaderTemplate, aFooterTemplate : string; - aIgnoreInvalidPageRanges, aPreferCSSPageSize : boolean); -var - TempParams : ICefDictionaryValue; -begin - try - TempParams := TCefDictionaryValueRef.New; - - if aUseChromiumPDFSettings then - begin - TempParams.SetBool('landscape', Chromium1.PDFPrintOptions.landscape); - TempParams.SetBool('displayHeaderFooter', Chromium1.PDFPrintOptions.header_footer_enabled); - TempParams.SetBool('printBackground', Chromium1.PDFPrintOptions.backgrounds_enabled); - TempParams.SetDouble('scale', Chromium1.PDFPrintOptions.scale_factor / 100); // integer percent to double - TempParams.SetDouble('paperWidth', Chromium1.PDFPrintOptions.page_width * 0.000039370); // microns to inches - TempParams.SetDouble('paperHeight', Chromium1.PDFPrintOptions.page_height * 0.000039370); // microns to inches - TempParams.SetDouble('marginTop', Chromium1.PDFPrintOptions.margin_top / 72); // points to inches - TempParams.SetDouble('marginBottom', Chromium1.PDFPrintOptions.margin_bottom / 72); // points to inches - TempParams.SetDouble('marginLeft', Chromium1.PDFPrintOptions.margin_left / 72); // points to inches - TempParams.SetDouble('marginRight', Chromium1.PDFPrintOptions.margin_right / 72); // points to inches - end; - - if (length(aPageRanges) > 0) then - begin - TempParams.SetString('pageRanges', aPageRanges); - TempParams.SetBool('ignoreInvalidPageRanges', aIgnoreInvalidPageRanges); - end; - - if (length(aHeaderTemplate) > 0) then - TempParams.SetString('headerTemplate', aHeaderTemplate); - - if (length(aFooterTemplate) > 0) then - TempParams.SetString('footerTemplate', aFooterTemplate); - - TempParams.SetBool('preferCSSPageSize', aPreferCSSPageSize); - TempParams.SetString('transferMode', 'ReturnAsBase64'); - - inc(FDevToolsMsgID); - FPrintToPDFMsgID := FDevToolsMsgID; - Chromium1.ExecuteDevToolsMethod(FPrintToPDFMsgID, 'Page.printToPDF', TempParams); - finally - TempParams := nil; - end; -end; - -procedure TMiniBrowserFrm.PrinttoPDFstream1Click(Sender: TObject); -begin - PrintToPDFStream; -end; - procedure TMiniBrowserFrm.PreferencesAvailableMsg(var aMessage : TMessage); begin if (aMessage.lParam <> 0) then @@ -1181,10 +1123,10 @@ procedure TMiniBrowserFrm.Chromium1DevToolsMethodResult( Sender : TObje success : Boolean; const result : ICefValue); var - TempDict : ICefDictionaryValue; - TempValue : ICefValue; - TempResult : WPARAM; - TempCode : integer; + TempDict : ICefDictionaryValue; + TempValue : ICefValue; + TempResult : WPARAM; + TempCode : integer; TempMessage : string; begin FDevToolsMsgValue := ''; @@ -1249,16 +1191,10 @@ begin SaveDialog1.Filter := 'PNG files (*.png)|*.PNG'; end else - if (aMessage.LParam = FPrintToPDFMsgID) then - begin - SaveDialog1.DefaultExt := 'pdf'; - SaveDialog1.Filter := 'PDF files (*.pdf)|*.PDF'; - end - else - begin - SaveDialog1.DefaultExt := ''; - SaveDialog1.Filter := 'All files (*.*)|*.*'; - end; + begin + SaveDialog1.DefaultExt := ''; + SaveDialog1.Filter := 'All files (*.*)|*.*'; + end; if SaveDialog1.Execute then try diff --git a/source/uCEFChromiumCore.pas b/source/uCEFChromiumCore.pas index 575fa35c..15123135 100644 --- a/source/uCEFChromiumCore.pas +++ b/source/uCEFChromiumCore.pas @@ -336,6 +336,7 @@ type function GetZoomStep : byte; function GetIsPopUp : boolean; function GetWindowHandle : TCefWindowHandle; + function GetOpenerWindowHandle : TCefWindowHandle; function GetWindowlessFrameRate : integer; function GetFrameIsFocused : boolean; function GetInitialized : boolean; @@ -353,6 +354,7 @@ type function GetBrowserById(aID : integer) : ICefBrowser; function GetBrowserCount : integer; function GetBrowserIdByIndex(aIndex : integer) : integer; + function GetMouseCursorChangeDisabled : boolean; procedure SetDoNotTrack(aValue : boolean); procedure SetSendReferrer(aValue : boolean); @@ -395,6 +397,7 @@ type procedure SetQuicAllowed(aValue : boolean); procedure SetJavascriptEnabled(aValue : boolean); procedure SetLoadImagesAutomatically(aValue : boolean); + procedure SetMouseCursorChangeDisabled(aValue : boolean); function CreateBrowserHost(aWindowInfo : PCefWindowInfo; const aURL : ustring; const aSettings : PCefBrowserSettings; const aExtraInfo : ICefDictionaryValue; const aContext : ICefRequestContext): boolean; function CreateBrowserHostSync(aWindowInfo : PCefWindowInfo; const aURL : ustring; const aSettings : PCefBrowserSettings; const aExtraInfo : ICefDictionaryValue; const aContext : ICefRequestContext): Boolean; @@ -732,7 +735,8 @@ type procedure UpdateSupportedSchemes(const aSchemes : TStrings; aIncludeDefaults : boolean = True); procedure ShowDevTools(const inspectElementAt: TPoint; aWindowInfo: PCefWindowInfo); - procedure CloseDevTools(const aDevToolsWnd : TCefWindowHandle = 0); + procedure CloseDevTools; overload; + procedure CloseDevTools(const aDevToolsWnd : TCefWindowHandle); overload; function SendDevToolsMessage(const message_: ustring): boolean; function ExecuteDevToolsMethod(message_id: integer; const method: ustring; const params: ICefDictionaryValue): Integer; function AddDevToolsMessageObserver(const observer: ICefDevToolsMessageObserver): ICefRegistration; @@ -797,6 +801,9 @@ type procedure IMEFinishComposingText(keep_selection : boolean); procedure IMECancelComposition; + procedure ReplaceMisspelling(const aWord : ustring); + procedure AddWordToDictionary(const aWord : ustring); + // ICefMediaRouter methods function AddObserver(const observer: ICefMediaObserver): ICefRegistration; function GetSource(const urn: ustring): ICefMediaSource; @@ -845,6 +852,7 @@ type property CanGoForward : boolean read GetCanGoForward; property IsPopUp : boolean read GetIsPopUp; property WindowHandle : TCefWindowHandle read GetWindowHandle; + property OpenerWindowHandle : TCefWindowHandle read GetOpenerWindowHandle; {$IFDEF MSWINDOWS} property BrowserHandle : THandle read FBrowserCompHWND; property WidgetHandle : THandle read FWidgetCompHWND; @@ -886,6 +894,7 @@ type property QuicAllowed : boolean read FQuicAllowed write SetQuicAllowed; property JavascriptEnabled : boolean read FJavascriptEnabled write SetJavascriptEnabled; property LoadImagesAutomatically : boolean read FLoadImagesAutomatically write SetLoadImagesAutomatically; + property MouseCursorChangeDisabled : boolean read GetMouseCursorChangeDisabled write SetMouseCursorChangeDisabled; property WebRTCIPHandlingPolicy : TCefWebRTCHandlingPolicy read FWebRTCIPHandlingPolicy write SetWebRTCIPHandlingPolicy; property WebRTCMultipleRoutes : TCefState read FWebRTCMultipleRoutes write SetWebRTCMultipleRoutes; @@ -1205,7 +1214,7 @@ begin FHyperlinkAuditing := True; // - // Somo focus issues in CEF seem to be fixed when you use WS_EX_NOACTIVATE in + // Some focus issues in CEF seem to be fixed when you use WS_EX_NOACTIVATE in // FDefaultWindowInfoExStyle to initialize the browser with that ExStyle but // it may cause side effects. Read these links for more information : // https://www.briskbard.com/forum/viewtopic.php?f=10&t=723 @@ -2534,10 +2543,18 @@ end; function TChromiumCore.GetWindowHandle : TCefWindowHandle; begin + InitializeWindowHandle(Result); + if Initialized then - Result := Browser.Host.WindowHandle - else - Result := 0; + Result := Browser.Host.WindowHandle; +end; + +function TChromiumCore.GetOpenerWindowHandle : TCefWindowHandle; +begin + InitializeWindowHandle(Result); + + if Initialized then + Result := Browser.Host.OpenerWindowHandle; end; function TChromiumCore.GetFrameIsFocused : boolean; @@ -2665,6 +2682,11 @@ begin Result := Initialized and Browser.host.RequestContext.IsGlobal; end; +function TChromiumCore.GetMouseCursorChangeDisabled : boolean; +begin + Result := Initialized and Browser.host.IsMouseCursorChangeDisabled; +end; + function TChromiumCore.GetAudioMuted : boolean; begin Result := Initialized and Browser.host.IsAudioMuted; @@ -2672,7 +2694,7 @@ end; function TChromiumCore.GetParentFormHandle : TCefWindowHandle; begin - Result := 0; + InitializeWindowHandle(Result); end; procedure TChromiumCore.SetMultiBrowserMode(aValue : boolean); @@ -2716,6 +2738,12 @@ begin end; end; +procedure TChromiumCore.SetMouseCursorChangeDisabled(aValue : boolean); +begin + if Initialized then + Browser.Host.SetMouseCursorChangeDisabled(aValue); +end; + procedure TChromiumCore.SetAudioMuted(aValue : boolean); begin if Initialized then @@ -3941,7 +3969,7 @@ begin UpdatePreference(aBrowser, 'webrtc.nonproxied_udp_enabled', (FWebRTCNonProxiedUDP = STATE_ENABLED)); UpdatePreference(aBrowser, 'net.network_prediction_options', integer(FNetworkPredictions)); - UpdatePreference(aBrowser, 'net.quic_allowed', FQuicAllowed); + UpdatePreference(aBrowser, 'net.quic_allowed', FQuicAllowed); UpdatePreference(aBrowser, 'webkit.webprefs.javascript_enabled', FJavascriptEnabled); UpdatePreference(aBrowser, 'webkit.webprefs.loads_images_automatically', FLoadImagesAutomatically); @@ -4779,12 +4807,18 @@ begin end; end; +procedure TChromiumCore.CloseDevTools; +begin + if Initialized then + Browser.Host.CloseDevTools; +end; + procedure TChromiumCore.CloseDevTools(const aDevToolsWnd : TCefWindowHandle); begin if Initialized then begin {$IFDEF MSWINDOWS} - if (aDevToolsWnd <> 0) then + if ValidCefWindowHandle(aDevToolsWnd) then SetParent(GetWindow(aDevToolsWnd, GW_CHILD), 0); {$ENDIF} @@ -6342,6 +6376,18 @@ begin Browser.Host.IMECancelComposition; end; +procedure TChromiumCore.ReplaceMisspelling(const aWord : ustring); +begin + if Initialized then + Browser.Host.ReplaceMisspelling(aWord); +end; + +procedure TChromiumCore.AddWordToDictionary(const aWord : ustring); +begin + if Initialized then + Browser.Host.AddWordToDictionary(aWord); +end; + // ICefMediaRouter methods function TChromiumCore.AddObserver(const observer: ICefMediaObserver): ICefRegistration; var diff --git a/source/uCEFMiscFunctions.pas b/source/uCEFMiscFunctions.pas index 0cafddb3..8ec99028 100644 --- a/source/uCEFMiscFunctions.pas +++ b/source/uCEFMiscFunctions.pas @@ -1438,7 +1438,7 @@ begin try if FileExists(aDLLFile) then begin - TempStream := TFileStream.Create(aDLLFile, fmOpenRead); + TempStream := TFileStream.Create(aDLLFile, fmOpenRead or fmShareDenyWrite); TempStream.seek(0, soFromBeginning); TempStream.ReadBuffer(TempHeader, SizeOf(TempHeader)); diff --git a/update_CEF4Delphi.json b/update_CEF4Delphi.json index b57d9836..ff5129ee 100644 --- a/update_CEF4Delphi.json +++ b/update_CEF4Delphi.json @@ -2,7 +2,7 @@ "UpdateLazPackages" : [ { "ForceNotify" : true, - "InternalVersion" : 187, + "InternalVersion" : 188, "Name" : "cef4delphi_lazarus.lpk", "Version" : "85.3.11.0" }