Skip to content

Commit e515c3d

Browse files
PR Batch Testerclaude
andcommitted
fix(mobile): gate MCP app full screen on the browser's user activation
A touch timer counted scrolls as taps, outlived a trip to full screen, and never fired for screen reader or keyboard users. The WebView's outer relay page is the app frame's parent, so it reads navigator.userActivation like web does and marks each relayed message; full screen needs that mark. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent f943fb8 commit e515c3d

1 file changed

Lines changed: 18 additions & 22 deletions

File tree

‎apps/mobile/src/features/threads/McpAppWebView.tsx‎

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ const ROW_BOTTOM_MARGIN = 8;
5656
// so, rather than letting a page T3 never served pose as the app. This is not
5757
// a confidentiality boundary: a frame can always navigate itself, so the app
5858
// could carry anything it read out in a URL either way.
59+
//
60+
// A tap in the app also activates the outer page, its parent, so each relayed
61+
// message says whether the reader just used the app (`active`), the same user
62+
// activation web reads. The app cannot set it: it runs no code in this page.
5963
function outerDocument(src: string, allow: string, secret: string) {
6064
const attribute = (value: string) =>
6165
value.replace(/&/g, "&amp;").replace(/"/g, "&quot;").replace(/</g, "&lt;");
6266
return `<!doctype html><html><head><meta name="viewport" content="width=device-width, initial-scale=1">
6367
<style>html,body{margin:0;height:100%;background:transparent}iframe{border:0;display:block;width:100%;height:100%}</style></head>
6468
<body><iframe id="app" sandbox="allow-scripts allow-forms" allow="${attribute(allow)}"></iframe>
6569
<script>(function(){var frame=document.getElementById("app"),secret=${JSON.stringify(secret)},loads=0,live=true;
66-
var send=function(m){window.ReactNativeWebView.postMessage(JSON.stringify({secret:secret,message:m}));};
70+
var send=function(m){var u=navigator.userActivation;window.ReactNativeWebView.postMessage(JSON.stringify({secret:secret,active:!!(u&&u.isActive)&&document.activeElement===frame,message:m}));};
6771
frame.addEventListener("load",function(){loads+=1;if(loads>1&&live){live=false;send({t3:"navigated"});}});
6872
window.addEventListener("message",function(e){if(live&&e.source===frame.contentWindow)send(e.data);});
6973
window.__t3McpAppReceive=function(m){live&&frame.contentWindow&&frame.contentWindow.postMessage(m,"*");};
@@ -97,12 +101,6 @@ const confirm = (title: string, message: string, action: string) =>
97101
/** Largest file an app may hand the user through `ui/download-file`. */
98102
const MAX_DOWNLOAD_BYTES = 25 * 1024 * 1024;
99103

100-
/**
101-
* How long after a touch in the app it may go full screen, the same window a
102-
* browser gives a click (transient user activation).
103-
*/
104-
const FULLSCREEN_AFTER_TOUCH_MS = 5_000;
105-
106104
/**
107105
* A captured MCP App, hosted in a WebView over the MCP Apps bridge: inline as
108106
* a content-sized row of the thread feed, or full screen in its own modal screen. A
@@ -288,9 +286,9 @@ export function ThreadMcpApp(props: {
288286
}, [props.environmentId, props.threadId, props.itemId, app.tool]);
289287
const webView = useRef<WebView<object>>(null);
290288
const hostRef = useRef<McpAppHost | null>(null);
291-
// When the reader last touched the app; a WebView has no user activation
292-
// to ask, so full screen is gated on this instead.
293-
const lastTouchAt = useRef(0);
289+
// Whether the reader had just used the app when the message being handled
290+
// was sent; the host reads it while handling that message.
291+
const userActive = useRef(false);
294292

295293
// One host per loaded document.
296294
useEffect(() => {
@@ -408,14 +406,15 @@ export function ThreadMcpApp(props: {
408406
},
409407
requestDisplayMode: async (mode) => {
410408
const current = latest.current.props;
411-
// Full screen opens only right after the reader touched the app: one
412-
// that asks on load or on every retry would otherwise reopen it each
413-
// time the reader closes it, and the thread could never be reached.
414-
// It would also cover the approval or question the agent waits on,
415-
// or a screen the user moved to.
409+
// Full screen opens only right after the reader used the app, as on
410+
// web: one that asks on load or on every retry would otherwise reopen
411+
// it each time the reader closes it, and the thread could never be
412+
// reached. Read before any await, while this request is the message
413+
// being handled. Full screen would also cover the approval or
414+
// question the agent waits on, or a screen the user moved to.
416415
if (
417416
mode === "fullscreen" &&
418-
(Date.now() - lastTouchAt.current > FULLSCREEN_AFTER_TOUCH_MS ||
417+
(!userActive.current ||
419418
latest.current.awaitingUser ||
420419
!latest.current.navigation.isFocused())
421420
) {
@@ -560,12 +559,7 @@ export function ThreadMcpApp(props: {
560559
}
561560

562561
return (
563-
<View
564-
style={fullscreen ? { flex: 1 } : { height, marginBottom: ROW_BOTTOM_MARGIN }}
565-
onTouchStart={() => {
566-
lastTouchAt.current = Date.now();
567-
}}
568-
>
562+
<View style={fullscreen ? { flex: 1 } : { height, marginBottom: ROW_BOTTOM_MARGIN }}>
569563
{navigatedAway ? (
570564
<View className="flex-1 items-center justify-center">
571565
<Text className="text-sm text-foreground-muted">
@@ -613,7 +607,9 @@ export function ThreadMcpApp(props: {
613607
setNavigatedAway(true);
614608
return;
615609
}
610+
userActive.current = envelope.active === true;
616611
hostRef.current?.receive(message);
612+
userActive.current = false;
617613
}}
618614
/>
619615
) : asset._tag === "Failure" || crashed ? (

0 commit comments

Comments
 (0)