| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 import 'dart:sky'; | 5 import 'dart:sky'; |
| 6 import 'dart:collection'; | 6 import 'dart:collection'; |
| 7 import 'fetch.dart'; | 7 import 'fetch.dart'; |
| 8 import 'package:mojom/mojo/url_response.mojom.dart'; | 8 import 'package:mojom/mojo/url_response.mojom.dart'; |
| 9 | 9 |
| 10 final HashMap<String, List<ImageDecoderCallback>> _pendingRequests = | 10 final HashMap<String, List<ImageDecoderCallback>> _pendingRequests = |
| 11 new HashMap<String, List<ImageDecoderCallback>>(); | 11 new HashMap<String, List<ImageDecoderCallback>>(); |
| 12 | 12 |
| 13 final HashMap<String, Image> _completedRequests = | 13 final HashMap<String, Image> _completedRequests = |
| 14 new HashMap<String, Image>(); | 14 new HashMap<String, Image>(); |
| 15 | 15 |
| 16 void _loadComplete(url, image) { |
| 17 _completedRequests[url] = image; |
| 18 _pendingRequests[url].forEach((c) => c(image)); |
| 19 _pendingRequests.remove(url); |
| 20 } |
| 21 |
| 16 void load(String url, ImageDecoderCallback callback) { | 22 void load(String url, ImageDecoderCallback callback) { |
| 17 Image result = _completedRequests[url]; | 23 Image result = _completedRequests[url]; |
| 18 if (result != null) { | 24 if (result != null) { |
| 19 callback(_completedRequests[url]); | 25 callback(_completedRequests[url]); |
| 20 return; | 26 return; |
| 21 } | 27 } |
| 22 | 28 |
| 23 bool newRequest = false; | 29 bool newRequest = false; |
| 24 _pendingRequests.putIfAbsent(url, () { | 30 _pendingRequests.putIfAbsent(url, () { |
| 25 newRequest = true; | 31 newRequest = true; |
| 26 return new List<ImageDecoderCallback>(); | 32 return new List<ImageDecoderCallback>(); |
| 27 }).add(callback); | 33 }).add(callback); |
| 28 if (newRequest) { | 34 if (newRequest) { |
| 29 fetchUrl(url).then((UrlResponse response) { | 35 fetchUrl(url).then((UrlResponse response) { |
| 30 new ImageDecoder(response.body.handle.h, (image) { | 36 if (response.statusCode >= 400) { |
| 31 _completedRequests[url] = image; | 37 _loadComplete(url, null); |
| 32 _pendingRequests[url].forEach((c) => c(image)); | 38 return; |
| 33 _pendingRequests.remove(url); | 39 } |
| 34 }); | 40 new ImageDecoder(response.body.handle.h, |
| 41 (image) => _loadComplete(url, image)); |
| 35 }); | 42 }); |
| 36 } | 43 } |
| 37 } | 44 } |
| OLD | NEW |