Page 1 of 1

Filtering response content

PostPosted: Fri May 20, 2011 8:48 am
by yyankov
As outlined in my post in the support forum Pluggable MIME filter equivalent, I needed to be able to look at and modify the content of the resources being downloaded by the browser. While this functionality can be emulated with the existing CEF API, it required me to employ some pretty invasive techniques. To avoid that, I made a few minor additions to CEF, which give me the ability to tap the incoming data stream before it is consumed by the browser - one simple interface and one additional callback method in CefHandler:

Code: Select all
class CefHandler : public CefBase
{
public:
  ...
  ...
  // Called on the UI thread after a response to the resource request is
  // received. Set |filter| if response content needs to be monitored
  // and/or modified as it arrives. The return value is currently ignored.
  /*--cef()--*/
  virtual RetVal HandleResourceReponse(CefRefPtr<CefBrowser> browser,
                                     const CefString& url,
                                     CefRefPtr<CefResponse> response,
                                     CefRefPtr<CefContentFilter>& filter) =0;
  ...
  ...
};

// Class used to represent a content filter interface. The methods of
// this class will always be called on the UI thread.
/*--cef(source=client)--*/
class CefContentFilter : public CefBase
{
public:
  // Set |substitute_data| to the replacement for the data in |data|
  // if data should be modified. The return value is currently ignored.
  /*--cef()--*/
  virtual bool ProcessData(void* data, int data_size, CefRefPtr<CefStreamReader>& substitute_data) =0;
 
  // Called when there is no more data to be processed. It is expected
  // that whatever data was retained in the last ProcessData() call,
  // it should be returned now by setting |remainder| if appropriate.
  // The return value is currently ignored.
  /*--cef()--*/
  virtual bool Drain(CefRefPtr<CefStreamReader>& remainder) =0;
};



I think CefHandler::HandleResourceResponse() has merit on its own even without the filter, as it gives the user the opportunity to peek into the actual response headers.
Not really sure if CefStreamReader is the best and most semantically correct class to use to pass the modified content back to CEF. Suggestions welcome.

Re: Filtering response content

PostPosted: Fri May 20, 2011 8:57 am
by magreenblatt
I like this idea conceptually. Please create an issue in the issue tracker and attach an uncompressed patch file.

Thanks,
Marshall

Re: Filtering response content

PostPosted: Fri May 20, 2011 9:33 am
by yyankov
magreenblatt wrote:I like this idea conceptually. Please create an issue in the issue tracker and attach an uncompressed patch file.


Done.