Skip to content

Commit

Permalink
[web] Guard the remaining calls to window.onPlatformMessage (flutter#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mdebbar authored Feb 25, 2020
1 parent 9ac76ad commit 7685e08
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 40 deletions.
28 changes: 16 additions & 12 deletions lib/web_ui/lib/src/engine/history.dart
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,13 @@ class BrowserHistory {
_setupFlutterEntry(_locationStrategy);

// 2. Send a 'popRoute' platform message so the app can handle it accordingly.
ui.window.onPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(_popRouteMethodCall),
(_) {},
);
if (ui.window.onPlatformMessage != null) {
ui.window.onPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(_popRouteMethodCall),
(_) {},
);
}
} else if (_isFlutterEntry(event.state)) {
// We get into this scenario when the user changes the url manually. It
// causes a new entry to be pushed on top of our "flutter" one. When this
Expand All @@ -111,13 +113,15 @@ class BrowserHistory {
_userProvidedRouteName = null;

// Send a 'pushRoute' platform message so the app handles it accordingly.
ui.window.onPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(
MethodCall('pushRoute', newRouteName),
),
(_) {},
);
if (ui.window.onPlatformMessage != null) {
ui.window.onPlatformMessage(
'flutter/navigation',
const JSONMethodCodec().encodeMethodCall(
MethodCall('pushRoute', newRouteName),
),
(_) {},
);
}
} else {
// The user has pushed a new entry on top of our flutter entry. This could
// happen when the user modifies the hash part of the url directly, for
Expand Down
4 changes: 4 additions & 0 deletions lib/web_ui/lib/src/engine/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class Keyboard {
static const JSONMessageCodec _messageCodec = JSONMessageCodec();

void _handleHtmlEvent(html.KeyboardEvent event) {
if (ui.window.onPlatformMessage == null) {
return;
}

if (_shouldIgnoreEvent(event)) {
return;
}
Expand Down
62 changes: 34 additions & 28 deletions lib/web_ui/lib/src/engine/text_editing/text_editing.dart
Original file line number Diff line number Diff line change
Expand Up @@ -819,44 +819,50 @@ class TextEditingChannel {

/// Sends the 'TextInputClient.updateEditingState' message to the framework.
void updateEditingState(int clientId, EditingState editingState) {
ui.window.onPlatformMessage(
'flutter/textinput',
const JSONMethodCodec().encodeMethodCall(
MethodCall('TextInputClient.updateEditingState', <dynamic>[
clientId,
editingState.toFlutter(),
]),
),
_emptyCallback,
);
if (ui.window.onPlatformMessage != null) {
ui.window.onPlatformMessage(
'flutter/textinput',
const JSONMethodCodec().encodeMethodCall(
MethodCall('TextInputClient.updateEditingState', <dynamic>[
clientId,
editingState.toFlutter(),
]),
),
_emptyCallback,
);
}
}

/// Sends the 'TextInputClient.performAction' message to the framework.
void performAction(int clientId, String inputAction) {
ui.window.onPlatformMessage(
'flutter/textinput',
const JSONMethodCodec().encodeMethodCall(
MethodCall(
'TextInputClient.performAction',
<dynamic>[clientId, inputAction],
if (ui.window.onPlatformMessage != null) {
ui.window.onPlatformMessage(
'flutter/textinput',
const JSONMethodCodec().encodeMethodCall(
MethodCall(
'TextInputClient.performAction',
<dynamic>[clientId, inputAction],
),
),
),
_emptyCallback,
);
_emptyCallback,
);
}
}

/// Sends the 'TextInputClient.onConnectionClosed' message to the framework.
void onConnectionClosed(int clientId) {
ui.window.onPlatformMessage(
'flutter/textinput',
const JSONMethodCodec().encodeMethodCall(
MethodCall(
'TextInputClient.onConnectionClosed',
<dynamic>[clientId],
if (ui.window.onPlatformMessage != null) {
ui.window.onPlatformMessage(
'flutter/textinput',
const JSONMethodCodec().encodeMethodCall(
MethodCall(
'TextInputClient.onConnectionClosed',
<dynamic>[clientId],
),
),
),
_emptyCallback,
);
_emptyCallback,
);
}
}
}

Expand Down

0 comments on commit 7685e08

Please sign in to comment.