- Allow asynchronous continuation of OnBeforeResourceLoad (issue #1593).

- Use CefRequestCallback for most asynchronous CefRequestHandler methods.
This commit is contained in:
Marshall Greenblatt
2015-04-02 17:21:46 +02:00
parent 0b795eed10
commit fe03339870
26 changed files with 574 additions and 416 deletions

View File

@ -85,7 +85,7 @@ class CefAccessTokenStore : public content::AccessTokenStore {
DISALLOW_COPY_AND_ASSIGN(CefAccessTokenStore);
};
class CefQuotaCallbackImpl : public CefQuotaCallback {
class CefQuotaCallbackImpl : public CefRequestCallback {
public:
explicit CefQuotaCallbackImpl(
const content::QuotaPermissionContext::PermissionCallback& callback)
@ -96,10 +96,10 @@ class CefQuotaCallbackImpl : public CefQuotaCallback {
if (!callback_.is_null()) {
// The callback is still pending. Cancel it now.
if (CEF_CURRENTLY_ON_IOT()) {
CancelNow(callback_);
RunNow(callback_, false);
} else {
CEF_POST_TASK(CEF_IOT,
base::Bind(&CefQuotaCallbackImpl::CancelNow, callback_));
base::Bind(&CefQuotaCallbackImpl::RunNow, callback_, false));
}
}
}
@ -107,9 +107,7 @@ class CefQuotaCallbackImpl : public CefQuotaCallback {
void Continue(bool allow) override {
if (CEF_CURRENTLY_ON_IOT()) {
if (!callback_.is_null()) {
callback_.Run(allow ?
content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW :
content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_DISALLOW);
RunNow(callback_, allow);
callback_.Reset();
}
} else {
@ -119,14 +117,7 @@ class CefQuotaCallbackImpl : public CefQuotaCallback {
}
void Cancel() override {
if (CEF_CURRENTLY_ON_IOT()) {
if (!callback_.is_null()) {
CancelNow(callback_);
callback_.Reset();
}
} else {
CEF_POST_TASK(CEF_IOT, base::Bind(&CefQuotaCallbackImpl::Cancel, this));
}
Continue(false);
}
void Disconnect() {
@ -134,11 +125,13 @@ class CefQuotaCallbackImpl : public CefQuotaCallback {
}
private:
static void CancelNow(
const content::QuotaPermissionContext::PermissionCallback& callback) {
static void RunNow(
const content::QuotaPermissionContext::PermissionCallback& callback,
bool allow) {
CEF_REQUIRE_IOT();
callback.Run(
content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_CANCELLED);
callback.Run(allow ?
content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_ALLOW :
content::QuotaPermissionContext::QUOTA_PERMISSION_RESPONSE_DISALLOW);
}
content::QuotaPermissionContext::PermissionCallback callback_;
@ -147,8 +140,7 @@ class CefQuotaCallbackImpl : public CefQuotaCallback {
DISALLOW_COPY_AND_ASSIGN(CefQuotaCallbackImpl);
};
class CefAllowCertificateErrorCallbackImpl
: public CefAllowCertificateErrorCallback {
class CefAllowCertificateErrorCallbackImpl : public CefRequestCallback {
public:
typedef base::Callback<void(bool)> // NOLINT(readability/function)
CallbackType;
@ -157,24 +149,46 @@ class CefAllowCertificateErrorCallbackImpl
: callback_(callback) {
}
~CefAllowCertificateErrorCallbackImpl() {
if (!callback_.is_null()) {
// The callback is still pending. Cancel it now.
if (CEF_CURRENTLY_ON_UIT()) {
RunNow(callback_, false);
} else {
CEF_POST_TASK(CEF_UIT,
base::Bind(&CefAllowCertificateErrorCallbackImpl::RunNow,
callback_, false));
}
}
}
void Continue(bool allow) override {
if (CEF_CURRENTLY_ON_UIT()) {
if (!callback_.is_null()) {
callback_.Run(allow);
RunNow(callback_, allow);
callback_.Reset();
}
} else {
CEF_POST_TASK(CEF_UIT,
base::Bind(&CefAllowCertificateErrorCallbackImpl::Continue,
this, allow));
this, allow));
}
}
void Cancel() override {
Continue(false);
}
void Disconnect() {
callback_.Reset();
}
private:
static void RunNow(const CallbackType& callback, bool allow) {
CEF_REQUIRE_IOT();
callback.Run(allow);
}
CallbackType callback_;
IMPLEMENT_REFCOUNTING(CefAllowCertificateErrorCallbackImpl);
@ -211,11 +225,6 @@ class CefGeolocationCallbackImpl : public CefGeolocationCallback {
}
private:
static void Run(const CallbackType& callback, bool allow) {
CEF_REQUIRE_UIT();
callback.Run(allow);
}
CallbackType callback_;
IMPLEMENT_REFCOUNTING(CefGeolocationCallbackImpl);

View File

@ -17,6 +17,132 @@
namespace {
class CefBeforeResourceLoadCallbackImpl : public CefRequestCallback {
public:
typedef net::CompletionCallback CallbackType;
CefBeforeResourceLoadCallbackImpl(
CefRefPtr<CefRequestImpl> cef_request,
GURL* new_url,
net::URLRequest* url_request,
const CallbackType& callback)
: cef_request_(cef_request),
new_url_(new_url),
url_request_(url_request),
callback_(callback) {
DCHECK(new_url);
DCHECK(url_request_);
// Add an association between the URLRequest and this object.
url_request_->SetUserData(UserDataKey(), new Disconnector(this));
}
~CefBeforeResourceLoadCallbackImpl() {
if (!callback_.is_null()) {
// The callback is still pending. Cancel it now.
if (CEF_CURRENTLY_ON_IOT()) {
RunNow(cef_request_, new_url_, url_request_, callback_, false);
} else {
CEF_POST_TASK(CEF_IOT,
base::Bind(&CefBeforeResourceLoadCallbackImpl::RunNow,
cef_request_, new_url_, url_request_, callback_, false));
}
}
}
void Continue(bool allow) override {
// Always continue asynchronously.
CEF_POST_TASK(CEF_IOT,
base::Bind(&CefBeforeResourceLoadCallbackImpl::ContinueNow,
this, allow));
}
void Cancel() override {
Continue(false);
}
void ContinueNow(bool allow) {
CEF_REQUIRE_IOT();
if (!callback_.is_null()) {
RunNow(cef_request_, new_url_, url_request_, callback_, allow);
Disconnect();
}
}
void Disconnect() {
CEF_REQUIRE_IOT();
cef_request_ = nullptr;
new_url_ = nullptr;
url_request_ = nullptr;
callback_.Reset();
}
private:
// Used to disconnect the callback when the associated URLRequest is
// destroyed.
class Disconnector : public base::SupportsUserData::Data {
public:
explicit Disconnector(CefBeforeResourceLoadCallbackImpl* callback)
: callback_(callback) {
}
~Disconnector() override {
if (callback_)
callback_->Disconnect();
}
void Disconnect() {
callback_ = NULL;
}
private:
CefBeforeResourceLoadCallbackImpl* callback_;
};
static void RunNow(CefRefPtr<CefRequestImpl> cef_request,
GURL* new_url,
net::URLRequest* request,
const CallbackType& callback,
bool allow) {
CEF_REQUIRE_IOT();
if (allow) {
const GURL& old_url = request->url();
GURL url = GURL(cef_request->GetURL().ToString());
if (old_url != url)
new_url->Swap(&url);
cef_request->Get(request);
}
// Remove the association between the URLRequest and this object.
Disconnector* disconnector =
static_cast<Disconnector*>(request->GetUserData(UserDataKey()));
DCHECK(disconnector);
disconnector->Disconnect();
request->RemoveUserData(UserDataKey());
callback.Run(allow ? net::OK : net::ERR_ABORTED);
}
static inline void* UserDataKey() {
return &kLocatorKey;
}
CefRefPtr<CefRequestImpl> cef_request_;
const GURL old_url_;
GURL* new_url_;
net::URLRequest* url_request_;
CallbackType callback_;
// The user data key.
static int kLocatorKey;
IMPLEMENT_REFCOUNTING(CefBeforeResourceLoadCallbackImpl);
DISALLOW_COPY_AND_ASSIGN(CefBeforeResourceLoadCallbackImpl);
};
int CefBeforeResourceLoadCallbackImpl::kLocatorKey = 0;
class CefAuthCallbackImpl : public CefAuthCallback {
public:
CefAuthCallbackImpl(const net::NetworkDelegate::AuthCallback& callback,
@ -98,27 +224,32 @@ int CefNetworkDelegate::OnBeforeURLRequest(
if (handler.get()) {
CefRefPtr<CefFrame> frame = browser->GetFrameForRequest(request);
GURL old_url = request->url();
// 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;
CefRefPtr<CefBeforeResourceLoadCallbackImpl> callbackImpl(
new CefBeforeResourceLoadCallbackImpl(requestPtr, new_url, request,
callback));
// Give the client an opportunity to evaluate the request.
cef_return_value_t retval = handler->OnBeforeResourceLoad(
browser.get(), frame, requestPtr.get(), callbackImpl.get());
if (retval == RV_CANCEL) {
// Cancel the request.
callbackImpl->Continue(false);
} else if (retval == RV_CONTINUE) {
// Continue the request.
callbackImpl->Continue(true);
}
GURL url = GURL(std::string(requestPtr->GetURL()));
if (old_url != url)
new_url ->Swap(&url);
requestPtr->Get(request);
// Continue or cancel the request asynchronously.
return net::ERR_IO_PENDING;
}
}
}
// Continue the request immediately.
return net::OK;
}

View File

@ -26,7 +26,7 @@ class CefNetworkDelegate : public net::NetworkDelegateImpl {
const AuthCallback& callback,
net::AuthCredentials* credentials) override;
bool OnCanAccessFile(const net::URLRequest& request,
const base::FilePath& path) const;
const base::FilePath& path) const override;
DISALLOW_COPY_AND_ASSIGN(CefNetworkDelegate);
};