diff --git a/src/slic3r/GUI/WebViewPanel.cpp b/src/slic3r/GUI/WebViewPanel.cpp index 5618b0c008..8307a3bde7 100644 --- a/src/slic3r/GUI/WebViewPanel.cpp +++ b/src/slic3r/GUI/WebViewPanel.cpp @@ -223,6 +223,7 @@ void WebViewPanel::on_show(wxShowEvent& evt) if (m_do_late_webview_create) { m_do_late_webview_create = false; late_create(); + return; } if (m_load_default_url) { m_load_default_url = false; @@ -254,7 +255,7 @@ void WebViewPanel::on_idle(wxIdleEvent& WXUNUSED(evt)) // So we just reset the handler here. if (!m_script_message_hadler_names.empty()) { m_browser->RemoveScriptMessageHandler(Slic3r::GUI::from_u8(m_script_message_hadler_names.front())); - bool b = m_browser->AddScriptMessageHandler(Slic3r::GUI::from_u8(m_script_message_hadler_names.front())); + m_browser->AddScriptMessageHandler(Slic3r::GUI::from_u8(m_script_message_hadler_names.front())); } } @@ -270,6 +271,10 @@ void WebViewPanel::on_loaded(wxWebViewEvent& evt) if (evt.GetURL().IsEmpty()) return; m_load_default_url_on_next_error = false; + if (evt.GetURL().Find(GUI::format_wxstr("/web/%1%.html", m_loading_html)) != wxNOT_FOUND && m_load_default_url) { + m_load_default_url = false; + load_default_url(); + } } /** @@ -850,16 +855,28 @@ PrinterWebViewPanel::PrinterWebViewPanel(wxWindow* parent, const wxString& defau { } +void PrinterWebViewPanel::on_navigation_request(wxWebViewEvent &evt) +{ + const wxString url = evt.GetURL(); + if (url.StartsWith(m_default_url) && !m_api_key_sent) { + if (!m_usr.empty() && !m_psk.empty()) { + send_credentials(); + } + } +} + void PrinterWebViewPanel::on_loaded(wxWebViewEvent& evt) { if (evt.GetURL().IsEmpty()) return; m_load_default_url_on_next_error = false; - + if (evt.GetURL().Find(GUI::format_wxstr("/web/%1%.html", m_loading_html)) != wxNOT_FOUND && m_load_default_url) { + m_load_default_url = false; + load_default_url(); + return; + } if (!m_api_key.empty()) { send_api_key(); - } else if (!m_usr.empty() && !m_psk.empty()) { - send_credentials(); } } void PrinterWebViewPanel::on_script_message(wxWebViewEvent& evt) @@ -903,15 +920,12 @@ void PrinterWebViewPanel::send_credentials() return; m_browser->RemoveAllUserScripts(); m_browser->AddUserScript("sessionStorage.removeItem('authType'); sessionStorage.removeItem('apiKey'); console.log('Session Storage cleared');"); - m_browser->Reload(); + // reload would be done only if called from on_loaded + //m_browser->Reload(); m_api_key_sent = true; setup_webview_with_credentials(m_browser, m_usr, m_psk); } -void PrinterWebViewPanel::sys_color_changed() -{ -} - PrintablesWebViewPanel::PrintablesWebViewPanel(wxWindow* parent) : WebViewPanel(parent, GUI::from_u8(Utils::ServiceConfig::instance().printables_url()), { "ExternalApp" }, "other_loading", "other_error", false) @@ -973,9 +987,17 @@ wxString PrintablesWebViewPanel::get_default_url() const void PrintablesWebViewPanel::on_loaded(wxWebViewEvent& evt) { + if (evt.GetURL().Find(GUI::format_wxstr("/web/%1%.html", m_loading_html)) != wxNOT_FOUND && m_load_default_url) { + m_load_default_url = false; + load_default_url(); + return; + } #ifdef _WIN32 // This is needed only once after add_request_authorization - remove_request_authorization(m_browser); + if (m_remove_request_auth) { + m_remove_request_auth = false; + remove_request_authorization(m_browser); + } #endif m_load_default_url_on_next_error = false; } @@ -1054,7 +1076,7 @@ void PrintablesWebViewPanel::logout(const std::string& override_url/* = std::str } delete_cookies(m_browser, Utils::ServiceConfig::instance().printables_url()); m_browser->RunScript("localStorage.clear();"); - + std::string next_url = override_url.empty() ? get_url_lang_theme(m_browser->GetCurrentURL()) : get_url_lang_theme(from_u8(override_url)); @@ -1110,6 +1132,7 @@ void PrintablesWebViewPanel::load_default_url() // add token to first request #ifdef _WIN32 add_request_authorization(m_browser, m_default_url, access_token); + m_remove_request_auth = true; load_url(GUI::from_u8(actual_default_url)); #else load_request(m_browser, actual_default_url, access_token); diff --git a/src/slic3r/GUI/WebViewPanel.hpp b/src/slic3r/GUI/WebViewPanel.hpp index 4a16b90a7d..969d0a9ea8 100644 --- a/src/slic3r/GUI/WebViewPanel.hpp +++ b/src/slic3r/GUI/WebViewPanel.hpp @@ -155,6 +155,7 @@ public: void on_loaded(wxWebViewEvent& evt) override; void on_script_message(wxWebViewEvent& evt) override; + void on_navigation_request(wxWebViewEvent &evt) override; void send_api_key(); void send_credentials(); void set_api_key(const std::string &key) @@ -169,7 +170,6 @@ public: m_psk = psk; } void clear() { m_api_key.clear(); m_usr.clear(); m_psk.clear(); m_api_key_sent = false; } - void sys_color_changed() override; private: std::string m_api_key; std::string m_usr; @@ -207,6 +207,9 @@ private: std::map> m_events; std::string m_next_show_url; +#ifdef _WIN32 + bool m_remove_request_auth { false }; +#endif /* Eventy Slicer -> Printables accessTokenWillChange diff --git a/src/slic3r/GUI/WebViewPlatformUtilsWin32.cpp b/src/slic3r/GUI/WebViewPlatformUtilsWin32.cpp index 2beeed0dce..8a9f8c79c0 100644 --- a/src/slic3r/GUI/WebViewPlatformUtilsWin32.cpp +++ b/src/slic3r/GUI/WebViewPlatformUtilsWin32.cpp @@ -26,11 +26,13 @@ void setup_webview_with_credentials(wxWebView* webview, const std::string& usern { ICoreWebView2 *webView2 = static_cast(webview->GetNativeBackend()); if (!webView2) { + BOOST_LOG_TRIVIAL(error) << "setup_webview_with_credentials Failed: Webview 2 is null."; return; } wxCOMPtr wv2_10; HRESULT hr = webView2->QueryInterface(IID_PPV_ARGS(&wv2_10)); if (FAILED(hr)) { + BOOST_LOG_TRIVIAL(error) << "setup_webview_with_credentials Failed: ICoreWebView2_10 is null."; return; } @@ -68,11 +70,13 @@ void remove_webview_credentials(wxWebView* webview) { ICoreWebView2 *webView2 = static_cast(webview->GetNativeBackend()); if (!webView2) { + BOOST_LOG_TRIVIAL(error) << "remove_webview_credentials Failed: webView2 is null."; return; } wxCOMPtr wv2_10; HRESULT hr = webView2->QueryInterface(IID_PPV_ARGS(&wv2_10)); if (FAILED(hr)) { + BOOST_LOG_TRIVIAL(error) << "remove_webview_credentials Failed: ICoreWebView2_10 is null."; return; } @@ -93,6 +97,7 @@ void delete_cookies(wxWebView* webview, const std::string& url) { ICoreWebView2 *webView2 = static_cast(webview->GetNativeBackend()); if (!webView2) { + BOOST_LOG_TRIVIAL(error) << "delete_cookies Failed: webView2 is null."; return; } @@ -183,11 +188,13 @@ void add_request_authorization(wxWebView* webview, const wxString& address, cons // The filter needs to be removed to stop adding the auth header ICoreWebView2 *webView2 = static_cast(webview->GetNativeBackend()); if (!webView2) { + BOOST_LOG_TRIVIAL(error) << "Adding request Authorization Failed: Webview 2 is null."; return; } wxCOMPtr wv2_2; HRESULT hr = webView2->QueryInterface(IID_PPV_ARGS(&wv2_2)); if (FAILED(hr)) { + BOOST_LOG_TRIVIAL(error) << "Adding request Authorization Failed: QueryInterface ICoreWebView2_2 has failed."; return; } filter_patern = address + "/*"; @@ -238,8 +245,10 @@ void remove_request_authorization(wxWebView* webview) { ICoreWebView2 *webView2 = static_cast(webview->GetNativeBackend()); if (!webView2) { + BOOST_LOG_TRIVIAL(error) << "remove_request_authorization Failed: webView2 is null."; return; } + BOOST_LOG_TRIVIAL(info) << "remove_request_authorization"; webView2->RemoveWebResourceRequestedFilter(filter_patern.c_str(), COREWEBVIEW2_WEB_RESOURCE_CONTEXT_DOCUMENT); if(FAILED(webView2->remove_WebResourceRequested( m_webResourceRequestedTokenForImageBlocking))) { BOOST_LOG_TRIVIAL(error) << "WebView: Failed to remove resources"; @@ -256,6 +265,7 @@ void load_request(wxWebView* web_view, const std::string& address, const std::st ICoreWebView2 *webView2 = static_cast(web_view->GetNativeBackend()); if (!webView2) { + BOOST_LOG_TRIVIAL(error) << "load_request Failed: webView2 is null."; return; } @@ -263,12 +273,14 @@ void load_request(wxWebView* web_view, const std::string& address, const std::st wxCOMPtr webViewEnvironment; //webViewEnvironment = static_cast(web_view->GetEnviroment()); if (!webViewEnvironment.Get()) { + BOOST_LOG_TRIVIAL(error) << "load_request Failed: ICoreWebView2Environment is null."; return; } wxCOMPtr webViewEnvironment2; if (FAILED(webViewEnvironment->QueryInterface(IID_PPV_ARGS(&webViewEnvironment2)))) { + BOOST_LOG_TRIVIAL(error) << "load_request Failed: ICoreWebView2Environment2 is null."; return; } wxCOMPtr webResourceRequest; @@ -277,14 +289,17 @@ void load_request(wxWebView* web_view, const std::string& address, const std::st L"https://www.printables.com/", L"GET", NULL, L"Content-Type: application/x-www-form-urlencoded", &webResourceRequest))) { + BOOST_LOG_TRIVIAL(error) << "load_request Failed: CreateWebResourceRequest failed."; return; } wxCOMPtr wv2_2; if (FAILED(webView2->QueryInterface(IID_PPV_ARGS(&wv2_2)))) { + BOOST_LOG_TRIVIAL(error) << "load_request Failed: ICoreWebView2_2 is null."; return; } if (FAILED(wv2_2->NavigateWithWebResourceRequest(webResourceRequest.get()))) { + BOOST_LOG_TRIVIAL(error) << "load_request Failed: NavigateWithWebResourceRequest failed."; return; } }