1/**************************************************************************/
2/* api.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "api.h"
32
33#include "javascript_bridge_singleton.h"
34#include "web_tools_editor_plugin.h"
35
36#include "core/config/engine.h"
37
38static JavaScriptBridge *javascript_bridge_singleton;
39
40void register_web_api() {
41 WebToolsEditorPlugin::initialize();
42 GDREGISTER_ABSTRACT_CLASS(JavaScriptObject);
43 GDREGISTER_ABSTRACT_CLASS(JavaScriptBridge);
44 javascript_bridge_singleton = memnew(JavaScriptBridge);
45 Engine::get_singleton()->add_singleton(Engine::Singleton("JavaScriptBridge", javascript_bridge_singleton));
46}
47
48void unregister_web_api() {
49 memdelete(javascript_bridge_singleton);
50}
51
52JavaScriptBridge *JavaScriptBridge::singleton = nullptr;
53
54JavaScriptBridge *JavaScriptBridge::get_singleton() {
55 return singleton;
56}
57
58JavaScriptBridge::JavaScriptBridge() {
59 ERR_FAIL_COND_MSG(singleton != nullptr, "JavaScriptBridge singleton already exist.");
60 singleton = this;
61}
62
63JavaScriptBridge::~JavaScriptBridge() {}
64
65void JavaScriptBridge::_bind_methods() {
66 ClassDB::bind_method(D_METHOD("eval", "code", "use_global_execution_context"), &JavaScriptBridge::eval, DEFVAL(false));
67 ClassDB::bind_method(D_METHOD("get_interface", "interface"), &JavaScriptBridge::get_interface);
68 ClassDB::bind_method(D_METHOD("create_callback", "callable"), &JavaScriptBridge::create_callback);
69 {
70 MethodInfo mi;
71 mi.name = "create_object";
72 mi.arguments.push_back(PropertyInfo(Variant::STRING, "object"));
73 ClassDB::bind_vararg_method(METHOD_FLAGS_DEFAULT, "create_object", &JavaScriptBridge::_create_object_bind, mi);
74 }
75 ClassDB::bind_method(D_METHOD("download_buffer", "buffer", "name", "mime"), &JavaScriptBridge::download_buffer, DEFVAL("application/octet-stream"));
76 ClassDB::bind_method(D_METHOD("pwa_needs_update"), &JavaScriptBridge::pwa_needs_update);
77 ClassDB::bind_method(D_METHOD("pwa_update"), &JavaScriptBridge::pwa_update);
78 ClassDB::bind_method(D_METHOD("force_fs_sync"), &JavaScriptBridge::force_fs_sync);
79 ADD_SIGNAL(MethodInfo("pwa_update_available"));
80}
81
82#if !defined(WEB_ENABLED) || !defined(JAVASCRIPT_EVAL_ENABLED)
83
84Variant JavaScriptBridge::eval(const String &p_code, bool p_use_global_exec_context) {
85 return Variant();
86}
87
88Ref<JavaScriptObject> JavaScriptBridge::get_interface(const String &p_interface) {
89 return Ref<JavaScriptObject>();
90}
91
92Ref<JavaScriptObject> JavaScriptBridge::create_callback(const Callable &p_callable) {
93 return Ref<JavaScriptObject>();
94}
95
96Variant JavaScriptBridge::_create_object_bind(const Variant **p_args, int p_argcount, Callable::CallError &r_error) {
97 if (p_argcount < 1) {
98 r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
99 r_error.argument = 0;
100 return Ref<JavaScriptObject>();
101 }
102 if (p_args[0]->get_type() != Variant::STRING) {
103 r_error.error = Callable::CallError::CALL_ERROR_INVALID_ARGUMENT;
104 r_error.argument = 0;
105 r_error.expected = Variant::STRING;
106 return Ref<JavaScriptObject>();
107 }
108 return Ref<JavaScriptObject>();
109}
110
111#endif
112
113#if !defined(WEB_ENABLED)
114
115bool JavaScriptBridge::pwa_needs_update() const {
116 return false;
117}
118
119Error JavaScriptBridge::pwa_update() {
120 return ERR_UNAVAILABLE;
121}
122
123void JavaScriptBridge::force_fs_sync() {
124}
125
126void JavaScriptBridge::download_buffer(Vector<uint8_t> p_arr, const String &p_name, const String &p_mime) {
127}
128
129#endif
130