Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Conversation

@amirh
Copy link
Contributor

@amirh amirh commented Jan 25, 2019

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:

WebView(
  javascriptChannels: <JavascriptChannel>[
    JavascriptChannel(name: 'Print', onMessageReceived: (String msg) {print(msg); }),
  ].toSet(),
),

JavaScript (running inside the WebView):

Print.postMessage('Hello!');

Fixes: flutter/flutter#24837

@amirh amirh requested a review from mklim January 25, 2019 00:20
Copy link
Contributor

@mklim mklim left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

throwsA(anything),
);
});

Copy link
Contributor

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.

Copy link
Contributor Author

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.
Copy link
Contributor

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.

It's described as "Nested Namespacing" on this page.

Copy link
Contributor Author

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');
Copy link
Contributor

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?

Copy link
Contributor Author

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.

@amirh
Copy link
Contributor Author

amirh commented Jan 31, 2019

@mklim PTAL, changes since your last review:

  1. Updated documentation: b57117f
  2. Bump version and update change log: 59e1248
  3. Updated the example app: 9a350dd

amirh added a commit that referenced this pull request Jan 31, 2019
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
Copy link
Contributor

@mklim mklim left a 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
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks, fixed.

@amirh amirh changed the title Add WebView JavaScript channels (Dart side only). Add WebView JavaScript channels (Dart side). Feb 4, 2019
amirh added a commit that referenced this pull request Feb 4, 2019
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
@amirh amirh merged commit ac64477 into flutter:master Feb 4, 2019
andreidiaconu pushed a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
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
andreidiaconu pushed a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
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
andreidiaconu pushed a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
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
andreidiaconu added a commit to andreidiaconu/plugins that referenced this pull request Feb 17, 2019
karantanwar pushed a commit to karantanwar/plugins that referenced this pull request Feb 19, 2019
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
karantanwar pushed a commit to karantanwar/plugins that referenced this pull request Feb 19, 2019
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
karantanwar pushed a commit to karantanwar/plugins that referenced this pull request Feb 19, 2019
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
karantanwar pushed a commit to karantanwar/plugins that referenced this pull request Feb 19, 2019
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
@kingctan
Copy link

is there any way to add callback in js side which can postmessage and get a return object?

1 similar comment
@yexiaodong
Copy link

is there any way to add callback in js side which can postmessage and get a return object?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[WebView] Provide a Dart<->Javascript communication channel

5 participants