summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
authorAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-04-30 15:15:02 +0200
committerAllan Sandfeld Jensen <allan.jensen@theqtcompany.com>2015-05-13 11:18:31 +0000
commit3b585086049089224422e6f839485be5a4235524 (patch)
tree51de03f5a29a16b0fa4909346a63cbeaf2122d5b /src/core
parent78de547bc25b708000f6ee725be8cbde7c373d4f (diff)
Add redirect and error API for custom URL
Adds API for failing or redirecting custom URL requests. Change-Id: Ia633bff2c0b8484fd6fdb8d42982fda2e427db4c Reviewed-by: Andras Becsi <andras.becsi@theqtcompany.com>
Diffstat (limited to 'src/core')
-rw-r--r--src/core/url_request_custom_job.cpp42
-rw-r--r--src/core/url_request_custom_job.h5
-rw-r--r--src/core/url_request_custom_job_delegate.cpp37
-rw-r--r--src/core/url_request_custom_job_delegate.h13
4 files changed, 97 insertions, 0 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<URLRequestCustomJob> 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 <QByteArray>
@@ -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);