mirror of
https://git.mirrors.martin98.com/https://github.com/prusa3d/PrusaSlicer.git
synced 2025-08-14 09:56:04 +08:00
SPE-2570: Refresh access token if Printables fire accessTokenExpired event
Add fancy loading overlay during refresh period missing include
This commit is contained in:
parent
ce5008cc8d
commit
5ba8f21c77
@ -915,11 +915,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
|
|||||||
|
|
||||||
this->q->Bind(EVT_OPEN_EXTERNAL_LOGIN_WIZARD, open_external_login);
|
this->q->Bind(EVT_OPEN_EXTERNAL_LOGIN_WIZARD, open_external_login);
|
||||||
this->q->Bind(EVT_OPEN_EXTERNAL_LOGIN, open_external_login);
|
this->q->Bind(EVT_OPEN_EXTERNAL_LOGIN, open_external_login);
|
||||||
|
|
||||||
// void on_account_login(const std::string& token);
|
|
||||||
// void on_account_will_refresh();
|
|
||||||
// void on_account_did_refresh(const std::string& token);
|
|
||||||
// void on_account_logout();
|
|
||||||
this->q->Bind(EVT_UA_LOGGEDOUT, [this](UserAccountSuccessEvent& evt) {
|
this->q->Bind(EVT_UA_LOGGEDOUT, [this](UserAccountSuccessEvent& evt) {
|
||||||
user_account->clear();
|
user_account->clear();
|
||||||
std::string text = _u8L("Logged out from Prusa Account.");
|
std::string text = _u8L("Logged out from Prusa Account.");
|
||||||
@ -1032,7 +1028,7 @@ Plater::priv::priv(Plater *q, MainFrame *main_frame)
|
|||||||
wxGetApp().handle_connect_request_printer_select_inner(evt.data);
|
wxGetApp().handle_connect_request_printer_select_inner(evt.data);
|
||||||
});
|
});
|
||||||
this->q->Bind(EVT_UA_PRUSACONNECT_PRINTER_DATA_FAIL, [this](UserAccountFailEvent& evt) {
|
this->q->Bind(EVT_UA_PRUSACONNECT_PRINTER_DATA_FAIL, [this](UserAccountFailEvent& evt) {
|
||||||
BOOST_LOG_TRIVIAL(error) << "Failed communication with Prusa Account: " << evt.data;
|
BOOST_LOG_TRIVIAL(error) << "Failed communication with Connect Printer endpoint: " << evt.data;
|
||||||
user_account->on_communication_fail();
|
user_account->on_communication_fail();
|
||||||
std::string msg = _u8L("Failed to select printer from Prusa Connect.");
|
std::string msg = _u8L("Failed to select printer from Prusa Connect.");
|
||||||
this->notification_manager->close_notification_of_type(NotificationType::SelectFilamentFromConnect);
|
this->notification_manager->close_notification_of_type(NotificationType::SelectFilamentFromConnect);
|
||||||
|
@ -103,6 +103,10 @@ void UserAccount::enqueue_printer_data_action(const std::string& uuid)
|
|||||||
{
|
{
|
||||||
m_communication->enqueue_printer_data_action(uuid);
|
m_communication->enqueue_printer_data_action(uuid);
|
||||||
}
|
}
|
||||||
|
void UserAccount::request_refresh()
|
||||||
|
{
|
||||||
|
m_communication->request_refresh();
|
||||||
|
}
|
||||||
|
|
||||||
bool UserAccount::on_login_code_recieved(const std::string& url_message)
|
bool UserAccount::on_login_code_recieved(const std::string& url_message)
|
||||||
{
|
{
|
||||||
|
@ -48,6 +48,7 @@ public:
|
|||||||
void enqueue_connect_printer_models_action();
|
void enqueue_connect_printer_models_action();
|
||||||
void enqueue_avatar_action();
|
void enqueue_avatar_action();
|
||||||
void enqueue_printer_data_action(const std::string& uuid);
|
void enqueue_printer_data_action(const std::string& uuid);
|
||||||
|
void request_refresh();
|
||||||
// Clears all data and connections, called on logout or EVT_UA_RESET
|
// Clears all data and connections, called on logout or EVT_UA_RESET
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
|
@ -470,6 +470,13 @@ void UserAccountCommunication::enqueue_printer_data_action(const std::string& uu
|
|||||||
}
|
}
|
||||||
wakeup_session_thread();
|
wakeup_session_thread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UserAccountCommunication::request_refresh()
|
||||||
|
{
|
||||||
|
m_token_timer->Stop();
|
||||||
|
enqueue_refresh();
|
||||||
|
}
|
||||||
|
|
||||||
void UserAccountCommunication::enqueue_refresh()
|
void UserAccountCommunication::enqueue_refresh()
|
||||||
{
|
{
|
||||||
{
|
{
|
||||||
@ -529,8 +536,7 @@ void UserAccountCommunication::on_activate_app(bool active)
|
|||||||
#endif
|
#endif
|
||||||
if (active && m_next_token_refresh_at > 0 && m_next_token_refresh_at - now < refresh_threshold) {
|
if (active && m_next_token_refresh_at > 0 && m_next_token_refresh_at - now < refresh_threshold) {
|
||||||
BOOST_LOG_TRIVIAL(info) << "Enqueue access token refresh on activation";
|
BOOST_LOG_TRIVIAL(info) << "Enqueue access token refresh on activation";
|
||||||
m_token_timer->Stop();
|
request_refresh();
|
||||||
enqueue_refresh();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -548,7 +554,7 @@ void UserAccountCommunication::set_refresh_time(int seconds)
|
|||||||
assert(m_token_timer);
|
assert(m_token_timer);
|
||||||
m_token_timer->Stop();
|
m_token_timer->Stop();
|
||||||
const auto prior_expiration_secs = 5 * 60;
|
const auto prior_expiration_secs = 5 * 60;
|
||||||
int milliseconds = std::max((seconds - prior_expiration_secs) * 1000, 60000);
|
int milliseconds = std::max((seconds - prior_expiration_secs) * 1000, 50000);
|
||||||
m_next_token_refresh_at = std::time(nullptr) + milliseconds / 1000;
|
m_next_token_refresh_at = std::time(nullptr) + milliseconds / 1000;
|
||||||
m_token_timer->StartOnce(milliseconds);
|
m_token_timer->StartOnce(milliseconds);
|
||||||
}
|
}
|
||||||
|
@ -53,6 +53,7 @@ public:
|
|||||||
void enqueue_test_connection();
|
void enqueue_test_connection();
|
||||||
void enqueue_printer_data_action(const std::string& uuid);
|
void enqueue_printer_data_action(const std::string& uuid);
|
||||||
void enqueue_refresh();
|
void enqueue_refresh();
|
||||||
|
void request_refresh();
|
||||||
|
|
||||||
// Callbacks - called from UI after receiving Event from Session thread. Some might use Session thread.
|
// Callbacks - called from UI after receiving Event from Session thread. Some might use Session thread.
|
||||||
//
|
//
|
||||||
|
@ -9,7 +9,6 @@
|
|||||||
#include <boost/regex.hpp>
|
#include <boost/regex.hpp>
|
||||||
#include <boost/property_tree/ptree.hpp>
|
#include <boost/property_tree/ptree.hpp>
|
||||||
#include <boost/property_tree/json_parser.hpp>
|
#include <boost/property_tree/json_parser.hpp>
|
||||||
#include <boost/beast/core/detail/base64.hpp>
|
|
||||||
#include <curl/curl.h>
|
#include <curl/curl.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
@ -155,7 +154,6 @@ void UserAccountSession::token_success_callback(const std::string& body)
|
|||||||
BOOST_LOG_TRIVIAL(debug) << "Access token refreshed";
|
BOOST_LOG_TRIVIAL(debug) << "Access token refreshed";
|
||||||
// Data we need
|
// Data we need
|
||||||
std::string access_token, refresh_token, shared_session_key;
|
std::string access_token, refresh_token, shared_session_key;
|
||||||
int expires_in = 300;
|
|
||||||
try {
|
try {
|
||||||
std::stringstream ss(body);
|
std::stringstream ss(body);
|
||||||
pt::ptree ptree;
|
pt::ptree ptree;
|
||||||
@ -164,25 +162,20 @@ void UserAccountSession::token_success_callback(const std::string& body)
|
|||||||
const auto access_token_optional = ptree.get_optional<std::string>("access_token");
|
const auto access_token_optional = ptree.get_optional<std::string>("access_token");
|
||||||
const auto refresh_token_optional = ptree.get_optional<std::string>("refresh_token");
|
const auto refresh_token_optional = ptree.get_optional<std::string>("refresh_token");
|
||||||
const auto shared_session_key_optional = ptree.get_optional<std::string>("shared_session_key");
|
const auto shared_session_key_optional = ptree.get_optional<std::string>("shared_session_key");
|
||||||
const auto expires_in_optional = ptree.get_optional<int>("expires_in");
|
|
||||||
|
|
||||||
if (access_token_optional)
|
if (access_token_optional)
|
||||||
access_token = *access_token_optional;
|
access_token = *access_token_optional;
|
||||||
if (refresh_token_optional)
|
if (refresh_token_optional)
|
||||||
refresh_token = *refresh_token_optional;
|
refresh_token = *refresh_token_optional;
|
||||||
if (shared_session_key_optional)
|
if (shared_session_key_optional)
|
||||||
shared_session_key = *shared_session_key_optional;
|
shared_session_key = *shared_session_key_optional;
|
||||||
assert(expires_in_optional);
|
|
||||||
if (expires_in_optional)
|
|
||||||
expires_in = *expires_in_optional;
|
|
||||||
}
|
}
|
||||||
catch (const std::exception&) {
|
catch (const std::exception&) {
|
||||||
std::string msg = "Could not parse server response after code exchange.";
|
std::string msg = "Could not parse server response after code exchange.";
|
||||||
wxQueueEvent(p_evt_handler, new UserAccountFailEvent(EVT_UA_RESET, std::move(msg)));
|
wxQueueEvent(p_evt_handler, new UserAccountFailEvent(EVT_UA_RESET, std::move(msg)));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
int expires_in = Utils::get_exp_seconds(access_token);
|
||||||
if (access_token.empty() || refresh_token.empty() || shared_session_key.empty()) {
|
if (access_token.empty() || refresh_token.empty() || shared_session_key.empty() || expires_in <= 0) {
|
||||||
// just debug msg, no need to translate
|
// just debug msg, no need to translate
|
||||||
std::string msg = GUI::format("Failed read tokens after POST.\nAccess token: %1%\nRefresh token: %2%\nShared session token: %3%\nbody: %4%", access_token, refresh_token, shared_session_key, body);
|
std::string msg = GUI::format("Failed read tokens after POST.\nAccess token: %1%\nRefresh token: %2%\nShared session token: %3%\nbody: %4%", access_token, refresh_token, shared_session_key, body);
|
||||||
{
|
{
|
||||||
|
@ -122,7 +122,7 @@ public:
|
|||||||
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS] = std::make_unique<UserActionGetWithEvent>("CONNECT_STATUS", sc.connect_status_url(), EVT_UA_PRUSACONNECT_STATUS_SUCCESS, EVT_UA_FAIL);
|
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_STATUS] = std::make_unique<UserActionGetWithEvent>("CONNECT_STATUS", sc.connect_status_url(), EVT_UA_PRUSACONNECT_STATUS_SUCCESS, EVT_UA_FAIL);
|
||||||
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS] = std::make_unique<UserActionGetWithEvent>("CONNECT_PRINTER_MODELS", sc.connect_printer_list_url(), EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, EVT_UA_FAIL);
|
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_PRINTER_MODELS] = std::make_unique<UserActionGetWithEvent>("CONNECT_PRINTER_MODELS", sc.connect_printer_list_url(), EVT_UA_PRUSACONNECT_PRINTER_MODELS_SUCCESS, EVT_UA_FAIL);
|
||||||
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR] = std::make_unique<UserActionGetWithEvent>("AVATAR", sc.media_url(), EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL);
|
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_AVATAR] = std::make_unique<UserActionGetWithEvent>("AVATAR", sc.media_url(), EVT_UA_AVATAR_SUCCESS, EVT_UA_FAIL);
|
||||||
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID] = std::make_unique<UserActionGetWithEvent>("USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID", sc.connect_printers_url(), EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, EVT_UA_FAIL);
|
m_actions[UserAccountActionID::USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID] = std::make_unique<UserActionGetWithEvent>("USER_ACCOUNT_ACTION_CONNECT_DATA_FROM_UUID", sc.connect_printers_url(), EVT_UA_PRUSACONNECT_PRINTER_DATA_SUCCESS, EVT_UA_PRUSACONNECT_PRINTER_DATA_FAIL);
|
||||||
}
|
}
|
||||||
~UserAccountSession()
|
~UserAccountSession()
|
||||||
{
|
{
|
||||||
|
@ -782,6 +782,9 @@ void ConnectWebViewPanel::on_navigation_request(wxWebViewEvent &evt)
|
|||||||
if (m_reached_default_url && !evt.GetURL().StartsWith(m_default_url)) {
|
if (m_reached_default_url && !evt.GetURL().StartsWith(m_default_url)) {
|
||||||
BOOST_LOG_TRIVIAL(info) << evt.GetURL() << " does not start with default url. Vetoing.";
|
BOOST_LOG_TRIVIAL(info) << evt.GetURL() << " does not start with default url. Vetoing.";
|
||||||
evt.Veto();
|
evt.Veto();
|
||||||
|
} else if (m_reached_default_url && evt.GetURL().Find(GUI::format_wxstr("/web/%1%.html", m_loading_html)) != wxNOT_FOUND) {
|
||||||
|
// Do not allow back button to loading screen
|
||||||
|
evt.Veto();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -859,9 +862,13 @@ void PrinterWebViewPanel::on_navigation_request(wxWebViewEvent &evt)
|
|||||||
{
|
{
|
||||||
const wxString url = evt.GetURL();
|
const wxString url = evt.GetURL();
|
||||||
if (url.StartsWith(m_default_url) && !m_api_key_sent) {
|
if (url.StartsWith(m_default_url) && !m_api_key_sent) {
|
||||||
|
m_reached_default_url = true;
|
||||||
if (!m_usr.empty() && !m_psk.empty()) {
|
if (!m_usr.empty() && !m_psk.empty()) {
|
||||||
send_credentials();
|
send_credentials();
|
||||||
}
|
}
|
||||||
|
} else if (m_reached_default_url && evt.GetURL().Find(GUI::format_wxstr("/web/%1%.html", m_loading_html)) != wxNOT_FOUND) {
|
||||||
|
// Do not allow back button to loading screen
|
||||||
|
evt.Veto();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -977,6 +984,9 @@ void PrintablesWebViewPanel::on_navigation_request(wxWebViewEvent &evt)
|
|||||||
} else if (m_reached_default_url && url.StartsWith("http")) {
|
} else if (m_reached_default_url && url.StartsWith("http")) {
|
||||||
BOOST_LOG_TRIVIAL(info) << evt.GetURL() << " does not start with default url. Vetoing.";
|
BOOST_LOG_TRIVIAL(info) << evt.GetURL() << " does not start with default url. Vetoing.";
|
||||||
evt.Veto();
|
evt.Veto();
|
||||||
|
} else if (m_reached_default_url && evt.GetURL().Find(GUI::format_wxstr("/web/%1%.html", m_loading_html)) != wxNOT_FOUND) {
|
||||||
|
// Do not allow back button to loading screen
|
||||||
|
evt.Veto();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -992,6 +1002,11 @@ void PrintablesWebViewPanel::on_loaded(wxWebViewEvent& evt)
|
|||||||
load_default_url();
|
load_default_url();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (evt.GetURL().StartsWith(m_default_url)) {
|
||||||
|
define_css();
|
||||||
|
} else {
|
||||||
|
m_styles_defined = false;
|
||||||
|
}
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
// This is needed only once after add_request_authorization
|
// This is needed only once after add_request_authorization
|
||||||
if (m_remove_request_auth) {
|
if (m_remove_request_auth) {
|
||||||
@ -1074,6 +1089,8 @@ void PrintablesWebViewPanel::logout(const std::string& override_url/* = std::str
|
|||||||
if (!m_shown || !m_browser) {
|
if (!m_shown || !m_browser) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
|
||||||
|
hide_loading_overlay();
|
||||||
delete_cookies(m_browser, Utils::ServiceConfig::instance().printables_url());
|
delete_cookies(m_browser, Utils::ServiceConfig::instance().printables_url());
|
||||||
m_browser->RunScript("localStorage.clear();");
|
m_browser->RunScript("localStorage.clear();");
|
||||||
|
|
||||||
@ -1086,17 +1103,18 @@ void PrintablesWebViewPanel::logout(const std::string& override_url/* = std::str
|
|||||||
// We cannot do simple reload here, it would keep the access token in the header
|
// We cannot do simple reload here, it would keep the access token in the header
|
||||||
load_request(m_browser, next_url, std::string());
|
load_request(m_browser, next_url, std::string());
|
||||||
#endif //
|
#endif //
|
||||||
|
|
||||||
}
|
}
|
||||||
void PrintablesWebViewPanel::login(const std::string& access_token, const std::string& override_url/* = std::string()*/)
|
void PrintablesWebViewPanel::login(const std::string& access_token, const std::string& override_url/* = std::string()*/)
|
||||||
{
|
{
|
||||||
if (!m_shown) {
|
if (!m_shown) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
|
||||||
|
hide_loading_overlay();
|
||||||
// We cannot add token to header as when making the first request.
|
// We cannot add token to header as when making the first request.
|
||||||
// In fact, we shall not do request here, only run scripts.
|
// In fact, we shall not do request here, only run scripts.
|
||||||
// postMessage accessTokenWillChange -> postMessage accessTokenChange -> window.location.reload();
|
// postMessage accessTokenWillChange -> postMessage accessTokenChange -> window.location.reload();
|
||||||
|
|
||||||
wxString script = "window.postMessage(JSON.stringify({ event: 'accessTokenWillChange' }))";
|
wxString script = "window.postMessage(JSON.stringify({ event: 'accessTokenWillChange' }))";
|
||||||
run_script(script);
|
run_script(script);
|
||||||
|
|
||||||
@ -1119,16 +1137,19 @@ void PrintablesWebViewPanel::load_default_url()
|
|||||||
if (!m_browser) {
|
if (!m_browser) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
hide_loading_overlay();
|
||||||
std::string actual_default_url = get_url_lang_theme(from_u8(Utils::ServiceConfig::instance().printables_url() + "/homepage"));
|
std::string actual_default_url = get_url_lang_theme(from_u8(Utils::ServiceConfig::instance().printables_url() + "/homepage"));
|
||||||
const std::string access_token = wxGetApp().plater()->get_user_account()->get_access_token();
|
const std::string access_token = wxGetApp().plater()->get_user_account()->get_access_token();
|
||||||
|
|
||||||
// in case of opening printables logged out - delete cookies and localstorage to get rid of last login
|
// in case of opening printables logged out - delete cookies and localstorage to get rid of last login
|
||||||
if (access_token.empty()) {
|
if (access_token.empty()) {
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " logout";
|
||||||
delete_cookies(m_browser, Utils::ServiceConfig::instance().printables_url());
|
delete_cookies(m_browser, Utils::ServiceConfig::instance().printables_url());
|
||||||
m_browser->AddUserScript("localStorage.clear();");
|
m_browser->AddUserScript("localStorage.clear();");
|
||||||
load_url(actual_default_url);
|
load_url(actual_default_url);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " login";
|
||||||
|
|
||||||
// add token to first request
|
// add token to first request
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
add_request_authorization(m_browser, m_default_url, access_token);
|
add_request_authorization(m_browser, m_default_url, access_token);
|
||||||
@ -1144,6 +1165,7 @@ void PrintablesWebViewPanel::send_refreshed_token(const std::string& access_toke
|
|||||||
if (m_load_default_url) {
|
if (m_load_default_url) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
hide_loading_overlay();
|
||||||
wxString script = GUI::format_wxstr("window.postMessage(JSON.stringify({"
|
wxString script = GUI::format_wxstr("window.postMessage(JSON.stringify({"
|
||||||
"event: 'accessTokenChange',"
|
"event: 'accessTokenChange',"
|
||||||
"token: '%1%'"
|
"token: '%1%'"
|
||||||
@ -1162,7 +1184,7 @@ void PrintablesWebViewPanel::send_will_refresh()
|
|||||||
|
|
||||||
void PrintablesWebViewPanel::on_script_message(wxWebViewEvent& evt)
|
void PrintablesWebViewPanel::on_script_message(wxWebViewEvent& evt)
|
||||||
{
|
{
|
||||||
BOOST_LOG_TRIVIAL(error) << "received message from Printables: " << evt.GetString();
|
BOOST_LOG_TRIVIAL(debug) << "received message from Printables: " << evt.GetString();
|
||||||
handle_message(into_u8(evt.GetString()));
|
handle_message(into_u8(evt.GetString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1176,10 +1198,17 @@ void PrintablesWebViewPanel::sys_color_changed()
|
|||||||
|
|
||||||
void PrintablesWebViewPanel::on_printables_event_access_token_expired(const std::string& message_data)
|
void PrintablesWebViewPanel::on_printables_event_access_token_expired(const std::string& message_data)
|
||||||
{
|
{
|
||||||
// accessTokenExpired
|
// { "event": "accessTokenExpired:)
|
||||||
// Printables pozaduje refresh access tokenu, muze byt volano nekolikrat.Nechme na Mobilni aplikaci at zaridi to ze zareaguje jen jednou
|
// There seems to be a situation where we get accessTokenExpired when there is active token from Slicer POW
|
||||||
|
// We need get new token and freeze webview until its not refreshed
|
||||||
// We do no react on this event now - Our Acount managment should know when to renew our tokens.
|
if (m_refreshing_token) {
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__ << " already refreshing";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
|
||||||
|
m_refreshing_token = true;
|
||||||
|
show_loading_overlay();
|
||||||
|
wxGetApp().plater()->get_user_account()->request_refresh();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintablesWebViewPanel::on_reload_event(const std::string& message_data)
|
void PrintablesWebViewPanel::on_reload_event(const std::string& message_data)
|
||||||
@ -1286,22 +1315,47 @@ void PrintablesWebViewPanel::on_printables_event_required_login(const std::strin
|
|||||||
wxGetApp().printables_login_request();
|
wxGetApp().printables_login_request();
|
||||||
}
|
}
|
||||||
|
|
||||||
void PrintablesWebViewPanel::show_download_notification(const std::string& filename)
|
void PrintablesWebViewPanel::define_css()
|
||||||
{
|
{
|
||||||
// Here we create a javascript that is injected to the webpage.
|
if (m_styles_defined) {
|
||||||
// The script contains styles and everything.
|
return;
|
||||||
// There was a trouble with passing wide characters to the script (it was displayed wrong)
|
}
|
||||||
// Solution is to URL-encode the strings here and pass it.
|
m_styles_defined = true;
|
||||||
// Then inside javascript decode it.
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
|
||||||
const std::string message_filename = Http::url_encode(GUI::format(_u8L("Downloading %1%"),filename));
|
const std::string script = R"(
|
||||||
const std::string message_dest = Http::url_encode(GUI::format(_u8L("To %1%"), wxGetApp().app_config->get("url_downloader_dest")));
|
var style = document.createElement('style');
|
||||||
std::string script = GUI::format(R"(
|
style.innerHTML = `
|
||||||
// Inject custom CSS
|
|
||||||
var style = document.createElement('style');
|
|
||||||
style.innerHTML = `
|
|
||||||
body {
|
body {
|
||||||
/* Add your body styles here */
|
/* Add your body styles here */
|
||||||
}
|
}
|
||||||
|
.slic3r-loading-overlay {
|
||||||
|
position: fixed;
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
right: 0;
|
||||||
|
bottom: 0;
|
||||||
|
background-color: rgba(127 127 127 / 50%);
|
||||||
|
z-index: 50;
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
.slic3r-loading-anim {
|
||||||
|
width: 60px;
|
||||||
|
aspect-ratio: 4;
|
||||||
|
--_g: no-repeat radial-gradient(circle closest-side,#000 90%,#0000);
|
||||||
|
background:
|
||||||
|
var(--_g) 0% 50%,
|
||||||
|
var(--_g) 50% 50%,
|
||||||
|
var(--_g) 100% 50%;
|
||||||
|
background-size: calc(100%/3) 100%;
|
||||||
|
animation: slic3r-loading-anim 1s infinite linear;
|
||||||
|
}
|
||||||
|
@keyframes slic3r-loading-anim {
|
||||||
|
33%{background-size:calc(100%/3) 0% ,calc(100%/3) 100%,calc(100%/3) 100%}
|
||||||
|
50%{background-size:calc(100%/3) 100%,calc(100%/3) 0% ,calc(100%/3) 100%}
|
||||||
|
66%{background-size:calc(100%/3) 100%,calc(100%/3) 100%,calc(100%/3) 0% }
|
||||||
|
}
|
||||||
.notification-popup {
|
.notification-popup {
|
||||||
position: fixed;
|
position: fixed;
|
||||||
right: 10px;
|
right: 10px;
|
||||||
@ -1353,11 +1407,28 @@ void PrintablesWebViewPanel::show_download_notification(const std::string& filen
|
|||||||
color: #ffa500; /* Orange "X" */
|
color: #ffa500; /* Orange "X" */
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
document.head.appendChild(style);
|
document.head.appendChild(style);
|
||||||
|
)";
|
||||||
|
run_script(script);
|
||||||
|
}
|
||||||
|
|
||||||
// Define the notification functions
|
void PrintablesWebViewPanel::show_download_notification(const std::string& filename)
|
||||||
function appendNotification() {
|
{
|
||||||
|
// There was a trouble with passing wide characters to the script (it was displayed wrong)
|
||||||
|
// Solution is to URL-encode the strings here and pass it.
|
||||||
|
// Then inside javascript decodes it.
|
||||||
|
const std::string message_filename = Http::url_encode(GUI::format(_u8L("Downloading %1%"),filename));
|
||||||
|
const std::string message_dest = Http::url_encode(GUI::format(_u8L("To %1%"), wxGetApp().app_config->get("url_downloader_dest")));
|
||||||
|
//std::string message_filename = GUI::format(_u8L("Downloading %1%"),filename);
|
||||||
|
//std::string message_dest = GUI::format(_u8L("To %1%"), escape_string_cstyle(wxGetApp().app_config->get("url_downloader_dest")));
|
||||||
|
std::string script = GUI::format(R"(
|
||||||
|
function removeNotification() {
|
||||||
|
const notifDiv = document.getElementById('slicer-notification');
|
||||||
|
if (notifDiv)
|
||||||
|
notifDiv.remove();
|
||||||
|
}
|
||||||
|
function appendNotification() {
|
||||||
const body = document.getElementsByTagName('body')[0];
|
const body = document.getElementsByTagName('body')[0];
|
||||||
const notifDiv = document.createElement('div');
|
const notifDiv = document.createElement('div');
|
||||||
notifDiv.innerHTML = `
|
notifDiv.innerHTML = `
|
||||||
@ -1372,16 +1443,42 @@ void PrintablesWebViewPanel::show_download_notification(const std::string& filen
|
|||||||
|
|
||||||
window.setTimeout(removeNotification, 5000);
|
window.setTimeout(removeNotification, 5000);
|
||||||
}
|
}
|
||||||
|
appendNotification();
|
||||||
function removeNotification() {
|
)", message_filename, message_dest);
|
||||||
const notifDiv = document.getElementById('slicer-notification');
|
|
||||||
if (notifDiv)
|
|
||||||
notifDiv.remove();
|
|
||||||
}
|
|
||||||
|
|
||||||
appendNotification();
|
|
||||||
)", message_filename, message_dest);
|
|
||||||
run_script(script);
|
run_script(script);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void PrintablesWebViewPanel::show_loading_overlay()
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
|
||||||
|
std::string script = R"(
|
||||||
|
function slic3r_showLoadingOverlay() {
|
||||||
|
const body = document.getElementsByTagName('body')[0];
|
||||||
|
const overlayDiv = document.createElement('div');
|
||||||
|
overlayDiv.className = 'slic3r-loading-overlay'
|
||||||
|
overlayDiv.id = 'slic3r-loading-overlay';
|
||||||
|
overlayDiv.innerHTML = '<div class="slic3r-loading-anim"></div>';
|
||||||
|
body.appendChild(overlayDiv);
|
||||||
|
}
|
||||||
|
slic3r_showLoadingOverlay();
|
||||||
|
)";
|
||||||
|
run_script(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
void PrintablesWebViewPanel::hide_loading_overlay()
|
||||||
|
{
|
||||||
|
BOOST_LOG_TRIVIAL(debug) << __FUNCTION__;
|
||||||
|
m_refreshing_token = false;
|
||||||
|
std::string script = R"(
|
||||||
|
function slic3r_hideLoadingOverlay() {
|
||||||
|
const overlayDiv = document.getElementById('slic3r-loading-overlay');
|
||||||
|
if (overlayDiv)
|
||||||
|
overlayDiv.remove();
|
||||||
|
}
|
||||||
|
slic3r_hideLoadingOverlay();
|
||||||
|
)";
|
||||||
|
run_script(script);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace slic3r::GUI
|
} // namespace slic3r::GUI
|
@ -203,10 +203,16 @@ private:
|
|||||||
void on_printables_event_required_login(const std::string& message_data);
|
void on_printables_event_required_login(const std::string& message_data);
|
||||||
void load_default_url() override;
|
void load_default_url() override;
|
||||||
std::string get_url_lang_theme(const wxString& url) const;
|
std::string get_url_lang_theme(const wxString& url) const;
|
||||||
|
void define_css();
|
||||||
void show_download_notification(const std::string& filename);
|
void show_download_notification(const std::string& filename);
|
||||||
|
void show_loading_overlay();
|
||||||
|
void hide_loading_overlay();
|
||||||
|
|
||||||
std::map<std::string, std::function<void(const std::string&)>> m_events;
|
std::map<std::string, std::function<void(const std::string&)>> m_events;
|
||||||
std::string m_next_show_url;
|
std::string m_next_show_url;
|
||||||
|
|
||||||
|
bool m_refreshing_token {false};
|
||||||
|
bool m_styles_defined {false};
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
bool m_remove_request_auth { false };
|
bool m_remove_request_auth { false };
|
||||||
#endif
|
#endif
|
||||||
|
@ -8,19 +8,21 @@
|
|||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
namespace Slic3r::Utils {
|
namespace Slic3r::Utils {
|
||||||
|
|
||||||
bool verify_exp(const std::string& token)
|
namespace {
|
||||||
|
boost::optional<double> get_exp(const std::string& token)
|
||||||
{
|
{
|
||||||
size_t payload_start = token.find('.');
|
size_t payload_start = token.find('.');
|
||||||
if (payload_start == std::string::npos)
|
if (payload_start == std::string::npos)
|
||||||
return false;
|
return boost::none;
|
||||||
payload_start += 1; // payload starts after dot
|
payload_start += 1; // payload starts after dot
|
||||||
|
|
||||||
const size_t payload_end = token.find('.', payload_start);
|
const size_t payload_end = token.find('.', payload_start);
|
||||||
if (payload_end == std::string::npos)
|
if (payload_end == std::string::npos)
|
||||||
return false;
|
return boost::none;
|
||||||
|
|
||||||
size_t encoded_length = payload_end - payload_start;
|
size_t encoded_length = payload_end - payload_start;
|
||||||
size_t decoded_length = boost::beast::detail::base64::decoded_size(encoded_length);
|
size_t decoded_length = boost::beast::detail::base64::decoded_size(encoded_length);
|
||||||
@ -40,7 +42,7 @@ bool verify_exp(const std::string& token)
|
|||||||
std::tie(written_bytes, read_bytes) = boost::beast::detail::base64::decode(json.data(), json_b64.data(), json_b64.length());
|
std::tie(written_bytes, read_bytes) = boost::beast::detail::base64::decode(json.data(), json_b64.data(), json_b64.length());
|
||||||
json.resize(written_bytes);
|
json.resize(written_bytes);
|
||||||
if (written_bytes == 0)
|
if (written_bytes == 0)
|
||||||
return false;
|
return boost::none;
|
||||||
|
|
||||||
namespace pt = boost::property_tree;
|
namespace pt = boost::property_tree;
|
||||||
|
|
||||||
@ -48,10 +50,26 @@ bool verify_exp(const std::string& token)
|
|||||||
std::istringstream iss(json);
|
std::istringstream iss(json);
|
||||||
pt::json_parser::read_json(iss, payload);
|
pt::json_parser::read_json(iss, payload);
|
||||||
|
|
||||||
auto exp_opt = payload.get_optional<double>("exp");
|
return payload.get_optional<double>("exp");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int get_exp_seconds(const std::string& token)
|
||||||
|
{
|
||||||
|
auto exp_opt = get_exp(token);
|
||||||
|
if (!exp_opt)
|
||||||
|
return 0;
|
||||||
|
auto now = std::chrono::system_clock::now();
|
||||||
|
auto now_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch()).count();
|
||||||
|
double remaining_time = *exp_opt - now_in_seconds;
|
||||||
|
return (int)remaining_time;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool verify_exp(const std::string& token)
|
||||||
|
{
|
||||||
|
auto exp_opt = get_exp(token);
|
||||||
if (!exp_opt)
|
if (!exp_opt)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
auto now = time(nullptr);
|
auto now = time(nullptr);
|
||||||
return exp_opt.get() > now;
|
return exp_opt.get() > now;
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,6 @@
|
|||||||
namespace Slic3r::Utils {
|
namespace Slic3r::Utils {
|
||||||
|
|
||||||
bool verify_exp(const std::string& token);
|
bool verify_exp(const std::string& token);
|
||||||
|
int get_exp_seconds(const std::string& token);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user