From 874c73b1e18aa2112689e153f1c602ad615e2fc7 Mon Sep 17 00:00:00 2001 From: Marshall Greenblatt Date: Fri, 7 Jan 2011 01:24:17 +0000 Subject: [PATCH] Add CefHandler::HandleAuthenticationRequest for handling authentication requests (issue #150). git-svn-id: https://chromiumembedded.googlecode.com/svn/trunk@158 5089003a-bbd8-11dd-ad1f-f1f9622dbc98 --- include/cef.h | 13 +++++++++++++ include/cef_capi.h | 10 ++++++++++ libcef/browser_resource_loader_bridge.cc | 21 +++++++++++++++++++++ libcef_dll/cpptoc/handler_cpptoc.cc | 23 +++++++++++++++++++++++ libcef_dll/ctocpp/handler_ctocpp.cc | 14 ++++++++++++++ libcef_dll/ctocpp/handler_ctocpp.h | 3 +++ tests/cefclient/cefclient.h | 16 +++++++++++++++- tests/unittests/test_handler.h | 11 +++++++++++ 8 files changed, 110 insertions(+), 1 deletion(-) diff --git a/include/cef.h b/include/cef.h index 57149ce80..1312f4fe3 100644 --- a/include/cef.h +++ b/include/cef.h @@ -651,6 +651,19 @@ public: int64 contentLength, CefRefPtr& handler) =0; + // Called when the browser needs credentials from the user. |isProxy| + // indicates whether the host is a proxy server. |host| contains the hostname + // and port number. Set |username| and |password| and return RV_HANDLED to + // handle the request. Return RV_CONTINUE to cancel the request. + /*--cef()--*/ + virtual RetVal HandleAuthenticationRequest(CefRefPtr browser, + bool isProxy, + const CefString& host, + const CefString& realm, + const CefString& scheme, + CefString& username, + CefString& password) =0; + // Structure representing menu information. typedef cef_handler_menuinfo_t MenuInfo; diff --git a/include/cef_capi.h b/include/cef_capi.h index cf924e94e..97f1e0b44 100644 --- a/include/cef_capi.h +++ b/include/cef_capi.h @@ -460,6 +460,16 @@ typedef struct _cef_handler_t const cef_string_t* mimeType, const cef_string_t* fileName, int64 contentLength, struct _cef_download_handler_t** handler); + // Called when the browser needs credentials from the user. |isProxy| + // indicates whether the host is a proxy server. |host| contains the hostname + // and port number. Set |username| and |password| and return RV_HANDLED to + // handle the request. Return RV_CONTINUE to cancel the request. + enum cef_retval_t (CEF_CALLBACK *handle_authentication_request)( + struct _cef_handler_t* self, struct _cef_browser_t* browser, int isProxy, + const cef_string_t* host, const cef_string_t* realm, + const cef_string_t* scheme, cef_string_t* username, + cef_string_t* password); + // Event called before a context menu is displayed. To cancel display of the // default context menu return RV_HANDLED. enum cef_retval_t (CEF_CALLBACK *handle_before_menu)( diff --git a/libcef/browser_resource_loader_bridge.cc b/libcef/browser_resource_loader_bridge.cc index 0d573764a..141a9dba7 100644 --- a/libcef/browser_resource_loader_bridge.cc +++ b/libcef/browser_resource_loader_bridge.cc @@ -54,6 +54,7 @@ #include "base/timer.h" #include "base/thread.h" #include "base/waitable_event.h" +#include "net/base/auth.h" #include "net/base/cookie_store.h" #include "net/base/file_stream.h" #include "net/base/io_buffer.h" @@ -552,6 +553,26 @@ class RequestProxy : public net::URLRequest::Delegate, } } + virtual void OnAuthRequired(net::URLRequest* request, + net::AuthChallengeInfo* auth_info) { + if (browser_.get()) { + CefRefPtr handler = browser_->GetHandler(); + if(handler.get()) { + CefString username, password; + CefHandler::RetVal rv = handler->HandleAuthenticationRequest( + browser_, auth_info->is_proxy, + auth_info->host_and_port, auth_info->realm, + auth_info->scheme, username, password); + if (rv == RV_HANDLED) { + request->SetAuth(username, password); + return; + } + } + } + + request->CancelAuth(); + } + virtual void OnSSLCertificateError(net::URLRequest* request, int cert_error, net::X509Certificate* cert) { diff --git a/libcef_dll/cpptoc/handler_cpptoc.cc b/libcef_dll/cpptoc/handler_cpptoc.cc index 503a05439..9f4c5bf72 100644 --- a/libcef_dll/cpptoc/handler_cpptoc.cc +++ b/libcef_dll/cpptoc/handler_cpptoc.cc @@ -237,6 +237,27 @@ enum cef_retval_t CEF_CALLBACK handler_handle_download_response( return rv; } +enum cef_retval_t CEF_CALLBACK handler_handle_authentication_request( + struct _cef_handler_t* self, cef_browser_t* browser, int isProxy, + const cef_string_t* host, const cef_string_t* realm, + const cef_string_t* scheme, cef_string_t* username, + cef_string_t* password) +{ + DCHECK(self); + DCHECK(browser); + DCHECK(username && password); + if (!self || !browser || !username || !password) + return RV_CONTINUE; + + CefString usernameStr(username); + CefString passwordStr(password); + + return CefHandlerCppToC::Get(self)-> + HandleAuthenticationRequest(CefBrowserCToCpp::Wrap(browser), + (isProxy ? true : false), CefString(host), CefString(realm), + CefString(scheme), usernameStr, passwordStr); +} + enum cef_retval_t CEF_CALLBACK handler_handle_before_menu( struct _cef_handler_t* self, cef_browser_t* browser, const cef_handler_menuinfo_t* menuInfo) @@ -511,6 +532,8 @@ CefHandlerCppToC::CefHandlerCppToC(CefHandler* cls) struct_.struct_.handle_before_resource_load = handler_handle_before_resource_load; struct_.struct_.handle_download_response = handler_handle_download_response; + struct_.struct_.handle_authentication_request = + handler_handle_authentication_request; struct_.struct_.handle_before_menu = handler_handle_before_menu; struct_.struct_.handle_get_menu_label = handler_handle_get_menu_label; struct_.struct_.handle_menu_action = handler_handle_menu_action; diff --git a/libcef_dll/ctocpp/handler_ctocpp.cc b/libcef_dll/ctocpp/handler_ctocpp.cc index 52a5f7ddf..55bf57c8d 100644 --- a/libcef_dll/ctocpp/handler_ctocpp.cc +++ b/libcef_dll/ctocpp/handler_ctocpp.cc @@ -178,6 +178,20 @@ CefHandler::RetVal CefHandlerCToCpp::HandleDownloadResponse( return rv; } +CefHandler::RetVal CefHandlerCToCpp::HandleAuthenticationRequest( + CefRefPtr browser, bool isProxy, const CefString& host, + const CefString& realm, const CefString& scheme, CefString& username, + CefString& password) +{ + if(CEF_MEMBER_MISSING(struct_, handle_authentication_request)) + return RV_CONTINUE; + + return struct_->handle_authentication_request(struct_, + CefBrowserCppToC::Wrap(browser), isProxy, + host.GetStruct(), realm.GetStruct(), scheme.GetStruct(), + username.GetWritableStruct(), password.GetWritableStruct()); +} + CefHandler::RetVal CefHandlerCToCpp::HandleBeforeMenu( CefRefPtr browser, const MenuInfo& menuInfo) { diff --git a/libcef_dll/ctocpp/handler_ctocpp.h b/libcef_dll/ctocpp/handler_ctocpp.h index 01aebbe9c..68b1a10e9 100644 --- a/libcef_dll/ctocpp/handler_ctocpp.h +++ b/libcef_dll/ctocpp/handler_ctocpp.h @@ -57,6 +57,9 @@ public: virtual RetVal HandleDownloadResponse(CefRefPtr browser, const CefString& mimeType, const CefString& fileName, int64 contentLength, CefRefPtr& handler); + virtual RetVal HandleAuthenticationRequest(CefRefPtr browser, + bool isProxy, const CefString& host, const CefString& realm, + const CefString& scheme, CefString& username, CefString& password); virtual RetVal HandleBeforeMenu(CefRefPtr browser, const MenuInfo& menuInfo); virtual RetVal HandleGetMenuLabel(CefRefPtr browser, diff --git a/tests/cefclient/cefclient.h b/tests/cefclient/cefclient.h index b2b7b3be9..d53e534ce 100644 --- a/tests/cefclient/cefclient.h +++ b/tests/cefclient/cefclient.h @@ -120,13 +120,27 @@ public: // no size was provided. Set |handler| to the CefDownloadHandler instance that // will recieve the file contents. Return RV_CONTINUE to download the file // or RV_HANDLED to cancel the file download. - /*--cef()--*/ virtual RetVal HandleDownloadResponse(CefRefPtr browser, const CefString& mimeType, const CefString& fileName, int64 contentLength, CefRefPtr& handler); + // Called when the browser needs credentials from the user. |isProxy| + // indicates whether the host is a proxy server. |host| contains the hostname + // and port number. Set |username| and |password| and return RV_HANDLED to + // handle the request. Return RV_CONTINUE to cancel the request. + virtual RetVal HandleAuthenticationRequest(CefRefPtr browser, + bool isProxy, + const CefString& host, + const CefString& realm, + const CefString& scheme, + CefString& username, + CefString& password) + { + return RV_CONTINUE; + } + // Event called before a context menu is displayed. To cancel display of the // default context menu return RV_HANDLED. virtual RetVal HandleBeforeMenu(CefRefPtr browser, diff --git a/tests/unittests/test_handler.h b/tests/unittests/test_handler.h index e57b24fdd..e9974c8a2 100644 --- a/tests/unittests/test_handler.h +++ b/tests/unittests/test_handler.h @@ -121,6 +121,17 @@ public: return RV_CONTINUE; } + virtual RetVal HandleAuthenticationRequest(CefRefPtr browser, + bool isProxy, + const CefString& host, + const CefString& realm, + const CefString& scheme, + CefString& username, + CefString& password) + { + return RV_CONTINUE; + } + virtual RetVal HandleBeforeMenu(CefRefPtr browser, const MenuInfo& menuInfo) {