From fe2c731ec2ea71ef4b9c2b0ce46f953993259b35 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Fri, 19 Jun 2015 11:01:27 -0700 Subject: [PATCH] Add package:sky/mojo/asset_bundle.dart to wrap mojo:asset_bundle This Dart library exposes a more idiomatic interface to the asset_bundle service and provides integration with dart:sky. R=viktorl@google.com Review URL: https://codereview.chromium.org/1200463002. --- sdk/BUILD.gn | 1 + sdk/lib/mojo/asset_bundle.dart | 49 ++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 sdk/lib/mojo/asset_bundle.dart diff --git a/sdk/BUILD.gn b/sdk/BUILD.gn index b7cc5f7a3d4..f8b4f72f02e 100644 --- a/sdk/BUILD.gn +++ b/sdk/BUILD.gn @@ -71,6 +71,7 @@ dart_pkg("sdk") { "lib/framework/theme/typography.dart", "lib/framework/theme/view_configuration.dart", "lib/internals.dart", + "lib/mojo/asset_bundle.dart", "lib/mojo/embedder.dart", "lib/mojo/net/fetch.dart", "lib/mojo/net/image_cache.dart", diff --git a/sdk/lib/mojo/asset_bundle.dart b/sdk/lib/mojo/asset_bundle.dart new file mode 100644 index 00000000000..58e81e45f7a --- /dev/null +++ b/sdk/lib/mojo/asset_bundle.dart @@ -0,0 +1,49 @@ +// Copyright 2015 The Chromium Authors. All rights reserved. +// 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:sky' as sky; +import 'dart:typed_data'; + +import 'package:mojo/core.dart' as core; +import 'package:mojom/mojo/asset_bundle/asset_bundle.mojom.dart'; + +import 'shell.dart' as shell; +import 'net/fetch.dart'; + +Future _decodeImage(core.MojoDataPipeConsumer assetData) { + Completer completer = new Completer(); + new sky.ImageDecoder(assetData.handle.h, completer.complete); + return completer.future; +} + +class AssetBundle { + AssetBundle(AssetBundleProxy this._bundle); + + void close() { + _bundle.close(); + _bundle = null; + } + + Future fetchImage(String path) async { + core.MojoDataPipeConsumer assetData = + (await _bundle.ptr.getAsStream(path)).assetData; + return await _decodeImage(assetData); + } + + AssetBundleProxy _bundle; +} + +Future fetchAssetBundle(String url) async { + assert(false); // mojo:asset_bundle doesn't exist yet, see https://codereview.chromium.org/1176153007 + core.MojoDataPipeConsumer bundleData = (await fetchUrl(url)).body; + + AssetUnpackerProxy unpacker = new AssetUnpackerProxy.unbound(); + shell.requestService("mojo:asset_bundle", unpacker); + AssetBundleProxy bundle = new AssetBundleProxy.unbound(); + unpacker.ptr.unpackZipStream(bundleData, bundle); + unpacker.close(); + + return new AssetBundle(bundle); +}