| 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:async'; | 5 import 'dart:async'; |
| 6 import 'dart:sky' as sky; | 6 import 'dart:sky' as sky; |
| 7 | 7 |
| 8 import 'package:mojo/core.dart' as core; | 8 import 'package:mojo/core.dart' as core; |
| 9 import 'package:mojom/mojo/asset_bundle/asset_bundle.mojom.dart'; | 9 import 'package:mojom/mojo/asset_bundle/asset_bundle.mojom.dart'; |
| 10 | 10 |
| 11 import 'net/fetch.dart'; |
| 12 import 'net/image_cache.dart' as image_cache; |
| 11 import 'shell.dart' as shell; | 13 import 'shell.dart' as shell; |
| 12 import 'net/fetch.dart'; | |
| 13 | |
| 14 Future<sky.Image> _decodeImage(core.MojoDataPipeConsumer assetData) { | |
| 15 Completer<sky.Image> completer = new Completer<sky.Image>(); | |
| 16 new sky.ImageDecoder(assetData.handle.h, completer.complete); | |
| 17 return completer.future; | |
| 18 } | |
| 19 | 14 |
| 20 abstract class AssetBundle { | 15 abstract class AssetBundle { |
| 21 void close(); | 16 void close(); |
| 22 Future<sky.Image> fetchImage(String key); | 17 Future<sky.Image> loadImage(String key); |
| 23 } | 18 } |
| 24 | 19 |
| 25 class NetworkAssetBundle extends AssetBundle { | 20 class NetworkAssetBundle extends AssetBundle { |
| 26 NetworkAssetBundle(Uri base_url) : _base_url = base_url; | 21 NetworkAssetBundle(Uri baseUrl) : _baseUrl = baseUrl; |
| 27 | 22 |
| 28 final Uri _base_url; | 23 final Uri _baseUrl; |
| 29 | 24 |
| 30 void close() { } | 25 void close() { } |
| 31 | 26 |
| 32 Future<sky.Image> fetchImage(String name) async { | 27 Future<sky.Image> loadImage(String name) { |
| 33 Uri url = _base_url.resolve(name); | 28 Uri url = _baseUrl.resolve(name); |
| 34 core.MojoDataPipeConsumer assetData = (await fetchUrl(url.toString())).body; | 29 return image_cache.load(url.toString()); |
| 35 return await _decodeImage(assetData); | |
| 36 } | 30 } |
| 37 } | 31 } |
| 38 | 32 |
| 39 Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async
{ | 33 Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async
{ |
| 40 core.MojoDataPipeConsumer bundleData = (await fetchUrl(url)).body; | 34 core.MojoDataPipeConsumer bundleData = (await fetchUrl(url)).body; |
| 41 AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound(); | 35 AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound(); |
| 42 shell.requestService("mojo:asset_bundle", unpacker); | 36 shell.requestService("mojo:asset_bundle", unpacker); |
| 43 unpacker.ptr.unpackZipStream(bundleData, bundle); | 37 unpacker.ptr.unpackZipStream(bundleData, bundle); |
| 44 unpacker.close(); | 38 unpacker.close(); |
| 45 } | 39 } |
| 46 | 40 |
| 47 class MojoAssetBundle extends AssetBundle { | 41 class MojoAssetBundle extends AssetBundle { |
| 48 MojoAssetBundle(AssetBundleProxy this._bundle); | 42 MojoAssetBundle(AssetBundleProxy this._bundle); |
| 49 | 43 |
| 50 factory MojoAssetBundle.fromNetwork(String relativeUrl) { | 44 factory MojoAssetBundle.fromNetwork(String relativeUrl) { |
| 51 AssetBundleProxy bundle = new AssetBundleProxy.unbound(); | 45 AssetBundleProxy bundle = new AssetBundleProxy.unbound(); |
| 52 _fetchAndUnpackBundle(relativeUrl, bundle); | 46 _fetchAndUnpackBundle(relativeUrl, bundle); |
| 53 return new AssetBundle(bundle); | 47 return new MojoAssetBundle(bundle); |
| 54 } | 48 } |
| 55 | 49 |
| 56 AssetBundleProxy _bundle; | 50 AssetBundleProxy _bundle; |
| 51 Map<String, Future<sky.Image>> _imageCache = new Map<String, Future<sky.Image>
>(); |
| 57 | 52 |
| 58 void close() { | 53 void close() { |
| 59 _bundle.close(); | 54 _bundle.close(); |
| 60 _bundle = null; | 55 _bundle = null; |
| 56 _imageCache = null; |
| 61 } | 57 } |
| 62 | 58 |
| 63 Future<sky.Image> fetchImage(String name) async { | 59 Future<sky.Image> loadImage(String name) { |
| 64 core.MojoDataPipeConsumer assetData = | 60 return _imageCache.putIfAbsent(name, () { |
| 65 (await _bundle.ptr.getAsStream(name)).assetData; | 61 Completer<sky.Image> completer = new Completer<sky.Image>(); |
| 66 return await _decodeImage(assetData); | 62 _bundle.ptr.getAsStream(name).then((response) { |
| 63 new sky.ImageDecoder(response.assetData.handle.h, completer.complete); |
| 64 }); |
| 65 return completer.future; |
| 66 }); |
| 67 } | 67 } |
| 68 } | 68 } |
| OLD | NEW |