Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

[solved] Canceling Downloads

umpani
Posts: 11
Joined: Thu Nov 01, 2018 2:17 pm

[solved] Canceling Downloads

Post by umpani »

Hi,

I have difficulties in canceling Downloads.

This is what I'm doing.
  • Starting Downloads with browser.startdownload
  • handling the download with the ondownloadupdate event
If the file exists at the URL, everything is fine. But if the file doesen't exists, cef raises an unknown error before calling the event ondownloadupdate .

Then I tryed to cancel the download by using the event onBrowserGetResponse. The resonse.status had been checked and the result variable was set in this context.
But cef seems to retry the download again and again.

Is there a way to cancel a download without using the ondownloadupdate event?´


You can try it with your minibroserdemo by calling "Chromium1.StartDownload('https://www.briskbard.com/test.jpg');"

Thanks
Last edited by umpani on Mon Nov 05, 2018 5:48 am, edited 1 time in total.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Canceling Downloads

Post by salvadordf »

Hi,

Use TChromium.OnResourceResponse and check the response.Status value.
umpani
Posts: 11
Joined: Thu Nov 01, 2018 2:17 pm

Re: Canceling Downloads

Post by umpani »

Hi,

thank you for the answer.

I tried using OnResourceResponse
Then I tryed to cancel the download by using the event onBrowserGetResponse. The resonse.status had been checked and the result variable was set in this context.
But cef seems to retry the download again and again.

Code: Select all

     
     if  ((response.Status >= 400) and (response.Status < 500)) then   //Testexample
      begin
        browser.StopLoad;
        result := true;
      end;
    
But OnResourceResponse was called again and again (without ending)

Thanks
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Canceling Downloads

Post by salvadordf »

I set a breakpoint in the TChromium.OnResourceResponse event and it's only triggered once.

The server logs only show a few entries with "test.jpg" and half of them were generated by my tests. I guess the rest (4 or 5) come from your IP.
...but perhaps those logs don't have all the latest entries. My hosting service provider updates them every now and then.

Are you using the latest CEF4Delphi version ?
Please, put a breakpoint in TChromium.StartDownload to make sure it's only called once.
umpani
Posts: 11
Joined: Thu Nov 01, 2018 2:17 pm

Re: Canceling Downloads

Post by umpani »

Yes, I'm using the lates CEF4.

Please try the following to watch the behaviour that I'm talking about.
  • Open your Minibrowser Demo.
  • Add a Button with this code

    Code: Select all

    Chromium1.StartDownload('https://www.briskbard.com/test.jpg');
  • Add this code into the event Chromium1ResourceResponse

    Code: Select all

      if (response.Status >= 400) and (response.Status <= 499) then
      begin
        Result := true;
    
        OutputDebugString(PChar('Chromium1ResourceResponse: '+IntToStr(response.Status)))
      end;
  • Start in Debug mode and push the button. In the Delphi debug box are the following messages repeating.

    Code: Select all

    Thread-Start: Thread-ID: 69804. Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)
    Debug-Ausgabe: Chromium1ResourceResponse: 404 Prozess MiniBrowser.exe (22364)

Thank you
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Canceling Downloads

Post by salvadordf »

Remove the "Result := True" in the Chromium1ResourceResponse event to fix this issue.
Returning true indicates that this resource must be redirected or retried.

These are code comments for that event :

Code: Select all

  ///
  // Called on the IO thread when a resource response is received. To allow the
  // resource to load normally return false (0). To redirect or retry the
  // resource modify |request| (url, headers or post body) and return true (1).
  // The |response| object cannot be modified in this callback.
  ///
umpani
Posts: 11
Joined: Thu Nov 01, 2018 2:17 pm

Re: Canceling Downloads

Post by umpani »

Ok,

but then, it raises an error, because the file doesen't exists.

I Think, I have to cancel the download in the event Chromium1ResourceResponse.
But how can I do this.
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Canceling Downloads

Post by salvadordf »

You don't need to stop it because It's already stopped when you see that error. CEF3 will only retry if Result is set to true.

At that point, the app may handle the error and show some warning to the user.
umpani
Posts: 11
Joined: Thu Nov 01, 2018 2:17 pm

Re: Canceling Downloads

Post by umpani »

Ok,
I can understand your probosal.

But where can I put the exceptionhandling for the downloadfunction in the app? I guess, that the download is threatening by CEF.
Can please you give me an example by using your minibrowser demo?

Thank you
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Canceling Downloads

Post by salvadordf »

umpani wrote: Sat Nov 03, 2018 7:14 pm But where can I put the exceptionhandling for the downloadfunction in the app?
It depends on how you need to show these notifications to the user. I just used a message box in BriskBard but other people prefer to load custom error pages. Read below for more info.
umpani wrote: Sat Nov 03, 2018 7:14 pm I guess, that the download is threatening by CEF.
These errors are normal HTTP status codes :
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

CEF doesn't include any way to tell if a web page is a threat.
If you need that, you have to implement the "Google Safe Browsing API" in your application.
https://developers.google.com/safe-browsing/
umpani wrote: Sat Nov 03, 2018 7:14 pm Can please you give me an example by using your minibrowser demo?
As I mentioned before, this depends on how you want to tell your users that an HTTP error has occurred.

Perhaps the easiest way is to load a local error page as explained here :
https://www.briskbard.com/forum/viewtopic.php?f=8&t=579

Other people prefer to register a custom scheme that shows a custom error page :
https://www.briskbard.com/forum/viewtopic.php?f=8&t=571

I use message windows in BriskBard but these forms needs to be created in the main thread, so I save the error information and send a custom message to the main form to show them.

All these examples use the TChromium.OnLoadError but in case of failed downloads you need to do this in TChromium.OnResourceResponse
Post Reply