1 /*
2 * This file is part of the KDE project.
3 *
4 * Copyright (C) 2008 Michael Howell <mhowell123@gmail.com>
5 * Copyright (C) 2008 Urs Wolfer <uwolfer @ kde.org>
6 * Copyright (C) 2009 Dawit Alemayehu <adawit @ kde.org>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 * Boston, MA 02110-1301, USA.
22 *
23 */
24
25 #include "kwebpluginfactory.h"
26 #include "kwebpage.h"
27 #include "kwebview.h"
28
29 #include <kmimetypetrader.h>
30 #include <kservicetypetrader.h>
31 #include <QDebug>
32
33 #include <kio/job.h>
34 #include <kparts/readonlypart.h>
35 #include <kparts/openurlarguments.h>
36
37 #include <QListIterator>
38 #include <QStringList>
39 #include <QList>
40 #include <qmimedatabase.h>
41
42 #include <QWebPluginFactory>
43 #include <QWebFrame>
44 #include <QWebView>
45
46 #define QL1S(x) QLatin1String(x)
47 #define QL1C(x) QLatin1Char(x)
48
KWebPluginFactory(QObject * parent)49 KWebPluginFactory::KWebPluginFactory(QObject *parent)
50 : QWebPluginFactory(parent), d(nullptr)
51 {
52 }
53
~KWebPluginFactory()54 KWebPluginFactory::~KWebPluginFactory()
55 {
56 }
57
create(const QString & _mimeType,const QUrl & url,const QStringList & argumentNames,const QStringList & argumentValues) const58 QObject *KWebPluginFactory::create(const QString &_mimeType, const QUrl &url, const QStringList &argumentNames, const QStringList &argumentValues) const
59 {
60 QString mimeType(_mimeType.trimmed());
61 // If no mimetype is provided, we do our best to correctly determine it here...
62 if (mimeType.isEmpty()) {
63 // qDebug() << "Looking up missing mimetype for plugin resource:" << url;
64 extractGuessedMimeType(url, &mimeType);
65 // qDebug() << "Updated mimetype to" << mimeType;
66 }
67
68 // Defer handling of flash content to QtWebKit's builtin viewer.
69 // If you want to use/test KDE's nspluginviewer, comment out the
70 // if statement below.
71 KParts::ReadOnlyPart *part = (excludedMimeType(mimeType) ? nullptr : createPartInstanceFrom(mimeType, argumentNames, argumentValues, nullptr, parent()));
72
73 // qDebug() << "Asked for" << mimeType << "plugin, got" << part;
74
75 if (part) {
76 QMap<QString, QString> metaData = part->arguments().metaData();
77 QString urlStr = url.toString(QUrl::RemovePath | QUrl::RemoveQuery | QUrl::RemoveFragment);
78 metaData.insert("PropagateHttpHeader", "true");
79 metaData.insert("referrer", urlStr);
80 metaData.insert("cross-domain", urlStr);
81 metaData.insert("main_frame_request", "TRUE");
82 metaData.insert("ssl_activate_warnings", "TRUE");
83
84 KWebPage *page = qobject_cast<KWebPage *>(parent());
85
86 if (page) {
87 const QString scheme = page->currentFrame()->url().scheme();
88 if (page && (QString::compare(scheme, QL1S("https"), Qt::CaseInsensitive) == 0 ||
89 QString::compare(scheme, QL1S("webdavs"), Qt::CaseInsensitive) == 0)) {
90 metaData.insert("ssl_was_in_use", "TRUE");
91 } else {
92 metaData.insert("ssl_was_in_use", "FALSE");
93 }
94 }
95
96 KParts::OpenUrlArguments openUrlArgs = part->arguments();
97 openUrlArgs.metaData() = metaData;
98 openUrlArgs.setMimeType(mimeType);
99 part->setArguments(openUrlArgs);
100 part->openUrl(url);
101 return part->widget();
102 }
103
104 return nullptr;
105 }
106
plugins() const107 QList<KWebPluginFactory::Plugin> KWebPluginFactory::plugins() const
108 {
109 QList<Plugin> plugins;
110 return plugins;
111 }
112
isHttpProtocol(const QUrl & url)113 static bool isHttpProtocol(const QUrl &url)
114 {
115 const QString scheme(url.scheme());
116 return (scheme.startsWith(QL1S("http"), Qt::CaseInsensitive)
117 || scheme.startsWith(QL1S("webdav"), Qt::CaseInsensitive));
118 }
119
extractGuessedMimeType(const QUrl & url,QString * mimeType) const120 void KWebPluginFactory::extractGuessedMimeType(const QUrl &url, QString *mimeType) const
121 {
122 if (mimeType) {
123 const QUrl reqUrl((isHttpProtocol(url) ? QUrl(url.path()) : url));
124 QMimeDatabase db;
125 QMimeType mime = db.mimeTypeForFile(reqUrl.path(), QMimeDatabase::MatchExtension);
126 if (!mime.isDefault() && !mime.name().startsWith(QL1S("inode/"))) {
127 *mimeType = mime.name();
128 }
129 }
130 }
131
createPartInstanceFrom(const QString & mimeType,const QStringList & argumentNames,const QStringList & argumentValues,QWidget * parentWidget,QObject * parentObj) const132 KParts::ReadOnlyPart *KWebPluginFactory::createPartInstanceFrom(const QString &mimeType,
133 const QStringList &argumentNames,
134 const QStringList &argumentValues,
135 QWidget *parentWidget,
136 QObject *parentObj) const
137 {
138 KParts::ReadOnlyPart *part = nullptr;
139
140 if (!mimeType.isEmpty()) {
141 // Only attempt to find a KPart for the supported mime types...
142 QVariantList arguments;
143 const int count = argumentNames.count();
144
145 for (int i = 0; i < count; ++i) {
146 arguments << QString(argumentNames.at(i) + QL1S("=\"") + argumentValues.at(i) + QL1C('\"'));
147 }
148 part = KMimeTypeTrader::createPartInstanceFromQuery<KParts::ReadOnlyPart>(mimeType, parentWidget, parentObj, QString(), arguments);
149 }
150
151 return part;
152 }
153
excludedMimeType(const QString & mimeType) const154 bool KWebPluginFactory::excludedMimeType(const QString &mimeType) const
155 {
156 if (mimeType.startsWith(QL1S("inode/"), Qt::CaseInsensitive)) {
157 return true;
158 }
159
160 if (mimeType.startsWith(QL1S("application/x-java"), Qt::CaseInsensitive)) {
161 return true;
162 }
163
164 if (mimeType == QL1S("application/x-shockwave-flash") ||
165 mimeType == QL1S("application/futuresplash")) {
166 return true;
167 }
168
169 return false;
170 }
171
172