From 3b585086049089224422e6f839485be5a4235524 Mon Sep 17 00:00:00 2001 From: Allan Sandfeld Jensen Date: Thu, 30 Apr 2015 15:15:02 +0200 Subject: Add redirect and error API for custom URL Adds API for failing or redirecting custom URL requests. Change-Id: Ia633bff2c0b8484fd6fdb8d42982fda2e427db4c Reviewed-by: Andras Becsi --- src/core/url_request_custom_job.cpp | 42 ++++++++++++++++++++++ src/core/url_request_custom_job.h | 5 +++ src/core/url_request_custom_job_delegate.cpp | 37 +++++++++++++++++++ src/core/url_request_custom_job_delegate.h | 13 +++++++ .../api/qwebengineurlrequestjob.cpp | 20 +++++++++-- .../api/qwebengineurlrequestjob_p.h | 11 ++++++ 6 files changed, 126 insertions(+), 2 deletions(-) diff --git a/src/core/url_request_custom_job.cpp b/src/core/url_request_custom_job.cpp index 0b8aaf9ba..cd71b6900 100644 --- a/src/core/url_request_custom_job.cpp +++ b/src/core/url_request_custom_job.cpp @@ -117,6 +117,18 @@ bool URLRequestCustomJob::GetCharset(std::string* charset) return false; } +bool URLRequestCustomJob::IsRedirectResponse(GURL* location, int* http_status_code) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + QMutexLocker lock(&m_mutex); + if (m_redirect.is_valid()) { + *location = m_redirect; + *http_status_code = 303; + return true; + } + return false; +} + void URLRequestCustomJob::setReplyMimeType(const std::string &mimeType) { DCHECK_CURRENTLY_ON(content::BrowserThread::UI); @@ -158,6 +170,36 @@ bool URLRequestCustomJob::ReadRawData(IOBuffer *buf, int bufSize, int *bytesRead return false; } +void URLRequestCustomJob::redirect(const GURL &url) +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + if (m_device || m_error) + return; + + QMutexLocker lock(&m_mutex); + m_redirect = url; + content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, base::Bind(&URLRequestCustomJob::notifyStarted, m_weakFactory.GetWeakPtr())); +} + +void URLRequestCustomJob::abort() +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); + QMutexLocker lock(&m_mutex); + if (m_device && m_device->isOpen()) + m_device->close(); + m_device = 0; + content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, base::Bind(&URLRequestCustomJob::notifyCanceled, m_weakFactory.GetWeakPtr())); +} + +void URLRequestCustomJob::notifyCanceled() +{ + DCHECK_CURRENTLY_ON(content::BrowserThread::IO); + if (m_started) + NotifyDone(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED)); + else + NotifyStartError(URLRequestStatus(URLRequestStatus::CANCELED, ERR_ABORTED)); +} + void URLRequestCustomJob::notifyStarted() { DCHECK_CURRENTLY_ON(content::BrowserThread::IO); diff --git a/src/core/url_request_custom_job.h b/src/core/url_request_custom_job.h index ca20c719d..4975e71d2 100644 --- a/src/core/url_request_custom_job.h +++ b/src/core/url_request_custom_job.h @@ -60,18 +60,22 @@ public: virtual bool ReadRawData(net::IOBuffer *buf, int bufSize, int *bytesRead) Q_DECL_OVERRIDE; virtual bool GetMimeType(std::string *mimeType) const Q_DECL_OVERRIDE; virtual bool GetCharset(std::string *charset) Q_DECL_OVERRIDE; + virtual bool IsRedirectResponse(GURL* location, int* http_status_code) Q_DECL_OVERRIDE; void setReplyMimeType(const std::string &); void setReplyCharset(const std::string &); void setReplyDevice(QIODevice *); + void redirect(const GURL &url); void fail(int); + void abort(); protected: virtual ~URLRequestCustomJob(); void startAsync(); void notifyStarted(); void notifyFailure(); + void notifyCanceled(); private: QMutex m_mutex; @@ -81,6 +85,7 @@ private: std::string m_mimeType; std::string m_charset; int m_error; + GURL m_redirect; bool m_started; base::WeakPtrFactory m_weakFactory; diff --git a/src/core/url_request_custom_job_delegate.cpp b/src/core/url_request_custom_job_delegate.cpp index d324da347..caf7a0e3e 100644 --- a/src/core/url_request_custom_job_delegate.cpp +++ b/src/core/url_request_custom_job_delegate.cpp @@ -38,6 +38,7 @@ #include "url_request_custom_job_delegate.h" #include "type_conversion.h" +#include "net/base/net_errors.h" #include @@ -63,4 +64,40 @@ void URLRequestCustomJobDelegate::setReply(const QByteArray &contentType, QIODev m_job->setReplyDevice(device); } +void URLRequestCustomJobDelegate::abort() +{ + m_job->abort(); +} + +void URLRequestCustomJobDelegate::redirect(const QUrl &url) +{ + m_job->redirect(toGurl(url)); +} + +void URLRequestCustomJobDelegate::fail(Error error) +{ + int net_error = 0; + switch (error) { + case NoError: + break; + case UrlInvalid: + net_error = net::ERR_INVALID_URL; + break; + case UrlNotFound: + net_error = net::ERR_FILE_NOT_FOUND; + break; + case RequestAborted: + net_error = net::ERR_ABORTED; + break; + case RequestDenied: + net_error = net::ERR_ACCESS_DENIED; + break; + case RequestFailed: + net_error = net::ERR_FAILED; + break; + } + if (net_error) + m_job->fail(net_error); +} + } // namespace diff --git a/src/core/url_request_custom_job_delegate.h b/src/core/url_request_custom_job_delegate.h index e0b802897..580149ecf 100644 --- a/src/core/url_request_custom_job_delegate.h +++ b/src/core/url_request_custom_job_delegate.h @@ -53,9 +53,22 @@ class QWEBENGINE_EXPORT URLRequestCustomJobDelegate : public QObject { public: ~URLRequestCustomJobDelegate(); + enum Error { + NoError = 0, + UrlNotFound, + UrlInvalid, + RequestAborted, + RequestDenied, + RequestFailed + }; + QUrl url() const; void setReply(const QByteArray &contentType, QIODevice *device); + void redirect(const QUrl& url); + void abort(); + + void fail(Error); private: URLRequestCustomJobDelegate(URLRequestCustomJob *job); diff --git a/src/webenginewidgets/api/qwebengineurlrequestjob.cpp b/src/webenginewidgets/api/qwebengineurlrequestjob.cpp index 323cdcc72..4a813236c 100644 --- a/src/webenginewidgets/api/qwebengineurlrequestjob.cpp +++ b/src/webenginewidgets/api/qwebengineurlrequestjob.cpp @@ -40,6 +40,8 @@ #include "url_request_custom_job_delegate.h" +using QtWebEngineCore::URLRequestCustomJobDelegate; + QT_BEGIN_NAMESPACE /*! @@ -51,7 +53,7 @@ QT_BEGIN_NAMESPACE A QWebEngineUrlRequestJob is given to QWebEngineUrlSchemeHandler::requestStarted() and must be handled by the derived implementations of class. - A job can be handled by calling setReply(). + A job can be handled by calling either setReply(), redirect() or setError(). The class is owned by QtWebEngine and does not need to be deleted. Note QtWebEngine may delete the job when it is no longer needed, so the signal QObject::destroyed() must be monitored if @@ -63,7 +65,7 @@ QT_BEGIN_NAMESPACE /*! \internal */ -QWebEngineUrlRequestJob::QWebEngineUrlRequestJob(QtWebEngineCore::URLRequestCustomJobDelegate * p) +QWebEngineUrlRequestJob::QWebEngineUrlRequestJob(URLRequestCustomJobDelegate * p) : QObject(p) // owned by the jobdelegate and deleted when the job is done , d_ptr(p) { @@ -92,6 +94,20 @@ void QWebEngineUrlRequestJob::setReply(const QByteArray &contentType, QIODevice d_ptr->setReply(contentType, device); } +/*! + Fails the request with error \a error. + */ +void QWebEngineUrlRequestJob::setError(Error r) +{ + d_ptr->fail((URLRequestCustomJobDelegate::Error)r); +} +/*! + Tell the request is redirected to \a url. + */ +void QWebEngineUrlRequestJob::setRedirect(const QUrl &url) +{ + d_ptr->redirect(url); +} QT_END_NAMESPACE diff --git a/src/webenginewidgets/api/qwebengineurlrequestjob_p.h b/src/webenginewidgets/api/qwebengineurlrequestjob_p.h index 72c9dc836..e496e4051 100644 --- a/src/webenginewidgets/api/qwebengineurlrequestjob_p.h +++ b/src/webenginewidgets/api/qwebengineurlrequestjob_p.h @@ -56,8 +56,19 @@ class QWEBENGINEWIDGETS_EXPORT QWebEngineUrlRequestJob : public QObject { public: ~QWebEngineUrlRequestJob(); + enum Error { + NoError = 0, + UrlNotFound, + UrlInvalid, + RequestAborted, + RequestDenied, + RequestFailed + }; + QUrl requestUrl() const; void setReply(const QByteArray &contentType, QIODevice *device); + void setError(Error error); + void setRedirect(const QUrl &url); private: QWebEngineUrlRequestJob(QtWebEngineCore::URLRequestCustomJobDelegate *); -- cgit v1.2.3