From 98159091f4193ba337ea381d965766f3f056354a Mon Sep 17 00:00:00 2001 From: Marek Kotewicz Date: Fri, 24 Oct 2014 16:45:18 +0200 Subject: [PATCH] qwebconnector is not being recreated --- alethzero/MainWin.cpp | 22 +++++++++++++--------- alethzero/MainWin.h | 1 + libqethereum/QEthereum.cpp | 20 +++++++++++++++++--- libqethereum/QEthereum.h | 10 ++++++---- third/MainWin.cpp | 20 +++++++++++--------- third/MainWin.h | 3 ++- 6 files changed, 50 insertions(+), 26 deletions(-) diff --git a/alethzero/MainWin.cpp b/alethzero/MainWin.cpp index 588b0d7ef..ce12a8596 100644 --- a/alethzero/MainWin.cpp +++ b/alethzero/MainWin.cpp @@ -84,6 +84,12 @@ static QString fromRaw(dev::h256 _n, unsigned* _inc = nullptr) return QString(); } +static std::vector keysAsVector(QList const& keys) +{ + auto list = keys.toStdList(); + return {std::begin(list), std::end(list)}; +} + Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f"); Main::Main(QWidget *parent) : @@ -133,20 +139,21 @@ Main::Main(QWidget *parent) : m_webThree.reset(new WebThreeDirect(string("AlethZero/v") + dev::Version + "/" DEV_QUOTED(ETH_BUILD_TYPE) "/" DEV_QUOTED(ETH_BUILD_PLATFORM), getDataDir() + "/AlethZero", false, {"eth", "shh"})); + m_qwebConnector = new QWebThreeConnector(); + m_server = unique_ptr(new WebThreeStubServer(m_qwebConnector, *web3(), keysAsVector(owned()))); + m_server->StartListening(); + + connect(ui->webView, &QWebView::loadStarted, [this]() { // NOTE: no need to delete as QETH_INSTALL_JS_NAMESPACE adopts it. m_qweb = new QWebThree(this); auto qweb = m_qweb; + m_qwebConnector->setQWeb(qweb); QWebSettings::globalSettings()->setAttribute(QWebSettings::DeveloperExtrasEnabled, true); QWebFrame* f = ui->webView->page()->mainFrame(); f->disconnect(SIGNAL(javaScriptWindowObjectCleared())); - - auto list = owned().toStdList(); - m_server = unique_ptr(new WebThreeStubServer(new QWebThreeConnector(qweb), *web3(), {std::begin(list), std::end(list)})); - m_server->StartListening(); - connect(f, &QWebFrame::javaScriptWindowObjectCleared, QETH_INSTALL_JS_NAMESPACE(f, this, qweb)); }); @@ -1126,10 +1133,7 @@ void Main::ourAccountsRowsMoved() m_myKeys = myKeys; if (m_server.get()) - { - auto list = owned().toStdList(); - m_server->setAccounts({std::begin(list), std::end(list)}); - } + m_server->setAccounts(keysAsVector(owned())); } void Main::on_inject_triggered() diff --git a/alethzero/MainWin.h b/alethzero/MainWin.h index 0e6e07aa0..e0be9ff61 100644 --- a/alethzero/MainWin.h +++ b/alethzero/MainWin.h @@ -249,5 +249,6 @@ private: bool m_logChanged = true; std::unique_ptr m_server; + QWebThreeConnector* m_qwebConnector = nullptr; QWebThree* m_qweb = nullptr; }; diff --git a/libqethereum/QEthereum.cpp b/libqethereum/QEthereum.cpp index a7167f396..a8f702e09 100644 --- a/libqethereum/QEthereum.cpp +++ b/libqethereum/QEthereum.cpp @@ -149,7 +149,7 @@ void QWebThree::onDataProcessed(QString _json, QString _addInfo) response(formatOutput(f)); } -QWebThreeConnector::QWebThreeConnector(QWebThree* _q): m_qweb(_q) +QWebThreeConnector::QWebThreeConnector() { } @@ -158,10 +158,24 @@ QWebThreeConnector::~QWebThreeConnector() StopListening(); } +void QWebThreeConnector::setQWeb(QWebThree* _q) +{ + m_qweb = _q; + if (m_isListening) + { + StopListening(); + StartListening(); + } +} + bool QWebThreeConnector::StartListening() { - connect(m_qweb, SIGNAL(processData(QString, QString)), this, SLOT(onProcessData(QString, QString))); - connect(this, SIGNAL(dataProcessed(QString, QString)), m_qweb, SLOT(onDataProcessed(QString, QString))); + m_isListening = true; + if (m_qweb) + { + connect(m_qweb, SIGNAL(processData(QString, QString)), this, SLOT(onProcessData(QString, QString))); + connect(this, SIGNAL(dataProcessed(QString, QString)), m_qweb, SLOT(onDataProcessed(QString, QString))); + } return true; } diff --git a/libqethereum/QEthereum.h b/libqethereum/QEthereum.h index 1aa0705a9..9a10e87ad 100644 --- a/libqethereum/QEthereum.h +++ b/libqethereum/QEthereum.h @@ -57,13 +57,14 @@ class QWebThreeConnector: public QObject, public jsonrpc::AbstractServerConnecto Q_OBJECT public: - QWebThreeConnector(QWebThree* _q); + QWebThreeConnector(); virtual ~QWebThreeConnector(); + void setQWeb(QWebThree *_q); + virtual bool StartListening(); virtual bool StopListening(); - - bool virtual SendResponse(std::string const& _response, void* _addInfo = NULL); + virtual bool SendResponse(std::string const& _response, void* _addInfo = NULL); public slots: void onProcessData(QString const& _json, QString const& _addInfo); @@ -72,7 +73,8 @@ signals: void dataProcessed(QString const& _json, QString const& _addInfo); private: - QWebThree* m_qweb; + QWebThree* m_qweb = nullptr; + bool m_isListening; }; #define QETH_INSTALL_JS_NAMESPACE(_frame, _env, qweb) [_frame, _env, qweb]() \ diff --git a/third/MainWin.cpp b/third/MainWin.cpp index 200ee6971..73fe34d78 100644 --- a/third/MainWin.cpp +++ b/third/MainWin.cpp @@ -73,6 +73,11 @@ static QString fromRaw(dev::h256 _n, unsigned* _inc = nullptr) return QString(); } +static std::vector keysAsVector(QList const& keys) +{ + auto list = keys.toStdList(); + return {std::begin(list), std::end(list)}; +} Address c_config = Address("661005d2720d855f1d9976f88bb10c1a3398c77f"); @@ -103,19 +108,19 @@ Main::Main(QWidget *parent) : m_web3.reset(new WebThreeDirect("Third", getDataDir() + "/Third", false, {"eth", "shh"})); m_web3->connect(Host::pocHost()); + m_qwebConnector = new QWebThreeConnector(); + m_server = unique_ptr(new WebThreeStubServer(m_qwebConnector, *web3(), keysAsVector(owned()))); + m_server->StartListening(); + connect(ui->webView, &QWebView::loadStarted, [this]() { // NOTE: no need to delete as QETH_INSTALL_JS_NAMESPACE adopts it. m_qweb = new QWebThree(this); auto qweb = m_qweb; + m_qwebConnector->setQWeb(qweb); QWebFrame* f = ui->webView->page()->mainFrame(); f->disconnect(SIGNAL(javaScriptWindowObjectCleared())); - - auto list = owned().toStdList(); - m_server = auto_ptr(new WebThreeStubServer(new QWebThreeConnector(qweb), *web3(), {std::begin(list), std::end(list)})); - m_server->StartListening(); - connect(f, &QWebFrame::javaScriptWindowObjectCleared, QETH_INSTALL_JS_NAMESPACE(f, this, qweb)); }); @@ -543,10 +548,7 @@ void Main::ourAccountsRowsMoved() m_myKeys = myKeys; if (m_server.get()) - { - auto list = owned().toStdList(); - m_server->setAccounts({std::begin(list), std::end(list)}); - } + m_server->setAccounts(keysAsVector(owned())); } void Main::on_ourAccounts_doubleClicked() diff --git a/third/MainWin.h b/third/MainWin.h index b74b72d7f..1f30a60eb 100644 --- a/third/MainWin.h +++ b/third/MainWin.h @@ -133,6 +133,7 @@ private: QNetworkAccessManager m_webCtrl; - std::auto_ptr m_server; + std::unique_ptr m_server; + QWebThreeConnector* m_qwebConnector = nullptr; QWebThree* m_qweb = nullptr; };