| 5 |
| #include "display_list.h" |
| 6 | #include "filters.h" |
| 7 | #include "renderer.h" |
| 8 | #include "twidget/gesture_policy.h" |
| 9 | #include "twidget/widget_host.h" |
| 10 | #include <algorithm> |
| 11 | #include <atomic> |
| 12 | #include <chrono> |
| 13 | #include <cmath> |
| 14 | #include <cstring> |
| 15 | #include <jni.h> |
| 16 | #include <map> |
| 17 | #include <mutex> |
| 18 | #include <optional> |
| 19 | #include <stdexcept> |
| 20 | #include <unordered_map> |
| 21 | #ifdef __ANDROID__ |
| 22 | #include <android/bitmap.h> |
| 23 | #include <android/trace.h> |
| 24 | #endif |
| 25 |
| 26 | namespace { |
| 27 | // Debug: the conformance runner (WidgetConformanceRunner) reads each widget's `data-conformance` verdict. |
| 28 | std::atomic<bool> conformanceReports{false}; |
| 29 | // Debug (the stress kit's measure pass, packages/stress/CLIENTS.md): limits every new session takes instead of the |
| 30 | // defaults, as JSON {"policy","cpuQuotaMs","cpuQuotaWindowMs","mainThreadQuotaMs","maxTimers","memoryLimitBytes"}. |
| 31 | std::mutex limitsMutex; |
| 32 | std::string limitsOverride; |
| 33 | // Debug: the attack runner (WidgetAttackRunner) hears of every URL that could reach the network (signal 10). |
| 34 | std::atomic<bool> attackScan{false}; |
| 35 |
| 36 | // widget-sandbox's attack check (packages/desktop/harness/main.cpp, `twidget-run --attacks`) on the ops this |
| 37 | // renderer gets: anything but data: and #fragment URLs in them is a leak. A URL inside a data: payload is inert |
| 38 | // (an image may not fetch), and in CSS only the url() family and @import can fetch. |
| 39 | bool mentionsNetwork(std::string l) { |
| 40 | std::transform(l.begin(), l.end(), l.begin(), [](unsigned char c) { return char(std::tolower(c)); }); |
| 41 | return l.find("127.0.0.1") != std::string::npos || l.find("http://") != std::string::npos || |
| 42 | l.find("https://") != std::string::npos || l.find("ws://") != std::string::npos || |
| 43 | l.find("wss://") != std::string::npos || l.rfind("//", 0) == 0; |
| 44 | } |
| 45 |
| 46 | void scanCss(const std::string &where, const std::string &css, std::vector<std::string> &leaks) { |
| 47 | std::string l = css; |
| 48 | std::transform(l.begin(), l.end(), l.begin(), [](unsigned char c) { return char(std::tolower(c)); }); |
| 49 | if (l.find("@import") != std::string::npos) |
| 50 | leaks.push_back(where + ": @import survived: " + css.substr(0, 160)); |
| 51 | static const char *const fns[] = {"url(", "image-set(", "image(", "src(", "cross-fade("}; |
| 52 | for (const char *fn : fns) { |
| 53 | size_t len = std::strlen(fn); |
| 54 | for (size_t pos = l.find(fn); pos != std::string::npos; pos = l.find(fn, pos + len)) { |
| 55 | if (pos > 0 && (std::isalnum((unsigned char)l[pos - 1]) || l[pos - 1] == '-')) |
| 56 | continue; |
| 57 | size_t a = pos + len; |
| 58 | while (a < l.size() && (l[a] == ' ' || l[a] == '\t' || l[a] == '\n' || l[a] == '"' || l[a] == '\'')) |
| 59 | a++; |
| 60 | std::string arg = l.substr(a, 12); |
| 61 | if (arg.rfind("data:", 0) == 0 || arg.rfind("#", 0) == 0) |
| 62 | continue; |
| 63 | if (std::strcmp(fn, "image-set(") == 0 && (arg.rfind("url(", 0) == 0 || arg.rfind("image(", 0) == 0)) |
| 64 | continue; |
| 65 | leaks.push_back(where + ": " + css.substr(pos, 160)); |
| 66 | } |
| 67 | } |
| 68 | } |
| 69 |
| 70 | void scanOp(const twidget::Json &op, std::vector<std::string> &leaks) { |
| 71 | const std::string &kind = op["op"].asString(); |
| 72 | auto attr = [&](const std::string &where, const std::string &value) { |
| 73 | if (value.rfind("data:", 0) != 0 && mentionsNetwork(value)) |
| 74 | leaks.push_back(where + ": " + value.substr(0, 160)); |
| 75 | }; |
| 76 | if (kind == "el") { |
| 77 | for (const auto &pair : op["attrs"].arr()) { |
| 78 | const std::string &name = pair[size_t(0)].asString(); |
| 79 | if (name == "style") |
| 80 | scanCss("el.style", pair[size_t(1)].asString(), leaks); |
| 81 | else |
| 82 | attr("el." + name, pair[size_t(1)].asString()); |
| 83 | } |
| 84 | } else if (kind == "attr") { |
| 85 | if (op["name"].asString() == "style") |
| 86 | scanCss("attr style", op["value"].asString(), leaks); |
| 87 | else |
| 88 | attr("attr " + op["name"].asString(), op["value"].asString()); |
| 89 | } else if (kind == "style") { |
| 90 | scanCss("style " + op["name"].asString(), op["value"].asString(), leaks); |
| 91 | } else if (kind == "css") { |
| 92 | scanCss("css", op["text"].asString(), leaks); |
| 93 | } else if (kind == "canvas") { |
| 94 | for (const auto &cmd : op["cmds"].arr()) |
| 95 | for (const auto &a : cmd.arr()) |
| 96 | if (a.isString()) { |
| 97 | scanCss("canvas", a.asString(), leaks); |
| 98 | attr("canvas", a.asString()); |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | JavaVM *vm = nullptr; |
| 103 |
| 104 | // Systrace sections (API 23, the client's minimum). |
| 105 | struct TraceScope { |
| 106 | explicit TraceScope(const char *name) { |
| 107 | #ifdef __ANDROID__ |
| 108 | ATrace_beginSection(name); |
| 109 | #endif |
| 110 | } |
| 111 | ~TraceScope() { |
| 112 | #ifdef __ANDROID__ |
| 113 | ATrace_endSection(); |
| 114 | #endif |
| 115 | } |
| 116 | }; |
| 117 |
| 118 | struct ThreadEnv { |
| 119 | JNIEnv *env = nullptr; |
| 120 | bool attached = false; |
| 121 | ThreadEnv() { |
| 122 | if (vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6) != JNI_OK) { |
| 123 | #ifdef __ANDROID__ |
| 124 | attached = vm->AttachCurrentThread(&env, nullptr) == JNI_OK; |
| 125 | #else |
| 126 | attached = vm->AttachCurrentThread(reinterpret_cast<void **>(&env), nullptr) == JNI_OK; |
| 127 | #endif |
| 128 | } |
| 129 | } |
| 130 | ~ThreadEnv() { |
| 131 | if (attached) |
| 132 | vm->DetachCurrentThread(); |
| 133 | } |
| 134 | }; |
| 135 | JNIEnv *env() { |
| 136 | thread_local ThreadEnv thread; |
| 137 | return thread.env; |
| 138 | } |
| 139 |
| 140 | std::string bytes(JNIEnv *e, jbyteArray value) { |
| 141 | if (!value) |
| 142 | return {}; |
| 143 | std::string result(e->GetArrayLength(value), '\0'); |
| 144 | e->GetByteArrayRegion(value, 0, result.size(), reinterpret_cast<jbyte *>(result.data())); |
| 145 | return result; |
| 146 | } |
| 147 | jbyteArray bytes(JNIEnv *e, const std::string &value) { |
| 148 | jbyteArray result = e->NewByteArray(value.size()); |
| 149 | if (result) |
| 150 | e->SetByteArrayRegion(result, 0, value.size(), reinterpret_cast<const jbyte *>(value.data())); |
| 151 | return result; |
| 152 | } |
| 153 | void check(JNIEnv *e) { |
| 154 | if (!e->ExceptionCheck()) |
| 155 | return; |
| 156 | e->ExceptionDescribe(); |
| 157 | e->ExceptionClear(); |
| 158 | throw std::runtime_error("Android renderer callback failed"); |
| 159 | } |
| 160 |
| 161 | // A canvas batch for WidgetCanvas#run as int words, floats by their bits, in place of twlite::to_json: JSON took most of |
| 162 | // a canvas batch on a weak phone. One op: its kind, its clips (but Reset and PutImage), then by kind, as written here. |
| 163 | void putFloat(std::vector<jint> &out, float value) { |
| 164 | jint bits; |
| 165 | std::memcpy(&bits, &value, sizeof(bits)); |
| 166 | out.push_back(bits); |
| 167 | } |
| 168 |
| 169 | jint argbOf(const twlite::Color &c) { |
| 170 | return jint((uint32_t(c.a) << 24) | (uint32_t(c.r) << 16) | (uint32_t(c.g) << 8) | c.b); |
| 171 | } |
| 172 |
| 173 | void putGeometry(std::vector<jint> &out, const twlite::Geometry &path) { |
| 174 | out.push_back(jint(path.ops.size())); |
| 175 | out.push_back(jint(path.points.size())); |
| 176 | for (auto op : path.ops) |
| 177 | out.push_back(jint(op)); |
| 178 | for (float point : path.points) |
| 179 | putFloat(out, point); |
| 180 | } |
| 181 |
| 182 | // Bytes, little-endian in each word, after their count. |
| 183 | void putBytes(std::vector<jint> &out, const uint8_t *bytes, size_t size) { |
| 184 | out.push_back(jint(size)); |
| 185 | size_t at = out.size(); |
| 186 | out.resize(at + (size + 3) / 4, 0); |
| 187 | if (size) |
| 188 | std::memcpy(out.data() + at, bytes, size); |
| 189 | } |
| 190 |
| 191 | void encodeCanvasOp(std::vector<jint> &out, const twlite::CanvasOp &op) { |
| 192 | using Op = twlite::CanvasOp; |
| 193 | out.push_back(jint(op.kind)); |
| 194 | if (op.kind == Op::Reset) |
| 195 | return; |
| 196 | if (op.kind == Op::PutImage) { |
| 197 | putFloat(out, op.dx); |
| 198 | putFloat(out, op.dy); |
| 199 | out.push_back(op.w); |
| 200 | out.push_back(op.h); |
| 201 | putBytes(out, op.rgba ? op.rgba->data() : nullptr, op.rgba ? op.rgba->size() : 0); |
| 202 | return; |
| 203 | } |
| 204 | out.push_back(op.clips ? jint(op.clips->size()) : 0); |
| 205 | if (op.clips) { |
| 206 | for (const auto &clip : *op.clips) { |
| 207 | putGeometry(out, clip.path); |
| 208 | out.push_back(clip.evenodd); |
| 209 | } |
| 210 | } |
| 211 | if (op.kind == Op::Fill || op.kind == Op::Stroke || op.kind == Op::Clear) |
| 212 | putGeometry(out, op.path); |
| 213 | if (op.kind == Op::Clear) |
| 214 | return; |
| 215 | out.push_back(op.evenodd); |
| 216 | for (float value : op.matrix) |
| 217 | putFloat(out, value); |
| 218 | putFloat(out, op.alpha); |
| 219 | out.push_back(jint(op.composite)); |
| 220 | out.push_back(op.has_shadow()); |
| 221 | if (op.has_shadow()) { |
| 222 | out.push_back(argbOf(op.shadow_color)); |
| 223 | putFloat(out, op.shadow_x); |
| 224 | putFloat(out, op.shadow_y); |
| 225 | putFloat(out, op.shadow_blur); |
| 226 | } |
| 227 | if (op.kind == Op::Fill || op.kind == Op::Stroke || op.kind == Op::Text) { |
| 228 | out.push_back(jint(op.paint.kind)); |
| 229 | if (op.paint.kind == twlite::CanvasPaint::Solid) { |
| 230 | out.push_back(argbOf(op.paint.color)); |
| 231 | } else { |
| 232 | for (float value : op.paint.args) |
| 233 | putFloat(out, value); |
| 234 | out.push_back(jint(op.paint.stops.size())); |
| 235 | for (const auto &stop : op.paint.stops) { |
| 236 | putFloat(out, stop.offset); |
| 237 | out.push_back(argbOf(stop.color)); |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | if (op.kind == Op::Text) { |
| 242 | out.push_back(op.font); |
| 243 | putBytes(out, reinterpret_cast<const uint8_t *>(op.text.data()), op.text.size()); |
| 244 | for (float value : {op.x, op.y, op.scale_x, op.text_dx}) |
| 245 | putFloat(out, value); |
| 246 | out.push_back(op.stroke_text); |
| 247 | } |
| 248 | if (op.kind == Op::Stroke || (op.kind == Op::Text && op.stroke_text)) { |
| 249 | putFloat(out, op.line_width); |
| 250 | out.push_back(op.line_cap); |
| 251 | out.push_back(op.line_join); |
| 252 | putFloat(out, op.miter_limit); |
| 253 | out.push_back(jint(op.dashes.size())); |
| 254 | for (float dash : op.dashes) |
| 255 | putFloat(out, dash); |
| 256 | putFloat(out, op.dash_offset); |
| 257 | } |
| 258 | if (op.kind == Op::Image) { |
| 259 | out.push_back(op.canvas); |
| 260 | out.push_back(op.image); |
| 261 | for (float value : {op.sx, op.sy, op.sw, op.sh, op.dx, op.dy, op.dw, op.dh}) |
| 262 | putFloat(out, value); |
| 263 | out.push_back(op.smoothing); |
| 264 | } |
| 265 | } |
| 266 |
| 267 | class AndroidBackend final : public twlite::Backend { |
| 268 | public: |
| 269 | AndroidBackend(JNIEnv *e, jobject value) : object_(e->NewGlobalRef(value)) { |
| 270 | jclass type = e->GetObjectClass(value); |
| 271 | font_ = e->GetMethodID(type, "createFont", "([BFIZ[B)[F"); |
| 272 | delete_ = e->GetMethodID(type, "deleteFont", "(I)V"); |
| 273 | width_ = e->GetMethodID(type, "textWidth", "(I[B)F"); |
| 274 | image_ = e->GetMethodID(type, "loadImage", "(I[B)[F"); |
| 275 | record_ = e->GetMethodID(type, "record", "([B[BDIIFFII)V"); |
| 276 | signal_ = e->GetMethodID(type, "signal", "(I[B)V"); |
| 277 | intl_ = e->GetMethodID(type, "formatIntl", "([B)[B"); |
| 278 | canvas_run_ = e->GetMethodID(type, "canvasRun", "(III[I)V"); |
| 279 | canvas_read_ = e->GetMethodID(type, "canvasRead", "(IIIII)[B"); |
| 280 | canvas_release_ = e->GetMethodID(type, "canvasRelease", "(I)V"); |
| 281 | layer_ = e->GetMethodID(type, "recordLayer", "(II[B)V"); |
| 282 | text_ = e->GetMethodID(type, "selectableText", "([B)V"); |
| 283 | e->DeleteLocalRef(type); |
| 284 | check(e); |
| 285 | } |
| 286 | #ifdef WIDGET_SANDBOX_TESTING |
| 287 | jobject test_object(JNIEnv *e) { return e->NewLocalRef(object_); } |
| 288 | #endif |
| 289 | ~AndroidBackend() override { env()->DeleteGlobalRef(object_); } |
| 290 | int create_font(const std::string &families, float size, int weight, bool italic, |
| 291 | const std::string &features, twlite::FontMetrics &out) override { |
| 292 | auto *e = env(); |
| 293 | auto text = bytes(e, families); |
| 294 | // OpenType features as `tag=value` pairs (`tnum=1,zero=1`), empty for the font's defaults. |
| 295 | auto settings = bytes(e, features); |
| 296 | auto values = static_cast<jfloatArray>( |
| 297 | e->CallObjectMethod(object_, font_, text, size, weight, jboolean(italic), settings)); |
| 298 | e->DeleteLocalRef(text); |
| 299 | e->DeleteLocalRef(settings); |
| 300 | check(e); |
| 301 | if (!values) |
| 302 | return 0; |
| 303 | jfloat data[6] = {}; |
| 304 | e->GetFloatArrayRegion(values, 0, 6, data); |
| 305 | e->DeleteLocalRef(values); |
| 306 | out = {data[1], data[2], data[3], data[4], data[5]}; |
| 307 | return int(data[0]); |
| 308 | } |
| 309 | void delete_font(int id) override { |
| 310 | auto *e = env(); |
| 311 | e->CallVoidMethod(object_, delete_, id); |
| 312 | // Called from document destructors: never let an exception escape. |
| 313 | if (e->ExceptionCheck()) { |
| 314 | e->ExceptionDescribe(); |
| 315 | e->ExceptionClear(); |
| 316 | } |
| 317 | } |
| 318 | float text_width(int id, const std::string &value) override { |
| 319 | auto *e = env(); |
| 320 | auto text = bytes(e, value); |
| 321 | float result = e->CallFloatMethod(object_, width_, id, text); |
| 322 | e->DeleteLocalRef(text); |
| 323 | check(e); |
| 324 | return result; |
| 325 | } |
| 326 | bool load_image(int id, const std::string &url, float &w, float &h) override { |
| 327 | // The backend loads data: URLs only; with the validator in front no other may come here. |
| 328 | if (attackScan && url.rfind("data:", 0) != 0) |
| 329 | signal(10, "renderer asked for " + url.substr(0, 160)); |
| 330 | auto *e = env(); |
| 331 | auto text = bytes(e, url); |
| 332 | auto values = static_cast<jfloatArray>(e->CallObjectMethod(object_, image_, id, text)); |
| 333 | e->DeleteLocalRef(text); |
| 334 | check(e); |
| 335 | if (!values) |
| 336 | return false; |
| 337 | jfloat data[2] = {}; |
| 338 | e->GetFloatArrayRegion(values, 0, 2, data); |
| 339 | e->DeleteLocalRef(values); |
| 340 | w = data[0]; |
| 341 | h = data[1]; |
| 342 | return w > 0 && h > 0; |
| 343 | } |
| 344 | std::string intl(const std::string &request) { |
| 345 | auto *e = env(); |
| 346 | auto text = bytes(e, request); |
| 347 | auto result = static_cast<jbyteArray>(e->CallObjectMethod(object_, intl_, text)); |
| 348 | e->DeleteLocalRef(text); |
| 349 | check(e); |
| 350 | auto value = bytes(e, result); |
| 351 | if (result) |
| 352 | e->DeleteLocalRef(result); |
| 353 | return value; |
| 354 | } |
| 355 | void signal(int kind, const std::string &value = {}) { |
| 356 | auto *e = env(); |
| 357 | auto text = bytes(e, value); |
| 358 | e->CallVoidMethod(object_, signal_, kind, text); |
| 359 | e->DeleteLocalRef(text); |
| 360 | check(e); |
| 361 | } |
| 362 | void canvas_run(int id, int w, int h, const std::vector<twlite::CanvasOp> &ops) override { |
| 363 | auto *e = env(); |
| 364 | canvasWords_.clear(); |
| 365 | for (const auto &op : ops) |
| 366 | encodeCanvasOp(canvasWords_, op); |
| 367 | auto data = e->NewIntArray(jsize(canvasWords_.size())); |
| 368 | if (!data) { |
| 369 | check(e); |
| 370 | return; |
| 371 | } |
| 372 | e->SetIntArrayRegion(data, 0, jsize(canvasWords_.size()), canvasWords_.data()); |
| 373 | e->CallVoidMethod(object_, canvas_run_, id, w, h, data); |
| 374 | e->DeleteLocalRef(data); |
| 375 | check(e); |
| 376 | } |
| 377 | bool canvas_read(int id, int x, int y, int w, int h, std::vector<uint8_t> &rgba) override { |
| 378 | auto *e = env(); |
| 379 | auto data = static_cast<jbyteArray>(e->CallObjectMethod(object_, canvas_read_, id, x, y, w, h)); |
| 380 | check(e); |
| 381 | if (!data) |
| 382 | return false; |
| 383 | size_t size = e->GetArrayLength(data); |
| 384 | bool ok = w > 0 && h > 0 && size == size_t(w) * size_t(h) * 4; |
| 385 | if (ok) { |
| 386 | rgba.resize(size); |
| 387 | e->GetByteArrayRegion(data, 0, size, reinterpret_cast<jbyte *>(rgba.data())); |
| 388 | } |
| 389 | e->DeleteLocalRef(data); |
| 390 | return ok; |
| 391 | } |
| 392 | void canvas_release(int id) override { |
| 393 | auto *e = env(); |
| 394 | e->CallVoidMethod(object_, canvas_release_, id); |
| 395 | // Called while the document is torn down: never let an exception escape. |
| 396 | if (e->ExceptionCheck()) { |
| 397 | e->ExceptionDescribe(); |
| 398 | e->ExceptionClear(); |
| 399 | } |
| 400 | } |
| 401 | // The text the widget made selectable (Renderer::selectable_text_json), before the frame it belongs to. |
| 402 | void selectable_text(const std::string &runs) { |
| 403 | auto *e = env(); |
| 404 | auto text = bytes(e, runs); |
| 405 | e->CallVoidMethod(object_, text_, text); |
| 406 | e->DeleteLocalRef(text); |
| 407 | check(e); |
| 408 | } |
| 409 | // A layer's content, recorded by the backend before the frame that names it. |
| 410 | void layer(int index, int version, const std::string &list) { |
| 411 | auto *e = env(); |
| 412 | auto text = bytes(e, list); |
| 413 | e->CallVoidMethod(object_, layer_, index, version, text); |
| 414 | e->DeleteLocalRef(text); |
| 415 | check(e); |
| 416 | } |
| 417 | void record(const std::string &list, const std::string &composition, double clock, int w, int h, float dpr, |
| 418 | const twlite::BuildStats &stats) { |
| 419 | auto *e = env(); |
| 420 | auto text = bytes(e, list); |
| 421 | auto layers = bytes(e, composition); |
| 422 | e->CallVoidMethod(object_, record_, text, layers, clock, w, h, dpr, float(stats.parse_ms), stats.passes, |
| 423 | stats.builds); |
| 424 | e->DeleteLocalRef(text); |
| 425 | e->DeleteLocalRef(layers); |
| 426 | check(e); |
| 427 | } |
| 428 |
| 429 | private: |
| 430 | jobject object_; |
| 431 | std::vector<jint> canvasWords_; |
| 432 | jmethodID font_, delete_, width_, image_, record_, signal_, intl_, canvas_run_, canvas_read_, canvas_release_, text_, |
| 433 | layer_; |
| 434 | }; |
| 435 |
| 436 | // Android ICU through Java; what it cannot answer (API < 24, ListFormatter < 26) the engine's own |
| 437 | // BasicIntl formats, English-style, rather than leaving the widget without a value. |
| 438 | class AndroidIntl final : public twidget::IntlProvider { |
| 439 | public: |
| 440 | explicit AndroidIntl(std::shared_ptr<AndroidBackend> backend) : backend_(std::move(backend)) {} |
| 441 | std::string format(const twidget::Json &request) override { |
| 442 | std::string answer; |
| 443 | try { |
| 444 | answer = backend_->intl(request.dump()); |
| 445 | } catch (...) { |
| 446 | } |
| 447 | return answer.empty() ? basic_.format(request) : answer; |
| 448 | } |
| 449 |
| 450 | private: |
| 451 | std::shared_ptr<AndroidBackend> backend_; |
| 452 | twidget::BasicIntl basic_; |
| 453 | }; |
| 454 |
| 455 | class AndroidRenderer final : public twidget::Renderer { |
| 456 | public: |
| 457 | AndroidRenderer(std::shared_ptr<AndroidBackend> backend, const std::string &theme, float width, float dpr) |
| 458 | : backend_(std::move(backend)), document_(width, dpr, theme == "dark", *backend_) { |
| 459 | // Transform and opacity animations become RenderNode layers the UI thread plays without native work. |
| 460 | document_.set_compositing(true); |
| 461 | } |
| 462 | void load() override { |
| 463 | guard([&] { |
| 464 | if (onMessage) |
| 465 | onMessage("{\"type\":\"ready\"}"); |
| 466 | dirty(); |
| 467 | }); |
| 468 | } |
| 469 | void send(const std::string &json) override { |
| 470 | guard([&] { |
| 471 | auto message = twidget::Json::parse(json); |
| 472 | if (!message) |
| 473 | return; |
| 474 | if ((*message)["type"].asString() == "requestFrame") { |
| 475 | backend_->signal(1); |
| 476 | return; |
| 477 | } |
| 478 | if ((*message)["type"].asString() == "frozen") |
| 479 | frozen_ = true; |
| 480 | if (conformanceReports) |
| 481 | reportConformance(*message); |
| 482 | TraceScope trace("WidgetSandbox:ReceiveLayout"); |
| 483 | double started = nowMs(); |
| 484 | if (frameSentAt_ > 0 && !(*message)["ops"].arr().empty()) { |
| 485 | opsLatencyMs_ += started - frameSentAt_; |
| 486 | opsLatencies_++; |
| 487 | frameSentAt_ = 0; |
| 488 | } |
| 489 | dispatch(document_.receive(*message)); |
| 490 | receiveMs_ += nowMs() - started; |
| 491 | dirty(); |
| 492 | }); |
| 493 | } |
| 494 | // The conformance set's widgets publish their verdict in the root's `data-conformance` attribute |
| 495 | // (packages/conformance/CLIENTS.md): every new value goes to the backend as signal 9. |
| 496 | // The stress kit's widgets publish `data-stress` the same way (signal 16), and its runner counts the validated ops |
| 497 | // of every batch (signal 17). |
| 498 | void reportConformance(const twidget::Json &message) { |
| 499 | const auto &ops = message["ops"].arr(); |
| 500 | if (!ops.empty()) |
| 501 | backend_->signal(17, std::to_string(ops.size())); |
| 502 | for (const auto &op : ops) { |
| 503 | for (const char *name : {"data-conformance", "data-stress"}) { |
| 504 | const twidget::Json *value = nullptr; |
| 505 | if (op["name"].asString() == name) |
| 506 | value = op.find("value"); |
| 507 | else if (const auto *attrs = op.find("attrs")) |
| 508 | for (const auto &kv : attrs->arr()) |
| 509 | if (kv[size_t(0)].asString() == name) |
| 510 | value = &kv[size_t(1)]; |
| 511 | if (value && value->isString()) |
| 512 | backend_->signal(name[5] == 'c' ? 9 : 16, value->asString()); |
| 513 | } |
| 514 | } |
| 515 | } |
| 516 | void setTheme(const std::string &theme) override { |
| 517 | guard([&] { |
| 518 | std::vector<twidget::Json> out; |
| 519 | document_.set_theme(theme == "dark", out); |
| 520 | dispatch(out); |
| 521 | dirty(); |
| 522 | }); |
| 523 | } |
| 524 | void setWidth(double width) override { |
| 525 | guard([&] { |
| 526 | std::vector<twidget::Json> out; |
| 527 | TraceScope trace("WidgetSandbox:ResizeLayout"); |
| 528 | document_.set_width(width, out); |
| 529 | dispatch(out); |
| 530 | dirty(); |
| 531 | }); |
| 532 | } |
| 533 | void destroy() override { destroyed_ = true; } |
| 534 | void repaint() { |
| 535 | guard([&] { |
| 536 | // The UI lost its display lists: layers are recorded again too. |
| 537 | repaintNeeded_ = true; |
| 538 | layerVersions_.clear(); |
| 539 | backend_->signal(2); |
| 540 | }); |
| 541 | } |
| 542 | // A mouse sends no touch events; `modifiers` bit0 alt, bit1 ctrl, bit2 meta, bit3 shift; `button` 2 the secondary. |
| 543 | void touch(int action, float x, float y, int pointer, bool primary, bool mouse, int modifiers, int button) { |
| 544 | guard([&] { |
| 545 | // TwidgetCore keeps the finger rule itself (widget-sandbox v0.3.0, twidget 710bb77): a finger's pointerdown |
| 546 | // and mousedown are untrusted, a mouse keeps its press. |
| 547 | dispatch(document_.touch(action, x, y, pointer, primary, mouse, modifiers, button)); |
| 548 | // The primary press ended: the host's haptic gate counts its 5 s after a touch from here (widget-sandbox |
| 549 | // v0.2.2), as the framework's renderer core reports it; TwidgetCore sends only the press's start (`gesture`). |
| 550 | if (primary && (action == 2 || action == 3) && onMessage) |
| 551 | onMessage("{\"type\":\"release\"}"); |
| 552 | if (mouse) |
| 553 | cursor(); |
| 554 | dirty(); |
| 555 | }); |
| 556 | } |
| 557 | // A mouse over the widget with no button down (0) or leaving it (1): :hover and the over/out events. |
| 558 | void hover(int action, float x, float y, int modifiers) { |
| 559 | guard([&] { |
| 560 | dispatch(document_.hover(action, x, y, modifiers)); |
| 561 | cursor(); |
| 562 | dirty(); |
| 563 | }); |
| 564 | } |
| 565 | void tick(float ms, bool frame, bool animate, bool paint) { |
| 566 | guard([&] { |
| 567 | ticks_++; |
| 568 | if (frame && !frozen_ && onMessage) { |
| 569 | frameSentAt_ = nowMs(); |
| 570 | framesSent_++; |
| 571 | onMessage(twidget::Json::object().set("type", "frame").set("time", double(ms)).dump()); |
| 572 | } |
| 573 | if (animate) { |
| 574 | TraceScope trace("WidgetSandbox:Animate"); |
| 575 | double started = nowMs(); |
| 576 | dispatch(document_.tick()); |
| 577 | animateMs_ += nowMs() - started; |
| 578 | dirty(); |
| 579 | } |
| 580 | if (!paint || (!document_.dirty() && !repaintNeeded_)) |
| 581 | return; |
| 582 | // One frame is recorded at a time: while the recorder works, this one waits (it asks for the tick that |
| 583 | // paints it when it is done). The host thread meanwhile receives the widget's next ops. |
| 584 | if (recording_) { |
| 585 | paintWaiting_ = true; |
| 586 | return; |
| 587 | } |
| 588 | TraceScope trace("WidgetSandbox:Paint"); |
| 589 | double paintStarted = nowMs(); |
| 590 | auto job = std::make_shared<RecordJob>(); |
| 591 | job->list = document_.paint(); |
| 592 | job->composition = document_.composition(); |
| 593 | job->paintMs = nowMs() - paintStarted; |
| 594 | // Layer contents go first; the frame that names them follows. Unchanged contents are reused. |
| 595 | std::unordered_map<int64_t, uint64_t> versions; |
| 596 | for (size_t i = 0; i < job->composition.layers.size(); i++) { |
| 597 | const auto &layer = job->composition.layers[i]; |
| 598 | versions[layer.id] = layer.content_version; |
| 599 | auto known = layerVersions_.find(layer.id); |
| 600 | if (known == layerVersions_.end() || known->second != layer.content_version) |
| 601 | job->layers.push_back(int(i)); |
| 602 | } |
| 603 | layerVersions_ = std::move(versions); |
| 604 | // The selectable text changes only with a layout; it goes out when it differs. |
| 605 | if (document_.layouts() != textLayouts_) { |
| 606 | textLayouts_ = document_.layouts(); |
| 607 | std::string text = document_.selectable_text_json(); |
| 608 | if (text != lastText_) { |
| 609 | lastText_ = text; |
| 610 | job->text = std::move(text); |
| 611 | job->hasText = true; |
| 612 | } |
| 613 | } |
| 614 | job->clock = twlite::Renderer::clock(); |
| 615 | job->width = document_.px_width(); |
| 616 | job->height = document_.px_height(); |
| 617 | job->dpr = document_.dpr(); |
| 618 | job->stats = document_.stats(); |
| 619 | if (conformanceReports) { |
| 620 | job->work = twidget::Json::object() |
| 621 | .set("receive", receiveMs_) |
| 622 | .set("animate", animateMs_) |
| 623 | .set("paint", job->paintMs) |
| 624 | .set("ticks", double(ticks_)) |
| 625 | .set("framesSent", double(framesSent_)) |
| 626 | .set("opsLatency", opsLatencies_ ? opsLatencyMs_ / opsLatencies_ : -1.0); |
| 627 | } |
| 628 | receiveMs_ = animateMs_ = opsLatencyMs_ = 0; |
| 629 | ticks_ = framesSent_ = opsLatencies_ = 0; |
| 630 | recording_ = true; |
| 631 | recorder_.async([this, job] { record(*job); }); |
| 632 | repaintNeeded_ = false; |
| 633 | }); |
| 634 | try { |
| 635 | backend_->signal(6); |
| 636 | } catch (...) { |
| 637 | } |
| 638 | } |
| 639 |
| 640 | private: |
| 641 | // A painted frame on its way to Java (WidgetSandboxBackend#record): the list, the layers it names and their |
| 642 | // contents that changed, the selectable text when it changed. |
| 643 | struct RecordJob { |
| 644 | twlite::DisplayList list; |
| 645 | twlite::Composition composition; |
| 646 | std::vector<int> layers; |
| 647 | std::string text; |
| 648 | bool hasText = false; |
| 649 | double clock = 0, paintMs = 0; |
| 650 | int width = 0, height = 0; |
| 651 | float dpr = 1; |
| 652 | twlite::BuildStats stats; |
| 653 | twidget::Json work; |
| 654 | }; |
| 655 | // The recorder thread: serialises the frame and records it into a RenderNode in Java, so the host thread is |
| 656 | // free for the widget's next ops meanwhile. |
| 657 | void record(RecordJob &job) { |
| 658 | if (!destroyed_) { |
| 659 | try { |
| 660 | TraceScope trace("WidgetSandbox:Record"); |
| 661 | double serializeMs = 0; |
| 662 | for (int i : job.layers) { |
| 663 | const auto &layer = job.composition.layers[size_t(i)]; |
| 664 | double started = nowMs(); |
| 665 | std::string content = twlite::to_json(layer.content); |
| 666 | serializeMs += nowMs() - started; |
| 667 | backend_->layer(i, int(layer.content_version), content); |
| 668 | } |
| 669 | if (job.hasText) |
| 670 | backend_->selectable_text(job.text); |
| 671 | double started = nowMs(); |
| 672 | std::string listJson = twlite::to_json(job.list), compositionJson = twlite::to_json(job.composition); |
| 673 | serializeMs += nowMs() - started; |
| 674 | if (job.work.isObject()) |
| 675 | backend_->signal(18, job.work.set("serialize", serializeMs).dump()); |
| 676 | backend_->record(listJson, compositionJson, job.clock, job.width, job.height, job.dpr, job.stats); |
| 677 | } catch (const std::exception &error) { |
| 678 | if (onDiag) |
| 679 | onDiag("android-renderer", error.what()); |
| 680 | } |
| 681 | } |
| 682 | recording_ = false; |
| 683 | // A frame painted while this one was recording waited for it: ask for the tick that paints it. |
| 684 | if (paintWaiting_.exchange(false) && !destroyed_) { |
| 685 | try { |
| 686 | backend_->signal(2); |
| 687 | } catch (...) { |
| 688 | } |
| 689 | } |
| 690 | } |
| 691 |
| 692 | template <class F> void guard(F body) { |
| 693 | if (destroyed_) |
| 694 | return; |
| 695 | try { |
| 696 | body(); |
| 697 | } catch (const std::exception &error) { |
| 698 | if (onDiag) |
| 699 | onDiag("android-renderer", error.what()); |
| 700 | } |
| 701 | } |
| 702 | void dirty() { |
| 703 | if (document_.dirty()) |
| 704 | backend_->signal(2); |
| 705 | schedule(); |
| 706 | } |
| 707 | // Tells the View when the next tick is due: every frame, after a delay, or never. |
| 708 | void schedule() { |
| 709 | double delay = -1; |
| 710 | if (document_.wants_tick()) |
| 711 | delay = 0; |
| 712 | else { |
| 713 | double next = document_.next_tick_in(); |
| 714 | if (std::isfinite(next)) |
| 715 | delay = std::max(1.0, next * 1000); |
| 716 | } |
| 717 | if (delay == scheduled_ && delay != 0) |
| 718 | return; |
| 719 | if (delay == 0 && scheduled_ == 0) |
| 720 | return; |
| 721 | scheduled_ = delay; |
| 722 | backend_->signal(7, std::to_string(delay)); |
| 723 | } |
| 724 | // The CSS cursor where the mouse is, to the View when it changes (signal 11). |
| 725 | void cursor() { |
| 726 | auto now = document_.cursor(); |
| 727 | if (now == cursor_) |
| 728 | return; |
| 729 | cursor_ = now; |
| 730 | backend_->signal(11, now); |
| 731 | } |
| 732 | void dispatch(const std::vector<twidget::Json> &output) { |
| 733 | for (const auto &message : output) { |
| 734 | auto kind = message["type"].asString(); |
| 735 | if (kind == "violation" && onDiag) |
| 736 | onDiag("renderer-violation", message["detail"].asString()); |
| 737 | if (!onMessage) |
| 738 | continue; |
| 739 | onMessage(message.dump()); |
| 740 | } |
| 741 | } |
| 742 | std::shared_ptr<AndroidBackend> backend_; |
| 743 | twlite::Renderer document_; |
| 744 | std::unordered_map<int64_t, uint64_t> layerVersions_; |
| 745 | uint64_t textLayouts_ = ~0ull; |
| 746 | std::string lastText_ = "[]"; |
| 747 | std::string cursor_ = "auto"; |
| 748 | double scheduled_ = -1; |
| 749 | // The native work since the last recorded frame, ms (the stress runner's breakdown). |
| 750 | double receiveMs_ = 0, animateMs_ = 0; |
| 751 | // The frame pipeline since the last recorded frame: ticks, rAF frames sent to the widget, and how long the widget |
| 752 | // took from a frame to the ops it answered with. |
| 753 | double frameSentAt_ = 0, opsLatencyMs_ = 0; |
| 754 | int ticks_ = 0, framesSent_ = 0, opsLatencies_ = 0; |
| 755 | static double nowMs() { |
| 756 | return std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now().time_since_epoch()).count(); |
| 757 | } |
| 758 | std::atomic<bool> destroyed_{false}; |
| 759 | bool frozen_ = false, repaintNeeded_ = false; |
| 760 | std::atomic<bool> recording_{false}, paintWaiting_{false}; |
| 761 | // Last, so it stops (and finishes its job) before anything it uses goes. |
| 762 | twidget::JSThread recorder_{"widget-record"}; |
| 763 | }; |
| 764 |
| 765 | // {"colorScheme","params":{key:"#rrggbb"},"button":{...},"text":{...}} from WidgetSandboxTheme (docs/sdk-v1.md §2.3, §2.4). |
| 766 | twidget::WidgetTheme parseTheme(const std::string &json) { |
| 767 | twidget::WidgetTheme theme; |
| 768 | auto value = twidget::Json::parse(json); |
| 769 | if (!value) |
| 770 | return theme; |
| 771 | theme.colorScheme = (*value)["colorScheme"].asString() == "dark" ? "dark" : "light"; |
| 772 | for (const auto &[key, color] : (*value)["params"].obj()) |
| 773 | if (color.isString()) |
| 774 | theme.params[key] = color.asString(); |
| 775 | const auto &button = (*value)["button"]; |
| 776 | auto text = [&](const char *key) -> std::optional<std::string> { |
| 777 | const auto &v = button[key]; |
| 778 | return v.isString() ? std::optional<std::string>(v.asString()) : std::nullopt; |
| 779 | }; |
| 780 | auto number = [&](const char *key) -> std::optional<double> { |
| 781 | const auto &v = button[key]; |
| 782 | return v.isNumber() ? std::optional<double>(v.asNumber()) : std::nullopt; |
| 783 | }; |
| 784 | theme.button.bg = text("bg"); |
| 785 | theme.button.text = text("text"); |
| 786 | theme.button.radius = text("radius"); |
| 787 | theme.button.font = text("font"); |
| 788 | theme.button.padding = text("padding"); |
| 789 | theme.button.height = text("height"); |
| 790 | theme.button.pressedOpacity = number("pressedOpacity"); |
| 791 | theme.button.disabledOpacity = number("disabledOpacity"); |
| 792 | const auto &text_tokens = (*value)["text"]; |
| 793 | if (text_tokens["fontSize"].isNumber()) |
| 794 | theme.text.fontSize = text_tokens["fontSize"].asNumber(); |
| 795 | if (text_tokens["lineHeight"].isNumber()) |
| 796 | theme.text.lineHeight = text_tokens["lineHeight"].asNumber(); |
| 797 | if (text_tokens["padding"].isString()) |
| 798 | theme.text.padding = text_tokens["padding"].asString(); |
| 799 | return theme; |
| 800 | } |
| 801 | // The widget's state and where it is (docs/sdk-v1.md §5.11), as WidgetMedia builds it: |
| 802 | // {"data":{"chatData","publicData"},"context":{"isCopy","canQuery","backgroundQuery","chatType","viewer":{"id", |
| 803 | // "firstName","lastName","username","isPremium"},"preview","wallet"}}; absent or null fields are none. |
| 804 | twidget::WidgetData parseData(const twidget::Json &value) { |
| 805 | twidget::WidgetData data; |
| 806 | if (value["chatData"].isString()) |
| 807 | data.chatData = value["chatData"].asString(); |
| 808 | if (value["publicData"].isString()) |
| 809 | data.publicData = value["publicData"].asString(); |
| 810 | return data; |
| 811 | } |
| 812 | twidget::WidgetContext parseContext(const twidget::Json &value) { |
| 813 | twidget::WidgetContext context; |
| 814 | context.isCopy = value["isCopy"].asBool(); |
| 815 | context.canQuery = value["canQuery"].asBool(); |
| 816 | context.backgroundQuery = value["backgroundQuery"].asBool(); |
| 817 | context.preview = value["preview"].asBool(); |
| 818 | // Gram Wallet for this user (docs/sdk-v1.md §5.13): the widget gets TelegramWidget.wallet where it may ask the bot. |
| 819 | context.wallet = value["wallet"].asBool(); |
| 820 | if (value["chatType"].isString()) |
| 821 | context.chatType = value["chatType"].asString(); |
| 822 | const auto &viewer = value["viewer"]; |
| 823 | if (viewer["id"].isNumber()) { |
| 824 | twidget::WidgetViewer v; |
| 825 | v.id = int64_t(viewer["id"].asNumber()); |
| 826 | if (viewer["firstName"].isString()) |
| 827 | v.firstName = viewer["firstName"].asString(); |
| 828 | if (viewer["lastName"].isString()) |
| 829 | v.lastName = viewer["lastName"].asString(); |
| 830 | if (viewer["username"].isString()) |
| 831 | v.username = viewer["username"].asString(); |
| 832 | v.isPremium = viewer["isPremium"].asBool(); |
| 833 | context.viewer = v; |
| 834 | } |
| 835 | return context; |
| 836 | } |
| 837 | // {"top","right","bottom","left","cornerLeft","cornerRight"} in CSS px (docs/sdk-v1.md §2.17); a missing one is 0. |
| 838 | twidget::WidgetContentSafeArea parseSafeArea(const twidget::Json &value) { |
| 839 | twidget::WidgetContentSafeArea area; |
| 840 | auto get = [&](const char *name) { return value[name].isNumber() ? value[name].asNumber() : 0.0; }; |
| 841 | area.top = get("top"); |
| 842 | area.right = get("right"); |
| 843 | area.bottom = get("bottom"); |
| 844 | area.left = get("left"); |
| 845 | area.cornerLeft = get("cornerLeft"); |
| 846 | area.cornerRight = get("cornerRight"); |
| 847 | return area; |
| 848 | } |
| 849 | // {"local":{k:v},"session":{k:v}} as WidgetSandboxStorage keeps it. |
| 850 | twidget::WidgetStorage parseStorage(const std::string &json) { |
| 851 | twidget::WidgetStorage storage; |
| 852 | auto value = twidget::Json::parse(json); |
| 853 | if (!value) |
| 854 | return storage; |
| 855 | for (const auto &[key, v] : (*value)["local"].obj()) |
| 856 | storage.local[key] = v.asString(); |
| 857 | for (const auto &[key, v] : (*value)["session"].obj()) |
| 858 | storage.session[key] = v.asString(); |
| 859 | return storage; |
| 860 | } |
| 861 |
| 862 | struct Session { |
| 863 | // Owner commands serialize here. Waiting for the host thread never blocks UI. |
| 864 | twidget::JSThread control{"widget-control"}; |
| 865 | std::shared_ptr<AndroidBackend> backend; |
| 866 | std::unique_ptr<twidget::WidgetHost> host; |
| 867 | std::atomic<bool> closing{false}; |
| 868 | // The widget's requests and wallet calls the client is answering (WidgetSandboxNative#answerRequest), by id. |
| 869 | std::mutex requestsMutex; |
| 870 | std::unordered_map<int64_t, std::function<void(const twidget::WidgetRequestOutcome &)>> requests; |
| 871 | int64_t nextRequest = 1; |
| 872 | void shutdown() { |
| 873 | if (closing.exchange(true)) |
| 874 | return; |
| 875 | control.join(); |
| 876 | host.reset(); // upstream destructor joins engine/validator/host threads |
| 877 | backend.reset(); |
| 878 | } |
| 879 | ~Session() { shutdown(); } |
| 880 | template <class F> void input(F body) { |
| 881 | if (closing) |
| 882 | return; |
| 883 | control.async([this, body] { |
| 884 | if (!host || closing) |
| 885 | return; |
| 886 | host->onHostThread([&] { body(static_cast<AndroidRenderer &>(host->renderer())); }); |
| 887 | }); |
| 888 | } |
| 889 | }; |
| 890 | std::mutex registryMutex; |
| 891 | std::unordered_map<jlong, std::shared_ptr<Session>> sessions; |
| 892 | std::atomic<jlong> nextId{1}; |
| 893 | std::shared_ptr<Session> get(jlong id) { |
| 894 | std::lock_guard lock(registryMutex); |
| 895 | auto it = sessions.find(id); |
| 896 | return it == sessions.end() ? nullptr : it->second; |
| 897 | } |
| 898 | std::mutex resourcesMutex; |
| 899 | std::unordered_map<std::string, std::shared_ptr<twidget::SandboxResources>> resourceCache; |
| 900 | std::shared_ptr<twidget::SandboxResources> resources(const std::string &path) { |
| 901 | std::lock_guard lock(resourcesMutex); |
| 902 | auto &value = resourceCache[path]; |
| 903 | if (!value) |
| 904 | value = twidget::SandboxResources::fromDirectory(path); |
| 905 | return value; |
| 906 | } |
| 907 | } // namespace |
| 908 |
| 909 | extern "C" JNIEXPORT jlong JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_create( |
| 910 | JNIEnv *e, jclass, jbyteArray path, jbyteArray html, jbyteArray theme, jfloat width, jfloat dpr, |
| 911 | jint cpuMultiplier, jbyteArray locale, jbyteArray storage, jbyteArray sdk, jbyteArray stoppedLabel, |
| 912 | jboolean visible, jobject backend) { |
| 913 | try { |
| 914 | auto session = std::make_shared<Session>(); |
| 915 | session->backend = std::make_shared<AndroidBackend>(e, backend); |
| 916 | twidget::WidgetHostOptions options; |
| 917 | // The widget's first task runs as shown or hidden from the start: a hidden widget's timers wait at |
| 918 | // least a second and its frames not at all, so a shown one must not boot hidden. |
| 919 | options.visible = visible; |
| 920 | options.devicePixelRatio = dpr; |
| 921 | options.features = {"webAnimations"}; |
| 922 | options.locale = bytes(e, locale); |
| 923 | options.storage = parseStorage(bytes(e, storage)); |
| 924 | if (auto value = twidget::Json::parse(bytes(e, sdk))) { |
| 925 | options.data = parseData((*value)["data"]); |
| 926 | options.context = parseContext((*value)["context"]); |
| 927 | // The client's time over the widget's corner at boot, so the first height already has its band. |
| 928 | options.contentSafeArea = parseSafeArea((*value)["contentSafeArea"]); |
| 929 | } |
| 930 | options.cpuLimitMs *= cpuMultiplier; |
| 931 | options.cpuQuotaMs *= cpuMultiplier; |
| 932 | options.mainThreadQuotaMs *= cpuMultiplier; |
| 933 | { |
| 934 | std::lock_guard lock(limitsMutex); |
| 935 | if (auto limits = twidget::Json::parse(limitsOverride); limits && limits->isObject()) { |
| 936 | const auto &l = *limits; |
| 937 | if (l["policy"].isString()) |
| 938 | options.policy = l["policy"].asString(); |
| 939 | if (l["cpuQuotaMs"].isNumber()) |
| 940 | options.cpuQuotaMs = l["cpuQuotaMs"].asNumber(); |
| 941 | if (l["cpuQuotaWindowMs"].isNumber()) |
| 942 | options.cpuQuotaWindowMs = l["cpuQuotaWindowMs"].asNumber(); |
| 943 | if (l["mainThreadQuotaMs"].isNumber()) |
| 944 | options.mainThreadQuotaMs = l["mainThreadQuotaMs"].asNumber(); |
| 945 | if (l["maxTimers"].isNumber()) |
| 946 | options.maxTimers = l["maxTimers"].asInt(); |
| 947 | if (l["memoryLimitBytes"].isNumber()) |
| 948 | options.memoryLimitBytes = size_t(l["memoryLimitBytes"].asNumber()); |
| 949 | } |
| 950 | } |
| 951 | auto factory = [b = session->backend, dpr](auto, const std::string &theme, double w) { |
| 952 | return std::make_unique<AndroidRenderer>(b, theme, float(w), dpr); |
| 953 | }; |
| 954 | session->host = std::make_unique<twidget::WidgetHost>( |
| 955 | bytes(e, html), parseTheme(bytes(e, theme)), width, options, resources(bytes(e, path)), factory, |
| 956 | std::make_shared<AndroidIntl>(session->backend)); |
| 957 | session->host->stoppedLabel = bytes(e, stoppedLabel); |
| 958 | auto target = session->backend; |
| 959 | session->host->onChange = [target](const twidget::WidgetDiagnostics &d) { |
| 960 | auto j = twidget::Json::object(); |
| 961 | j.set("loaded", d.loaded) |
| 962 | .set("rendered", d.rendered) |
| 963 | .set("nodes", d.nodes) |
| 964 | .set("jsMs", d.stats.jsMs) |
| 965 | .set("tasks", d.stats.tasks); |
| 966 | j.set("queries", d.stats.queries) |
| 967 | .set("queryMs", d.stats.queryMs) |
| 968 | .set("throttled", d.stats.throttledTasks); |
| 969 | j.set("memoryBytes", double(d.stats.memoryBytes)).set("height", d.height); |
| 970 | // The edge that stays put when the height changes (docs/sdk-v1.md §2.12): set before the size it anchors. |
| 971 | j.set("resizeAnchor", d.resizeAnchor); |
| 972 | j.set("dead", d.dead.value_or("")).set("error", d.lastError.value_or("")); |
| 973 | if (!d.diags.empty()) { |
| 974 | auto last = d.diags.rbegin(); |
| 975 | j.set("diagnostic", |
| 976 | last->first + ": " + (last->second.second.empty() ? "" : last->second.second.back())); |
| 977 | } |
| 978 | try { |
| 979 | target->signal(4, j.dump()); |
| 980 | } catch (...) { |
| 981 | } |
| 982 | }; |
| 983 | // A request to the bot, checked (docs/sdk-v1.md §5.11): the client answers it with |
| 984 | // messages.getBotWidgetQueryAnswer through WidgetSandboxNative#answerRequest (signal 12, {"id","data","wallet"}; |
| 985 | // `wallet`: the server attaches the user's wallet card, §5.13). |
| 986 | std::weak_ptr<Session> weak = session; |
| 987 | session->host->onRequest = [target, weak](const twidget::WidgetRequest &request, |
| 988 | std::function<void(const twidget::WidgetRequestOutcome &)> respond) { |
| 989 | auto owner = weak.lock(); |
| 990 | if (!owner) |
| 991 | return; |
| 992 | int64_t id; |
| 993 | { |
| 994 | std::lock_guard lock(owner->requestsMutex); |
| 995 | id = owner->nextRequest++; |
| 996 | owner->requests[id] = std::move(respond); |
| 997 | } |
| 998 | try { |
| 999 | target->signal(12, twidget::Json::object() |
| 1000 | .set("id", double(id)) |
| 1001 | .set("data", request.data) |
| 1002 | .set("wallet", request.wallet) |
| 1003 | .dump()); |
| 1004 | } catch (...) { |
| 1005 | } |
| 1006 | }; |
| 1007 | // A wallet call, checked (docs/sdk-v1.md §5.13; only with context.wallet): the client shows its wallet window, |
| 1008 | // sends the outcome to the bot and answers with the bot's answer through WidgetSandboxNative#answerRequest |
| 1009 | // (signal 19, {"id","call":{"method","transaction"|"payload","data"}}, TON Connect's field names). |
| 1010 | session->host->onWallet = [target, weak](const twidget::WalletCall &call, |
| 1011 | std::function<void(const twidget::WidgetRequestOutcome &)> respond) { |
| 1012 | auto owner = weak.lock(); |
| 1013 | if (!owner) |
| 1014 | return; |
| 1015 | int64_t id; |
| 1016 | { |
| 1017 | std::lock_guard lock(owner->requestsMutex); |
| 1018 | id = owner->nextRequest++; |
| 1019 | owner->requests[id] = std::move(respond); |
| 1020 | } |
| 1021 | try { |
| 1022 | target->signal(19, twidget::Json::object().set("id", double(id)).set("call", call.toJson()).dump()); |
| 1023 | } catch (...) { |
| 1024 | } |
| 1025 | }; |
| 1026 | // TelegramWidget.copyText on a tap: the client copies it and shows it (signal 13). |
| 1027 | session->host->onCopyText = [target](const std::string &text) { |
| 1028 | try { |
| 1029 | target->signal(13, text); |
| 1030 | } catch (...) { |
| 1031 | } |
| 1032 | }; |
| 1033 | // TelegramWidget.watchPublicData() / unwatchPublicData(): poll a shared widget's public_data while on (signal 14). |
| 1034 | session->host->onWatchPublicData = [target](bool on) { |
| 1035 | try { |
| 1036 | target->signal(14, on ? "1" : "0"); |
| 1037 | } catch (...) { |
| 1038 | } |
| 1039 | }; |
| 1040 | // What the widget asks the client to do, already checked (docs/sdk-v1.md §5.2, §5.3). |
| 1041 | session->host->onAction = [target](const twidget::WidgetAction &action) { |
| 1042 | try { |
| 1043 | target->signal(5, action.toJson().dump()); |
| 1044 | } catch (...) { |
| 1045 | } |
| 1046 | }; |
| 1047 | // The new full content of a Web Storage area (docs/sdk-v1.md §5.4). |
| 1048 | session->host->onStorageCommit = [target](const std::string &area, |
| 1049 | const std::map<std::string, std::string> &values) { |
| 1050 | auto content = twidget::Json::object(); |
| 1051 | for (const auto &[key, value] : values) |
| 1052 | content.set(key, value); |
| 1053 | try { |
| 1054 | target->signal(8, twidget::Json::object().set("area", area).set("values", content).dump()); |
| 1055 | } catch (...) { |
| 1056 | } |
| 1057 | }; |
| 1058 | // What the widget claims of this touch: horizontal drags and holds; `y` is always false since v0.1.3 (docs/sdk-v1.md §5.5). |
| 1059 | session->host->onGesture = [target](const twidget::WidgetGestureClaim &claim) { |
| 1060 | try { |
| 1061 | target->signal(3, twidget::Json::object().set("x", claim.x).set("y", claim.y).set("hold", claim.hold).dump()); |
| 1062 | } catch (...) { |
| 1063 | } |
| 1064 | }; |
| 1065 | if (attackScan) |
| 1066 | session->host->onRenderOps = [target](const std::string &clean) { |
| 1067 | std::vector<std::string> leaks; |
| 1068 | if (auto ops = twidget::Json::parse(clean)) |
| 1069 | for (const auto &op : ops->arr()) |
| 1070 | scanOp(op, leaks); |
| 1071 | for (const auto &leak : leaks) { |
| 1072 | try { |
| 1073 | target->signal(10, "ops " + leak); |
| 1074 | } catch (...) { |
| 1075 | } |
| 1076 | } |
| 1077 | }; |
| 1078 | jlong id = nextId.fetch_add(1); |
| 1079 | { |
| 1080 | std::lock_guard lock(registryMutex); |
| 1081 | sessions[id] = session; |
| 1082 | } |
| 1083 | session->host->start(); |
| 1084 | return id; |
| 1085 | } catch (const std::exception &error) { |
| 1086 | jclass type = e->FindClass("java/lang/IllegalStateException"); |
| 1087 | e->ThrowNew(type, error.what()); |
| 1088 | e->DeleteLocalRef(type); |
| 1089 | return 0; |
| 1090 | } |
| 1091 | } |
| 1092 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_destroy(JNIEnv *, |
| 1093 | jclass, |
| 1094 | jlong id) { |
| 1095 | std::shared_ptr<Session> session; |
| 1096 | { |
| 1097 | std::lock_guard lock(registryMutex); |
| 1098 | auto it = sessions.find(id); |
| 1099 | if (it == sessions.end()) |
| 1100 | return; |
| 1101 | session = std::move(it->second); |
| 1102 | sessions.erase(it); |
| 1103 | } |
| 1104 | session->shutdown(); |
| 1105 | } |
| 1106 | extern "C" JNIEXPORT void JNICALL |
| 1107 | Java_org_telegram_utils_twidget_WidgetSandboxNative_visible(JNIEnv *, jclass, jlong id, jboolean visible) { |
| 1108 | auto s = get(id); |
| 1109 | if (s) |
| 1110 | s->input([owner = s.get(), visible](auto &) { owner->host->setVisible(visible); }); |
| 1111 | } |
| 1112 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_theme(JNIEnv *e, jclass, |
| 1113 | jlong id, |
| 1114 | jbyteArray theme) { |
| 1115 | auto s = get(id); |
| 1116 | if (s) |
| 1117 | s->input([owner = s.get(), value = parseTheme(bytes(e, theme))](auto &) { owner->host->setTheme(value); }); |
| 1118 | } |
| 1119 | // What the client draws over the widget changed with the bubble's layout (docs/sdk-v1.md §2.17): the full area, as |
| 1120 | // `contentSafeArea` of the sdk JSON. Never per frame. |
| 1121 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_safeArea(JNIEnv *e, jclass, |
| 1122 | jlong id, |
| 1123 | jbyteArray area) { |
| 1124 | auto s = get(id); |
| 1125 | if (!s) |
| 1126 | return; |
| 1127 | auto value = twidget::Json::parse(bytes(e, area)); |
| 1128 | if (value) |
| 1129 | s->input([owner = s.get(), inset = parseSafeArea(*value)](auto &) { owner->host->setContentSafeArea(inset); }); |
| 1130 | } |
| 1131 | // The widget's state changed (updateMessageWidgetData, a polled public_data): the full state, `data` of the sdk JSON. |
| 1132 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_data(JNIEnv *e, jclass, |
| 1133 | jlong id, |
| 1134 | jbyteArray data) { |
| 1135 | auto s = get(id); |
| 1136 | if (!s) |
| 1137 | return; |
| 1138 | auto value = twidget::Json::parse(bytes(e, data)); |
| 1139 | if (value) |
| 1140 | s->input([owner = s.get(), state = parseData(*value)](auto &) { owner->host->setData(state); }); |
| 1141 | } |
| 1142 | // The client's answer to request or wallet call `request` (signal 12, 19): `failure` 0 answered (`result`, null for |
| 1143 | // none), 1 timeout, 2 rate limited, 3 failed; a wallet call's also 4 rejected (the user declined), 5 bad request (the |
| 1144 | // wallet refused it). Once; an unknown or answered id is ignored. |
| 1145 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_answerRequest( |
| 1146 | JNIEnv *e, jclass, jlong id, jlong request, jint failure, jbyteArray result) { |
| 1147 | auto s = get(id); |
| 1148 | if (!s) |
| 1149 | return; |
| 1150 | std::function<void(const twidget::WidgetRequestOutcome &)> respond; |
| 1151 | { |
| 1152 | std::lock_guard lock(s->requestsMutex); |
| 1153 | auto it = s->requests.find(request); |
| 1154 | if (it == s->requests.end()) |
| 1155 | return; |
| 1156 | respond = std::move(it->second); |
| 1157 | s->requests.erase(it); |
| 1158 | } |
| 1159 | twidget::WidgetRequestOutcome outcome; |
| 1160 | outcome.failure = failure == 1 ? twidget::WidgetRequestFailure::Timeout |
| 1161 | : failure == 2 ? twidget::WidgetRequestFailure::RateLimited |
| 1162 | : failure == 3 ? twidget::WidgetRequestFailure::Failed |
| 1163 | : failure == 4 ? twidget::WidgetRequestFailure::Rejected |
| 1164 | : failure == 5 ? twidget::WidgetRequestFailure::BadRequest |
| 1165 | : twidget::WidgetRequestFailure::None; |
| 1166 | if (outcome.failure == twidget::WidgetRequestFailure::None && result) |
| 1167 | outcome.answer.result = bytes(e, result); |
| 1168 | respond(outcome); |
| 1169 | } |
| 1170 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_width(JNIEnv *, jclass, |
| 1171 | jlong id, |
| 1172 | jfloat width) { |
| 1173 | auto s = get(id); |
| 1174 | if (s) |
| 1175 | s->input([width](auto &r) { r.setWidth(width); }); |
| 1176 | } |
| 1177 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_tick( |
| 1178 | JNIEnv *, jclass, jlong id, jfloat ms, jboolean frame, jboolean animate, jboolean paint) { |
| 1179 | auto s = get(id); |
| 1180 | if (s) |
| 1181 | s->input([=](auto &r) { r.tick(ms, frame, animate, paint); }); |
| 1182 | } |
| 1183 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_repaint(JNIEnv *, |
| 1184 | jclass, |
| 1185 | jlong id) { |
| 1186 | auto s = get(id); |
| 1187 | if (s) |
| 1188 | s->input([](auto &r) { r.repaint(); }); |
| 1189 | } |
| 1190 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_touch( |
| 1191 | JNIEnv *, jclass, jlong id, jint action, jfloat x, jfloat y, jint pointer, jboolean primary, jboolean mouse, |
| 1192 | jint modifiers, jint button) { |
| 1193 | auto s = get(id); |
| 1194 | if (s) |
| 1195 | s->input([=](auto &r) { r.touch(action, x, y, pointer, primary, mouse, modifiers, button); }); |
| 1196 | } |
| 1197 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_hover( |
| 1198 | JNIEnv *, jclass, jlong id, jint action, jfloat x, jfloat y, jint modifiers) { |
| 1199 | auto s = get(id); |
| 1200 | if (s) |
| 1201 | s->input([=](auto &r) { r.hover(action, x, y, modifiers); }); |
| 1202 | } |
| 1203 | // widget-sandbox's gesture policy (twidget/gesture_policy.h) for the chat's recognizers over a widget |
| 1204 | // (WidgetGestureArbiter): one arbiter per player, on the UI thread. A verdict crosses as bits: owner (0-1), |
| 1205 | // gesture (2-4), cancel (5), the bubble's action + 1 (6-8), the widget's event + 1 (9-10), edge (11); -1 for |
| 1206 | // no press. |
| 1207 | namespace { |
| 1208 | jint verdictBits(const twidget::GestureVerdict &v) { |
| 1209 | return jint(int(v.owner) | int(v.gesture) << 2 | (v.cancel ? 1 : 0) << 5 | (v.action ? int(*v.action) + 1 : 0) << 6 | |
| 1210 | (v.event ? int(*v.event) + 1 : 0) << 9 | (v.edge ? 1 : 0) << 11); |
| 1211 | } |
| 1212 | jint verdictBits(const std::optional<twidget::GestureVerdict> &v) { return v ? verdictBits(*v) : -1; } |
| 1213 | twidget::GestureArbiter *arbiterOf(jlong h) { return reinterpret_cast<twidget::GestureArbiter *>(h); } |
| 1214 | } // namespace |
| 1215 |
| 1216 | extern "C" JNIEXPORT jlong JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterCreate(JNIEnv *, jclass) { |
| 1217 | return reinterpret_cast<jlong>(new twidget::GestureArbiter()); |
| 1218 | } |
| 1219 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterDestroy(JNIEnv *, jclass, |
| 1220 | jlong h) { |
| 1221 | delete arbiterOf(h); |
| 1222 | } |
| 1223 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterDown( |
| 1224 | JNIEnv *, jclass, jlong h, jdouble t, jdouble x, jdouble y, jboolean claimX, jboolean claimY, jboolean hold, |
| 1225 | jboolean mouse, jint button) { |
| 1226 | twidget::WidgetGestureClaim claim; |
| 1227 | claim.x = claimX; |
| 1228 | claim.y = claimY; |
| 1229 | claim.hold = hold; |
| 1230 | return verdictBits(arbiterOf(h)->down(t, x, y, claim, mouse ? twidget::PointerKind::Mouse : twidget::PointerKind::Touch, button)); |
| 1231 | } |
| 1232 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterMove(JNIEnv *, jclass, |
| 1233 | jlong h, jdouble t, |
| 1234 | jdouble x, jdouble y) { |
| 1235 | return verdictBits(arbiterOf(h)->move(t, x, y)); |
| 1236 | } |
| 1237 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterTick(JNIEnv *, jclass, |
| 1238 | jlong h, jdouble t) { |
| 1239 | return verdictBits(arbiterOf(h)->tick(t)); |
| 1240 | } |
| 1241 | extern "C" JNIEXPORT jint JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterUp(JNIEnv *, jclass, |
| 1242 | jlong h, jdouble t, |
| 1243 | jdouble x, jdouble y) { |
| 1244 | return verdictBits(arbiterOf(h)->up(t, x, y)); |
| 1245 | } |
| 1246 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterCancel(JNIEnv *, jclass, |
| 1247 | jlong h) { |
| 1248 | arbiterOf(h)->cancel(); |
| 1249 | } |
| 1250 | extern "C" JNIEXPORT jdouble JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_arbiterHoldDeadline(JNIEnv *, |
| 1251 | jclass, |
| 1252 | jlong h) { |
| 1253 | auto deadline = arbiterOf(h)->holdDeadline(); |
| 1254 | return deadline ? *deadline : -1; |
| 1255 | } |
| 1256 |
| 1257 | // CSS filters where RenderEffect is missing (before API 31) or the canvas is a bitmap's: a FilterPush's |
| 1258 | // filters ("fl", display_list.h's Filter as [kind, amount, x, y, ARGB]) over an ARGB_8888 bitmap's |
| 1259 | // premultiplied pixels, in place. `scale`: the bitmap's px per CSS px. |
| 1260 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_filterPixels( |
| 1261 | JNIEnv *e, jclass, jobject bitmap, jbyteArray filters, jfloat scale) { |
| 1262 | auto parsed = twidget::Json::parse(bytes(e, filters)); |
| 1263 | if (!parsed || !parsed->isArray()) |
| 1264 | return; |
| 1265 | twlite::Filters steps; |
| 1266 | for (const auto &f : parsed->arr()) { |
| 1267 | int kind = f[size_t(0)].asInt(-1); |
| 1268 | if (kind < 0 || kind > int(twlite::Filter::DropShadow)) |
| 1269 | continue; |
| 1270 | twlite::Filter step; |
| 1271 | step.kind = twlite::Filter::Kind(kind); |
| 1272 | step.amount = float(f[size_t(1)].asNumber()); |
| 1273 | step.x = float(f[size_t(2)].asNumber()); |
| 1274 | step.y = float(f[size_t(3)].asNumber()); |
| 1275 | auto argb = uint32_t(f[size_t(4)].asNumber()); |
| 1276 | step.color = twlite::Color{uint8_t(argb >> 16), uint8_t(argb >> 8), uint8_t(argb), uint8_t(argb >> 24)}; |
| 1277 | steps.push_back(step); |
| 1278 | } |
| 1279 | AndroidBitmapInfo info; |
| 1280 | void *pixels = nullptr; |
| 1281 | if (steps.empty() || AndroidBitmap_getInfo(e, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1282 | info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) |
| 1283 | return; |
| 1284 | if (AndroidBitmap_lockPixels(e, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS) |
| 1285 | return; |
| 1286 | twlite::filter_pixels(static_cast<uint8_t *>(pixels), int(info.width), int(info.height), info.stride, false, |
| 1287 | steps, scale); |
| 1288 | AndroidBitmap_unlockPixels(e, bitmap); |
| 1289 | } |
| 1290 |
| 1291 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_conformance(JNIEnv *, jclass, |
| 1292 | jboolean enabled) { |
| 1293 | conformanceReports = enabled; |
| 1294 | } |
| 1295 |
| 1296 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_limits(JNIEnv *e, jclass, |
| 1297 | jbyteArray json) { |
| 1298 | std::lock_guard lock(limitsMutex); |
| 1299 | limitsOverride = json ? bytes(e, json) : std::string(); |
| 1300 | } |
| 1301 |
| 1302 | extern "C" JNIEXPORT void JNICALL Java_org_telegram_utils_twidget_WidgetSandboxNative_attackScan(JNIEnv *, jclass, |
| 1303 | jboolean enabled) { |
| 1304 | attackScan = enabled; |
| 1305 | } |
| 1306 |
| 1307 | #ifdef WIDGET_SANDBOX_TESTING |
| 1308 | extern "C" JNIEXPORT jobject JNICALL |
| 1309 | Java_org_telegram_ui_Components_WidgetSandboxTestNative_backend(JNIEnv *e, jclass, jlong id) { |
| 1310 | auto s = get(id); |
| 1311 | return s && !s->closing ? s->backend->test_object(e) : nullptr; |
| 1312 | } |
| 1313 | // Host harness only; Android release builds expose no arbitrary JS evaluation |
| 1314 | // API. |
| 1315 | extern "C" JNIEXPORT jbyteArray JNICALL |
| 1316 | Java_org_telegram_ui_Components_WidgetSandboxTestNative_probe(JNIEnv *e, jclass, jlong id, jbyteArray code) { |
| 1317 | auto s = get(id); |
| 1318 | return bytes(e, s && !s->closing ? s->host->probe(bytes(e, code)) : "closed"); |
| 1319 | } |
| 1320 | extern "C" JNIEXPORT jint JNICALL |
| 1321 | Java_org_telegram_ui_Components_WidgetSandboxTestNative_liveSessions(JNIEnv *, jclass) { |
| 1322 | std::lock_guard lock(registryMutex); |
| 1323 | return sessions.size(); |
| 1324 | } |
| 1325 | #endif |
| 1326 |
| 1327 | extern "C" bool registerTwidgetNatives(JNIEnv *e) { |
| 1328 | if (!e || e->GetJavaVM(&vm) != JNI_OK) |
| 1329 | return false; |
| 1330 | jclass type = e->FindClass("org/telegram/utils/twidget/WidgetSandboxNative"); |
| 1331 | if (!type) |
| 1332 | return false; |
| 1333 | #define BIND(name, signature) \ |
| 1334 | { \ |
| 1335 | const_cast<char *>(#name), const_cast<char *>(signature), \ |
| 1336 | reinterpret_cast<void *>(Java_org_telegram_utils_twidget_WidgetSandboxNative_##name) \ |
| 1337 | } |
| 1338 | const JNINativeMethod methods[] = { |
| 1339 | BIND(create, "([B[B[BFFI[B[B[B[BZLorg/telegram/utils/twidget/WidgetSandboxBackend;)J"), |
| 1340 | BIND(destroy, "(J)V"), |
| 1341 | BIND(visible, "(JZ)V"), |
| 1342 | BIND(theme, "(J[B)V"), |
| 1343 | BIND(width, "(JF)V"), |
| 1344 | BIND(data, "(J[B)V"), |
| 1345 | BIND(safeArea, "(J[B)V"), |
| 1346 | BIND(answerRequest, "(JJI[B)V"), |
| 1347 | BIND(repaint, "(J)V"), |
| 1348 | BIND(tick, "(JFZZZ)V"), |
| 1349 | BIND(touch, "(JIFFIZZII)V"), |
| 1350 | BIND(hover, "(JIFFI)V"), |
| 1351 | BIND(filterPixels, "(Landroid/graphics/Bitmap;[BF)V"), |
| 1352 | BIND(conformance, "(Z)V"), |
| 1353 | BIND(attackScan, "(Z)V"), |
| 1354 | BIND(arbiterCreate, "()J"), |
| 1355 | BIND(arbiterDestroy, "(J)V"), |
| 1356 | BIND(arbiterDown, "(JDDDZZZZI)I"), |
| 1357 | BIND(arbiterMove, "(JDDD)I"), |
| 1358 | BIND(arbiterTick, "(JD)I"), |
| 1359 | BIND(arbiterUp, "(JDDD)I"), |
| 1360 | BIND(arbiterCancel, "(J)V"), |
| 1361 | BIND(arbiterHoldDeadline, "(J)D")}; |
| 1362 | #undef BIND |
| 1363 | int status = e->RegisterNatives(type, methods, sizeof(methods) / sizeof(methods[0])); |
| 1364 | e->DeleteLocalRef(type); |
| 1365 | return status == JNI_OK; |
| 1366 | } |
| 1367 |