From ab7da2095d495da0e346cee2745b61d83588abe6 Mon Sep 17 00:00:00 2001 From: Xiao Zhang Date: Tue, 3 Dec 2013 14:16:38 +0800 Subject: Add qrc protocol handler. Support qrc resources in html. For example: Change-Id: I490efb7cc300bf894659e79948b65729a12ad73d Reviewed-by: Milian Wolff Reviewed-by: Pierre Rossi --- src/core/core_gyp_generator.pro | 8 +- src/core/qrc_protocol_handler_qt.cpp | 61 +++++++++++++ src/core/qrc_protocol_handler_qt.h | 68 +++++++++++++++ src/core/url_request_context_getter_qt.cpp | 4 + src/core/url_request_qrc_job_qt.cpp | 132 +++++++++++++++++++++++++++++ src/core/url_request_qrc_job_qt.h | 75 ++++++++++++++++ 6 files changed, 346 insertions(+), 2 deletions(-) create mode 100644 src/core/qrc_protocol_handler_qt.cpp create mode 100644 src/core/qrc_protocol_handler_qt.h create mode 100644 src/core/url_request_qrc_job_qt.cpp create mode 100644 src/core/url_request_qrc_job_qt.h (limited to 'src') diff --git a/src/core/core_gyp_generator.pro b/src/core/core_gyp_generator.pro index 1cfcc4609..4388cdb1c 100644 --- a/src/core/core_gyp_generator.pro +++ b/src/core/core_gyp_generator.pro @@ -52,7 +52,9 @@ SOURCES = \ web_contents_view_qt.cpp \ web_engine_context.cpp \ web_event_factory.cpp \ - yuv_video_node.cpp + yuv_video_node.cpp \ + qrc_protocol_handler_qt.cpp \ + url_request_qrc_job_qt.cpp HEADERS = \ backing_store_qt.h \ @@ -77,7 +79,9 @@ HEADERS = \ web_contents_view_qt.h \ web_engine_context.h \ web_event_factory.h \ - yuv_video_node.h + yuv_video_node.h \ + qrc_protocol_handler_qt.h \ + url_request_qrc_job_qt.h OUTPUT_DIR = $$getOutDir()/$$getConfigDir() target.files = $$OUTPUT_DIR/lib/lib$${TARGET}.so diff --git a/src/core/qrc_protocol_handler_qt.cpp b/src/core/qrc_protocol_handler_qt.cpp new file mode 100644 index 000000000..1a1266d78 --- /dev/null +++ b/src/core/qrc_protocol_handler_qt.cpp @@ -0,0 +1,61 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "qrc_protocol_handler_qt.h" +#include "url_request_qrc_job_qt.h" + +#include "net/base/net_errors.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_error_job.h" + +using namespace net; + +QrcProtocolHandlerQt::QrcProtocolHandlerQt() +{ +} + +URLRequestJob *QrcProtocolHandlerQt::MaybeCreateJob(URLRequest *request, NetworkDelegate *networkDelegate) const +{ + if (!networkDelegate) + return new URLRequestErrorJob(request, Q_NULLPTR, ERR_ACCESS_DENIED); + + return new URLRequestQrcJobQt(request, networkDelegate); +} diff --git a/src/core/qrc_protocol_handler_qt.h b/src/core/qrc_protocol_handler_qt.h new file mode 100644 index 000000000..9fa22a0e3 --- /dev/null +++ b/src/core/qrc_protocol_handler_qt.h @@ -0,0 +1,68 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef QRC_PROTOCOL_HANDLER_QT_H_ +#define QRC_PROTOCOL_HANDLER_QT_H_ + +#include "net/url_request/url_request_job_factory.h" + +#include // Needed for Q_DECL_OVERRIDE + +namespace net { + +class NetworkDelegate; +class URLRequestJob; + +} // namespace + +// Implements a ProtocolHandler for qrc file jobs. If |network_delegate_| is NULL, +// then all file requests will fail with ERR_ACCESS_DENIED. +class QrcProtocolHandlerQt : public net::URLRequestJobFactory::ProtocolHandler { + +public: + QrcProtocolHandlerQt(); + virtual net::URLRequestJob *MaybeCreateJob(net::URLRequest *request, net::NetworkDelegate *networkDelegate) const Q_DECL_OVERRIDE; + +private: + DISALLOW_COPY_AND_ASSIGN(QrcProtocolHandlerQt); +}; + +#endif // QRC_PROTOCOL_HANDLER_QT_H_ diff --git a/src/core/url_request_context_getter_qt.cpp b/src/core/url_request_context_getter_qt.cpp index 99bf68b1c..933df845c 100644 --- a/src/core/url_request_context_getter_qt.cpp +++ b/src/core/url_request_context_getter_qt.cpp @@ -63,6 +63,9 @@ #include "net/url_request/file_protocol_handler.h" #include "network_delegate_qt.h" +#include "qrc_protocol_handler_qt.h" + +static const char kQrcSchemeQt[] = "qrc"; using content::BrowserThread; @@ -159,6 +162,7 @@ net::URLRequestContext *URLRequestContextGetterQt::GetURLRequestContext() m_jobFactory.reset(new net::URLRequestJobFactoryImpl()); m_jobFactory->SetProtocolHandler(chrome::kDataScheme, new net::DataProtocolHandler()); m_jobFactory->SetProtocolHandler(chrome::kFileScheme, new net::FileProtocolHandler()); + m_jobFactory->SetProtocolHandler(kQrcSchemeQt, new QrcProtocolHandlerQt()); m_urlRequestContext->set_job_factory(m_jobFactory.get()); } diff --git a/src/core/url_request_qrc_job_qt.cpp b/src/core/url_request_qrc_job_qt.cpp new file mode 100644 index 000000000..ccdb4f9b6 --- /dev/null +++ b/src/core/url_request_qrc_job_qt.cpp @@ -0,0 +1,132 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include "url_request_qrc_job_qt.h" + +#include "net/base/net_errors.h" +#include "net/base/io_buffer.h" + +#include +#include +#include +#include + +using namespace net; + +URLRequestQrcJobQt::URLRequestQrcJobQt(URLRequest *request, NetworkDelegate *networkDelegate) + : URLRequestJob(request, networkDelegate) + , m_remainingBytes(0) + , m_weakFactory(this) +{ +} + +URLRequestQrcJobQt::~URLRequestQrcJobQt() +{ + if (m_file.isOpen()) + m_file.close(); +} + +void URLRequestQrcJobQt::Start() +{ + base::MessageLoop::current()->PostTask(FROM_HERE, base::Bind(&URLRequestQrcJobQt::startGetHead, m_weakFactory.GetWeakPtr())); +} + +void URLRequestQrcJobQt::Kill() +{ + if (m_file.isOpen()) + m_file.close(); + m_weakFactory.InvalidateWeakPtrs(); + + URLRequestJob::Kill(); +} + +bool URLRequestQrcJobQt::GetMimeType(std::string *mimeType) const +{ + DCHECK(request_); + if (m_mimeType.size() > 0) { + *mimeType = m_mimeType; + return true; + } + return false; +} + +bool URLRequestQrcJobQt::ReadRawData(IOBuffer *buf, int bufSize, int *bytesRead) +{ + DCHECK(bytesRead); + DCHECK_GE(m_remainingBytes, 0); + // File has been read finished. + if (!m_remainingBytes || !bufSize) { + *bytesRead = 0; + return true; + } + if (m_remainingBytes < bufSize) + bufSize = static_cast(m_remainingBytes); + qint64 rv = m_file.read(buf->data(), bufSize); + if (rv >= 0) { + *bytesRead = static_cast(rv); + m_remainingBytes -= rv; + DCHECK_GE(m_remainingBytes, 0); + return true; + } else { + NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, ERR_FAILED)); + } + return false; +} + +void URLRequestQrcJobQt::startGetHead() +{ + // Get qrc file path. + QString qrcFilePath = ':' + QString::fromStdString(request_->url().path()); + m_file.setFileName(qrcFilePath); + QFileInfo qrcFileInfo(m_file); + // Get qrc file mime type. + QMimeDatabase mimeDatabase; + QMimeType mimeType = mimeDatabase.mimeTypeForFile(qrcFileInfo); + m_mimeType = mimeType.name().toStdString(); + // Open file + if (m_file.open(QIODevice::ReadOnly)) { + m_remainingBytes = m_file.size(); + // Notify that the headers are complete + NotifyHeadersComplete(); + } else { + NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, ERR_INVALID_URL)); + } +} diff --git a/src/core/url_request_qrc_job_qt.h b/src/core/url_request_qrc_job_qt.h new file mode 100644 index 000000000..b9c4ed222 --- /dev/null +++ b/src/core/url_request_qrc_job_qt.h @@ -0,0 +1,75 @@ +/**************************************************************************** +** +** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). +** Contact: http://www.qt-project.org/legal +** +** This file is part of the QtWebEngine module of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#ifndef URL_REQUEST_QRC_JOB_QT_H_ +#define URL_REQUEST_QRC_JOB_QT_H_ + +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_job.h" + +#include // Needed for Q_DECL_OVERRIDE +#include + +// A request job that handles reading qrc file URLs +class URLRequestQrcJobQt : public net::URLRequestJob { + +public: + URLRequestQrcJobQt(net::URLRequest *request, net::NetworkDelegate *networkDelegate); + virtual void Start() Q_DECL_OVERRIDE; + virtual void Kill() Q_DECL_OVERRIDE; + virtual bool ReadRawData(net::IOBuffer *buf, int bufSize, int *bytesRead) Q_DECL_OVERRIDE; + virtual bool GetMimeType(std::string *mimeType) const Q_DECL_OVERRIDE; + +protected: + virtual ~URLRequestQrcJobQt(); + // Get file mime type and try open file on a background thread. + void startGetHead(); + +private: + qint64 m_remainingBytes; + QFile m_file; + std::string m_mimeType; + base::WeakPtrFactory m_weakFactory; + + DISALLOW_COPY_AND_ASSIGN(URLRequestQrcJobQt); +}; + +#endif // URL_REQUEST_QRC_JOB_QT_H_ -- cgit v1.2.3