Thanks. I was able to implement CefDownloadHandler, the PDF is now downloaded. However, the user experience is still far from perfect:
- When the user clicks the link, a blank popup window is opened.
- A "save as" dialog is opened. The user selects the folder and clicks "OK".
- The PDF is downloaded to the selected folder. However, the popup window stays open and shows the error message "ERR_ABORTED".
Here's what happens in code:
- KisCefLifeSpanHandler::OnBeforePopup, the url argument is the URL of the PDF
- KisCefLifeSpanHandler::OnAfterCreated
- KisCefApp::OnBrowserCreated
- KisCefLoadHandler::OnLoadingStateChange: isLoading=true
- KisCefRequestHandler::OnBeforeBrowse, isUserGesture=1, isRedirect=0
- KisCefRequestHandler::GetResourceRequestHandler, isNavigation=1, isDownload=0, initiator=null
- KisCefLoadHandler::OnLoadError: ERR_ABORTED.
- KisCefLoadHandler::OnLoadingStateChange: isLoading=false
My implementation of OnLoadError creates an error page (data URL) and loads it into the browser with LoadUrl:
- KisCefLoadHandler::OnLoadingStateChange: isLoading=true
- KisCefRequestHandler::OnBeforeBrowse, isUserGesture=0, isRedirect=0
- KisCefDownloadHandler::OnBeforeDownload
- KisCefRequestHandler::GetResourceRequestHandler, isNavigation=1, isDownload=0, initiator=null. The url is my error page (data url).
At this point, the new popup window is still blank, and the "save as" dialog is showing. When the user hits "OK":
- KisCefApp::OnBrowserCreated
- KisCefApp::OnContextCreated
- KisCefDisplayHandler::OnAddressChange, the url is my error page
- KisCefLoadHandler::OnLoadEnd, HTTP Status=200
- KisCefLoadHandler::OnLoadingStateChange, isLoading=false
I basically want to prevent the popup from opening (but I still want the "save as" dialog, and I don't want to block popups in general). If that's not easily possible, I would at least want that the popup automatically closes when the download starts. However, OnLoadEnd with Status 200 refers to loading the error page, not to the download. I can implement OnDownloadUpdated to detect when the download completes (not sure if this would be a reliable method, though), but I can't tell if I should close the browser at this point, because the download could have been started otherwise (not via popup).
The Chrome browser seems to be able to handle this. It opens a tab and immediately closes it as the download starts. How do I do this?