Framework callback for engine memory pressure events (#8778)

This commit is contained in:
Jason Simmons
2017-03-15 09:54:10 -07:00
committed by GitHub
parent 7843afd4d6
commit 054b566915
3 changed files with 62 additions and 0 deletions

View File

@@ -179,6 +179,9 @@ class _WidgetsAppState extends State<WidgetsApp> implements WidgetsBindingObserv
@override
void didChangeAppLifecycleState(AppLifecycleState state) { }
@override
void didHaveMemoryPressure() { }
@override
Widget build(BuildContext context) {
if (config.onLocaleChanged != null && _localeData == null) {

View File

@@ -48,6 +48,9 @@ abstract class WidgetsBindingObserver {
/// Called when the system puts the app in the background or returns
/// the app to the foreground.
void didChangeAppLifecycleState(AppLifecycleState state) { }
/// Called when the system is running low on memory.
void didHaveMemoryPressure() { }
}
/// The glue between the widgets layer and the Flutter engine.
@@ -60,6 +63,7 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren
ui.window.onLocaleChanged = handleLocaleChanged;
PlatformMessages.setJSONMessageHandler('flutter/navigation', _handleNavigationMessage);
PlatformMessages.setStringMessageHandler('flutter/lifecycle', _handleLifecycleMessage);
PlatformMessages.setJSONMessageHandler('flutter/system', _handleSystemMessage);
}
/// The current [WidgetsBinding], if one has been created.
@@ -213,6 +217,15 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren
return null;
}
Future<dynamic> _handleSystemMessage(Map<String, dynamic> message) async {
final String type = message['type'];
if (type == 'memoryPressure') {
for (WidgetsBindingObserver observer in _observers)
observer.didHaveMemoryPressure();
}
return null;
}
bool _needToReportFirstFrame = true;
bool _thisFrameWasUseful = true;

View File

@@ -0,0 +1,46 @@
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:typed_data';
import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
class MemoryPressureObserver implements WidgetsBindingObserver {
bool sawMemoryPressure = false;
@override
void didHaveMemoryPressure() {
sawMemoryPressure = true;
}
@override
Future<bool> didPopRoute() => new Future<bool>.value(false);
@override
void didChangeMetrics() { }
@override
void didChangeLocale(Locale locale) { }
@override
void didChangeAppLifecycleState(AppLifecycleState state) { }
}
void main() {
setUp(() {
WidgetsFlutterBinding.ensureInitialized();
});
testWidgets('didHaveMemoryPressure callback', (WidgetTester tester) async {
final MemoryPressureObserver observer = new MemoryPressureObserver();
WidgetsBinding.instance.addObserver(observer);
final ByteData message = const JSONMessageCodec().encodeMessage(
<String, dynamic>{'type': 'memoryPressure'});
await PlatformMessages.handlePlatformMessage('flutter/system', message, (_) {});
expect(observer.sawMemoryPressure, true);
WidgetsBinding.instance.removeObserver(observer);
});
}