[web] Restore the ability to set a custom url strategy (flutter/engine#22371)

This commit is contained in:
Mouad Debbar
2020-11-09 22:28:48 -08:00
committed by GitHub
parent 47ab2f504e
commit d256cd55de
3 changed files with 58 additions and 21 deletions

View File

@@ -174,6 +174,10 @@ void initializeEngine() {
return;
}
// Setup the hook that allows users to customize URL strategy before running
// the app.
_addUrlStrategyListener();
// Called by the Web runtime just before hot restarting the app.
//
// This extension cleans up resources that are registered with browser's
@@ -247,6 +251,16 @@ void initializeEngine() {
MouseCursor.initialize();
}
void _addUrlStrategyListener() {
_jsSetUrlStrategy = allowInterop((JsUrlStrategy? jsStrategy) {
customUrlStrategy =
jsStrategy == null ? null : CustomUrlStrategy.fromJs(jsStrategy);
});
registerHotRestartListener(() {
_jsSetUrlStrategy = null;
});
}
class _NullTreeSanitizer implements html.NodeTreeSanitizer {
@override
void sanitizeTree(html.Node node) {}

View File

@@ -8,6 +8,20 @@ part of engine;
/// When set to true, all platform messages will be printed to the console.
const bool/*!*/ _debugPrintPlatformMessages = false;
/// Whether [_customUrlStrategy] has been set or not.
///
/// It is valid to set [_customUrlStrategy] to null, so we can't use a null
/// check to determine whether it was set or not. We need an extra boolean.
bool _isUrlStrategySet = false;
/// A custom URL strategy set by the app before running.
UrlStrategy? _customUrlStrategy;
set customUrlStrategy(UrlStrategy? strategy) {
assert(!_isUrlStrategySet, 'Cannot set URL strategy more than once.');
_isUrlStrategySet = true;
_customUrlStrategy = strategy;
}
/// The Web implementation of [ui.Window].
// TODO(gspencergoog): Once the framework no longer uses ui.Window, make this extend
// ui.SingletonFlutterWindow instead.
@@ -16,33 +30,26 @@ class EngineFlutterWindow extends ui.Window {
final EnginePlatformDispatcher engineDispatcher = platformDispatcher as EnginePlatformDispatcher;
engineDispatcher._windows[_windowId] = this;
engineDispatcher._windowConfigurations[_windowId] = ui.ViewConfiguration();
_addUrlStrategyListener();
if (_isUrlStrategySet) {
_browserHistory =
MultiEntriesBrowserHistory(urlStrategy: _customUrlStrategy);
}
}
final Object _windowId;
final ui.PlatformDispatcher platformDispatcher;
void _addUrlStrategyListener() {
_jsSetUrlStrategy = allowInterop((JsUrlStrategy? jsStrategy) {
assert(
_browserHistory == null,
'Cannot set URL strategy more than once.',
);
final UrlStrategy? strategy =
jsStrategy == null ? null : CustomUrlStrategy.fromJs(jsStrategy);
_browserHistory = MultiEntriesBrowserHistory(urlStrategy: strategy);
});
registerHotRestartListener(() {
_jsSetUrlStrategy = null;
});
}
/// Handles the browser history integration to allow users to use the back
/// button, etc.
@visibleForTesting
BrowserHistory get browserHistory {
final UrlStrategy? urlStrategy = _isUrlStrategySet
? _customUrlStrategy
: _createDefaultUrlStrategy();
// Prevent any further customization of URL strategy.
_isUrlStrategySet = true;
return _browserHistory ??=
MultiEntriesBrowserHistory(urlStrategy: _createDefaultUrlStrategy());
MultiEntriesBrowserHistory(urlStrategy: urlStrategy);
}
BrowserHistory? _browserHistory;
@@ -58,9 +65,12 @@ class EngineFlutterWindow extends ui.Window {
@visibleForTesting
Future<void> debugInitializeHistory(
UrlStrategy? strategy, {
required bool useSingle,
}) async {
UrlStrategy? strategy, {
required bool useSingle,
}) async {
// Prevent any further customization of URL strategy.
_isUrlStrategySet = true;
await _browserHistory?.tearDown();
if (useSingle) {
_browserHistory = SingleEntryBrowserHistory(urlStrategy: strategy);
@@ -73,6 +83,10 @@ class EngineFlutterWindow extends ui.Window {
Future<void> debugResetHistory() async {
await _browserHistory?.tearDown();
_browserHistory = null;
// Reset the globals too.
_isUrlStrategySet = false;
_customUrlStrategy = null;
}
Future<bool> handleNavigationMessage(

View File

@@ -9,7 +9,8 @@ import 'dart:typed_data';
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';
import 'package:ui/src/engine.dart' hide window;
import 'package:ui/ui.dart' as ui;
import 'engine/history_test.dart';
import 'matchers.dart';
@@ -23,8 +24,16 @@ void main() {
}
void testMain() {
EngineSingletonFlutterWindow window;
setUp(() {
ui.webOnlyInitializeEngine();
window = EngineSingletonFlutterWindow(0, EnginePlatformDispatcher.instance);
});
tearDown(() async {
await window.debugResetHistory();
window = null;
});
test('window.defaultRouteName should not change', () async {