Skip to content

Commit

Permalink
Add image stream error handling mechanism (flutter#18424)
Browse files Browse the repository at this point in the history
  • Loading branch information
xster authored Jul 31, 2018
1 parent e9c8e36 commit 35b55d5
Show file tree
Hide file tree
Showing 4 changed files with 756 additions and 294 deletions.
195 changes: 147 additions & 48 deletions packages/flutter/lib/src/painting/image_stream.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,17 @@ class ImageInfo {
/// same stack frame as the call to [ImageStream.addListener]).
typedef void ImageListener(ImageInfo image, bool synchronousCall);

/// Signature for reporting errors when resolving images.
///
/// Used by [ImageStream] and [precacheImage] to report errors.
typedef void ImageErrorListener(dynamic exception, StackTrace stackTrace);

class _ImageListenerPair {
_ImageListenerPair(this.listener, this.errorListener);
final ImageListener listener;
final ImageErrorListener errorListener;
}

/// A handle to an image resource.
///
/// ImageStream represents a handle to a [dart:ui.Image] object and its scale
Expand Down Expand Up @@ -96,7 +107,7 @@ class ImageStream extends Diagnosticable {
ImageStreamCompleter get completer => _completer;
ImageStreamCompleter _completer;

List<ImageListener> _listeners;
List<_ImageListenerPair> _listeners;

/// Assigns a particular [ImageStreamCompleter] to this [ImageStream].
///
Expand All @@ -110,9 +121,14 @@ class ImageStream extends Diagnosticable {
assert(_completer == null);
_completer = value;
if (_listeners != null) {
final List<ImageListener> initialListeners = _listeners;
final List<_ImageListenerPair> initialListeners = _listeners;
_listeners = null;
initialListeners.forEach(_completer.addListener);
for (_ImageListenerPair listenerPair in initialListeners) {
_completer.addListener(
listenerPair.listener,
onError: listenerPair.errorListener,
);
}
}
}

Expand All @@ -127,19 +143,32 @@ class ImageStream extends Diagnosticable {
/// occurred. If the listener is added within a render object paint function,
/// then use this flag to avoid calling [RenderObject.markNeedsPaint] during
/// a paint.
void addListener(ImageListener listener) {
///
/// An [ImageErrorListener] can also optionally be added along with the
/// `listener`. If an error occurred, `onError` will be called instead of
/// `listener`.
///
/// Many `listener`s can have the same `onError` and one `listener` can also
/// have multiple `onError` by invoking [addListener] multiple times with
/// a different `onError` each time.
///
/// Repeated `onError` will only be called once when an error occurs.
void addListener(ImageListener listener, { ImageErrorListener onError }) {
if (_completer != null)
return _completer.addListener(listener);
_listeners ??= <ImageListener>[];
_listeners.add(listener);
return _completer.addListener(listener, onError: onError);
_listeners ??= <_ImageListenerPair>[];
_listeners.add(new _ImageListenerPair(listener, onError));
}

/// Stop listening for new concrete [ImageInfo] objects.
/// Stop listening for new concrete [ImageInfo] objects and errors from
/// the `listener`'s associated [ImageErrorListener].
void removeListener(ImageListener listener) {
if (_completer != null)
return _completer.removeListener(listener);
assert(_listeners != null);
_listeners.remove(listener);
_listeners.removeWhere((_ImageListenerPair listenerPair) {
return listenerPair.listener == listener;
});
}

/// Returns an object which can be used with `==` to determine if this
Expand All @@ -164,7 +193,7 @@ class ImageStream extends Diagnosticable {
ifPresent: _completer?.toStringShort(),
ifNull: 'unresolved',
));
properties.add(new ObjectFlagProperty<List<ImageListener>>(
properties.add(new ObjectFlagProperty<List<_ImageListenerPair>>(
'listeners',
_listeners,
ifPresent: '${_listeners?.length} listener${_listeners?.length == 1 ? "" : "s" }',
Expand All @@ -182,12 +211,14 @@ class ImageStream extends Diagnosticable {
/// [ImageProvider] subclass will return an [ImageStream] and automatically
/// configure it with the right [ImageStreamCompleter] when possible.
abstract class ImageStreamCompleter extends Diagnosticable {
final List<ImageListener> _listeners = <ImageListener>[];
ImageInfo _current;
final List<_ImageListenerPair> _listeners = <_ImageListenerPair>[];
ImageInfo _currentImage;
FlutterErrorDetails _currentError;

/// Adds a listener callback that is called whenever a new concrete [ImageInfo]
/// object is available. If a concrete image is already available, this object
/// will call the listener synchronously.
/// object is available or an error is reported. If a concrete image is
/// already available, or if an error has been already reported, this object
/// will call the listener or error listener synchronously.
///
/// If the [ImageStreamCompleter] completes multiple images over its lifetime,
/// this listener will fire multiple times.
Expand All @@ -196,54 +227,125 @@ abstract class ImageStreamCompleter extends Diagnosticable {
/// occurred. If the listener is added within a render object paint function,
/// then use this flag to avoid calling [RenderObject.markNeedsPaint] during
/// a paint.
void addListener(ImageListener listener) {
_listeners.add(listener);
if (_current != null) {
void addListener(ImageListener listener, { ImageErrorListener onError }) {
_listeners.add(new _ImageListenerPair(listener, onError));
if (_currentImage != null) {
try {
listener(_current, true);
listener(_currentImage, true);
} catch (exception, stack) {
_handleImageError('by a synchronously-called image listener', exception, stack);
reportError(
context: 'by a synchronously-called image listener',
exception: exception,
stack: stack,
);
}
}
if (_currentError != null && onError != null) {
try {
onError(_currentError.exception, _currentError.stack);
} catch (exception, stack) {
FlutterError.reportError(
new FlutterErrorDetails(
exception: exception,
library: 'image resource service',
context: 'by a synchronously-called image error listener',
stack: stack,
),
);
}
}
}

/// Stop listening for new concrete [ImageInfo] objects.
/// Stop listening for new concrete [ImageInfo] objects and errors from
/// its associated [ImageErrorListener].
void removeListener(ImageListener listener) {
_listeners.remove(listener);
_listeners.removeWhere((_ImageListenerPair listenerPair) {
return listenerPair.listener == listener;
});
}

/// Calls all the registered listeners to notify them of a new image.
@protected
void setImage(ImageInfo image) {
_current = image;
_currentImage = image;
if (_listeners.isEmpty)
return;
final List<ImageListener> localListeners = new List<ImageListener>.from(_listeners);
final List<ImageListener> localListeners = _listeners.map<ImageListener>(
(_ImageListenerPair listenerPair) => listenerPair.listener
).toList();
for (ImageListener listener in localListeners) {
try {
listener(image, false);
} catch (exception, stack) {
_handleImageError('by an image listener', exception, stack);
reportError(
context: 'by an image listener',
exception: exception,
stack: stack,
);
}
}
}

void _handleImageError(String context, dynamic exception, dynamic stack) {
FlutterError.reportError(new FlutterErrorDetails(
/// Calls all the registered error listeners to notify them of an error that
/// occurred while resolving the image.
///
/// If the same error listener is attached with multiple listeners, that
/// error listener will only be notified once.
///
/// If no error listeners are attached, a [FlutterError] will be reported
/// instead.
@protected
void reportError({
String context,
dynamic exception,
StackTrace stack,
InformationCollector informationCollector,
bool silent = false,
}) {
_currentError = new FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'image resource service',
context: context
));
context: context,
informationCollector: informationCollector,
silent: silent,
);

// Many listeners can have the same error listener. De-duplicate.
final List<ImageErrorListener> localErrorListeners =
_listeners.map<ImageErrorListener>(
(_ImageListenerPair listenerPair) => listenerPair.errorListener
).where(
(ImageErrorListener errorListener) => errorListener != null
).toList();

if (localErrorListeners.isEmpty) {
FlutterError.reportError(_currentError);
} else {
for (ImageErrorListener errorListener in localErrorListeners) {
try {
errorListener(exception, stack);
} catch (exception, stack) {
FlutterError.reportError(
new FlutterErrorDetails(
context: 'by an image error listener',
library: 'image resource service',
exception: exception,
stack: stack,
),
);
}
}
}
}

/// Accumulates a list of strings describing the object's state. Subclasses
/// should override this to have their information included in [toString].
@override
void debugFillProperties(DiagnosticPropertiesBuilder description) {
super.debugFillProperties(description);
description.add(new DiagnosticsProperty<ImageInfo>('current', _current, ifNull: 'unresolved', showName: false));
description.add(new ObjectFlagProperty<List<ImageListener>>(
description.add(new DiagnosticsProperty<ImageInfo>('current', _currentImage, ifNull: 'unresolved', showName: false));
description.add(new ObjectFlagProperty<List<_ImageListenerPair>>(
'listeners',
_listeners,
ifPresent: '${_listeners?.length} listener${_listeners?.length == 1 ? "" : "s" }',
Expand Down Expand Up @@ -271,14 +373,13 @@ class OneFrameImageStreamCompleter extends ImageStreamCompleter {
OneFrameImageStreamCompleter(Future<ImageInfo> image, { InformationCollector informationCollector })
: assert(image != null) {
image.then<void>(setImage, onError: (dynamic error, StackTrace stack) {
FlutterError.reportError(new FlutterErrorDetails(
reportError(
context: 'resolving a single-frame image stream',
exception: error,
stack: stack,
library: 'services',
context: 'resolving a single-frame image stream',
informationCollector: informationCollector,
silent: true,
));
);
});
}
}
Expand Down Expand Up @@ -334,14 +435,13 @@ class MultiFrameImageStreamCompleter extends ImageStreamCompleter {
_framesEmitted = 0,
_timer = null {
codec.then<void>(_handleCodecReady, onError: (dynamic error, StackTrace stack) {
FlutterError.reportError(new FlutterErrorDetails(
reportError(
context: 'resolving an image codec',
exception: error,
stack: stack,
library: 'services',
context: 'resolving an image codec',
informationCollector: informationCollector,
silent: true,
));
);
});
}

Expand Down Expand Up @@ -397,14 +497,13 @@ class MultiFrameImageStreamCompleter extends ImageStreamCompleter {
try {
_nextFrame = await _codec.getNextFrame();
} catch (exception, stack) {
FlutterError.reportError(new FlutterErrorDetails(
exception: exception,
stack: stack,
library: 'services',
context: 'resolving an image frame',
informationCollector: _informationCollector,
silent: true,
));
reportError(
context: 'resolving an image frame',
exception: exception,
stack: stack,
informationCollector: _informationCollector,
silent: true,
);
return;
}
if (_codec.frameCount == 1) {
Expand All @@ -424,11 +523,11 @@ class MultiFrameImageStreamCompleter extends ImageStreamCompleter {
bool get _hasActiveListeners => _listeners.isNotEmpty;

@override
void addListener(ImageListener listener) {
void addListener(ImageListener listener, { ImageErrorListener onError }) {
if (!_hasActiveListeners && _codec != null) {
_decodeNextFrameAndSchedule();
}
super.addListener(listener);
super.addListener(listener, onError: onError);
}

@override
Expand Down
27 changes: 24 additions & 3 deletions packages/flutter/lib/src/widgets/image.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size si
/// Prefetches an image into the image cache.
///
/// Returns a [Future] that will complete when the first image yielded by the
/// [ImageProvider] is available.
/// [ImageProvider] is available or failed to load.
///
/// If the image is later used by an [Image] or [BoxDecoration] or [FadeInImage],
/// it will probably be loaded faster. The consumer of the image does not need
Expand All @@ -65,17 +65,38 @@ ImageConfiguration createLocalImageConfiguration(BuildContext context, { Size si
/// The [BuildContext] and [Size] are used to select an image configuration
/// (see [createLocalImageConfiguration]).
///
/// The `onError` argument can be used to manually handle errors while precaching.
///
/// See also:
///
/// * [ImageCache], which holds images that may be reused.
Future<Null> precacheImage(ImageProvider provider, BuildContext context, { Size size }) {
Future<Null> precacheImage(
ImageProvider provider,
BuildContext context, {
Size size,
ImageErrorListener onError,
}) {
final ImageConfiguration config = createLocalImageConfiguration(context, size: size);
final Completer<Null> completer = new Completer<Null>();
final ImageStream stream = provider.resolve(config);
void listener(ImageInfo image, bool sync) {
completer.complete();
}
stream.addListener(listener);
void errorListener(dynamic exception, StackTrace stackTrace) {
completer.complete();
if (onError != null) {
onError(exception, stackTrace);
} else {
FlutterError.reportError(new FlutterErrorDetails(
context: 'image failed to precache',
library: 'image resource service',
exception: exception,
stack: stackTrace,
silent: true,
));
}
}
stream.addListener(listener, onError: errorListener);
completer.future.then((Null _) { stream.removeListener(listener); });
return completer.future;
}
Expand Down
Loading

0 comments on commit 35b55d5

Please sign in to comment.