New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Read assets out of APK on Android #4742
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// Copyright 2018 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. | ||
|
||
#ifndef FLUTTER_ASSETS_ASSET_PROVIDER_H_ | ||
#define FLUTTER_ASSETS_ASSET_PROVIDER_H_ | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
#include "lib/fxl/memory/ref_counted.h" | ||
|
||
namespace blink { | ||
|
||
class AssetProvider | ||
: public fxl::RefCountedThreadSafe<AssetProvider> | ||
{ | ||
public: | ||
virtual bool GetAsBuffer(const std::string& asset_name, | ||
std::vector<uint8_t>* data) = 0; | ||
virtual ~AssetProvider() = default; | ||
}; | ||
|
||
} // namespace blink | ||
#endif // FLUTTER_ASSETS_ASSET_PROVIDER_H |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,6 +32,7 @@ | |
#include "flutter/assets/directory_asset_bundle.h" | ||
#include "flutter/assets/unzipper_provider.h" | ||
#include "flutter/assets/zip_asset_store.h" | ||
#include "flutter/assets/asset_provider.h" | ||
#include "flutter/common/settings.h" | ||
#include "flutter/common/threads.h" | ||
#include "flutter/glue/trace_event.h" | ||
|
@@ -288,7 +289,6 @@ void Engine::Init(const std::string& bundle_path) { | |
#else | ||
#error Unknown OS | ||
#endif | ||
|
||
blink::InitRuntime(vm_snapshot_data, vm_snapshot_instr, | ||
default_isolate_snapshot_data, | ||
default_isolate_snapshot_instr, bundle_path); | ||
|
@@ -312,7 +312,7 @@ void Engine::RunBundle(const std::string& bundle_path, | |
return; | ||
} | ||
std::vector<uint8_t> snapshot; | ||
if (!GetAssetAsBuffer(blink::kSnapshotAssetKey, &snapshot)) | ||
if (!GetAssetAsBuffer(blink::kSnapshotAssetKey, &snapshot)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indentation seems off here. |
||
return; | ||
runtime_->dart_controller()->RunFromScriptSnapshot( | ||
snapshot.data(), snapshot.size(), entrypoint); | ||
|
@@ -573,6 +573,12 @@ void Engine::SetSemanticsEnabled(bool enabled) { | |
} | ||
|
||
void Engine::ConfigureAssetBundle(const std::string& path) { | ||
auto platform_view = platform_view_.lock(); | ||
asset_provider_ = platform_view->GetAssetProvider(); | ||
if (!asset_provider_) { | ||
asset_provider_ = fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path); | ||
} | ||
|
||
struct stat stat_result = {}; | ||
|
||
// TODO(abarth): We should reset directory_asset_bundle_, but that might break | ||
|
@@ -585,8 +591,6 @@ void Engine::ConfigureAssetBundle(const std::string& path) { | |
|
||
std::string flx_path; | ||
if (S_ISDIR(stat_result.st_mode)) { | ||
directory_asset_bundle_ = | ||
fxl::MakeRefCounted<blink::DirectoryAssetBundle>(path); | ||
flx_path = files::GetDirectoryName(path) + "/app.flx"; | ||
} else if (S_ISREG(stat_result.st_mode)) { | ||
flx_path = path; | ||
|
@@ -618,11 +622,11 @@ void Engine::DidCreateMainIsolate(Dart_Isolate isolate) { | |
blink::TestFontSelector::Install(); | ||
if (!blink::Settings::Get().using_blink) | ||
blink::FontCollection::ForProcess().RegisterTestFonts(); | ||
} else if (directory_asset_bundle_) { | ||
blink::AssetFontSelector::Install(directory_asset_bundle_); | ||
} else if (asset_provider_) { | ||
blink::AssetFontSelector::Install(asset_provider_); | ||
if (!blink::Settings::Get().using_blink) { | ||
blink::FontCollection::ForProcess().RegisterFontsFromDirectoryAssetBundle( | ||
directory_asset_bundle_); | ||
blink::FontCollection::ForProcess().RegisterFontsFromAssetProvider( | ||
asset_provider_); | ||
} | ||
} | ||
} | ||
|
@@ -693,7 +697,6 @@ void Engine::HandleAssetPlatformMessage( | |
const auto& data = message->data(); | ||
std::string asset_name(reinterpret_cast<const char*>(data.data()), | ||
data.size()); | ||
|
||
std::vector<uint8_t> asset_data; | ||
if (GetAssetAsBuffer(asset_name, &asset_data)) { | ||
response->Complete(std::move(asset_data)); | ||
|
@@ -704,9 +707,9 @@ void Engine::HandleAssetPlatformMessage( | |
|
||
bool Engine::GetAssetAsBuffer(const std::string& name, | ||
std::vector<uint8_t>* data) { | ||
return (directory_asset_bundle_ && | ||
directory_asset_bundle_->GetAsBuffer(name, data)) || | ||
(asset_store_ && asset_store_->GetAsBuffer(name, data)); | ||
return ((asset_provider_ && | ||
asset_provider_->GetAsBuffer(name, data)) || | ||
(asset_store_ && asset_store_->GetAsBuffer(name, data))); | ||
} | ||
|
||
} // namespace shell |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
|
||
#include <memory> | ||
|
||
#include "flutter/assets/asset_provider.h" | ||
#include "flutter/flow/texture.h" | ||
#include "flutter/lib/ui/semantics/semantics_node.h" | ||
#include "flutter/shell/common/engine.h" | ||
|
@@ -64,6 +65,8 @@ class PlatformView : public std::enable_shared_from_this<PlatformView> { | |
virtual void HandlePlatformMessage( | ||
fxl::RefPtr<blink::PlatformMessage> message); | ||
|
||
virtual fxl::RefPtr<blink::AssetProvider> GetAssetProvider(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove extra space after |
||
|
||
// Called once per texture, on the platform thread. | ||
void RegisterTexture(std::shared_ptr<flow::Texture> texture); | ||
|
||
|
@@ -97,6 +100,8 @@ class PlatformView : public std::enable_shared_from_this<PlatformView> { | |
flow::TextureRegistry texture_registry_; | ||
std::unique_ptr<Engine> engine_; | ||
std::unique_ptr<VsyncWaiter> vsync_waiter_; | ||
fxl::RefPtr<blink::AssetProvider> asset_provider_; | ||
|
||
SkISize size_; | ||
|
||
private: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include <unistd.h> | ||
#include <algorithm> | ||
#include <sstream> | ||
|
||
#include "flutter/shell/platform/android/apk_asset_provider.h" | ||
#include "lib/fxl/logging.h" | ||
|
||
namespace blink { | ||
|
||
bool APKAssetProvider::GetAsBuffer(const std::string& asset_name, | ||
std::vector<uint8_t>* data) { | ||
std::stringstream ss; | ||
ss << directory_.c_str() << "/" << asset_name; | ||
AAsset* asset = AAssetManager_open(assetManager_, ss.str().c_str(), AASSET_MODE_RANDOM); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think we need random access to the asset bytes. We only want to fill a buffer, right? https://developer.android.com/ndk/reference/group___asset.html |
||
if (!asset) { | ||
return false; | ||
} | ||
|
||
uint8_t* buffer = (uint8_t*)AAsset_getBuffer(asset); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if (!buffer) { | ||
FXL_LOG(ERROR) << "Got null trying to acquire buffer for asset:" << asset; | ||
return false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing |
||
} | ||
|
||
data->resize(AAsset_getLength(asset)); | ||
std::copy(buffer, buffer + data->size() , data->begin()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove space before |
||
AAsset_close(asset); | ||
return true; | ||
} | ||
|
||
APKAssetProvider::~APKAssetProvider() {} | ||
|
||
APKAssetProvider::APKAssetProvider(JNIEnv* env, jobject jassetManager, std::string directory) | ||
: directory_(std::move(directory)) { | ||
assetManager_ = AAssetManager_fromJava(env, jassetManager); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe hold a VM reference to the |
||
} | ||
|
||
} // namespace blink |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we leave out
blink::
here?