_network_image_io.dart 4.66 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5

6 7 8 9 10 11 12
import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';

13
import 'binding.dart';
14 15 16 17
import 'debug.dart';
import 'image_provider.dart' as image_provider;
import 'image_stream.dart';

18
/// The dart:io implementation of [image_provider.NetworkImage].
19
@immutable
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
class NetworkImage extends image_provider.ImageProvider<image_provider.NetworkImage> implements image_provider.NetworkImage {
  /// Creates an object that fetches the image at the given URL.
  ///
  /// The arguments [url] and [scale] must not be null.
  const NetworkImage(this.url, { this.scale = 1.0, this.headers })
    : assert(url != null),
      assert(scale != null);

  @override
  final String url;

  @override
  final double scale;

  @override
35
  final Map<String, String>? headers;
36 37 38 39 40 41 42

  @override
  Future<NetworkImage> obtainKey(image_provider.ImageConfiguration configuration) {
    return SynchronousFuture<NetworkImage>(this);
  }

  @override
43
  ImageStreamCompleter load(image_provider.NetworkImage key, image_provider.DecoderCallback decode) {
44 45 46
    // Ownership of this controller is handed off to [_loadAsync]; it is that
    // method's responsibility to close the controller's stream when the image
    // has been loaded or an error is thrown.
47 48 49
    final StreamController<ImageChunkEvent> chunkEvents = StreamController<ImageChunkEvent>();

    return MultiFrameImageStreamCompleter(
50
      codec: _loadAsync(key as NetworkImage, chunkEvents, decode),
51 52
      chunkEvents: chunkEvents.stream,
      scale: key.scale,
53
      debugLabel: key.url,
54 55 56 57 58 59 60 61 62
      informationCollector: () {
        return <DiagnosticsNode>[
          DiagnosticsProperty<image_provider.ImageProvider>('Image provider', this),
          DiagnosticsProperty<image_provider.NetworkImage>('Image key', key),
        ];
      },
    );
  }

63 64 65 66 67 68 69 70 71 72
  // Do not access this field directly; use [_httpClient] instead.
  // We set `autoUncompress` to false to ensure that we can trust the value of
  // the `Content-Length` HTTP header. We automatically uncompress the content
  // in our call to [consolidateHttpClientResponseBytes].
  static final HttpClient _sharedHttpClient = HttpClient()..autoUncompress = false;

  static HttpClient get _httpClient {
    HttpClient client = _sharedHttpClient;
    assert(() {
      if (debugNetworkImageHttpClientProvider != null)
73
        client = debugNetworkImageHttpClientProvider!();
74 75 76 77
      return true;
    }());
    return client;
  }
78 79 80 81

  Future<ui.Codec> _loadAsync(
    NetworkImage key,
    StreamController<ImageChunkEvent> chunkEvents,
82
    image_provider.DecoderCallback decode,
83 84 85 86 87
  ) async {
    try {
      assert(key == this);

      final Uri resolved = Uri.base.resolve(key.url);
88

89
      final HttpClientRequest request = await _httpClient.getUrl(resolved);
90

91 92
      headers?.forEach((String name, String value) {
        request.headers.add(name, value);
93
      });
94
      final HttpClientResponse response = await request.close();
95 96 97 98
      if (response.statusCode != HttpStatus.ok) {
        // The network may be only temporarily unavailable, or the file will be
        // added on the server later. Avoid having future calls to resolve
        // fail to check the network again.
99
        await response.drain<List<int>>();
100
        throw image_provider.NetworkImageLoadException(statusCode: response.statusCode, uri: resolved);
101
      }
102

103 104
      final Uint8List bytes = await consolidateHttpClientResponseBytes(
        response,
105
        onBytesReceived: (int cumulative, int? total) {
106 107 108 109 110
          chunkEvents.add(ImageChunkEvent(
            cumulativeBytesLoaded: cumulative,
            expectedTotalBytes: total,
          ));
        },
111
      );
112
      if (bytes.lengthInBytes == 0)
113 114
        throw Exception('NetworkImage is an empty file: $resolved');

115
      return decode(bytes);
116 117 118 119 120
    } catch (e) {
      // Depending on where the exception was thrown, the image cache may not
      // have had a chance to track the key in the cache at all.
      // Schedule a microtask to give the cache a chance to add the key.
      scheduleMicrotask(() {
121
        PaintingBinding.instance!.imageCache!.evict(key);
122 123
      });
      rethrow;
124 125 126 127 128 129
    } finally {
      chunkEvents.close();
    }
  }

  @override
130
  bool operator ==(Object other) {
131 132
    if (other.runtimeType != runtimeType)
      return false;
133 134 135
    return other is NetworkImage
        && other.url == url
        && other.scale == scale;
136 137 138 139 140 141
  }

  @override
  int get hashCode => ui.hashValues(url, scale);

  @override
142
  String toString() => '${objectRuntimeType(this, 'NetworkImage')}("$url", scale: $scale)';
143
}