Skip to content

Need a way to make synchronous calls from plugin platform code to Dart code #28310

@amirh

Description

@amirh

This came up as part of #25329:

The Android WebView API allows passing in a delegate that is invoked on the platform thread before a page is about to be loaded, the delegate needs to synchronously return true or false to determine whether to allow the navigation or to block it.

We needed to expose the same functionality in the webview_flutter plugin, ideally by delegating the decision to the Flutter application's Dart code. But this is currently impossible due to the combination of:

  1. Communication over platform channels is asynchronous.
  2. Messages sent from Dart to the platform channels are only delivered to the platform thread(by posting a task to the message loop).

Even if we were to block the platform thread waiting for the Dart code to make a decision, we would not be able to receive the message back from Dart(as the platform thread is blocked) and create a deadlock.

One potential approach for overriding this is to allow "platform channels" that deliver message to platform code on a separate thread. We could then:

  1. Send a message to Dart from the platform thread.
  2. Block the platform thread with some thread synchronization primitive.
  3. Dart code will send the response to a dedicated thread.
  4. The dedicated thread will release the latch that the platform thread is waiting on.

If we do this we should probably require that the "special thread" is always different from the platform thread.

For #25329 we're currently proceeding with a workaround that makes some security compromises(which means when an app sets a navigation delegate it should only load trusted content). But we should remove that workaround as soon as this issue is resolved.

cc @cbracken @jason-simmons

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projecta: pluginsSupport for writing, building, and running plugin packagesc: new featureNothing broken; request for a new capabilityengineflutter/engine repository. See also e: labels.team-engineOwned by Engine teamtriaged-engineTriaged by Engine team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions