Flutter Engine
The Flutter Engine
apk_asset_provider.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/shell/platform/android/apk_asset_provider.h"
6
7#include <unistd.h>
8
9#include <algorithm>
10#include <sstream>
11#include <utility>
12
13#include "flutter/assets/asset_resolver.h"
14#include "flutter/fml/logging.h"
15
16namespace flutter {
17
19 public:
20 explicit APKAssetMapping(AAsset* asset) : asset_(asset) {}
21
22 ~APKAssetMapping() override { AAsset_close(asset_); }
23
24 size_t GetSize() const override { return AAsset_getLength(asset_); }
25
26 const uint8_t* GetMapping() const override {
27 return reinterpret_cast<const uint8_t*>(AAsset_getBuffer(asset_));
28 }
29
30 bool IsDontNeedSafe() const override { return !AAsset_isAllocated(asset_); }
31
32 private:
33 AAsset* const asset_;
34
35 FML_DISALLOW_COPY_AND_ASSIGN(APKAssetMapping);
36};
37
39 public:
40 explicit APKAssetProviderImpl(JNIEnv* env,
41 jobject jassetManager,
42 std::string directory)
43 : java_asset_manager_(env, jassetManager),
44 directory_(std::move(directory)) {
45 asset_manager_ = AAssetManager_fromJava(env, jassetManager);
46 }
47
49
50 std::unique_ptr<fml::Mapping> GetAsMapping(
51 const std::string& asset_name) const override {
52 std::stringstream ss;
53 ss << directory_.c_str() << "/" << asset_name;
54 AAsset* asset = AAssetManager_open(asset_manager_, ss.str().c_str(),
55 AASSET_MODE_BUFFER);
56 if (!asset) {
57 return nullptr;
58 }
59
60 return std::make_unique<APKAssetMapping>(asset);
61 };
62
63 private:
65 AAssetManager* asset_manager_;
66 const std::string directory_;
67
68 FML_DISALLOW_COPY_AND_ASSIGN(APKAssetProviderImpl);
69};
70
72 jobject assetManager,
73 std::string directory)
74 : impl_(std::make_shared<APKAssetProviderImpl>(env,
75 assetManager,
76 std::move(directory))) {}
77
79 std::shared_ptr<APKAssetProviderInternal> impl)
80 : impl_(std::move(impl)) {}
81
82// |AssetResolver|
83bool APKAssetProvider::IsValid() const {
84 return true;
85}
86
87// |AssetResolver|
88bool APKAssetProvider::IsValidAfterAssetManagerChange() const {
89 return true;
90}
91
92// |AssetResolver|
93AssetResolver::AssetResolverType APKAssetProvider::GetType() const {
94 return AssetResolver::AssetResolverType::kApkAssetProvider;
95}
96
97// |AssetResolver|
98std::unique_ptr<fml::Mapping> APKAssetProvider::GetAsMapping(
99 const std::string& asset_name) const {
100 return impl_->GetAsMapping(asset_name);
101}
102
103std::unique_ptr<APKAssetProvider> APKAssetProvider::Clone() const {
104 return std::make_unique<APKAssetProvider>(impl_);
105}
106
108 auto other_provider = other.as_apk_asset_provider();
109 if (!other_provider) {
110 return false;
111 }
112 return impl_ == other_provider->impl_;
113}
114
115} // namespace flutter
size_t GetSize() const override
const uint8_t * GetMapping() const override
bool IsDontNeedSafe() const override
std::unique_ptr< fml::Mapping > GetAsMapping(const std::string &asset_name) const override
APKAssetProviderImpl(JNIEnv *env, jobject jassetManager, std::string directory)
bool operator==(const AssetResolver &other) const override
APKAssetProvider(JNIEnv *env, jobject assetManager, std::string directory)
std::unique_ptr< APKAssetProvider > Clone() const
AssetResolverType
Identifies the type of AssetResolver an instance is.
virtual const APKAssetProvider * as_apk_asset_provider() const
Definition: __init__.py:1
Definition: ref_ptr.h:256