Add CefResponse parameter to CefRequestHandler::OnResourceRedirect

This commit is contained in:
Marshall Greenblatt 2016-08-24 16:19:57 +03:00
parent 69334e22c2
commit 3b5fb0d526
8 changed files with 48 additions and 9 deletions

View File

@ -146,12 +146,15 @@ typedef struct _cef_request_handler_t {
/// ///
// Called on the IO thread when a resource load is redirected. The |request| // Called on the IO thread when a resource load is redirected. The |request|
// parameter will contain the old URL and other request-related information. // parameter will contain the old URL and other request-related information.
// The |new_url| parameter will contain the new URL and can be changed if // The |response| parameter will contain the response that resulted in the
// desired. The |request| object cannot be modified in this callback. // redirect. The |new_url| parameter will contain the new URL and can be
// changed if desired. The |request| object cannot be modified in this
// callback.
/// ///
void (CEF_CALLBACK *on_resource_redirect)(struct _cef_request_handler_t* self, void (CEF_CALLBACK *on_resource_redirect)(struct _cef_request_handler_t* self,
struct _cef_browser_t* browser, struct _cef_frame_t* frame, struct _cef_browser_t* browser, struct _cef_frame_t* frame,
struct _cef_request_t* request, cef_string_t* new_url); struct _cef_request_t* request, struct _cef_response_t* response,
cef_string_t* new_url);
/// ///
// Called on the IO thread when a resource response is received. To allow the // Called on the IO thread when a resource response is received. To allow the

View File

@ -159,13 +159,16 @@ class CefRequestHandler : public virtual CefBase {
/// ///
// Called on the IO thread when a resource load is redirected. The |request| // Called on the IO thread when a resource load is redirected. The |request|
// parameter will contain the old URL and other request-related information. // parameter will contain the old URL and other request-related information.
// The |new_url| parameter will contain the new URL and can be changed if // The |response| parameter will contain the response that resulted in the
// desired. The |request| object cannot be modified in this callback. // redirect. The |new_url| parameter will contain the new URL and can be
// changed if desired. The |request| object cannot be modified in this
// callback.
/// ///
/*--cef()--*/ /*--cef()--*/
virtual void OnResourceRedirect(CefRefPtr<CefBrowser> browser, virtual void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, CefRefPtr<CefRequest> request,
CefRefPtr<CefResponse> response,
CefString& new_url) {} CefString& new_url) {}
/// ///

View File

@ -73,10 +73,14 @@ net::URLRequestJob* CefRequestInterceptor::MaybeInterceptRedirect(
static_cast<CefRequestImpl*>(cefRequest.get())->Set(request); static_cast<CefRequestImpl*>(cefRequest.get())->Set(request);
static_cast<CefRequestImpl*>(cefRequest.get())->SetReadOnly(true); static_cast<CefRequestImpl*>(cefRequest.get())->SetReadOnly(true);
CefRefPtr<CefResponse> cefResponse = new CefResponseImpl();
static_cast<CefResponseImpl*>(cefResponse.get())->Set(request);
static_cast<CefResponseImpl*>(cefResponse.get())->SetReadOnly(true);
// Give the client an opportunity to redirect the request. // Give the client an opportunity to redirect the request.
CefString newUrlStr = location.spec(); CefString newUrlStr = location.spec();
handler->OnResourceRedirect(browser.get(), frame, cefRequest, handler->OnResourceRedirect(browser.get(), frame, cefRequest,
newUrlStr); cefResponse, newUrlStr);
if (newUrlStr != location.spec()) { if (newUrlStr != location.spec()) {
const GURL new_url = GURL(newUrlStr.ToString()); const GURL new_url = GURL(newUrlStr.ToString());
if (!new_url.is_empty() && new_url.is_valid()) { if (!new_url.is_empty() && new_url.is_valid()) {

View File

@ -164,7 +164,8 @@ struct _cef_resource_handler_t* CEF_CALLBACK request_handler_get_resource_handle
void CEF_CALLBACK request_handler_on_resource_redirect( void CEF_CALLBACK request_handler_on_resource_redirect(
struct _cef_request_handler_t* self, cef_browser_t* browser, struct _cef_request_handler_t* self, cef_browser_t* browser,
cef_frame_t* frame, cef_request_t* request, cef_string_t* new_url) { cef_frame_t* frame, cef_request_t* request,
struct _cef_response_t* response, cef_string_t* new_url) {
// AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING // AUTO-GENERATED CONTENT - DELETE THIS COMMENT BEFORE MODIFYING
DCHECK(self); DCHECK(self);
@ -182,6 +183,10 @@ void CEF_CALLBACK request_handler_on_resource_redirect(
DCHECK(request); DCHECK(request);
if (!request) if (!request)
return; return;
// Verify param: response; type: refptr_diff
DCHECK(response);
if (!response)
return;
// Verify param: new_url; type: string_byref // Verify param: new_url; type: string_byref
DCHECK(new_url); DCHECK(new_url);
if (!new_url) if (!new_url)
@ -195,6 +200,7 @@ void CEF_CALLBACK request_handler_on_resource_redirect(
CefBrowserCToCpp::Wrap(browser), CefBrowserCToCpp::Wrap(browser),
CefFrameCToCpp::Wrap(frame), CefFrameCToCpp::Wrap(frame),
CefRequestCToCpp::Wrap(request), CefRequestCToCpp::Wrap(request),
CefResponseCToCpp::Wrap(response),
new_urlStr); new_urlStr);
} }

View File

@ -162,7 +162,7 @@ CefRefPtr<CefResourceHandler> CefRequestHandlerCToCpp::GetResourceHandler(
void CefRequestHandlerCToCpp::OnResourceRedirect(CefRefPtr<CefBrowser> browser, void CefRequestHandlerCToCpp::OnResourceRedirect(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request,
CefString& new_url) { CefRefPtr<CefResponse> response, CefString& new_url) {
cef_request_handler_t* _struct = GetStruct(); cef_request_handler_t* _struct = GetStruct();
if (CEF_MEMBER_MISSING(_struct, on_resource_redirect)) if (CEF_MEMBER_MISSING(_struct, on_resource_redirect))
return; return;
@ -181,12 +181,17 @@ void CefRequestHandlerCToCpp::OnResourceRedirect(CefRefPtr<CefBrowser> browser,
DCHECK(request.get()); DCHECK(request.get());
if (!request.get()) if (!request.get())
return; return;
// Verify param: response; type: refptr_diff
DCHECK(response.get());
if (!response.get())
return;
// Execute // Execute
_struct->on_resource_redirect(_struct, _struct->on_resource_redirect(_struct,
CefBrowserCppToC::Wrap(browser), CefBrowserCppToC::Wrap(browser),
CefFrameCppToC::Wrap(frame), CefFrameCppToC::Wrap(frame),
CefRequestCppToC::Wrap(request), CefRequestCppToC::Wrap(request),
CefResponseCppToC::Wrap(response),
new_url.GetWritableStruct()); new_url.GetWritableStruct());
} }

View File

@ -44,7 +44,7 @@ class CefRequestHandlerCToCpp
CefRefPtr<CefRequest> request) override; CefRefPtr<CefRequest> request) override;
void OnResourceRedirect(CefRefPtr<CefBrowser> browser, void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request,
CefString& new_url) override; CefRefPtr<CefResponse> response, CefString& new_url) override;
bool OnResourceResponse(CefRefPtr<CefBrowser> browser, bool OnResourceResponse(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request, CefRefPtr<CefFrame> frame, CefRefPtr<CefRequest> request,
CefRefPtr<CefResponse> response) override; CefRefPtr<CefResponse> response) override;

View File

@ -780,6 +780,7 @@ class RedirectTestHandler : public TestHandler {
void OnResourceRedirect(CefRefPtr<CefBrowser> browser, void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, CefRefPtr<CefRequest> request,
CefRefPtr<CefResponse> response,
CefString& new_url) override { CefString& new_url) override {
// Should be called for each redirected URL. // Should be called for each redirected URL.
@ -788,14 +789,29 @@ class RedirectTestHandler : public TestHandler {
// Called due to the nav1 redirect response. // Called due to the nav1 redirect response.
got_nav1_redirect_.yes(); got_nav1_redirect_.yes();
EXPECT_EQ(302, response->GetStatus());
EXPECT_STREQ("Found", response->GetStatusText().ToString().c_str());
EXPECT_STREQ("text/html", response->GetMimeType().ToString().c_str());
EXPECT_STREQ(kRNav2, response->GetHeader("Location").ToString().c_str());
// Change the redirect to the 3rd URL. // Change the redirect to the 3rd URL.
new_url = kRNav3; new_url = kRNav3;
} else if (old_url == kRNav1 && new_url == kRNav3) { } else if (old_url == kRNav1 && new_url == kRNav3) {
// Called due to the redirect change above. // Called due to the redirect change above.
got_nav2_redirect_.yes(); got_nav2_redirect_.yes();
EXPECT_EQ(307, response->GetStatus());
EXPECT_STREQ("Internal Redirect",
response->GetStatusText().ToString().c_str());
EXPECT_TRUE(response->GetMimeType().empty());
EXPECT_STREQ(kRNav3, response->GetHeader("Location").ToString().c_str());
} else if (old_url == kRNav3 && new_url == kRNav4) { } else if (old_url == kRNav3 && new_url == kRNav4) {
// Called due to the nav3 redirect response. // Called due to the nav3 redirect response.
got_nav3_redirect_.yes(); got_nav3_redirect_.yes();
EXPECT_EQ(200, response->GetStatus());
EXPECT_TRUE(response->GetStatusText().empty());
EXPECT_STREQ("text/html", response->GetMimeType().ToString().c_str());
} else { } else {
got_invalid_redirect_.yes(); got_invalid_redirect_.yes();
} }
@ -862,6 +878,7 @@ class RedirectDestroyTestHandler : public TestHandler {
void OnResourceRedirect(CefRefPtr<CefBrowser> browser, void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, CefRefPtr<CefRequest> request,
CefRefPtr<CefResponse> response,
CefString& new_url) override { CefString& new_url) override {
const std::string& old_url = request->GetURL(); const std::string& old_url = request->GetURL();
if (old_url == kRNav1 && new_url == kRNav2) { if (old_url == kRNav1 && new_url == kRNav2) {

View File

@ -643,6 +643,7 @@ class ResourceResponseTest : public TestHandler {
void OnResourceRedirect(CefRefPtr<CefBrowser> browser, void OnResourceRedirect(CefRefPtr<CefBrowser> browser,
CefRefPtr<CefFrame> frame, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, CefRefPtr<CefRequest> request,
CefRefPtr<CefResponse> response,
CefString& new_url) override { CefString& new_url) override {
EXPECT_IO_THREAD(); EXPECT_IO_THREAD();
EXPECT_EQ(browser_id_, browser->GetIdentifier()); EXPECT_EQ(browser_id_, browser->GetIdentifier());