From ae517c2fee3f556d5e3786b54b40eb7da69308ef Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 9 Feb 2016 10:07:42 -0800 Subject: [PATCH] Add a way to override the snapshot when running an FLX bundle --- services/asset_bundle/zip_asset_bundle.cc | 29 +++++++++++++++++-- services/asset_bundle/zip_asset_bundle.h | 19 ++++++++---- sky/services/engine/sky_engine.mojom | 4 +++ .../sky/shell/PlatformViewAndroid.java | 8 +++-- .../org/domokit/sky/shell/SkyActivity.java | 15 ++-------- sky/shell/ui/engine.cc | 19 ++++++++++++ sky/shell/ui/engine.h | 2 ++ 7 files changed, 73 insertions(+), 23 deletions(-) diff --git a/services/asset_bundle/zip_asset_bundle.cc b/services/asset_bundle/zip_asset_bundle.cc index 20cdf249c66..08442b08728 100644 --- a/services/asset_bundle/zip_asset_bundle.cc +++ b/services/asset_bundle/zip_asset_bundle.cc @@ -6,18 +6,26 @@ #include "base/bind.h" #include "base/location.h" -#include "base/message_loop/message_loop.h" #include "base/task_runner.h" +#include "base/message_loop/message_loop.h" +#include "mojo/data_pipe_utils/data_pipe_utils.h" #include "third_party/zlib/contrib/minizip/unzip.h" namespace mojo { namespace asset_bundle { -void ZipAssetBundle::Create( +namespace { + +void Ignored(bool) { +} + +} // namespace + +ZipAssetBundle* ZipAssetBundle::Create( InterfaceRequest request, const base::FilePath& zip_path, scoped_refptr worker_runner) { - new ZipAssetBundle(request.Pass(), zip_path, worker_runner.Pass()); + return new ZipAssetBundle(request.Pass(), zip_path, worker_runner.Pass()); } ZipAssetBundle::ZipAssetBundle( @@ -32,12 +40,27 @@ ZipAssetBundle::ZipAssetBundle( ZipAssetBundle::~ZipAssetBundle() { } +void ZipAssetBundle::AddOverlayFile(const std::string& asset_name, + const base::FilePath& file_path) { + overlay_files_.insert(std::make_pair(String(asset_name), file_path)); +} + void ZipAssetBundle::GetAsStream( const String& asset_name, const Callback& callback) { DataPipe pipe; callback.Run(pipe.consumer_handle.Pass()); + auto overlay = overlay_files_.find(asset_name); + if (overlay != overlay_files_.end()) { + common::CopyFromFile(overlay->second, + pipe.producer_handle.Pass(), + 0, + worker_runner_.get(), + base::Bind(&Ignored)); + return; + } + ZipAssetHandler* handler = new ZipAssetHandler( zip_path_, asset_name.To(), diff --git a/services/asset_bundle/zip_asset_bundle.h b/services/asset_bundle/zip_asset_bundle.h index 21165e1c487..9adb04fc0d4 100644 --- a/services/asset_bundle/zip_asset_bundle.h +++ b/services/asset_bundle/zip_asset_bundle.h @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. +#include + #include "base/task_runner.h" #include "base/files/file_path.h" #include "base/memory/weak_ptr.h" @@ -18,9 +20,9 @@ namespace asset_bundle { // ZIP archive. class ZipAssetBundle : public AssetBundle { public: - static void Create(InterfaceRequest request, - const base::FilePath& zip_path, - scoped_refptr worker_runner); + static ZipAssetBundle* Create(InterfaceRequest request, + const base::FilePath& zip_path, + scoped_refptr worker_runner); ~ZipAssetBundle() override; // AssetBundle implementation @@ -28,6 +30,10 @@ class ZipAssetBundle : public AssetBundle { const String& asset_name, const Callback& callback) override; + // Serve this asset from another file instead of using the ZIP contents. + void AddOverlayFile(const std::string& asset_name, + const base::FilePath& file_path); + private: ZipAssetBundle(InterfaceRequest request, const base::FilePath& zip_path, @@ -36,6 +42,7 @@ class ZipAssetBundle : public AssetBundle { StrongBinding binding_; const base::FilePath zip_path_; scoped_refptr worker_runner_; + std::map overlay_files_; DISALLOW_COPY_AND_ASSIGN(ZipAssetBundle); }; @@ -46,9 +53,9 @@ class ZipAssetHandler { public: ZipAssetHandler(const base::FilePath& zip_path, - const std::string& asset_name, - ScopedDataPipeProducerHandle producer, - scoped_refptr worker_runner); + const std::string& asset_name, + ScopedDataPipeProducerHandle producer, + scoped_refptr worker_runner); ~ZipAssetHandler(); void Start(); diff --git a/sky/services/engine/sky_engine.mojom b/sky/services/engine/sky_engine.mojom index 647e162ddc7..65cda4227fd 100644 --- a/sky/services/engine/sky_engine.mojom +++ b/sky/services/engine/sky_engine.mojom @@ -55,4 +55,8 @@ interface SkyEngine { RunFromBundle(string path); RunFromAssetBundle(string url, mojo.asset_bundle.AssetBundle bundle); + + // Run the app from a bundle, but obtain the snapshot from snapshot_path + // instead of using the snapshot within the bundle. + RunFromBundleAndSnapshot(string bundle_path, string snapshot_path); }; diff --git a/sky/shell/platform/android/org/domokit/sky/shell/PlatformViewAndroid.java b/sky/shell/platform/android/org/domokit/sky/shell/PlatformViewAndroid.java index 5a7456415bb..b8e7aab53c6 100644 --- a/sky/shell/platform/android/org/domokit/sky/shell/PlatformViewAndroid.java +++ b/sky/shell/platform/android/org/domokit/sky/shell/PlatformViewAndroid.java @@ -305,7 +305,7 @@ public class PlatformViewAndroid extends SurfaceView mServiceProvider = new PlatformServiceProvider(core, getContext(), localRegistry); } - void runFromBundle(String path) { + void runFromBundle(String bundlePath, String snapshotPath) { if (mServiceProvider != null) { mServiceProvider.close(); @@ -330,7 +330,11 @@ public class PlatformViewAndroid extends SurfaceView resetAccessibilityTree(); - mSkyEngine.runFromBundle(path); + if (snapshotPath != null) { + mSkyEngine.runFromBundleAndSnapshot(bundlePath, snapshotPath); + } else { + mSkyEngine.runFromBundle(bundlePath); + } } private static native long nativeAttach(int inputObserverHandle); diff --git a/sky/shell/platform/android/org/domokit/sky/shell/SkyActivity.java b/sky/shell/platform/android/org/domokit/sky/shell/SkyActivity.java index 73ad14f58fa..3b1d5248fd3 100644 --- a/sky/shell/platform/android/org/domokit/sky/shell/SkyActivity.java +++ b/sky/shell/platform/android/org/domokit/sky/shell/SkyActivity.java @@ -141,7 +141,7 @@ public class SkyActivity extends Activity { File dataDir = new File(PathUtils.getDataDirectory(this)); File appBundle = new File(dataDir, SkyApplication.APP_BUNDLE); if (appBundle.exists()) { - mView.runFromBundle(appBundle.getPath()); + mView.runFromBundle(appBundle.getPath(), null); return; } } @@ -154,7 +154,8 @@ public class SkyActivity extends Activity { String action = intent.getAction(); if (Intent.ACTION_RUN.equals(action)) { - mView.runFromBundle(intent.getDataString()); + mView.runFromBundle(intent.getDataString(), + intent.getStringExtra("snapshot")); String route = intent.getStringExtra("route"); if (route != null) mView.getEngine().pushRoute(route); @@ -164,16 +165,6 @@ public class SkyActivity extends Activity { return false; } - public boolean loadBundleByName(String name) { - File dataDir = new File(PathUtils.getDataDirectory(this)); - File bundle = new File(dataDir, name); - if (!bundle.exists()) { - return false; - } - mView.runFromBundle(bundle.getPath()); - return true; - } - public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); diff --git a/sky/shell/ui/engine.cc b/sky/shell/ui/engine.cc index c711b2d8628..39fb8d002ba 100644 --- a/sky/shell/ui/engine.cc +++ b/sky/shell/ui/engine.cc @@ -244,6 +244,25 @@ void Engine::RunFromAssetBundle(const mojo::String& url, weak_factory_.GetWeakPtr(), url_str)); } +void Engine::RunFromBundleAndSnapshot(const mojo::String& bundle_path, + const mojo::String& snapshot_path) { + TRACE_EVENT0("flutter", "Engine::RunFromBundleAndSnapshot"); + std::string bundle_path_str = bundle_path; + ZipAssetBundle* asset_bundle = ZipAssetBundle::Create( + mojo::GetProxy(&root_bundle_), + base::FilePath(bundle_path_str), + base::WorkerPool::GetTaskRunner(true)); + + std::string snapshot_path_str = snapshot_path; + asset_bundle->AddOverlayFile(kSnapshotKey, + base::FilePath(snapshot_path_str)); + + root_bundle_->GetAsStream(kSnapshotKey, + base::Bind(&Engine::RunFromSnapshotStream, + weak_factory_.GetWeakPtr(), + bundle_path_str)); +} + void Engine::PushRoute(const mojo::String& route) { if (sky_view_) sky_view_->PushRoute(route); diff --git a/sky/shell/ui/engine.h b/sky/shell/ui/engine.h index fb22e5db980..23fddc20398 100644 --- a/sky/shell/ui/engine.h +++ b/sky/shell/ui/engine.h @@ -67,6 +67,8 @@ class Engine : public UIDelegate, void RunFromBundle(const mojo::String& path) override; void RunFromAssetBundle(const mojo::String& url, mojo::asset_bundle::AssetBundlePtr bundle) override; + void RunFromBundleAndSnapshot(const mojo::String& bundle_path, + const mojo::String& snapshot_path) override; void PushRoute(const mojo::String& route) override; void PopRoute() override; void OnAppLifecycleStateChanged(sky::AppLifecycleState state) override;