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

import 'dart:async';
import 'dart:io';
import 'dart:typed_data';
import 'dart:ui' as ui;

import 'package:flutter/foundation.dart';

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

17
/// The dart:io implementation of [image_provider.NetworkImage].
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
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
  final Map<String, String> headers;

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

  @override
41
  ImageStreamCompleter load(image_provider.NetworkImage key, image_provider.DecoderCallback decode) {
42 43 44
    // 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.
45 46 47
    final StreamController<ImageChunkEvent> chunkEvents = StreamController<ImageChunkEvent>();

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

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  // 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)
        client = debugNetworkImageHttpClientProvider();
      return true;
    }());
    return client;
  }
75 76 77 78

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

      final Uri resolved = Uri.base.resolve(key.url);
85 86 87
      final HttpClientRequest request = await _httpClient.getUrl(resolved);
      headers?.forEach((String name, String value) {
        request.headers.add(name, value);
88
      });
89
      final HttpClientResponse response = await request.close();
90 91 92 93 94
      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.
        PaintingBinding.instance.imageCache.evict(key);
95
        throw image_provider.NetworkImageLoadException(statusCode: response.statusCode, uri: resolved);
96
      }
97

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

110
      return decode(bytes);
111 112 113 114 115 116
    } finally {
      chunkEvents.close();
    }
  }

  @override
117
  bool operator ==(Object other) {
118 119
    if (other.runtimeType != runtimeType)
      return false;
120 121 122
    return other is NetworkImage
        && other.url == url
        && other.scale == scale;
123 124 125 126 127 128
  }

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

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