Commit 054b5669 authored by Jason Simmons's avatar Jason Simmons Committed by GitHub

Framework callback for engine memory pressure events (#8778)

parent 7843afd4
...@@ -179,6 +179,9 @@ class _WidgetsAppState extends State<WidgetsApp> implements WidgetsBindingObserv ...@@ -179,6 +179,9 @@ class _WidgetsAppState extends State<WidgetsApp> implements WidgetsBindingObserv
@override @override
void didChangeAppLifecycleState(AppLifecycleState state) { } void didChangeAppLifecycleState(AppLifecycleState state) { }
@override
void didHaveMemoryPressure() { }
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
if (config.onLocaleChanged != null && _localeData == null) { if (config.onLocaleChanged != null && _localeData == null) {
......
...@@ -48,6 +48,9 @@ abstract class WidgetsBindingObserver { ...@@ -48,6 +48,9 @@ abstract class WidgetsBindingObserver {
/// Called when the system puts the app in the background or returns /// Called when the system puts the app in the background or returns
/// the app to the foreground. /// the app to the foreground.
void didChangeAppLifecycleState(AppLifecycleState state) { } 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. /// The glue between the widgets layer and the Flutter engine.
...@@ -60,6 +63,7 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren ...@@ -60,6 +63,7 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren
ui.window.onLocaleChanged = handleLocaleChanged; ui.window.onLocaleChanged = handleLocaleChanged;
PlatformMessages.setJSONMessageHandler('flutter/navigation', _handleNavigationMessage); PlatformMessages.setJSONMessageHandler('flutter/navigation', _handleNavigationMessage);
PlatformMessages.setStringMessageHandler('flutter/lifecycle', _handleLifecycleMessage); PlatformMessages.setStringMessageHandler('flutter/lifecycle', _handleLifecycleMessage);
PlatformMessages.setJSONMessageHandler('flutter/system', _handleSystemMessage);
} }
/// The current [WidgetsBinding], if one has been created. /// The current [WidgetsBinding], if one has been created.
...@@ -213,6 +217,15 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren ...@@ -213,6 +217,15 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren
return null; 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 _needToReportFirstFrame = true;
bool _thisFrameWasUseful = true; bool _thisFrameWasUseful = true;
......
// 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);
});
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment