2021-10-04 15:55:01 +08:00
|
|
|
/****************************************************************************
|
2021-10-04 16:56:22 +08:00
|
|
|
MIT License
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-04 16:56:22 +08:00
|
|
|
Portions copyright (c) 2017 Serge Zaitsev
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2022-10-01 16:24:52 +08:00
|
|
|
https://axmolengine.github.io/
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-04 15:55:01 +08:00
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
|
|
in the Software without restriction, including without limitation the rights
|
|
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
|
|
furnished to do so, subject to the following conditions:
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-04 15:55:01 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-10-04 15:55:01 +08:00
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
THE SOFTWARE.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-08-08 18:02:17 +08:00
|
|
|
#if AX_TARGET_PLATFORM == AX_PLATFORM_WIN32 && defined(_AX_HAVE_WEBVIEW2)
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "UIWebViewImpl-win32.h"
|
|
|
|
# include "UIWebView.h"
|
|
|
|
# include "base/CCDirector.h"
|
|
|
|
# include "platform/CCFileUtils.h"
|
|
|
|
# include "platform/CCGLView.h"
|
|
|
|
# include "base/base64.h"
|
|
|
|
# include "ui/UIHelper.h"
|
|
|
|
# include "rapidjson/document.h"
|
|
|
|
# include "rapidjson/stringbuffer.h"
|
|
|
|
# include "rapidjson/writer.h"
|
2022-04-26 15:16:02 +08:00
|
|
|
# include "base/ccUtils.h"
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
# include <Shlwapi.h>
|
|
|
|
# include <codecvt>
|
|
|
|
# include <utility>
|
|
|
|
# include <stdlib.h>
|
|
|
|
# include <Windows.h>
|
|
|
|
|
|
|
|
# pragma comment(lib, "user32.lib")
|
|
|
|
# pragma comment(lib, "Shlwapi.lib")
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
// Edge/Chromium headers and libs
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "WebView2.h"
|
|
|
|
# pragma comment(lib, "ole32.lib")
|
|
|
|
# pragma comment(lib, "oleaut32.lib")
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
# include "platform/CCPlatformConfig.h"
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2022-02-16 11:56:33 +08:00
|
|
|
# include "ntcvt/ntcvt.hpp"
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
USING_NS_AX;
|
2021-10-06 21:37:37 +08:00
|
|
|
using namespace rapidjson;
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
using msg_cb_t = std::function<void(std::string_view)>;
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
inline std::string htmlFromUri(std::string_view s)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
if (s.substr(0, 15) == "data:text/html,")
|
|
|
|
{
|
2021-10-05 09:50:01 +08:00
|
|
|
return utils::urlDecode(s.substr(15));
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2021-10-06 21:37:37 +08:00
|
|
|
inline int jsonUnescape(const char* s, size_t n, char* out)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
if (*s++ != '"')
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
while (n > 2)
|
|
|
|
{
|
|
|
|
char c = *s;
|
|
|
|
if (c == '\\')
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
n--;
|
|
|
|
switch (*s)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case 'b':
|
|
|
|
c = '\b';
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
c = '\f';
|
|
|
|
break;
|
|
|
|
case 'n':
|
|
|
|
c = '\n';
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
c = '\r';
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
c = '\t';
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
c = '\\';
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
c = '/';
|
|
|
|
break;
|
|
|
|
case '\"':
|
|
|
|
c = '\"';
|
|
|
|
break;
|
|
|
|
default: // TODO: support unicode decoding
|
|
|
|
return -1;
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (out != NULL)
|
|
|
|
{
|
|
|
|
*out++ = c;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
n--;
|
|
|
|
r++;
|
|
|
|
}
|
|
|
|
if (*s != '"')
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (out != NULL)
|
|
|
|
{
|
|
|
|
*out = '\0';
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2021-10-06 21:37:37 +08:00
|
|
|
// These are the results that must be returned by this method
|
|
|
|
// assert(jsonParse(R"({"foo":"bar"})", "foo", -1) == "bar");
|
|
|
|
// assert(jsonParse(R"({"foo":""})", "foo", -1) == "");
|
|
|
|
// assert(jsonParse(R"(["foo", "bar", "baz"])", "", 0) == "foo");
|
|
|
|
// assert(jsonParse(R"(["foo", "bar", "baz"])", "", 2) == "baz");
|
|
|
|
// The following is a special case, where the exact json string is not returned due
|
|
|
|
// to how rapidjson re-creates the nested object, original: "{"bar": 1}", parsed result: "{"bar":1}"
|
|
|
|
// assert(jsonParse(R"({"foo": {"bar": 1}})", "foo", -1) == R"({"bar":1})");
|
2021-12-31 12:12:40 +08:00
|
|
|
inline std::string jsonParse(std::string_view s, std::string_view key, const int index)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-10-06 21:37:37 +08:00
|
|
|
const char* value = nullptr;
|
|
|
|
size_t value_sz{};
|
|
|
|
StringBuffer sb;
|
|
|
|
Writer<StringBuffer> writer(sb);
|
|
|
|
Document d;
|
2021-12-31 12:12:40 +08:00
|
|
|
d.Parse(s.data());
|
2021-10-06 21:37:37 +08:00
|
|
|
if (key.empty() && index > -1)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-10-06 21:37:37 +08:00
|
|
|
if (d.IsArray())
|
|
|
|
{
|
|
|
|
auto&& jsonArray = d.GetArray();
|
|
|
|
if (SizeType(index) < jsonArray.Size())
|
|
|
|
{
|
|
|
|
auto&& arrayValue = jsonArray[SizeType(index)];
|
|
|
|
value = arrayValue.GetString();
|
|
|
|
value_sz = arrayValue.GetStringLength();
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
auto&& fieldItr = d.FindMember(key.data());
|
2021-10-06 21:37:37 +08:00
|
|
|
if (fieldItr != d.MemberEnd())
|
|
|
|
{
|
|
|
|
auto&& jsonValue = fieldItr->value;
|
|
|
|
if (jsonValue.IsString())
|
|
|
|
{
|
|
|
|
value = jsonValue.GetString();
|
|
|
|
value_sz = jsonValue.GetStringLength();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
jsonValue.Accept(writer);
|
|
|
|
value = sb.GetString();
|
|
|
|
value_sz = sb.GetLength();
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-10-06 21:37:37 +08:00
|
|
|
|
2021-10-04 15:55:01 +08:00
|
|
|
if (value != nullptr)
|
|
|
|
{
|
|
|
|
if (value[0] != '"')
|
|
|
|
{
|
|
|
|
return std::string(value, value_sz);
|
|
|
|
}
|
2021-10-06 21:37:37 +08:00
|
|
|
|
|
|
|
const auto n = jsonUnescape(value, value_sz, nullptr);
|
2021-10-04 15:55:01 +08:00
|
|
|
if (n > 0)
|
|
|
|
{
|
2021-10-06 21:37:37 +08:00
|
|
|
const auto decoded = std::unique_ptr<char[]>(new char[n + 1]);
|
|
|
|
jsonUnescape(value, value_sz, decoded.get());
|
|
|
|
return std::string(decoded.get(), n);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class ArgType>
|
|
|
|
static std::string getUriStringFromArgs(ArgType* args)
|
|
|
|
{
|
|
|
|
if (args != nullptr)
|
|
|
|
{
|
|
|
|
LPWSTR uri;
|
|
|
|
args->get_Uri(&uri);
|
|
|
|
std::wstring ws(uri);
|
|
|
|
std::string result = std::string(ws.begin(), ws.end());
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static std::string getDataURI(std::string_view data, std::string_view mime_type)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
char* encodedData;
|
2022-08-08 18:02:17 +08:00
|
|
|
ax::base64Encode(reinterpret_cast<const unsigned char*>(data.data()), static_cast<unsigned>(data.size()),
|
2021-12-25 10:04:45 +08:00
|
|
|
&encodedData);
|
2021-12-31 12:12:40 +08:00
|
|
|
return std::string{"data:"}.append(mime_type).append(";base64,").append(utils::urlEncode(encodedData));
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
2021-10-06 21:37:37 +08:00
|
|
|
static double getDeviceScaleFactor()
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
static double scale_factor = 1.0;
|
|
|
|
static bool initialized = false;
|
|
|
|
|
|
|
|
if (!initialized)
|
|
|
|
{
|
|
|
|
// This value is safe to cache for the life time of the app since the user
|
|
|
|
// must logout to change the DPI setting. This value also applies to all
|
|
|
|
// screens.
|
|
|
|
# if _WIN32
|
|
|
|
HDC screen_dc = ::GetDC(NULL);
|
|
|
|
int dpi_x = GetDeviceCaps(screen_dc, LOGPIXELSX);
|
|
|
|
scale_factor = static_cast<double>(dpi_x) / 96.0;
|
|
|
|
::ReleaseDC(NULL, screen_dc);
|
|
|
|
# endif
|
|
|
|
initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return scale_factor;
|
|
|
|
}
|
|
|
|
|
|
|
|
// declarate
|
|
|
|
class Win32WebControl
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Win32WebControl();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool createWebView(const std::function<bool(std::string_view)>& shouldStartLoading,
|
|
|
|
const std::function<void(std::string_view)>& didFinishLoading,
|
|
|
|
const std::function<void(std::string_view)>& didFailLoading,
|
|
|
|
const std::function<void(std::string_view)>& onJsCallback);
|
2021-10-04 15:55:01 +08:00
|
|
|
void removeWebView();
|
|
|
|
|
|
|
|
void setWebViewRect(const int left, const int top, const int width, const int height);
|
2021-12-31 12:12:40 +08:00
|
|
|
void setJavascriptInterfaceScheme(std::string_view scheme);
|
|
|
|
void loadHTMLString(std::string_view html, std::string_view baseURL);
|
|
|
|
void loadURL(std::string_view url, bool cleanCachedData);
|
|
|
|
void loadFile(std::string_view filePath);
|
2021-10-04 15:55:01 +08:00
|
|
|
void stopLoading();
|
|
|
|
void reload() const;
|
|
|
|
bool canGoBack() const;
|
|
|
|
bool canGoForward() const;
|
|
|
|
void goBack() const;
|
|
|
|
void goForward() const;
|
2021-12-31 12:12:40 +08:00
|
|
|
void evaluateJS(std::string_view js);
|
2021-10-04 15:55:01 +08:00
|
|
|
void setScalesPageToFit(const bool scalesPageToFit);
|
|
|
|
void setWebViewVisible(const bool visible) const;
|
|
|
|
void setBounces(bool bounces);
|
|
|
|
void setOpacityWebView(float opacity);
|
|
|
|
float getOpacityWebView() const;
|
|
|
|
void setBackgroundTransparent();
|
|
|
|
|
|
|
|
private:
|
|
|
|
ICoreWebView2* m_webview = nullptr;
|
|
|
|
ICoreWebView2Controller* m_controller = nullptr;
|
|
|
|
HWND m_window{};
|
|
|
|
POINT m_minsz = POINT{0, 0};
|
|
|
|
POINT m_maxsz = POINT{0, 0};
|
|
|
|
DWORD m_main_thread = GetCurrentThreadId();
|
|
|
|
|
|
|
|
std::string m_jsScheme;
|
2021-10-04 21:04:30 +08:00
|
|
|
bool _scalesPageToFit{};
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
std::function<bool(std::string_view)> _shouldStartLoading;
|
|
|
|
std::function<void(std::string_view)> _didFinishLoading;
|
|
|
|
std::function<void(std::string_view)> _didFailLoading;
|
|
|
|
std::function<void(std::string_view)> _onJsCallback;
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
static bool s_isInitialized;
|
|
|
|
static void lazyInit();
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
static LPWSTR to_lpwstr(std::string_view s)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-31 12:12:40 +08:00
|
|
|
const int n = MultiByteToWideChar(CP_UTF8, 0, s.data(), -1, NULL, 0);
|
2021-12-25 10:04:45 +08:00
|
|
|
auto* ws = new wchar_t[n];
|
2021-12-31 12:12:40 +08:00
|
|
|
MultiByteToWideChar(CP_UTF8, 0, s.data(), -1, ws, n);
|
2021-10-04 15:55:01 +08:00
|
|
|
return ws;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool embed(HWND wnd, bool debug, msg_cb_t cb)
|
|
|
|
{
|
|
|
|
std::atomic_flag flag = ATOMIC_FLAG_INIT;
|
|
|
|
flag.test_and_set();
|
|
|
|
|
|
|
|
char currentExePath[MAX_PATH];
|
|
|
|
GetModuleFileNameA(NULL, currentExePath, MAX_PATH);
|
|
|
|
char* currentExeName = PathFindFileNameA(currentExePath);
|
|
|
|
|
2022-02-16 11:56:33 +08:00
|
|
|
/*
|
|
|
|
* Note: New OS feature 'Beta: Use Unicode UTF-8 for worldwide language support' since win10/win11
|
|
|
|
* - OFF: GetACP() equal to current system locale, such as chinese simplified is 936, english is 437
|
|
|
|
* - ON: GetACP() always equal to 65001(UTF-8)
|
|
|
|
* Remark:
|
|
|
|
* The macro CP_ACP for ntcvt::from_chars works for converting chraset from current code page(936,437,65001) to utf-16
|
|
|
|
*/
|
|
|
|
std::wstring userDataFolder = ntcvt::from_chars(std::getenv("APPDATA"), CP_ACP);
|
|
|
|
std::wstring currentExeNameW = ntcvt::from_chars(currentExeName, CP_ACP);
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
HRESULT res = CreateCoreWebView2EnvironmentWithOptions(
|
|
|
|
nullptr, (userDataFolder + L"/" + currentExeNameW).c_str(), nullptr,
|
2021-12-25 10:04:45 +08:00
|
|
|
new webview2_com_handler(
|
|
|
|
wnd, cb,
|
|
|
|
[&](ICoreWebView2Controller* controller) {
|
|
|
|
m_controller = controller;
|
|
|
|
m_controller->get_CoreWebView2(&m_webview);
|
|
|
|
m_webview->AddRef();
|
|
|
|
flag.clear();
|
|
|
|
},
|
2021-12-31 12:12:40 +08:00
|
|
|
[this](std::string_view url) -> bool {
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto scheme = url.substr(0, url.find_first_of(':'));
|
|
|
|
if (scheme == m_jsScheme)
|
|
|
|
{
|
|
|
|
if (_onJsCallback)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_onJsCallback(url);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
return true;
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_shouldStartLoading && !url.empty())
|
|
|
|
{
|
|
|
|
return _shouldStartLoading(url);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
},
|
|
|
|
[this]() {
|
|
|
|
LPWSTR uri;
|
|
|
|
this->m_webview->get_Source(&uri);
|
|
|
|
std::wstring ws(uri);
|
|
|
|
const auto result = std::string(ws.begin(), ws.end());
|
|
|
|
if (_didFinishLoading)
|
|
|
|
_didFinishLoading(result);
|
|
|
|
},
|
|
|
|
[this]() {
|
|
|
|
LPWSTR uri;
|
|
|
|
this->m_webview->get_Source(&uri);
|
|
|
|
std::wstring ws(uri);
|
|
|
|
const auto result = std::string(ws.begin(), ws.end());
|
|
|
|
if (_didFailLoading)
|
|
|
|
_didFailLoading(result);
|
|
|
|
},
|
2021-12-31 12:12:40 +08:00
|
|
|
[this](std::string_view url) { loadURL(url, false); }));
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
if (res != S_OK)
|
|
|
|
{
|
|
|
|
CoUninitialize();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
MSG msg = {};
|
|
|
|
while (flag.test_and_set() && GetMessage(&msg, NULL, 0, 0))
|
|
|
|
{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
init("window.external={invoke:s=>window.chrome.webview.postMessage(s)}");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void resize(HWND wnd)
|
|
|
|
{
|
|
|
|
if (m_controller == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RECT bounds;
|
|
|
|
GetClientRect(wnd, &bounds);
|
|
|
|
m_controller->put_Bounds(bounds);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void navigate(std::string_view url)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
auto wurl = to_lpwstr(url);
|
|
|
|
m_webview->Navigate(wurl);
|
|
|
|
delete[] wurl;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void init(std::string_view js)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
LPCWSTR wjs = to_lpwstr(js);
|
|
|
|
m_webview->AddScriptToExecuteOnDocumentCreated(wjs, nullptr);
|
|
|
|
delete[] wjs;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void eval(std::string_view js)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
LPCWSTR wjs = to_lpwstr(js);
|
|
|
|
m_webview->ExecuteScript(wjs, nullptr);
|
|
|
|
delete[] wjs;
|
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void on_message(std::string_view msg)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-10-06 21:37:37 +08:00
|
|
|
const auto seq = jsonParse(msg, "id", 0);
|
|
|
|
const auto name = jsonParse(msg, "method", 0);
|
|
|
|
const auto args = jsonParse(msg, "params", 0);
|
2021-10-04 15:55:01 +08:00
|
|
|
if (bindings.find(name) == bindings.end())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto fn = bindings[name];
|
|
|
|
(*fn->first)(seq, args, fn->second);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
using binding_t = std::function<void(std::string, std::string, void*)>;
|
2021-10-04 15:55:01 +08:00
|
|
|
using binding_ctx_t = std::pair<binding_t*, void*>;
|
|
|
|
std::map<std::string, binding_ctx_t*> bindings;
|
|
|
|
|
|
|
|
class webview2_com_handler : public ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler,
|
|
|
|
public ICoreWebView2CreateCoreWebView2ControllerCompletedHandler,
|
|
|
|
public ICoreWebView2WebMessageReceivedEventHandler,
|
|
|
|
public ICoreWebView2PermissionRequestedEventHandler,
|
|
|
|
public ICoreWebView2NavigationStartingEventHandler,
|
|
|
|
public ICoreWebView2NavigationCompletedEventHandler,
|
|
|
|
public ICoreWebView2NewWindowRequestedEventHandler,
|
|
|
|
public ICoreWebView2ContentLoadingEventHandler,
|
|
|
|
public ICoreWebView2DOMContentLoadedEventHandler
|
|
|
|
{
|
|
|
|
using webview2_com_handler_cb_t = std::function<void(ICoreWebView2Controller*)>;
|
|
|
|
|
|
|
|
public:
|
2021-12-25 10:04:45 +08:00
|
|
|
webview2_com_handler(HWND hwnd,
|
|
|
|
msg_cb_t msgCb,
|
2021-10-04 15:55:01 +08:00
|
|
|
webview2_com_handler_cb_t cb,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::function<bool(std::string_view)> navStartingCallback,
|
2021-10-04 15:55:01 +08:00
|
|
|
std::function<void()> navCompleteCallback,
|
|
|
|
std::function<void()> navErrorCallback,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::function<void(std::string_view url)> loadUrlCallback)
|
2021-10-04 15:55:01 +08:00
|
|
|
: m_window(hwnd)
|
|
|
|
, m_msgCb(std::move(msgCb))
|
|
|
|
, m_cb(std::move(cb))
|
|
|
|
, m_navStartingCallback(std::move(navStartingCallback))
|
|
|
|
, m_navCompleteCallback(std::move(navCompleteCallback))
|
|
|
|
, m_navErrorCallback(std::move(navErrorCallback))
|
|
|
|
, m_loadUrlCallback(std::move(loadUrlCallback))
|
|
|
|
{}
|
|
|
|
ULONG STDMETHODCALLTYPE AddRef() { return 1; }
|
|
|
|
ULONG STDMETHODCALLTYPE Release() { return 1; }
|
|
|
|
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID riid, LPVOID* ppv) { return S_OK; }
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(HRESULT res, ICoreWebView2Environment* env)
|
|
|
|
{
|
|
|
|
env->CreateCoreWebView2Controller(m_window, this);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2CreateCoreWebView2ControllerCompletedHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(HRESULT res, ICoreWebView2Controller* controller)
|
|
|
|
{
|
|
|
|
controller->AddRef();
|
|
|
|
|
|
|
|
ICoreWebView2* webview;
|
|
|
|
::EventRegistrationToken token;
|
|
|
|
controller->get_CoreWebView2(&webview);
|
|
|
|
webview->add_WebMessageReceived(this, &token);
|
|
|
|
webview->add_PermissionRequested(this, &token);
|
|
|
|
webview->add_NavigationStarting(this, &token);
|
|
|
|
webview->add_NavigationCompleted(this, &token);
|
|
|
|
webview->add_ContentLoading(this, &token);
|
|
|
|
webview->add_NewWindowRequested(this, &token);
|
|
|
|
|
|
|
|
m_cb(controller);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2WebMessageReceivedEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2WebMessageReceivedEventArgs* args)
|
|
|
|
{
|
|
|
|
LPWSTR message;
|
|
|
|
args->TryGetWebMessageAsString(&message);
|
|
|
|
|
|
|
|
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> wideCharConverter;
|
|
|
|
m_msgCb(wideCharConverter.to_bytes(message));
|
|
|
|
sender->PostWebMessageAsString(message);
|
|
|
|
|
|
|
|
CoTaskMemFree(message);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2PermissionRequestedEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2PermissionRequestedEventArgs* args)
|
|
|
|
{
|
|
|
|
COREWEBVIEW2_PERMISSION_KIND kind;
|
|
|
|
args->get_PermissionKind(&kind);
|
|
|
|
if (kind == COREWEBVIEW2_PERMISSION_KIND_CLIPBOARD_READ)
|
|
|
|
{
|
|
|
|
args->put_State(COREWEBVIEW2_PERMISSION_STATE_ALLOW);
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2NavigationStartingEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2NavigationStartingEventArgs* args)
|
|
|
|
{
|
|
|
|
if (m_navStartingCallback)
|
|
|
|
{
|
|
|
|
const auto uriString = getUriStringFromArgs(args);
|
|
|
|
if (!m_navStartingCallback(uriString))
|
|
|
|
{
|
|
|
|
args->put_Cancel(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2NavigationCompletedEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2NavigationCompletedEventArgs* args)
|
|
|
|
{
|
|
|
|
BOOL success;
|
2021-10-05 06:22:48 +08:00
|
|
|
if (SUCCEEDED(args->get_IsSuccess(&success)) && success)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
if (m_navCompleteCallback)
|
|
|
|
{
|
|
|
|
m_navCompleteCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-10-05 06:22:48 +08:00
|
|
|
// example of how to get status error if required
|
2021-12-25 10:04:45 +08:00
|
|
|
// COREWEBVIEW2_WEB_ERROR_STATUS status;
|
|
|
|
// if (SUCCEEDED(args->get_WebErrorStatus(&status)))
|
2021-10-05 06:22:48 +08:00
|
|
|
//{
|
|
|
|
//}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
if (m_navErrorCallback)
|
|
|
|
{
|
|
|
|
m_navErrorCallback();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2ContentLoadingEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2ContentLoadingEventArgs* args)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2NewWindowRequestedEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs* args)
|
|
|
|
{
|
|
|
|
args->put_Handled(true);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2NewWindowRequestedEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2NewWindowRequestedEventArgs2* args)
|
|
|
|
{
|
|
|
|
args->put_Handled(true);
|
|
|
|
|
|
|
|
if (m_loadUrlCallback)
|
|
|
|
{
|
|
|
|
const auto uriString = getUriStringFromArgs(args);
|
|
|
|
m_loadUrlCallback(uriString);
|
|
|
|
}
|
|
|
|
args->put_Handled(true);
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
2021-10-04 20:50:04 +08:00
|
|
|
// ICoreWebView2DOMContentLoadedEventHandler
|
2021-10-04 15:55:01 +08:00
|
|
|
HRESULT STDMETHODCALLTYPE Invoke(ICoreWebView2* sender, ICoreWebView2DOMContentLoadedEventArgs* args)
|
|
|
|
{
|
|
|
|
return S_OK;
|
|
|
|
}
|
2021-10-04 20:50:04 +08:00
|
|
|
|
2021-10-04 15:55:01 +08:00
|
|
|
private:
|
|
|
|
HWND m_window;
|
|
|
|
msg_cb_t m_msgCb;
|
|
|
|
webview2_com_handler_cb_t m_cb;
|
2021-12-31 12:12:40 +08:00
|
|
|
std::function<bool(std::string_view)> m_navStartingCallback;
|
2021-10-04 15:55:01 +08:00
|
|
|
std::function<void()> m_navCompleteCallback;
|
|
|
|
std::function<void()> m_navErrorCallback;
|
2021-12-31 12:12:40 +08:00
|
|
|
std::function<void(std::string_view url)> m_loadUrlCallback;
|
2021-10-04 15:55:01 +08:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
namespace ui
|
|
|
|
{
|
|
|
|
WebViewImpl::WebViewImpl(WebView* webView) : _createSucceeded(false), _systemWebControl(nullptr), _webView(webView)
|
|
|
|
{
|
|
|
|
_systemWebControl = new Win32WebControl();
|
|
|
|
if (_systemWebControl == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
_createSucceeded = _systemWebControl->createWebView(
|
2021-12-31 12:12:40 +08:00
|
|
|
[this](std::string_view url) -> bool {
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto shouldStartLoading = _webView->getOnShouldStartLoading();
|
|
|
|
if (shouldStartLoading != nullptr)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return shouldStartLoading(_webView, url);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
return true;
|
|
|
|
},
|
2021-12-31 12:12:40 +08:00
|
|
|
[this](std::string_view url) {
|
2021-12-25 10:04:45 +08:00
|
|
|
WebView::ccWebViewCallback didFinishLoading = _webView->getOnDidFinishLoading();
|
|
|
|
if (didFinishLoading != nullptr)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
didFinishLoading(_webView, url);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
},
|
2021-12-31 12:12:40 +08:00
|
|
|
[this](std::string_view url) {
|
2021-12-25 10:04:45 +08:00
|
|
|
WebView::ccWebViewCallback didFailLoading = _webView->getOnDidFailLoading();
|
|
|
|
if (didFailLoading != nullptr)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
didFailLoading(_webView, url);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
},
|
2021-12-31 12:12:40 +08:00
|
|
|
[this](std::string_view url) {
|
2021-12-25 10:04:45 +08:00
|
|
|
WebView::ccWebViewCallback onJsCallback = _webView->getOnJSCallback();
|
|
|
|
if (onJsCallback != nullptr)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
onJsCallback(_webView, url);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
});
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
WebViewImpl::~WebViewImpl()
|
|
|
|
{
|
|
|
|
if (_systemWebControl != nullptr)
|
|
|
|
{
|
|
|
|
_systemWebControl->removeWebView();
|
|
|
|
delete _systemWebControl;
|
|
|
|
_systemWebControl = nullptr;
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void WebViewImpl::loadData(const Data& data,
|
2021-12-31 12:12:40 +08:00
|
|
|
std::string_view MIMEType,
|
|
|
|
std::string_view encoding,
|
|
|
|
std::string_view baseURL)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
const std::string dataString(reinterpret_cast<char*>(data.getBytes()),
|
|
|
|
static_cast<unsigned int>(data.getSize()));
|
|
|
|
const auto url = getDataURI(dataString, MIMEType);
|
|
|
|
_systemWebControl->loadURL(url, false);
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadHTMLString(std::string_view string, std::string_view baseURL)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
if (string.empty())
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_systemWebControl->loadHTMLString("data:text/html," + utils::urlEncode("<html></html>"), baseURL);
|
|
|
|
return;
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
const auto html = htmlFromUri(string);
|
|
|
|
if (!html.empty())
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_systemWebControl->loadHTMLString("data:text/html," + utils::urlEncode(html), baseURL);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
else
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
_systemWebControl->loadHTMLString(string, baseURL);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadURL(std::string_view url, bool cleanCachedData)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->loadURL(url, cleanCachedData);
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::loadFile(std::string_view fileName)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
const auto fullPath = FileUtils::getInstance()->fullPathForFilename(fileName);
|
|
|
|
_systemWebControl->loadFile(fullPath);
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void WebViewImpl::stopLoading()
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->stopLoading();
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void WebViewImpl::reload()
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->reload();
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool WebViewImpl::canGoBack()
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
return _systemWebControl->canGoBack();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
bool WebViewImpl::canGoForward()
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
return _systemWebControl->canGoForward();
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void WebViewImpl::goBack()
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->goBack();
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void WebViewImpl::goForward()
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->goForward();
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::setJavascriptInterfaceScheme(std::string_view scheme)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->setJavascriptInterfaceScheme(scheme);
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void WebViewImpl::evaluateJS(std::string_view js)
|
2021-12-25 10:04:45 +08:00
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->evaluateJS(js);
|
|
|
|
}
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void WebViewImpl::setScalesPageToFit(const bool scalesPageToFit)
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->setScalesPageToFit(scalesPageToFit);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::draw(Renderer* renderer, Mat4 const& transform, uint32_t flags)
|
|
|
|
{
|
|
|
|
if (_createSucceeded && (flags & Node::FLAGS_TRANSFORM_DIRTY))
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
const auto uiRect = ax::ui::Helper::convertBoundingBoxToScreen(_webView);
|
2021-12-25 10:04:45 +08:00
|
|
|
_systemWebControl->setWebViewRect(static_cast<int>(uiRect.origin.x), static_cast<int>(uiRect.origin.y),
|
|
|
|
static_cast<int>(uiRect.size.width), static_cast<int>(uiRect.size.height));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setVisible(bool visible)
|
|
|
|
{
|
|
|
|
if (_createSucceeded)
|
|
|
|
{
|
|
|
|
_systemWebControl->setWebViewVisible(visible);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setBounces(bool bounces)
|
|
|
|
{
|
|
|
|
_systemWebControl->setBounces(bounces);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setOpacityWebView(float opacity)
|
|
|
|
{
|
|
|
|
_systemWebControl->setOpacityWebView(opacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
float WebViewImpl::getOpacityWebView() const
|
|
|
|
{
|
|
|
|
return _systemWebControl->getOpacityWebView();
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebViewImpl::setBackgroundTransparent()
|
|
|
|
{
|
|
|
|
_systemWebControl->setBackgroundTransparent();
|
|
|
|
}
|
|
|
|
} // namespace ui
|
2022-08-29 20:51:22 +08:00
|
|
|
NS_AX_END // namespace ax
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// Implement Win32WebControl
|
|
|
|
//
|
|
|
|
bool Win32WebControl::s_isInitialized = false;
|
|
|
|
|
|
|
|
void Win32WebControl::lazyInit()
|
|
|
|
{
|
|
|
|
// reset the main windows style so that its drawing does not cover the webview sub window
|
2022-08-08 18:02:17 +08:00
|
|
|
auto hwnd = ax::Director::getInstance()->getOpenGLView()->getWin32Window();
|
2021-10-04 15:55:01 +08:00
|
|
|
const auto style = GetWindowLong(hwnd, GWL_STYLE);
|
|
|
|
SetWindowLong(hwnd, GWL_STYLE, style | WS_CLIPCHILDREN);
|
|
|
|
|
|
|
|
CoInitialize(NULL);
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
Win32WebControl::Win32WebControl() : _shouldStartLoading(nullptr), _didFinishLoading(nullptr), _didFailLoading(nullptr)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
if (!s_isInitialized)
|
|
|
|
{
|
|
|
|
lazyInit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
bool Win32WebControl::createWebView(const std::function<bool(std::string_view)>& shouldStartLoading,
|
|
|
|
const std::function<void(std::string_view)>& didFinishLoading,
|
|
|
|
const std::function<void(std::string_view)>& didFailLoading,
|
|
|
|
const std::function<void(std::string_view)>& onJsCallback)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
bool ret = false;
|
|
|
|
do
|
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
HWND hwnd = ax::Director::getInstance()->getOpenGLView()->getWin32Window();
|
2021-10-04 15:55:01 +08:00
|
|
|
HINSTANCE hInstance = GetModuleHandle(nullptr);
|
|
|
|
WNDCLASSEX wc;
|
|
|
|
ZeroMemory(&wc, sizeof(WNDCLASSEX));
|
|
|
|
wc.cbSize = sizeof(WNDCLASSEX);
|
|
|
|
wc.hInstance = hInstance;
|
|
|
|
wc.lpszClassName = L"webview";
|
|
|
|
wc.hIcon = nullptr;
|
|
|
|
wc.hIconSm = nullptr;
|
2021-12-25 10:04:45 +08:00
|
|
|
// wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
|
|
|
|
wc.lpfnWndProc = (WNDPROC)(+[](HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) -> int {
|
2021-10-04 15:55:01 +08:00
|
|
|
auto w = (Win32WebControl*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
|
|
|
|
if (!w)
|
|
|
|
{
|
|
|
|
return DefWindowProc(hwnd, msg, wp, lp);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (msg)
|
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
case WM_SIZE:
|
|
|
|
w->resize(hwnd);
|
|
|
|
break;
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case WM_CLOSE:
|
|
|
|
DestroyWindow(hwnd);
|
|
|
|
break;
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
case WM_GETMINMAXINFO:
|
|
|
|
{
|
|
|
|
auto lpmmi = (LPMINMAXINFO)lp;
|
|
|
|
if (w == nullptr)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-12-25 10:04:45 +08:00
|
|
|
return 0;
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
2021-12-25 10:04:45 +08:00
|
|
|
if (w->m_maxsz.x > 0 && w->m_maxsz.y > 0)
|
|
|
|
{
|
|
|
|
lpmmi->ptMaxSize = w->m_maxsz;
|
|
|
|
lpmmi->ptMaxTrackSize = w->m_maxsz;
|
|
|
|
}
|
|
|
|
if (w->m_minsz.x > 0 && w->m_minsz.y > 0)
|
|
|
|
{
|
|
|
|
lpmmi->ptMinTrackSize = w->m_minsz;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
default:
|
|
|
|
return DefWindowProc(hwnd, msg, wp, lp);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
RegisterClassEx(&wc);
|
2021-12-25 10:04:45 +08:00
|
|
|
m_window = CreateWindow(L"webview", L"", WS_VISIBLE | WS_CHILD, 0, 0, 0, 0, hwnd, nullptr, hInstance, nullptr);
|
2021-10-04 15:55:01 +08:00
|
|
|
SetWindowLongPtr(m_window, GWLP_USERDATA, (LONG_PTR)this);
|
|
|
|
|
|
|
|
ShowWindow(m_window, SW_SHOW);
|
|
|
|
UpdateWindow(m_window);
|
2021-10-04 21:14:50 +08:00
|
|
|
SetFocus(m_window);
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
auto cb = [this](std::string_view msg) { on_message(msg); };
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
if (!embed(m_window, false, cb))
|
|
|
|
{
|
2022-07-16 10:43:05 +08:00
|
|
|
AXLOG("Cannot create edge chromium webview");
|
2021-10-04 15:55:01 +08:00
|
|
|
ret = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
resize(m_window);
|
|
|
|
ret = true;
|
|
|
|
} while (0);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
removeWebView();
|
|
|
|
}
|
|
|
|
|
|
|
|
_shouldStartLoading = shouldStartLoading;
|
2021-12-25 10:04:45 +08:00
|
|
|
_didFinishLoading = didFinishLoading;
|
|
|
|
_didFailLoading = didFailLoading;
|
|
|
|
_onJsCallback = onJsCallback;
|
2021-10-04 15:55:01 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::removeWebView()
|
|
|
|
{
|
|
|
|
m_controller->Close();
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
m_controller = nullptr;
|
|
|
|
m_webview = nullptr;
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::setWebViewRect(const int left, const int top, const int width, const int height)
|
|
|
|
{
|
|
|
|
if (m_controller == nullptr)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RECT r;
|
|
|
|
r.left = left;
|
|
|
|
r.top = top;
|
|
|
|
r.right = left + width;
|
|
|
|
r.bottom = top - height;
|
|
|
|
AdjustWindowRect(&r, WS_CHILD | WS_VISIBLE, 0);
|
|
|
|
SetWindowPos(m_window, nullptr, left, top, width, height, SWP_NOZORDER);
|
|
|
|
|
2021-10-06 21:37:37 +08:00
|
|
|
m_controller->put_ZoomFactor(_scalesPageToFit ? getDeviceScaleFactor() : 1.0);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Win32WebControl::setJavascriptInterfaceScheme(std::string_view scheme)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
m_jsScheme = scheme;
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Win32WebControl::loadHTMLString(std::string_view html, std::string_view baseURL)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
if (!html.empty())
|
|
|
|
{
|
|
|
|
const auto whtml = to_lpwstr(html);
|
|
|
|
m_webview->NavigateToString(whtml);
|
|
|
|
delete[] whtml;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Win32WebControl::loadURL(std::string_view url, bool cleanCachedData)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2021-10-04 21:04:30 +08:00
|
|
|
if (cleanCachedData)
|
|
|
|
{
|
|
|
|
m_webview->CallDevToolsProtocolMethod(L"Network.clearBrowserCache", L"{}", nullptr);
|
|
|
|
}
|
2021-10-04 15:55:01 +08:00
|
|
|
navigate(url);
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Win32WebControl::loadFile(std::string_view filePath)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
2022-08-08 18:02:17 +08:00
|
|
|
auto fullPath = ax::FileUtils::getInstance()->fullPathForFilename(filePath);
|
2021-10-04 15:55:01 +08:00
|
|
|
if (fullPath.find("file:///") != 0)
|
|
|
|
{
|
|
|
|
if (fullPath[0] == '/')
|
|
|
|
{
|
|
|
|
fullPath = "file://" + fullPath;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fullPath = "file:///" + fullPath;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loadURL(fullPath, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::stopLoading()
|
|
|
|
{
|
|
|
|
m_webview->Stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::reload() const
|
|
|
|
{
|
|
|
|
m_webview->Reload();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Win32WebControl::canGoBack() const
|
|
|
|
{
|
|
|
|
BOOL result;
|
2021-10-05 06:22:48 +08:00
|
|
|
return SUCCEEDED(m_webview->get_CanGoBack(&result)) && result;
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Win32WebControl::canGoForward() const
|
|
|
|
{
|
|
|
|
BOOL result;
|
2021-10-05 06:22:48 +08:00
|
|
|
return SUCCEEDED(m_webview->get_CanGoForward(&result)) && result;
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::goBack() const
|
|
|
|
{
|
|
|
|
m_webview->GoBack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::goForward() const
|
|
|
|
{
|
|
|
|
m_webview->GoForward();
|
|
|
|
}
|
|
|
|
|
2021-12-31 12:12:40 +08:00
|
|
|
void Win32WebControl::evaluateJS(std::string_view js)
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
eval(js);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::setScalesPageToFit(const bool scalesPageToFit)
|
|
|
|
{
|
|
|
|
_scalesPageToFit = scalesPageToFit;
|
2021-10-06 21:37:37 +08:00
|
|
|
m_controller->put_ZoomFactor(_scalesPageToFit ? getDeviceScaleFactor() : 1.0);
|
2021-10-04 15:55:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::setWebViewVisible(const bool visible) const
|
|
|
|
{
|
|
|
|
ShowWindow(m_window, visible ? SW_SHOW : SW_HIDE);
|
|
|
|
if (!visible)
|
|
|
|
{
|
|
|
|
// When the frame window is minimized set the browser window size to 0x0 to
|
|
|
|
// reduce resource usage.
|
|
|
|
SetWindowPos(m_window, nullptr, 0, 0, 0, 0, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Win32WebControl::setBounces(bool bounces) {}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
void Win32WebControl::setOpacityWebView(float opacity) {}
|
2021-10-04 15:55:01 +08:00
|
|
|
|
|
|
|
float Win32WebControl::getOpacityWebView() const
|
|
|
|
{
|
|
|
|
return 1.0f;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Win32WebControl::setBackgroundTransparent()
|
|
|
|
{
|
|
|
|
ICoreWebView2Controller2* viewController2;
|
2021-12-25 10:04:45 +08:00
|
|
|
if (SUCCEEDED(m_controller->QueryInterface(__uuidof(ICoreWebView2Controller2),
|
|
|
|
reinterpret_cast<void**>(&viewController2))))
|
2021-10-04 15:55:01 +08:00
|
|
|
{
|
|
|
|
COREWEBVIEW2_COLOR color;
|
|
|
|
viewController2->get_DefaultBackgroundColor(&color);
|
2021-12-25 10:04:45 +08:00
|
|
|
color.A = 0; // Only supports 0 or 255. Other values not supported
|
2021-10-04 15:55:01 +08:00
|
|
|
viewController2->put_DefaultBackgroundColor(color);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-16 10:43:05 +08:00
|
|
|
#endif // AX_TARGET_PLATFORM == AX_PLATFORM_WIN32
|