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