Chromium Code Reviews| 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 import "dart:sky.internals" as internals; | 7 import 'dart:sky.internals' as internals; |
| 8 import 'dart:typed_data'; | |
| 8 | 9 |
| 9 import 'package:mojo/core.dart' as core; | 10 import 'package:mojo/core.dart' as core; |
| 10 import 'package:mojom/mojo/asset_bundle/asset_bundle.mojom.dart'; | 11 import 'package:mojom/mojo/asset_bundle/asset_bundle.mojom.dart'; |
| 11 | 12 |
| 12 import 'net/fetch.dart'; | 13 import 'net/fetch.dart'; |
| 13 import 'net/image_cache.dart' as image_cache; | 14 import 'net/image_cache.dart' as image_cache; |
| 14 import 'shell.dart' as shell; | 15 import 'shell.dart' as shell; |
| 15 | 16 |
| 16 abstract class AssetBundle { | 17 abstract class AssetBundle { |
| 17 void close(); | 18 void close(); |
| 18 Future<sky.Image> loadImage(String key); | 19 Future<sky.Image> loadImage(String key); |
| 20 Future<String> loadString(String key); | |
| 19 } | 21 } |
| 20 | 22 |
| 21 class NetworkAssetBundle extends AssetBundle { | 23 class NetworkAssetBundle extends AssetBundle { |
| 22 NetworkAssetBundle(Uri baseUrl) : _baseUrl = baseUrl; | 24 NetworkAssetBundle(Uri baseUrl) : _baseUrl = baseUrl; |
| 23 | 25 |
| 24 final Uri _baseUrl; | 26 final Uri _baseUrl; |
| 25 | 27 |
| 26 void close() { } | 28 void close() { } |
| 27 | 29 |
| 28 Future<sky.Image> loadImage(String name) { | 30 Future<sky.Image> loadImage(String key) { |
| 29 Uri url = _baseUrl.resolve(name); | 31 return image_cache.load(_baseUrl.resolve(key).toString()); |
| 30 return image_cache.load(url.toString()); | 32 } |
| 33 | |
| 34 Future<String> loadString(String key) { | |
|
viktorl
2015/07/10 16:39:37
It may be worth thinking of calling this loadText
abarth-chromium
2015/07/10 16:42:28
Yeah, that might be worthwhile. I was hoping to s
| |
| 35 return fetchString(_baseUrl.resolve(key).toString()); | |
| 31 } | 36 } |
| 32 } | 37 } |
| 33 | 38 |
| 34 Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async { | 39 Future _fetchAndUnpackBundle(String relativeUrl, AssetBundleProxy bundle) async { |
| 35 core.MojoDataPipeConsumer bundleData = (await fetchUrl(relativeUrl)).body; | 40 core.MojoDataPipeConsumer bundleData = (await fetchUrl(relativeUrl)).body; |
| 36 AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound(); | 41 AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound(); |
| 37 shell.requestService("mojo:asset_bundle", unpacker); | 42 shell.requestService("mojo:asset_bundle", unpacker); |
| 38 unpacker.ptr.unpackZipStream(bundleData, bundle); | 43 unpacker.ptr.unpackZipStream(bundleData, bundle); |
| 39 unpacker.close(); | 44 unpacker.close(); |
| 40 } | 45 } |
| 41 | 46 |
| 42 class MojoAssetBundle extends AssetBundle { | 47 class MojoAssetBundle extends AssetBundle { |
| 43 MojoAssetBundle(AssetBundleProxy this._bundle); | 48 MojoAssetBundle(AssetBundleProxy this._bundle); |
| 44 | 49 |
| 45 factory MojoAssetBundle.fromNetwork(String relativeUrl) { | 50 factory MojoAssetBundle.fromNetwork(String relativeUrl) { |
| 46 AssetBundleProxy bundle = new AssetBundleProxy.unbound(); | 51 AssetBundleProxy bundle = new AssetBundleProxy.unbound(); |
| 47 _fetchAndUnpackBundle(relativeUrl, bundle); | 52 _fetchAndUnpackBundle(relativeUrl, bundle); |
| 48 return new MojoAssetBundle(bundle); | 53 return new MojoAssetBundle(bundle); |
| 49 } | 54 } |
| 50 | 55 |
| 51 AssetBundleProxy _bundle; | 56 final AssetBundleProxy _bundle; |
| 52 Map<String, Future<sky.Image>> _imageCache = new Map<String, Future<sky.Image> >(); | 57 Map<String, Future<sky.Image>> _imageCache = new Map<String, Future<sky.Image> >(); |
| 58 Map<String, Future<String>> _stringCache = new Map<String, Future<String>>(); | |
| 53 | 59 |
| 54 void close() { | 60 void close() { |
| 55 _bundle.close(); | 61 _bundle.close(); |
| 56 _bundle = null; | 62 _bundle = null; |
| 57 _imageCache = null; | 63 _imageCache = null; |
| 58 } | 64 } |
| 59 | 65 |
| 60 Future<sky.Image> loadImage(String name) { | 66 Future<sky.Image> loadImage(String key) { |
| 61 return _imageCache.putIfAbsent(name, () { | 67 return _imageCache.putIfAbsent(key, () { |
| 62 Completer<sky.Image> completer = new Completer<sky.Image>(); | 68 Completer<sky.Image> completer = new Completer<sky.Image>(); |
| 63 _bundle.ptr.getAsStream(name).then((response) { | 69 _bundle.ptr.getAsStream(key).then((response) { |
| 64 new sky.ImageDecoder(response.assetData.handle.h, completer.complete); | 70 new sky.ImageDecoder(response.assetData.handle.h, completer.complete); |
| 65 }); | 71 }); |
| 66 return completer.future; | 72 return completer.future; |
| 67 }); | 73 }); |
| 68 } | 74 } |
| 75 | |
| 76 Future<String> _fetchString(String key) async { | |
| 77 core.MojoDataPipeConsumer pipe = (await _bundle.ptr.getAsStream(key)).assetD ata; | |
| 78 ByteData data = await core.DataPipeDrainer.drainHandle(pipe); | |
| 79 return new String.fromCharCodes(new Uint8List.view(data.buffer)); | |
| 80 } | |
| 81 | |
| 82 Future<String> loadString(String key) { | |
| 83 return _stringCache.putIfAbsent(key, () => _fetchString(key)); | |
| 84 } | |
| 69 } | 85 } |
| 70 | 86 |
| 71 AssetBundle _initRootBundle() { | 87 AssetBundle _initRootBundle() { |
| 72 try { | 88 try { |
| 73 AssetBundleProxy bundle = new AssetBundleProxy.fromHandle( | 89 AssetBundleProxy bundle = new AssetBundleProxy.fromHandle( |
| 74 new core.MojoHandle(internals.takeRootBundleHandle())); | 90 new core.MojoHandle(internals.takeRootBundleHandle())); |
| 75 return new MojoAssetBundle(bundle); | 91 return new MojoAssetBundle(bundle); |
| 76 } catch (e) { | 92 } catch (e) { |
| 77 return null; | 93 return null; |
| 78 } | 94 } |
| 79 } | 95 } |
| 80 | 96 |
| 81 final AssetBundle rootBundle = _initRootBundle(); | 97 final AssetBundle rootBundle = _initRootBundle(); |
| OLD | NEW |