Skip to content

Commit 65cd1d8

Browse files
authored
Add --android_tunnel flag to emrun (cross-origin isolation on Android device) (emscripten-core#20783)
This flag does two things: 1. we use 'localhost' as the hostname 2. we invoke 'adb reverse tcp:{port} tcp:{port}' before launching This means that the page will run with cross-origin isolation. Without cross-origin isolation, we typically can't have features such as SharedArrayBuffer, which is needed for apps using pthreads.
1 parent 7d639f7 commit 65cd1d8

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

emrun.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1533,6 +1533,14 @@ def parse_args():
15331533
help='Launches the page in a browser of an Android '
15341534
'device connected to an USB on the local system. (via adb)')
15351535

1536+
parser.add_argument('--android_tunnel', action='store_true',
1537+
help='Expose the port directly to the Android device '
1538+
'and connect to it as localhost, establishing '
1539+
'cross origin isolation. Implies --android. A '
1540+
'reverse socket connection is created by adb '
1541+
'reverse, and remains after emrun terminates (it '
1542+
'can be removed by adb reverse --remove).')
1543+
15361544
parser.add_argument('--system_info', action='store_true',
15371545
help='Prints information about the current system at startup.')
15381546

@@ -1567,6 +1575,9 @@ def run():
15671575

15681576
options = emrun_options = parse_args()
15691577

1578+
if options.android_tunnel:
1579+
options.android = True
1580+
15701581
if options.android:
15711582
global ADB
15721583
ADB = which('adb')
@@ -1636,7 +1647,12 @@ def run():
16361647
if not file_to_serve_is_url:
16371648
if len(options.cmdlineparams):
16381649
url += '?' + '&'.join(options.cmdlineparams)
1639-
hostname = socket.gethostbyname(socket.gethostname()) if options.android else options.hostname
1650+
if options.android_tunnel:
1651+
hostname = 'localhost'
1652+
elif options.android:
1653+
hostname = socket.gethostbyname(socket.gethostname())
1654+
else:
1655+
hostname = options.hostname
16401656
# create url for browser after opening the server so we have the final port number in case we are binding to port 0
16411657
url = 'http://' + hostname + ':' + str(options.port) + '/' + url
16421658

@@ -1671,6 +1687,9 @@ def run():
16711687
# 4. Type 'aapt d xmltree <packagename>.apk AndroidManifest.xml > manifest.txt' to extract the manifest from the package.
16721688
# 5. Locate the name of the main activity for the browser in manifest.txt and add an entry to above list in form 'appname/mainactivityname'
16731689

1690+
if options.android_tunnel:
1691+
subprocess.check_call([ADB, 'reverse', 'tcp:' + str(options.port), 'tcp:' + str(options.port)])
1692+
16741693
url = url.replace('&', '\\&')
16751694
browser = [ADB, 'shell', 'am', 'start', '-a', 'android.intent.action.VIEW', '-n', browser_app, '-d', url]
16761695
processname_killed_atexit = browser_app[:browser_app.find('/')]

0 commit comments

Comments
 (0)