From b77b7512c733e925a3ae1bc2c10198f65399dc5e Mon Sep 17 00:00:00 2001 From: Mouad Debbar Date: Tue, 25 Feb 2020 15:06:07 -0800 Subject: [PATCH] [web] Guard the remaining calls to window.onPlatformMessage (flutter/engine#16791) --- .../lib/web_ui/lib/src/engine/history.dart | 28 +++++---- .../lib/web_ui/lib/src/engine/keyboard.dart | 4 ++ .../src/engine/text_editing/text_editing.dart | 62 ++++++++++--------- 3 files changed, 54 insertions(+), 40 deletions(-) diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/history.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/history.dart index ecca63d0b5..0c46004dc0 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/history.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/history.dart @@ -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 @@ -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 diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard.dart index f192ef6c64..d42b44fc6f 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard.dart @@ -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; } diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart index 7c47ddb31c..369d711e59 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/text_editing/text_editing.dart @@ -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', [ - clientId, - editingState.toFlutter(), - ]), - ), - _emptyCallback, - ); + if (ui.window.onPlatformMessage != null) { + ui.window.onPlatformMessage( + 'flutter/textinput', + const JSONMethodCodec().encodeMethodCall( + MethodCall('TextInputClient.updateEditingState', [ + 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', - [clientId, inputAction], + if (ui.window.onPlatformMessage != null) { + ui.window.onPlatformMessage( + 'flutter/textinput', + const JSONMethodCodec().encodeMethodCall( + MethodCall( + 'TextInputClient.performAction', + [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', - [clientId], + if (ui.window.onPlatformMessage != null) { + ui.window.onPlatformMessage( + 'flutter/textinput', + const JSONMethodCodec().encodeMethodCall( + MethodCall( + 'TextInputClient.onConnectionClosed', + [clientId], + ), ), - ), - _emptyCallback, - ); + _emptyCallback, + ); + } } }