1/****************************************************************************
2**
3** Copyright (C) 2018 The Qt Company Ltd.
4** Contact: http://www.qt.io/licensing/
5**
6** This file is part of the QtWebView module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL3$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see http://www.qt.io/terms-conditions. For further
15** information use the contact form at http://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or later as published by the Free
28** Software Foundation and appearing in the file LICENSE.GPL included in
29** the packaging of this file. Please review the following information to
30** ensure the GNU General Public License version 2.0 requirements will be
31** met: http://www.gnu.org/licenses/gpl-2.0.html.
32**
33** $QT_END_LICENSE$
34**
35****************************************************************************/
36
37#include "qwebviewfactory_p.h"
38#include "qwebviewplugin_p.h"
39#include <private/qfactoryloader_p.h>
40#include <QtCore/qglobal.h>
41
42QT_BEGIN_NAMESPACE
43
44Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, (QWebViewPluginInterface_iid, QLatin1String("/webview")))
45
46static QString getPluginName()
47{
48 static const QString name = !qEnvironmentVariableIsEmpty(varName: "QT_WEBVIEW_PLUGIN")
49 ? QString::fromLatin1(str: qgetenv(varName: "QT_WEBVIEW_PLUGIN"))
50#ifdef Q_OS_MACOS
51 : QStringLiteral("webengine");
52#else
53 : QStringLiteral("native");
54#endif // Q_OS_MACOS
55 return name;
56}
57
58class QNullWebView : public QAbstractWebView
59{
60public:
61 void setParentView(QObject *view) override { Q_UNUSED(view); }
62 QObject *parentView() const override { return nullptr; }
63 void setGeometry(const QRect &geometry) override { Q_UNUSED(geometry); }
64 void setVisibility(QWindow::Visibility visibility) override { Q_UNUSED(visibility); }
65 void setVisible(bool visible) override { Q_UNUSED(visible); }
66
67 QString httpUserAgent() const override { return QString(); }
68 void setHttpUserAgent(const QString &userAgent) override { Q_UNUSED(userAgent) }
69 QUrl url() const override { return QUrl(); }
70 void setUrl(const QUrl &url) override { Q_UNUSED(url); }
71 bool canGoBack() const override { return false; }
72 bool canGoForward() const override { return false; }
73 QString title() const override { return QString(); }
74 int loadProgress() const override { return 0; }
75 bool isLoading() const override { return false; }
76 void goBack() override { }
77 void goForward() override { }
78 void stop() override { }
79 void reload() override { }
80 void loadHtml(const QString &html, const QUrl &baseUrl) override
81 { Q_UNUSED(html); Q_UNUSED(baseUrl); }
82 void runJavaScriptPrivate(const QString &script, int callbackId) override
83 { Q_UNUSED(script); Q_UNUSED(callbackId); }
84};
85
86QAbstractWebView *QWebViewFactory::createWebView()
87{
88 QAbstractWebView *wv = nullptr;
89 QWebViewPlugin *plugin = getPlugin();
90 if (plugin)
91 wv = plugin->create(QStringLiteral("webview"));
92
93 if (!wv || !plugin) {
94 qWarning(msg: "No WebView plug-in found!");
95 wv = new QNullWebView;
96 }
97
98 return wv;
99}
100
101bool QWebViewFactory::requiresExtraInitializationSteps()
102{
103 // The call to loader->indexOf(pluginName) will mess up winrt's main thread.
104#ifdef Q_OS_WINRT
105 return false;
106#endif
107 const QString pluginName = getPluginName();
108 const int index = pluginName.isEmpty() ? 0 : qMax<int>(a: 0, b: loader->indexOf(needle: pluginName));
109
110 const auto metaDataList = loader->metaData();
111 if (metaDataList.isEmpty())
112 return false;
113
114 const auto &pluginMetaData = metaDataList.at(i: index);
115 const auto iid = pluginMetaData.value(key: QLatin1String("IID"));
116 Q_ASSERT(iid == QJsonValue(QLatin1String(QWebViewPluginInterface_iid)));
117 const auto metaDataObject = pluginMetaData.value(key: QLatin1String("MetaData")).toObject();
118 const auto it = metaDataObject.find(key: QLatin1String("RequiresInit"));
119 if (it != pluginMetaData.constEnd())
120 return it->isBool() ? it->toBool() : false;
121
122 return false;
123}
124
125QWebViewPlugin *QWebViewFactory::getPlugin()
126{
127 // Plugin loading logic:
128 // 1. Get plugin name - plugin name is either user specified or "native"
129 // - Exception: macOS, which will default to using "webengine" until the native plugin is matured.
130 // 2. If neither a user specified or "default" plugin exists, then the first available is used.
131 const QString pluginName = getPluginName();
132 const int index = pluginName.isEmpty() ? 0 : qMax<int>(a: 0, b: loader->indexOf(needle: pluginName));
133 return qobject_cast<QWebViewPlugin *>(object: loader->instance(index));
134}
135
136QT_END_NAMESPACE
137

source code of qtwebview/src/webview/qwebviewfactory.cpp