标题:CEF3中如何截获XMLHttpRequest(HDR)请求和返回数据!
Title: How to intercept XMLHttpRequest in CEF3!
阅读对象:主要是给自己看的,免得以后忘记。
环境:[1]VS2013SP3 [2]cef_binary_3.1916.1804_windows32
第一次更新日期:2014-11-4
最近更新日期:2014-11-4
正文:
总共分为四步步骤:
第一步:
extern UINT WM_XMLHttpRequest;
CefRefPtr<CefResourceHandler> ClientHandler::GetResourceHandler(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request)
{
//http://www.magpcss.org/ceforum/viewtopic.php?f=6&t=10794
if (request->GetResourceType() == RT_XHR)
{
//Second?
CefRefPtr<KagulaResourceHandler> resHandler = new KagulaResourceHandler();
resHandler->_browser = browser;
resHandler->_frame = frame;
resHandler->_clientHandler = this;
return resHandler;
}
return NULL;
}
第二步:
KagulaResourceHandler.h清单
#pragma once
#include <include/cef_app.h>
#include <include/cef_urlrequest.h>
class ClientHandler;
class KagulaUrlRequestClient;
class KagulaResourceHandler : public CefResourceHandler
{
public:
KagulaResourceHandler() :
_offset(0)
{}
virtual bool ProcessRequest(CefRefPtr<CefRequest> request,
CefRefPtr<CefCallback> callback)
OVERRIDE;
virtual void GetResponseHeaders(CefRefPtr<CefResponse> response,
int64& response_length,
CefString& redirectUrl) OVERRIDE;
virtual void Cancel() OVERRIDE;
virtual bool ReadResponse(void* data_out, int bytes_to_read,
int& bytes_read,
CefRefPtr<CefCallback> callback)
OVERRIDE;
public:
CefRefPtr<ClientHandler> _clientHandler;
CefRefPtr<CefBrowser> _browser;
CefRefPtr<CefFrame> _frame;
CefRefPtr<CefCallback> _responseHeadersReadyCallback;
CefRefPtr<KagulaUrlRequestClient> _webRequestClient;
CefRefPtr<CefURLRequest> _webRequest;
std::string _cont