18807b8ac2
Over plain http on a tailnet address the page is not a secure context and
navigator.clipboard is undefined, so the web engine refused every copy.
ClipboardReportingMessenger now answers clipboard calls through the
browser's legacy clipboard when the API is missing (web only, conditional
import): copy uses execCommand('copy') with a one-shot copy-event listener
(no focus or selection change); a paste key's Clipboard.getData takes the
browser's own paste event, the key reported not-handled so the browser
still fires it. A Paste button/menu says to use Ctrl+V or the https address.
Secure pages keep the engine path. Capture kit: --insecure and clipboard=.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
130 lines
4.4 KiB
Dart
130 lines
4.4 KiB
Dart
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
import 'insecure_clipboard.dart';
|
|
|
|
/// The app's root messenger, for messages no screen owns.
|
|
final GlobalKey<ScaffoldMessengerState> appScaffoldMessengerKey = GlobalKey();
|
|
|
|
/// The message for a failed clipboard call [method] ([error]), or null for
|
|
/// a call whose failure is not the operator's to hear about.
|
|
///
|
|
/// The web reports a refused clipboard write or read (no permission, an
|
|
/// insecure page, a browser without the clipboard API) as an error reply.
|
|
String? clipboardFailureMessage(String method, PlatformException error) {
|
|
if (error.code == insecureClipboardCode) return error.message;
|
|
final detail = error.message?.trim() ?? '';
|
|
// The engine's refusal on a page without the Clipboard API (one the
|
|
// legacy clipboard did not answer).
|
|
if (detail == 'Clipboard is not available in the context.') {
|
|
return switch (method) {
|
|
'Clipboard.setData' => insecureCopyMessage(),
|
|
'Clipboard.getData' => insecurePasteMessage(),
|
|
_ => null,
|
|
};
|
|
}
|
|
// The web's own reason ("Clipboard is not available in the context." on a
|
|
// page that is not secure) helps; its generic "Clipboard.setData failed."
|
|
// does not.
|
|
final why = detail.isEmpty || detail.startsWith('Clipboard.')
|
|
? 'the browser refused access to the clipboard'
|
|
: detail;
|
|
return switch (method) {
|
|
'Clipboard.setData' => 'Could not copy: $why',
|
|
'Clipboard.getData' => 'Could not paste: $why',
|
|
_ => null,
|
|
};
|
|
}
|
|
|
|
/// Shows [message] in the app's root messenger; false when there is none.
|
|
bool showAppMessage(String message) {
|
|
final messenger = appScaffoldMessengerKey.currentState;
|
|
if (messenger == null) return false;
|
|
messenger
|
|
..hideCurrentSnackBar()
|
|
..showSnackBar(SnackBar(content: Text(message)));
|
|
return true;
|
|
}
|
|
|
|
/// A [BinaryMessenger] that reports failed clipboard writes and reads.
|
|
///
|
|
/// Every copy in the app (the selection's copy keys and menu, copy buttons,
|
|
/// the terminal) writes through [Clipboard] on the platform channel, and
|
|
/// most never look at the result: a refusal would otherwise only reach the
|
|
/// console. The reply still reaches the caller unchanged.
|
|
///
|
|
/// With [insecure] (the web), a page without the Clipboard API copies and
|
|
/// pastes through the browser's legacy clipboard instead ([InsecureClipboard]).
|
|
class ClipboardReportingMessenger implements BinaryMessenger {
|
|
ClipboardReportingMessenger(
|
|
this._inner, {
|
|
void Function(String)? report,
|
|
InsecureClipboard? insecure,
|
|
}) : _report = report ?? _show,
|
|
_insecure = insecure; // ignore: prefer_initializing_formals
|
|
|
|
final BinaryMessenger _inner;
|
|
final InsecureClipboard? _insecure;
|
|
final void Function(String message) _report;
|
|
|
|
static void _show(String message) => showAppMessage(message);
|
|
|
|
static const _codec = JSONMethodCodec();
|
|
|
|
@override
|
|
Future<ByteData?>? send(String channel, ByteData? message) {
|
|
if (channel != SystemChannels.platform.name || message == null) {
|
|
return _inner.send(channel, message);
|
|
}
|
|
final MethodCall call;
|
|
try {
|
|
call = _codec.decodeMethodCall(message);
|
|
} on Object {
|
|
return _inner.send(channel, message);
|
|
}
|
|
final method = call.method;
|
|
if (!method.startsWith('Clipboard.')) return _inner.send(channel, message);
|
|
final reply = _insecure?.answer(call) ?? _inner.send(channel, message);
|
|
if (reply == null) return reply;
|
|
return reply.then((data) {
|
|
_check(method, data);
|
|
return data;
|
|
});
|
|
}
|
|
|
|
void _check(String method, ByteData? data) {
|
|
if (data == null) return;
|
|
try {
|
|
_codec.decodeEnvelope(data);
|
|
} on PlatformException catch (error) {
|
|
final message = clipboardFailureMessage(method, error);
|
|
if (message != null) _report(message);
|
|
} on Object {
|
|
// Not a reply this messenger understands; the caller decodes it.
|
|
}
|
|
}
|
|
|
|
@override
|
|
// ignore: deprecated_member_use
|
|
Future<void> handlePlatformMessage(
|
|
String channel,
|
|
ByteData? data,
|
|
PlatformMessageResponseCallback? callback,
|
|
) =>
|
|
// ignore: deprecated_member_use
|
|
_inner.handlePlatformMessage(channel, data, callback);
|
|
|
|
@override
|
|
void setMessageHandler(String channel, MessageHandler? handler) {
|
|
final insecure = _insecure;
|
|
_inner.setMessageHandler(
|
|
channel,
|
|
handler != null &&
|
|
insecure != null &&
|
|
channel == SystemChannels.keyEvent.name
|
|
? insecure.keyEventHandler(handler)
|
|
: handler,
|
|
);
|
|
}
|
|
}
|