Add callbacks for network request access to cookies (issue #2374)

This commit is contained in:
Marshall Greenblatt
2018-02-05 19:21:14 -05:00
parent d7e1312e9d
commit d34d3d1b3b
15 changed files with 1177 additions and 11 deletions

View File

@@ -9,7 +9,7 @@
namespace net_util {
bool IsInternalRequest(net::URLRequest* request) {
bool IsInternalRequest(const net::URLRequest* request) {
// With PlzNavigate we now receive blob URLs. Ignore these URLs.
// See https://crbug.com/776884 for details.
if (request->url().SchemeIs(url::kBlobScheme)) {

View File

@@ -14,7 +14,7 @@ namespace net_util {
// Returns true if |request| is handled internally and should not be exposed via
// the CEF API.
bool IsInternalRequest(net::URLRequest* request);
bool IsInternalRequest(const net::URLRequest* request);
}; // namespace net_util

View File

@@ -9,6 +9,7 @@
#include "include/cef_urlrequest.h"
#include "libcef/browser/browser_host_impl.h"
#include "libcef/browser/cookie_manager_impl.h"
#include "libcef/browser/net/net_util.h"
#include "libcef/browser/net/source_stream.h"
#include "libcef/browser/net/url_request_user_data.h"
@@ -440,6 +441,64 @@ net::NetworkDelegate::AuthRequiredResponse CefNetworkDelegate::OnAuthRequired(
return AUTH_REQUIRED_RESPONSE_NO_ACTION;
}
bool CefNetworkDelegate::OnCanGetCookies(const net::URLRequest& request,
const net::CookieList& cookie_list) {
if (net_util::IsInternalRequest(&request))
return true;
CefRefPtr<CefBrowserHostImpl> browser =
CefBrowserHostImpl::GetBrowserForRequest(&request);
if (browser.get()) {
CefRefPtr<CefClient> client = browser->GetClient();
if (client.get()) {
CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler();
if (handler.get()) {
CefRefPtr<CefFrame> frame = browser->GetFrameForRequest(&request);
CefRefPtr<CefRequestImpl> cefRequest = new CefRequestImpl();
cefRequest->Set(&request);
cefRequest->SetReadOnly(true);
return handler->CanGetCookies(browser.get(), frame, cefRequest.get());
}
}
}
return true;
}
bool CefNetworkDelegate::OnCanSetCookie(const net::URLRequest& request,
const net::CanonicalCookie& cookie,
net::CookieOptions* options) {
if (net_util::IsInternalRequest(&request))
return true;
CefRefPtr<CefBrowserHostImpl> browser =
CefBrowserHostImpl::GetBrowserForRequest(&request);
if (browser.get()) {
CefRefPtr<CefClient> client = browser->GetClient();
if (client.get()) {
CefRefPtr<CefRequestHandler> handler = client->GetRequestHandler();
if (handler.get()) {
CefRefPtr<CefFrame> frame = browser->GetFrameForRequest(&request);
CefRefPtr<CefRequestImpl> cefRequest = new CefRequestImpl();
cefRequest->Set(&request);
cefRequest->SetReadOnly(true);
CefCookie cefCookie;
if (!CefCookieManagerImpl::GetCefCookie(cookie, cefCookie))
return true;
return handler->CanSetCookie(browser.get(), frame, cefRequest.get(),
cefCookie);
}
}
}
return true;
}
bool CefNetworkDelegate::OnCanAccessFile(
const net::URLRequest& request,
const base::FilePath& original_path,

View File

@@ -38,6 +38,11 @@ class CefNetworkDelegate : public net::NetworkDelegateImpl {
const AuthCallback& callback,
net::AuthCredentials* credentials) override;
void OnCompleted(net::URLRequest* request, bool started) override;
bool OnCanGetCookies(const net::URLRequest& request,
const net::CookieList& cookie_list) override;
bool OnCanSetCookie(const net::URLRequest& request,
const net::CanonicalCookie& cookie,
net::CookieOptions* options) override;
bool OnCanAccessFile(const net::URLRequest& request,
const base::FilePath& original_path,
const base::FilePath& absolute_path) const override;

View File

@@ -411,7 +411,7 @@ void CefResourceRequestJob::DoLoadCookies() {
void CefResourceRequestJob::CheckCookiePolicyAndLoad(
const net::CookieList& cookie_list) {
bool can_get_cookies = CanGetCookies(cookie_list);
bool can_get_cookies = !cookie_list.empty() && CanGetCookies(cookie_list);
if (can_get_cookies) {
net::CookieList::const_iterator it = cookie_list.begin();
for (; it != cookie_list.end(); ++it) {