cloudtrends / chromiumembedded

Automatically exported from code.google.com/p/chromiumembedded
1 stars 1 forks source link

Download attachment #6

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I am trying to download an attachment from Cef.
How can I do that?

In WebFrameLoaderClient::dispatchDecidePolicyForMIMEType, there is:
...
else if (TreatAsAttachment(response)) {
    // The server wants us to download instead of replacing the page contents.
    // Downloading is handled by the embedder, but we still get the initial
    // response so that we can ignore it and clean up properly.
    action = PolicyIgnore;
  } else if (!canShowMIMEType(mime_type)) {
    // Make sure that we can actually handle this type internally.
    action = PolicyIgnore;
  } else {

I don't know where the "embedder" is supposed to do the actual download.

I tried to implement
   virtual void DownloadUrl(const GURL& url, const GURL& referrer)
in BrowserWebViewDelegate, but it is never called.

Thanks in advance.

Original issue reported on code.google.com by amas...@gmail.com on 24 Jan 2009 at 1:45

GoogleCodeExporter commented 9 years ago
This might be a little late and I am pretty sure what we did to handle a 
similar situation is probably not the most elegant but it does work effectively 
for our situation (we have not had a chance yet to go through the chrome source 
to see how they handle the situation).  But with that being said, in 
BrowserWebViewDelegate::didFailProvisionalLoad we changed the following logic 
to deal specifically with zip files: 

// Don't display an error page if this is simply a cancelled load.  Aside
// from being dumb, WebCore doesn't expect it and it will cause a crash.
if (error.reason == net::ERR_ABORTED)
  return;

Basically, we changed the logic to something similar to this:

if(error.reason == net::ERR_ABORTED)
{
    CefRefPtr<CefHandler> handler = browser_->GetHandler();
    if(handler.get()) {
      const WebDataSource* failed_ds = frame->provisionalDataSource();
      std::wstring mime_type(failed_ds->response().mimeType().data());

      //basically we make the assumption that if the mime type is a zip file (or for that matter a file type that would normally be treated as an 
attachment - but our project only cared about zip files), the webkit code 
caused the abort
      //rather than the user or something else so we go ahead and send it on to a handler.
      if(mime_type.find(L"application/zip") == std::wstring::npos)
        return;

      handler->HandleLoadError(browser_,
        browser_->GetCefFrame(frame),
        static_cast<CefHandler::ErrorCode>(error.reason),
        UTF8ToWide(failed_ds->request().url().spec().data()), mime_type);
    }
    return;
}
Then in the HandleLoadError we grab that URL and download it using the WinHTTP 
library.  We chose this method initially because it involved the least 
amount of changes to the source (even though it is far from elegant).  
Obviously, we could have made some changes in the actual webkit code where it 
decides to basically ignore attachments but that would require too much 
patching with each source update.

Eventually, we are going to add our own custom patch to cef.h to add a new 
virtual function to the CefHandler that is specific to downloading (probably 
something like HandleDownloadAttachment(Browser, Frame, Url, MimeType, Headers) 
).

If anyone has any thoughts on how to handle this better, we'd love to hear 
them.  We're pretty new to this project and only have a rudimentary 
understanding of the source at best.

Just our 2 cents.

Original comment by rene.tor...@gmail.com on 23 Oct 2009 at 4:30

GoogleCodeExporter commented 9 years ago
Issue 101 has been merged into this issue.

Original comment by magreenb...@gmail.com on 15 Oct 2010 at 7:51

GoogleCodeExporter commented 9 years ago
Revision 117 adds the CefHandler::HandleDownloadResponse() method and 
CefDownloadHandler class to support file download in response to 
'Content-Disposition' headers.

Original comment by magreenb...@gmail.com on 16 Oct 2010 at 7:13