-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Add WebView JavaScript channels (Dart side). #1116
Conversation
mklim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| throwsA(anything), | ||
| ); | ||
| }); | ||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: It looks like the formatting here is off, it's breaking CI.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
| class JavascriptChannel { | ||
| /// Constructs a Javascript channel. | ||
| /// | ||
| /// The parameters `name` and `onMessageReceived` must not be null. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if somebody tries to pass in foo.bar as a name here? I'm asking because it's a common practice to try and namespace everything under one global namespaced foo object if you need to start creating objects in the global namespace at all in WFE development, so it's something that people are likely to try.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch.
I changed it to require that channel names follow the Dart identifier naming rules (only letters digits and underscores allowed, can't start with a digit).
| /// JavaScript code can call: | ||
| /// | ||
| /// ```javascript | ||
| /// Print.postMessage('Hello'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be worth establishing here how namespace collision is handled on the JS side. What happens if somebody tries to name their channel Array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed offline, we're going to override whatever window property that is there as this is what Android is doing. We don't think it's a security risk as the app developer has the power to do something like this anyway by injecting JavaScript.
cf433da to
9a350dd
Compare
iOS implementation of the method channel API for adding and removing JavaScript channels. #1116 adds the Dart side support, the current PR will land first. flutter/flutter#24837
mklim
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
| }, | ||
| ), | ||
| // We're using a Builder here so we have a context that is below the Scaffold | ||
| // to allow calling Scaffold.maketoast |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: "scaffold.makeToast"? It doesn't look like this is getting directly called anywhere though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, fixed.
Platform implementation of the method channel API for adding and removing JavaScript channels. #1116 adds the Dart side support, the current PR will land first. flutter/flutter#24837
iOS implementation of the method channel API for adding and removing JavaScript channels. flutter#1116 adds the Dart side support, the current PR will land first. flutter/flutter#24837
Platform implementation of the method channel API for adding and removing JavaScript channels. flutter#1116 adds the Dart side support, the current PR will land first. flutter/flutter#24837
Allows JavaScript code running in the WebView to send messages that will be received by the Flutter app's Dart code.
To keep the PRs smaller this does not include the platform side implementations which will be sent in following PRs.
Right now there are no return values on the JavaScript side, we could think of returning a JavaScript promise (or a promise equivalent) in the future, but as a first step I start without it (`evaluateJavascript` can be used to pass messages in the other direction so this shouldn't be a blocking limitation).
I believe that if we end up adding a return value on the JavaScript side this could be done without making a breaking change.
### Sample usage
Dart:
```dart
WebView(
javascriptChannels: <JavascriptChannel>[
JavascriptChannel(name: 'Print', onMessageReceived: (String msg) {print(msg); }),
].toSet(),
),
```
JavaScript (running inside the WebView):
```javascript
Print.postMessage('Hello!');
```
Fixes: flutter/flutter#24837
This reverts commit c1c59a0.
Platform implementation of the method channel API for adding and removing JavaScript channels. flutter#1116 adds the Dart side support, the current PR will land first. flutter/flutter#24837
Allows JavaScript code running in the WebView to send messages that will be received by the Flutter app's Dart code.
To keep the PRs smaller this does not include the platform side implementations which will be sent in following PRs.
Right now there are no return values on the JavaScript side, we could think of returning a JavaScript promise (or a promise equivalent) in the future, but as a first step I start without it (`evaluateJavascript` can be used to pass messages in the other direction so this shouldn't be a blocking limitation).
I believe that if we end up adding a return value on the JavaScript side this could be done without making a breaking change.
### Sample usage
Dart:
```dart
WebView(
javascriptChannels: <JavascriptChannel>[
JavascriptChannel(name: 'Print', onMessageReceived: (String msg) {print(msg); }),
].toSet(),
),
```
JavaScript (running inside the WebView):
```javascript
Print.postMessage('Hello!');
```
Fixes: flutter/flutter#24837
Platform implementation of the method channel API for adding and removing JavaScript channels. flutter#1116 adds the Dart side support, the current PR will land first. flutter/flutter#24837
Allows JavaScript code running in the WebView to send messages that will be received by the Flutter app's Dart code.
To keep the PRs smaller this does not include the platform side implementations which will be sent in following PRs.
Right now there are no return values on the JavaScript side, we could think of returning a JavaScript promise (or a promise equivalent) in the future, but as a first step I start without it (`evaluateJavascript` can be used to pass messages in the other direction so this shouldn't be a blocking limitation).
I believe that if we end up adding a return value on the JavaScript side this could be done without making a breaking change.
### Sample usage
Dart:
```dart
WebView(
javascriptChannels: <JavascriptChannel>[
JavascriptChannel(name: 'Print', onMessageReceived: (String msg) {print(msg); }),
].toSet(),
),
```
JavaScript (running inside the WebView):
```javascript
Print.postMessage('Hello!');
```
Fixes: flutter/flutter#24837
|
is there any way to add callback in js side which can postmessage and get a return object? |
1 similar comment
|
is there any way to add callback in js side which can postmessage and get a return object? |
Allows JavaScript code running in the WebView to send messages that will be received by the Flutter app's Dart code.
To keep the PRs smaller this does not include the platform side implementations which will be sent in following PRs.
Right now there are no return values on the JavaScript side, we could think of returning a JavaScript promise (or a promise equivalent) in the future, but as a first step I start without it (
evaluateJavascriptcan be used to pass messages in the other direction so this shouldn't be a blocking limitation).I believe that if we end up adding a return value on the JavaScript side this could be done without making a breaking change.
Sample usage
Dart:
JavaScript (running inside the WebView):
Fixes: flutter/flutter#24837