140 lines
5.5 KiB
PHP
140 lines
5.5 KiB
PHP
<?php
|
|
|
|
namespace JumPHP\Native;
|
|
|
|
use JumPHP\Async\Task;
|
|
use JumPHP\Bridge\JsBridge;
|
|
|
|
/**
|
|
* The browser build: links open in a new tab, text to the clipboard, and the system's file chooser (an <input type=file>: the chosen
|
|
* files are written into the engine's memory, /tmp/jumphp-picked/<name>, and the Task gives those paths; a folder or a save dialog: no).
|
|
* The page does them through JsBridge.
|
|
*/
|
|
final class BrowserProvider extends BaseProvider
|
|
{
|
|
protected array $features = ['openUrl', 'copy', 'open', 'notify'];
|
|
/** the page's notification permission: default (not asked), granted, denied, unsupported */
|
|
private string $permission = 'default';
|
|
private int $nextPick = 1;
|
|
/** @var array<int, array{task: Task, multiple: bool}> */
|
|
private array $picks = [];
|
|
private bool $listening = false;
|
|
|
|
public function __construct(private readonly JsBridge $bridge)
|
|
{
|
|
$this->bridge->on('notify-permission', function (array $e): void {
|
|
$this->permission = in_array($e['state'] ?? '', ['default', 'granted', 'denied', 'unsupported'], true) ? (string)$e['state'] : 'default';
|
|
});
|
|
// listening at once: the page's paste event of a Ctrl+V may come before the text field asks for it
|
|
$this->bridge->on('paste', function (array $e): void {
|
|
$this->pasted = isset($e['text']) && is_string($e['text']) ? $e['text'] : null;
|
|
$this->pastedAt = microtime(true);
|
|
$waiting = $this->pastes;
|
|
$this->pastes = [];
|
|
foreach ($waiting as $task) {
|
|
$task->settle($this->pasted);
|
|
}
|
|
});
|
|
}
|
|
|
|
/**
|
|
* A notification of the system (the browser's Notification API; a framed page: its parent shows it). Only once the
|
|
* person allowed them (requestPermission(), from a click); a hidden tab shows them too (the page keeps running a frame
|
|
* a second while hidden). Android Chrome shows none from a page: the JumPHP runtime does (jumphp/notification).
|
|
*/
|
|
public function notify(string $title, string $body): bool
|
|
{
|
|
if ($this->permission !== 'granted') {
|
|
return false;
|
|
}
|
|
return $this->bridge->send('notify', ['title' => $title, 'body' => $body]);
|
|
}
|
|
|
|
public function notificationPermission(): string
|
|
{
|
|
return $this->permission;
|
|
}
|
|
|
|
/** Asks the person (call it from a click: browsers want a gesture). The answer comes as the bridge event 'notify-permission'. */
|
|
public function requestNotifications(): void
|
|
{
|
|
$this->bridge->send('notify-permission');
|
|
}
|
|
|
|
public function uses(JsBridge $bridge): bool
|
|
{
|
|
return $this->bridge === $bridge;
|
|
}
|
|
|
|
public function open(array $options): Task
|
|
{
|
|
if (!empty($options['folder'])) {
|
|
return $this->unsupported('folder');
|
|
}
|
|
if (!$this->listening) {
|
|
$this->listening = true;
|
|
$this->bridge->on('filepick', function (array $e): void {
|
|
$pick = $this->picks[(int)($e['id'] ?? 0)] ?? null;
|
|
if ($pick === null) {
|
|
return;
|
|
}
|
|
unset($this->picks[(int)$e['id']]);
|
|
$paths = is_array($e['paths'] ?? null) && $e['paths'] !== [] ? array_map('strval', $e['paths']) : null;
|
|
$pick['task']->settle($paths === null ? null : ($pick['multiple'] ? $paths : $paths[0]));
|
|
});
|
|
}
|
|
$task = new Task();
|
|
$id = $this->nextPick++;
|
|
$multiple = !empty($options['multiple']);
|
|
$this->picks[$id] = ['task' => $task, 'multiple' => $multiple];
|
|
$accept = [];
|
|
foreach ((array)($options['filters'] ?? []) as $extensions) {
|
|
foreach ((array)$extensions as $ext) {
|
|
$accept[] = '.' . ltrim((string)$ext, '.');
|
|
}
|
|
}
|
|
$this->bridge->send('filepick', ['id' => $id, 'multiple' => $multiple, 'accept' => implode(',', $accept)]);
|
|
return $task;
|
|
}
|
|
|
|
/** The text of the last paste event of the page (Ctrl+V / the browser's own Paste), and when it came. */
|
|
private ?string $pasted = null;
|
|
private float $pastedAt = -10.0;
|
|
/** @var list<Task> pastes waiting for the page's event */
|
|
private array $pastes = [];
|
|
|
|
/**
|
|
* The clipboard's text. A page cannot read the clipboard without a permission (and a framed game, as on jumphp.net, is not
|
|
* given it), but the browser hands the text over in the "paste" event of the Ctrl+V itself (jumphp-device.js sends it): the
|
|
* text of that event when it came just before, else the page answers (the text of a paste event it just saw, else the
|
|
* clipboard API, which needs a permission: the menu's Paste).
|
|
*/
|
|
public function paste(): Task
|
|
{
|
|
$task = new Task();
|
|
if ($this->pasted !== null && microtime(true) - $this->pastedAt < 1.0) {
|
|
$text = $this->pasted;
|
|
$this->pasted = null; // one paste event, one paste
|
|
$task->settle($text);
|
|
return $task;
|
|
}
|
|
$this->pastes[] = $task;
|
|
$this->bridge->send('device', ['do' => 'paste']); // no event coming (the menu's Paste): the page asks the clipboard API
|
|
return $task;
|
|
}
|
|
|
|
public function name(): string
|
|
{
|
|
return 'browser';
|
|
}
|
|
|
|
public function copy(string $text): bool
|
|
{
|
|
return $this->bridge->send('device', ['do' => 'copy', 'text' => $text]);
|
|
}
|
|
|
|
public function openUrl(string $url): bool
|
|
{
|
|
return preg_match('#^https?://#i', $url) === 1 && $this->bridge->send('open', ['url' => $url]);
|
|
}
|
|
}
|