1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +01:00

Using WebPage and ExternalJsObject for communication with FxA Login page

Now Firefox Login page is opened in a WebPage object and the object
used for communication on the webchannel is registered using
ExternalJsObject::registerExtraObject.
This commit is contained in:
Prasenjit Kumar Shaw 2019-06-30 01:55:39 +05:30
parent 4fda04bc70
commit c1e49ec8a4
3 changed files with 16 additions and 30 deletions

View File

@ -1,11 +1,6 @@
new QWebChannel(qt.webChannelTransport, function(channel) {
communicator = channel.objects.communicator;
window.comm = communicator;
});
window.addEventListener('WebChannelMessageToChrome', function(event) {
let e = {type: event.type, detail: event.detail};
window.comm.receiveJSON(e);
external.extra.communicator.receiveJSON(e);
});
function sendMessage(response) {

View File

@ -16,6 +16,8 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
* ============================================================ */
#include "fxalogin.h"
#include "javascript/externaljsobject.h"
#include "webpage.h"
#include <QWebEnginePage>
#include <QWebEngineScript>
@ -28,43 +30,32 @@
FxALoginPage::FxALoginPage(QWidget* parent)
: QWebEngineView(parent)
{
m_page = new QWebEnginePage(this);
m_channel = new QWebChannel(m_page);
m_page->setWebChannel(m_channel);
m_communicator = new MessageReceiver(this);
ExternalJsObject::registerExtraObject(QString("communicator"), m_communicator);
connect(m_communicator, &MessageReceiver::signalMessageReceived,
this, &FxALoginPage::slotMessageReceived);
m_page = new WebPage(this);
m_page->load(FxALoginUrl);
setPage(m_page);
connect(m_page, &QWebEnginePage::loadFinished, this, &FxALoginPage::pageLoadFinished);
connect(m_page, &WebPage::loadFinished, this, &FxALoginPage::pageLoadFinished);
}
FxALoginPage::~FxALoginPage()
{
delete m_communicator;
delete m_channel;
}
void FxALoginPage::pageLoadFinished(bool pageLoaded)
{
if (pageLoaded) {
QFile apiFile(":/qtwebchannel/qwebchannel.js");
if (!apiFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't load Qt's Webchannel API!";
}
QString apiScript = QString::fromUtf8(apiFile.readAll());
apiFile.close();
m_page->runJavaScript(apiScript);
m_communicator = new MessageReceiver(this);
connect(m_communicator, &MessageReceiver::signalMessageReceived,
this, &FxALoginPage::slotMessageReceived);
m_channel->registerObject(QString("communicator"), m_communicator);
QFile scriptFile(":/data/inject.js");
if (!scriptFile.open(QIODevice::ReadOnly)) {
qWarning() << "Couldn't load JavaScript file to inject.";
}
QString injectScript = QString::fromUtf8(scriptFile.readAll());
scriptFile.close();
m_page->runJavaScript(injectScript);
m_page->runJavaScript(injectScript, WebPage::SafeJsWorld);
}
}
@ -115,7 +106,7 @@ void FxALoginPage::sendMessage(QJsonObject msg)
qDebug() << "<<< Sending to server:\n " << stringMsg;
QString srcCode = "sendMessage(" + stringMsg + ");";
m_page->runJavaScript(srcCode);
m_page->runJavaScript(srcCode, WebPage::SafeJsWorld);
}

View File

@ -26,6 +26,7 @@ class QWebEngineScript;
class QWebEnginePage;
class QJsonObject;
class MessageReceiver;
class WebPage;
class FxALoginPage : public QWebEngineView
{
@ -43,8 +44,7 @@ private:
void parseMessage(QJsonObject *msg);
void sendMessage(QJsonObject msg);
QWebEnginePage *m_page;
QWebChannel *m_channel;
WebPage *m_page;
MessageReceiver *m_communicator;
const QUrl FxALoginUrl = QUrl("https://accounts.firefox.com/signin?service=sync&context=fx_desktop_v3");
@ -60,10 +60,10 @@ public:
~MessageReceiver();
QJsonObject *getMessage();
public slots:
public Q_SLOTS:
void receiveJSON(const QVariantMap &data);
signals:
Q_SIGNALS:
void signalMessageReceived();
private: