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

Establish Communication with Firefox Accounts Login Page

Changed FxALoginPage class from QWebEnginePage to QWebEngineView.
Added webchannel for communication with FireFox Accounts.
Setup initial login message transfer with Login page.
This commit is contained in:
Prasenjit Kumar Shaw 2019-06-12 13:13:38 +05:30
parent 81d0fa9f20
commit c421f1b7d8
5 changed files with 171 additions and 34 deletions

View File

@ -29,8 +29,7 @@ SyncOptions::SyncOptions(QWidget* parent)
ui->setupUi(this);
loginPage = new FxALoginPage(this);
ui->fxaLoginView->setPage(loginPage);
ui->fxaLoginView->show();
ui->fxaloginframe->addWidget(loginPage);
}
SyncOptions::~SyncOptions()

View File

@ -20,8 +20,9 @@
#include <QWidget>
#include <QUrl>
#include <QWebEnginePage>
#include <QWebEngineView>
#include "fxalogin.h"
#include "qzcommon.h"
namespace Ui
@ -38,8 +39,7 @@ public:
~SyncOptions();
private:
const QUrl FxALoginUrl = QUrl("https://accounts.firefox.com/signin?service=sync&context=fx_desktop_v3");
QWebEnginePage *loginPage;
FxALoginPage *loginPage;
Ui::SyncOptions *ui;
};

View File

@ -22,23 +22,7 @@
<height>511</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QWebEngineView" name="fxaLoginView">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Expanding">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="url">
<url>
<string>about:blank</string>
</url>
</property>
</widget>
</item>
</layout>
<layout class="QVBoxLayout" name="fxaloginframe"/>
</widget>
<widget class="QWidget" name="verticalLayoutWidget_2">
<property name="geometry">
@ -97,13 +81,6 @@
</layout>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>QWebEngineView</class>
<extends>QWidget</extends>
<header location="global">QtWebEngineWidgets/QWebEngineView</header>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>

View File

@ -18,9 +18,131 @@
#include "fxalogin.h"
#include <QWebEnginePage>
#include <QWebEngineScript>
#include <QWebChannel>
#include <QJsonObject>
#include <QDebug>
#include <QFile>
#include <QJsonDocument>
FxALoginPage::FxALoginPage(QWidget* parent)
: QWebEnginePage(parent)
: QWebEngineView(parent)
{
this->load(FxALoginUrl);
page = new QWebEnginePage();
channel = new QWebChannel(page);
page->setWebChannel(channel);
page->load(FxALoginUrl);
this->setPage(page);
connect(page, SIGNAL(loadFinished(bool)), this, SLOT(pageLoadFinished(bool)));
}
FxALoginPage::~FxALoginPage()
{
delete communicator;
delete channel;
delete page;
}
void FxALoginPage::pageLoadFinished(bool pageLoaded)
{
if(pageLoaded) {
QFile apiFile(":/qtwebchannel/qwebchannel.js");
if(!apiFile.open(QIODevice::ReadOnly)) {
qDebug() << "Couldn't load Qt's Webchannel API!";
}
QString apiScript = QString::fromLatin1(apiFile.readAll());
apiFile.close();
page->runJavaScript(apiScript);
communicator = new MessageReceiver(this);
connect(communicator, SIGNAL(signalMessageReceived()),
this, SLOT(slotMessageReceived()));
channel->registerObject(QString("communicator"), communicator);
page->runJavaScript("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);"
"});"
"function sendMessage(response) { let e = new window.CustomEvent('WebChannelMessageToContent', {detail: response}); window.dispatchEvent(e)};"
);
}
}
void FxALoginPage::slotMessageReceived()
{
qDebug() << "===Msg Rcvd by FxALoginPage===";
QJsonObject *message = communicator->getMessage();
qDebug() << "Message Recieved: ";
QJsonObject *response = parseMessage(message);
sendMessage(response);
}
QJsonObject * FxALoginPage::parseMessage(QJsonObject *msg)
{
/* TODO:
* Parse the recived message and respond with proper resonse
*/
QJsonValue type = msg->value("type");
QJsonObject detail = msg->value("detail").toObject();
QJsonValue channelId = detail.value("id");
QJsonObject message = detail.value("message").toObject();
QJsonValue command = message.value("command");
QJsonObject data = message.value("data").toObject();
QJsonValue messageId = message.value("messageId");
QJsonObject resp_data;
resp_data.insert("ok", QJsonValue(true));
QJsonObject resp_message;
resp_message.insert("command", command);
resp_message.insert("data", QJsonValue(resp_data));
resp_message.insert("messageId", messageId);
QJsonObject *response = new QJsonObject();
response->insert("id", channelId);
response->insert("message", QJsonValue(resp_message));
return response;
}
void FxALoginPage::sendMessage(QJsonObject* msg)
{
QJsonDocument doc(*msg);
QString stringMsg(doc.toJson(QJsonDocument::Compact));
qDebug() << ">>> Sending to server: " << stringMsg;
QString srcCode = "sendMessage(" + stringMsg + ");";
page->runJavaScript(srcCode);
}
MessageReceiver::MessageReceiver(QObject *parent)
: QObject(parent)
{
}
MessageReceiver::~MessageReceiver()
{
delete message;
}
void MessageReceiver::receiveJSON(const QVariantMap &data)
{
QJsonObject obj = QJsonObject::fromVariantMap(data);
qDebug() << "Received JSON:\n " << obj;
message = new QJsonObject(obj);
emit signalMessageReceived();
}
QJsonObject * MessageReceiver::getMessage()
{
return message;
}

View File

@ -18,15 +18,54 @@
#pragma once
#include <QUrl>
#include <QWebEnginePage>
#include <QWebEngineView>
#include <QObject>
class FxALoginPage : public QWebEnginePage
class QWebChannel;
class QWebEngineScript;
class QWebEnginePage;
class QJsonObject;
class MessageReceiver;
class FxALoginPage : public QWebEngineView
{
Q_OBJECT
public:
explicit FxALoginPage(QWidget *parent = 0);
explicit FxALoginPage(QWidget *parent = nullptr);
~FxALoginPage();
protected slots:
void pageLoadFinished(bool pageLoaded);
void slotMessageReceived();
private:
QJsonObject * parseMessage(QJsonObject *msg);
void sendMessage(QJsonObject *msg);
QWebEnginePage *page;
QWebChannel *channel;
MessageReceiver *communicator;
const QUrl FxALoginUrl = QUrl("https://accounts.firefox.com/signin?service=sync&context=fx_desktop_v3");
};
class MessageReceiver : public QObject
{
Q_OBJECT
public:
MessageReceiver(QObject *parent = nullptr);
~MessageReceiver();
QJsonObject *getMessage();
public slots:
void receiveJSON(const QVariantMap &data);
signals:
void signalMessageReceived();
private:
QJsonObject *message;
};