2012-04-03 03:34:16 +02:00
|
|
|
// Copyright (c) 2012 The Chromium Embedded Framework Authors. All rights
|
|
|
|
// reserved. Use of this source code is governed by a BSD-style license that can
|
|
|
|
// be found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "libcef/browser/url_network_delegate.h"
|
|
|
|
|
|
|
|
#include <string>
|
|
|
|
|
2013-08-27 20:58:08 +02:00
|
|
|
#include "include/cef_urlrequest.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/browser/browser_host_impl.h"
|
|
|
|
#include "libcef/browser/thread_util.h"
|
2013-08-27 20:58:08 +02:00
|
|
|
#include "libcef/browser/url_request_user_data.h"
|
2012-04-03 03:34:16 +02:00
|
|
|
#include "libcef/common/request_impl.h"
|
|
|
|
|
2012-04-04 20:18:09 +02:00
|
|
|
#include "net/base/net_errors.h"
|
2012-04-11 20:00:55 +02:00
|
|
|
#include "net/url_request/url_request.h"
|
2012-04-04 20:18:09 +02:00
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class CefAuthCallbackImpl : public CefAuthCallback {
|
|
|
|
public:
|
|
|
|
CefAuthCallbackImpl(const net::NetworkDelegate::AuthCallback& callback,
|
|
|
|
net::AuthCredentials* credentials)
|
|
|
|
: callback_(callback),
|
|
|
|
credentials_(credentials) {
|
|
|
|
}
|
|
|
|
~CefAuthCallbackImpl() {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
// The auth callback is still pending. Cancel it now.
|
|
|
|
if (CEF_CURRENTLY_ON_IOT()) {
|
|
|
|
CancelNow(callback_);
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
2012-04-16 23:15:27 +02:00
|
|
|
base::Bind(&CefAuthCallbackImpl::CancelNow, callback_));
|
2012-04-03 03:34:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Continue(const CefString& username,
|
|
|
|
const CefString& password) OVERRIDE {
|
|
|
|
if (CEF_CURRENTLY_ON_IOT()) {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
credentials_->Set(username, password);
|
|
|
|
callback_.Run(net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_SET_AUTH);
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_IOT,
|
|
|
|
base::Bind(&CefAuthCallbackImpl::Continue, this, username, password));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void Cancel() OVERRIDE {
|
|
|
|
if (CEF_CURRENTLY_ON_IOT()) {
|
|
|
|
if (!callback_.is_null()) {
|
|
|
|
CancelNow(callback_);
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
CEF_POST_TASK(CEF_IOT, base::Bind(&CefAuthCallbackImpl::Cancel, this));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Disconnect() {
|
|
|
|
callback_.Reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static void CancelNow(const net::NetworkDelegate::AuthCallback& callback) {
|
|
|
|
CEF_REQUIRE_IOT();
|
|
|
|
callback.Run(net::NetworkDelegate::AUTH_REQUIRED_RESPONSE_NO_ACTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
net::NetworkDelegate::AuthCallback callback_;
|
|
|
|
net::AuthCredentials* credentials_;
|
|
|
|
|
|
|
|
IMPLEMENT_REFCOUNTING(CefAuthCallbackImpl);
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
CefNetworkDelegate::CefNetworkDelegate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
CefNetworkDelegate::~CefNetworkDelegate() {
|
|
|
|
}
|
|
|
|
|
|
|
|
int CefNetworkDelegate::OnBeforeURLRequest(
|
|
|
|
net::URLRequest* request,
|
|
|
|
const net::CompletionCallback& callback,
|
|
|
|
GURL* new_url) {
|
|
|
|
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);
|
|
|
|
|
2012-04-11 20:00:55 +02:00
|
|
|
GURL old_url = request->url();
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
// Populate the request data.
|
|
|
|
CefRefPtr<CefRequestImpl> requestPtr(new CefRequestImpl());
|
|
|
|
requestPtr->Set(request);
|
|
|
|
|
|
|
|
// Give the client an opportunity to cancel the request.
|
|
|
|
if (handler->OnBeforeResourceLoad(browser.get(), frame,
|
|
|
|
requestPtr.get())) {
|
|
|
|
return net::ERR_ABORTED;
|
|
|
|
}
|
|
|
|
|
2012-04-11 20:00:55 +02:00
|
|
|
GURL url = GURL(std::string(requestPtr->GetURL()));
|
|
|
|
if (old_url != url)
|
|
|
|
new_url ->Swap(&url);
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
requestPtr->Get(request);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return net::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
net::NetworkDelegate::AuthRequiredResponse CefNetworkDelegate::OnAuthRequired(
|
|
|
|
net::URLRequest* request,
|
|
|
|
const net::AuthChallengeInfo& auth_info,
|
|
|
|
const AuthCallback& callback,
|
|
|
|
net::AuthCredentials* credentials) {
|
|
|
|
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<CefAuthCallbackImpl> callbackPtr(
|
|
|
|
new CefAuthCallbackImpl(callback, credentials));
|
|
|
|
if (handler->GetAuthCredentials(browser.get(),
|
|
|
|
frame,
|
|
|
|
auth_info.is_proxy,
|
|
|
|
auth_info.challenger.host(),
|
|
|
|
auth_info.challenger.port(),
|
|
|
|
auth_info.realm,
|
|
|
|
auth_info.scheme,
|
|
|
|
callbackPtr.get())) {
|
|
|
|
return AUTH_REQUIRED_RESPONSE_IO_PENDING;
|
|
|
|
} else {
|
|
|
|
callbackPtr->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-27 20:58:08 +02:00
|
|
|
CefURLRequestUserData* user_data =
|
|
|
|
(CefURLRequestUserData*)request->GetUserData(
|
|
|
|
CefURLRequestUserData::kUserDataKey);
|
|
|
|
if (user_data) {
|
|
|
|
CefRefPtr<CefURLRequestClient> client = user_data->GetClient();
|
|
|
|
if (client.get()) {
|
|
|
|
CefRefPtr<CefAuthCallbackImpl> callbackPtr(
|
|
|
|
new CefAuthCallbackImpl(callback, credentials));
|
|
|
|
if (client->GetAuthCredentials(auth_info.is_proxy,
|
|
|
|
auth_info.challenger.host(),
|
|
|
|
auth_info.challenger.port(),
|
|
|
|
auth_info.realm,
|
|
|
|
auth_info.scheme,
|
|
|
|
callbackPtr.get())) {
|
|
|
|
return AUTH_REQUIRED_RESPONSE_IO_PENDING;
|
|
|
|
} else {
|
|
|
|
callbackPtr->Disconnect();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-04-03 03:34:16 +02:00
|
|
|
return AUTH_REQUIRED_RESPONSE_NO_ACTION;
|
|
|
|
}
|