Skip to content

Commit

Permalink
Add headers argument for webviews
Browse files Browse the repository at this point in the history
  • Loading branch information
kin4stat committed Oct 29, 2024
1 parent 4b8e59b commit 7cbcc5d
Showing 1 changed file with 120 additions and 46 deletions.
166 changes: 120 additions & 46 deletions client/src/bindings/WebView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,69 +124,143 @@ static void Constructor(const v8::FunctionCallbackInfo<v8::Value>& info)
V8_CHECK_CONSTRUCTOR();
V8_CHECK_ARGS_LEN_MIN_MAX(1, 4);

V8_ARG_TO_STRING(1, url);

alt::IResource* altres = V8ResourceImpl::GetResource(isolate->GetEnteredOrMicrotaskContext());
V8_CHECK(altres, "invalid resource");

alt::IWebView* view = nullptr;

if(info.Length() == 4)
if (info.Length() == 1 && info[0]->IsObject())
{
V8_ARG_TO_BOOLEAN(2, isOverlayBool);
V8_ARG_TO_OBJECT(3, pos);
V8_ARG_TO_OBJECT(4, size);

V8_OBJECT_GET_INT(pos, "x", posX);
V8_OBJECT_GET_INT(pos, "y", posY);
V8_ARG_TO_OBJECT(1, params);
using values_t = std::unordered_map<std::string, v8::Local<v8::Value>>;
std::optional<values_t> values = V8Helpers::CppValue<v8::Local<v8::Value>>(params);
V8_CHECK(values.has_value(), "Params are empty");
V8_CHECK(!values->empty(), "Params are empty");

std::unordered_map<std::string, std::string> headers;
int posX = 0;
int posY = 0;
int sizeX = 0;
int sizeY = 0;
int drawableHash = 0;
std::string targetTextureStr;
bool isOverlay = false;
bool onTextureView = false;
V8_TO_STRING(values->at("url"), url);

if (values->contains("pos"))
{
V8_TO_OBJECT(values->at("pos"), pos);
V8_OBJECT_GET_INT(pos, "x", v8posX);
V8_OBJECT_GET_INT(pos, "y", v8posY);
posX = v8posX;
posY = v8posY;
}
if (values->contains("size"))
{
V8_TO_OBJECT(values->at("size"), size);
V8_OBJECT_GET_INT(size, "x", v8sizeX);
V8_OBJECT_GET_INT(size, "y", v8sizeY);
sizeX = v8sizeX;
sizeY = v8sizeY;
}
if (values->contains("isOverlay"))
{
V8_TO_BOOLEAN(values->at("isOverlay"), v8isOverlay);
isOverlay = v8isOverlay;
}
if (values->contains("drawableHash"))
{
V8_CHECK(values->contains("targetTexture"), "targetTexture should be specified for drawableHash");
V8_TO_INTEGER(values->at("drawableHash"), v8drawableHash);
drawableHash = v8drawableHash;
onTextureView = true;
}
if (values->contains("targetTexture"))
{
V8_CHECK(values->contains("drawableHash"), "drawableHash should be specified for targetTexture");
V8_TO_STRING(values->at("targetTexture"), v8targetTexture);
targetTextureStr = v8targetTexture;
}
if (values->contains("headers"))
{
V8_TO_OBJECT(values->at("headers"), v8headers);
auto headers_opt = V8Helpers::CppValue<std::string>(v8headers);
V8_CHECK(headers_opt.has_value(), "headers object is empty");
headers = std::move(headers_opt).value();
}

V8_OBJECT_GET_INT(size, "x", sizeX);
V8_OBJECT_GET_INT(size, "y", sizeY);
if (onTextureView)
{
auto texture = alt::ICore::Instance().GetTextureFromDrawable(drawableHash, targetTextureStr);
V8_CHECK(texture != nullptr, "Texture not found");

view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { sizeX, sizeY }, true, isOverlayBool, altres);
}
else if(info.Length() == 3 && info[2]->IsObject())
view = alt::ICore::Instance().CreateWebView(url, (uint32_t)drawableHash, targetTextureStr, altres, headers);
V8_CHECK(view, "Interactive WebView cannot be created");
} else
{
view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { sizeX, sizeY }, true, isOverlay, altres, headers);
}
} else
{
V8_ARG_TO_OBJECT(2, pos);
V8_ARG_TO_OBJECT(3, size);
V8_ARG_TO_STRING(1, url);
if(info.Length() == 4)
{
V8_ARG_TO_BOOLEAN(2, isOverlayBool);
V8_ARG_TO_OBJECT(3, pos);
V8_ARG_TO_OBJECT(4, size);

V8_OBJECT_GET_INT(pos, "x", posX);
V8_OBJECT_GET_INT(pos, "y", posY);
V8_OBJECT_GET_INT(pos, "x", posX);
V8_OBJECT_GET_INT(pos, "y", posY);

V8_OBJECT_GET_INT(size, "x", sizeX);
V8_OBJECT_GET_INT(size, "y", sizeY);
V8_OBJECT_GET_INT(size, "x", sizeX);
V8_OBJECT_GET_INT(size, "y", sizeY);

view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { sizeX, sizeY }, true, false, altres);
}
else if(info.Length() == 3)
{
V8_ARG_TO_INT(2, drawableHash);
V8_ARG_TO_STRING(3, targetTextureStr);
view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { sizeX, sizeY }, true, isOverlayBool, altres);
}
else if(info.Length() == 3 && info[2]->IsObject())
{
V8_ARG_TO_OBJECT(2, pos);
V8_ARG_TO_OBJECT(3, size);

auto texture = alt::ICore::Instance().GetTextureFromDrawable(drawableHash, targetTextureStr);
V8_CHECK(texture != nullptr, "Texture not found");
V8_OBJECT_GET_INT(pos, "x", posX);
V8_OBJECT_GET_INT(pos, "y", posY);

view = alt::ICore::Instance().CreateWebView(url, (uint32_t)drawableHash, targetTextureStr, altres);
V8_CHECK(view, "Interactive WebView cannot be created");
}
else if(info.Length() == 2 && info[1]->IsObject())
{
V8_ARG_TO_OBJECT(2, pos);
V8_OBJECT_GET_INT(size, "x", sizeX);
V8_OBJECT_GET_INT(size, "y", sizeY);

V8_OBJECT_GET_INT(pos, "x", posX);
V8_OBJECT_GET_INT(pos, "y", posY);
view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { sizeX, sizeY }, true, false, altres);
}
else if(info.Length() == 3)
{
V8_ARG_TO_INT(2, drawableHash);
V8_ARG_TO_STRING(3, targetTextureStr);

view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { 0, 0 }, true, false, altres);
}
else if(info.Length() == 2)
{
V8_ARG_TO_BOOLEAN(2, isOverlayBool);
auto texture = alt::ICore::Instance().GetTextureFromDrawable(drawableHash, targetTextureStr);
V8_CHECK(texture != nullptr, "Texture not found");

view = alt::ICore::Instance().CreateWebView(url, { 0, 0 }, { 0, 0 }, true, isOverlayBool, altres);
}
else
{
view = alt::ICore::Instance().CreateWebView(url, { 0, 0 }, { 0, 0 }, true, false, altres);
view = alt::ICore::Instance().CreateWebView(url, (uint32_t)drawableHash, targetTextureStr, altres);
V8_CHECK(view, "Interactive WebView cannot be created");
}
else if(info.Length() == 2 && info[1]->IsObject())
{
V8_ARG_TO_OBJECT(2, pos);

V8_OBJECT_GET_INT(pos, "x", posX);
V8_OBJECT_GET_INT(pos, "y", posY);

view = alt::ICore::Instance().CreateWebView(url, { posX, posY }, { 0, 0 }, true, false, altres);
}
else if(info.Length() == 2)
{
V8_ARG_TO_BOOLEAN(2, isOverlayBool);

view = alt::ICore::Instance().CreateWebView(url, { 0, 0 }, { 0, 0 }, true, isOverlayBool, altres);
}
else
{
view = alt::ICore::Instance().CreateWebView(url, { 0, 0 }, { 0, 0 }, true, false, altres);
}
}

V8_BIND_BASE_OBJECT(view, "Failed to create WebView");
Expand Down

0 comments on commit 7cbcc5d

Please sign in to comment.