1
0

Unlock from background thread

This commit is contained in:
Scott E. Graves
2017-02-17 12:30:03 -06:00
parent 7b3a4d4e56
commit f76e377e82
5 changed files with 180 additions and 95 deletions

View File

@@ -49,98 +49,115 @@
margin: 0 auto;
width: 98%;
}
.disabled_overlay {
display: none;
margin: 0;
padding: 0;
position: fixed; /* Stay in place */
z-index: 50; /* Sit on top */
height: 100%;
width: 100%; /* Full width */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0, 0, 0); /* Fallback color */
background-color: rgba(0, 0, 0, 0.5);
}
</style>
</HEAD>
<BODY id="CSiaDriveDlg">
<div class="fill">
<h3>Sia Information</h3>
<div style="float: right;">
<table>
<tr>
<td style="text-align: right">Client Version:</td>
<td id="ID_ClientVersion"></td>
</tr>
<tr>
<td style="text-align: right">Server Version:</td>
<td id="ID_ServerVersion"></td>
</tr>
</table>
<div id="ID_Body">
<div class="fill">
<h3>Sia Information</h3>
<div style="float: right;">
<table>
<tr>
<td style="text-align: right">Client Version:</td>
<td id="ID_ClientVersion"></td>
</tr>
<tr>
<td style="text-align: right">Server Version:</td>
<td id="ID_ServerVersion"></td>
</tr>
</table>
</div>
<div>
<table>
<tr>
<td style="text-align: right">Available:</td>
<td id="ID_WalletConfirmedBalance"></td>
</tr>
<tr>
<td style="text-align: right">Pending:</td>
<td id="ID_WalletBalanceUnconfirmed"></td>
</tr>
<tr>
<td style="text-align: right">Total:</td>
<td id="ID_WalletTotalBalance"></td>
</tr>
</table>
</div>
</div>
<div>
<table>
<tr>
<td style="text-align: right">Available:</td>
<td id="ID_WalletConfirmedBalance"></td>
</tr>
<tr>
<td style="text-align: right">Pending:</td>
<td id="ID_WalletBalanceUnconfirmed"></td>
</tr>
<tr>
<td style="text-align: right">Total:</td>
<td id="ID_WalletTotalBalance"></td>
</tr>
</table>
<div id="main_window">
</div>
</div>
<div id="main_window">
<div id="exit_window" style="display: none" align="center">
<BUTTON STYLE="WIDTH: 100px" ID="ExitYes">Yes</BUTTON>&nbsp;<BUTTON STYLE="WIDTH: 100px" ID="ExitNo">No</BUTTON>
</div>
</div>
<div id="create_wallet" style="display: none" class="fill">
<h3>Create New Wallet</h3>
<p>Click 'Create' to create a new Sia wallet or exit the application if this is not what you want to do.</p>
<button ID="CreateWalletButton">Create</button>
</div>
<div id="exit_window" style="display: none" align="center">
<BUTTON STYLE="WIDTH: 100px" ID="ExitYes">Yes</BUTTON>&nbsp;<BUTTON STYLE="WIDTH: 100px" ID="ExitNo">No</BUTTON>
</div>
<div id="unlock_wallet" style="display: none" class="fill">
<h3>Unlock Wallet</h3>
<p>Enter wallet password and click 'Unlock' to continue.</p>
<input type="password" id="ID_WalletUnlockPwd" style="width: inherit"/><br/><br/>
<button ID="UnlockWalletButton">Unlock</button>
</div>
<div id="create_wallet" style="display: none" class="fill">
<h3>Create New Wallet</h3>
<p>Click 'Create' to create a new Sia wallet or exit the application if this is not what you want to do.</p>
<button ID="CreateWalletButton">Create</button>
</div>
<div id="disp_wallet_seed" style="display: none" class="fill">
<h3>Wallet Created</h3>
<p>Please save the following seed. You will need this to unlock and/or restore your wallet. Click 'Done' once you've backed-up your seed.</p>
<textarea rows=4 cols=60 width="100%" id="WalletCreatedSeed"></textarea><br/><br/>
<button ID="ConfirmSeedButton">Done</button>
</div>
<div id="unlock_wallet" style="display: none" class="fill">
<h3>Unlock Wallet</h3>
<p>Enter wallet password and click 'Unlock' to continue.</p>
<input type="password" id="ID_WalletUnlockPwd" style="width: inherit"/><br/><br/>
<button ID="UnlockWalletButton">Unlock</button>
</div>
<div id="disp_wallet_seed" style="display: none" class="fill">
<h3>Wallet Created</h3>
<p>Please save the following seed. You will need this to unlock and/or restore your wallet. Click 'Done' once you've backed-up your seed.</p>
<textarea rows=4 cols=60 width="100%" id="WalletCreatedSeed"></textarea><br/><br/>
<button ID="ConfirmSeedButton">Done</button>
</div>
<div id="tab_view" style="display: none" class="fill">
<h3>Receive Address</h3>
<label id="ID_WalletReceiveAddress"></label><br/><br/>
<TABLE class="fill">
<TR>
<TD ALIGN=CENTER VALIGN=TOP>
<select id="MountDrives"></select>
<input id="MountButton" type="button" value="Mount"/>
<input id="wallet_btn" type="button" value="Wallet" />
<input id="host_btn" type="button" value="Host" />
</TD>
</TR>
<tr align="center" valign="top">
<div id="tab_view" style="display: none" class="fill">
<h3>Receive Address</h3>
<label id="ID_WalletReceiveAddress"></label><br/><br/>
<TABLE class="fill">
<TR>
<TD ALIGN=CENTER VALIGN=TOP>
<select id="MountDrives"></select>
<input id="MountButton" type="button" value="Mount"/>
<input id="wallet_btn" type="button" value="Wallet" />
<input id="host_btn" type="button" value="Host" />
</TD>
</TR>
<tr align="center" valign="top">
</tr>
</TABLE>
</div>
</tr>
</TABLE>
</div>
<div class="footer">
<table class="fill">
<TR>
<TD ALIGN=RIGHT VALIGN=BOTTOM>
<BUTTON STYLE="WIDTH: 100px" ID="ButtonOK">OK</BUTTON>
<BUTTON STYLE="WIDTH: 100px" ID="ButtonCancel">Exit</BUTTON>
</TD>
</TR>
</table>
<div class="footer">
<table class="fill">
<TR>
<TD ALIGN=RIGHT VALIGN=BOTTOM>
<BUTTON STYLE="WIDTH: 100px" ID="ButtonOK">OK</BUTTON>
<BUTTON STYLE="WIDTH: 100px" ID="ButtonCancel">Exit</BUTTON>
</TD>
</TR>
</table>
</div>
</div>
<div id="ID_DisabledOverlay" class="disabled_overlay" style="top: 0;left: 0;">
</div>
<script src="res:/JS/#136" type="text/javascript"></script>
</BODY>
</HTML>

View File

@@ -8,6 +8,7 @@
#include "afxdialogex.h"
#include <stdbool.h>
#include <bitset>
#include <thread>
#ifdef _DEBUG
#define new DEBUG_NEW
@@ -88,6 +89,7 @@ END_DHTML_EVENT_MAP()
CSiaDriveDlg::CSiaDriveDlg(CWnd* pParent /*=NULL*/)
: CDHtmlDialog(IDD_SIADRIVE_DIALOG, IDR_HTML_SIADRIVE_DIALOG, pParent),
_uiThreadId(GetCurrentThreadId()),
_siaApi({L"localhost", 9980, L"1.1.0"})
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
@@ -181,6 +183,20 @@ void CSiaDriveDlg::OnPaint()
}
}
void CSiaDriveDlg::SetBodyEnabled(const bool& enabled)
{
CComPtr<IHTMLDOMNode> disabledNode;
CComPtr<IHTMLElement> disabledElem;
if (SUCCEEDED(GetDomNodeAndElementById(L"ID_DisabledOverlay", disabledNode, disabledElem)))
{
CComPtr<IHTMLStyle> style;
if (SUCCEEDED(disabledElem->get_style(&style)))
{
style->put_display(enabled ? L"none" : L"block");
}
}
}
BOOL CSiaDriveDlg::CallClientScript(LPCTSTR pStrFuncName, const String& data, CComVariant* pOutVarRes)
{
BOOL bRes = FALSE;
@@ -300,17 +316,28 @@ HRESULT CSiaDriveDlg::OnButtonConfirmSeed(IHTMLElement* /*pElement*/)
HRESULT CSiaDriveDlg::OnButtonUnlockWallet(IHTMLElement* /*pElement*/)
{
if (API_SUCCESS(SiaApiError, _siaApi.GetWallet()->Unlock(GetWalletUnlockPassword())))
{
SetWalletUnlockPassword(L"");
this->Navigate(this->m_strCurrentUrl);
}
else
{
SetWalletUnlockPassword(L"");
AfxMessageBox(L"Invalid password entered");
SetFocusToElement(L"ID_WalletUnlockPwd");
}
SetBodyEnabled(false);
String pwd = GetWalletUnlockPassword();
std::thread th([this, pwd]() {
if (API_SUCCESS(SiaApiError, _siaApi.GetWallet()->Unlock(pwd)))
{
QueueUiAction([this]() {
SetWalletUnlockPassword(L"");
SetBodyEnabled(true);
this->Navigate(this->m_strCurrentUrl);
});
}
else
{
QueueUiAction([this]() {
SetWalletUnlockPassword(L"");
AfxMessageBox(L"Invalid password entered");
SetFocusToElement(L"ID_WalletUnlockPwd");
SetBodyEnabled(true);
});
}
});
th.detach();
return S_OK;
}
@@ -392,6 +419,7 @@ String CSiaDriveDlg::GetWalletUnlockPassword()
void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR)
{
KillTimer(IDT_UPDATE);
KillTimer(IDT_UI_ACTION_QUEUE);
SetServerVersion(L"x.x.x.");
SetClientVersion(L"1.0.0");
@@ -401,6 +429,7 @@ void CSiaDriveDlg::OnDocumentComplete(LPDISPATCH, LPCTSTR)
SetWalletReceiveAddress(L"");
SetTimer(IDT_UPDATE, 2000, nullptr);
SetTimer(IDT_UI_ACTION_QUEUE, 1000, nullptr);
if ((_connected = UpdateUi(false)))
{
@@ -413,16 +442,45 @@ void CSiaDriveDlg::OnTimer(UINT_PTR nIDEvent)
switch (nIDEvent)
{
case IDT_UPDATE:
{
UpdateUi();
}
break;
{
UpdateUi();
}
break;
case IDT_UI_ACTION_QUEUE:
{
ProcessUiActionQueue();
}
break;
default:
break;
}
}
void CSiaDriveDlg::QueueUiAction(std::function<void()> fn)
{
if (GetCurrentThreadId() == _uiThreadId)
{
fn();
}
else
{
std::lock_guard<std::mutex> l(_uiActionQueueMutex);
_uiActionQueue.push_back(fn);
}
}
void CSiaDriveDlg::ProcessUiActionQueue()
{
std::lock_guard<std::mutex> l(_uiActionQueueMutex);
while (_uiActionQueue.size())
{
_uiActionQueue.front()();
_uiActionQueue.pop_front();
}
}
bool CSiaDriveDlg::UpdateSiaInfo()
{
const String serverVersion = _siaApi.GetServerVersion();

View File

@@ -5,6 +5,7 @@
#pragma once
#include <SiaApi.h>
#include "SiaDriveConfig.h"
using namespace Sia::Api;
// CSiaDriveDlg dialog
@@ -60,6 +61,7 @@ private:
BOOL CallClientScript(LPCTSTR pStrFuncName, const json& json, CComVariant* pOutVarRes);
BOOL CallClientScript(LPCTSTR pStrFuncName, const String& string, CComVariant* pOutVarRes);
BOOL CallClientScript(LPCTSTR pStrFuncName, CComVariant* pOutVarRes);
void SetBodyEnabled(const bool& enabled);
void SetServerVersion(const String& version);
void SetClientVersion(const String& version);
@@ -71,12 +73,19 @@ private:
String GetWalletUnlockPassword();
void SetWalletUnlockPassword(const String& address);
void QueueUiAction(std::function<void()> action);
void ProcessUiActionQueue();
private:
const DWORD _uiThreadId;
CSiaDriveConfig _siaConfig;
CSiaApi _siaApi;
CString _walletCreatedSeed;
std::mutex _uiActionQueueMutex;
std::deque<std::function<void()>> _uiActionQueue;
bool _connected = false;
bool _seedCreation = false;
static const UINT IDT_UPDATE = 1;
static const UINT IDT_UI_ACTION_QUEUE = 2;
static const std::uint8_t WALLET_TAB = 0;
};

View File

@@ -53,4 +53,5 @@
#endif
#endif
#include <mutex>
#include <deque>