• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_ASSET_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_ASSET_MANAGER_H
18 
19 #include <deque>
20 #include <vector>
21 
22 #include "flutter/assets/asset_resolver.h"
23 #include "flutter/fml/mapping.h"
24 
25 #include "base/log/event_report.h"
26 #include "base/memory/ace_type.h"
27 #include "base/resource/asset_manager.h"
28 #include "base/utils/macros.h"
29 
30 namespace OHOS::Ace {
31 
32 class FlutterAsset final : public Asset {
33 public:
FlutterAsset(std::unique_ptr<fml::Mapping> mapping)34     explicit FlutterAsset(std::unique_ptr<fml::Mapping> mapping) : mapping_(std::move(mapping)) {}
35     ~FlutterAsset() override = default;
36 
GetSize()37     size_t GetSize() const override
38     {
39         return mapping_ ? mapping_->GetSize() : 0;
40     }
41 
GetData()42     const uint8_t* GetData() const override
43     {
44         return mapping_ ? mapping_->GetMapping() : nullptr;
45     }
46 
47 private:
48     std::unique_ptr<fml::Mapping> mapping_;
49 };
50 
51 class FlutterAssetProvider : public AssetProvider {
52     DECLARE_ACE_TYPE(FlutterAssetProvider, AssetProvider);
53 
54 public:
55     virtual std::unique_ptr<fml::Mapping> GetAsMapping(const std::string& assetName) const = 0;
56 };
57 
58 class ACE_EXPORT FlutterAssetManager final : public AssetManager {
59     DECLARE_ACE_TYPE(FlutterAssetManager, AssetManager);
60 
61 public:
62     FlutterAssetManager() = default;
63     ~FlutterAssetManager() override = default;
64 
PushFront(RefPtr<AssetProvider> provider)65     void PushFront(RefPtr<AssetProvider> provider) override
66     {
67         if (!provider || !provider->IsValid()) {
68             return;
69         }
70         providers_.push_front(std::move(provider));
71     }
72 
PushBack(RefPtr<AssetProvider> provider)73     void PushBack(RefPtr<AssetProvider> provider) override
74     {
75         if (!provider || !provider->IsValid()) {
76             return;
77         }
78         providers_.push_back(std::move(provider));
79     }
80 
81     RefPtr<Asset> GetAsset(const std::string& assetName) override;
82 
83     std::string GetAssetPath(const std::string& assetName) override;
84 
SetLibPath(const std::string & appLibPathKey,const std::vector<std::string> & packagePath)85     void SetLibPath(const std::string& appLibPathKey, const std::vector<std::string>& packagePath) override
86     {
87         appLibPathKey_ = appLibPathKey;
88         packagePath_ = packagePath;
89     }
90 
GetLibPath()91     std::vector<std::string> GetLibPath() const override
92     {
93         return packagePath_;
94     }
95 
GetAppLibPathKey()96     std::string GetAppLibPathKey() const override
97     {
98         return appLibPathKey_;
99     }
100 
101     void GetAssetList(const std::string& path, std::vector<std::string>& assetList) const override;
102 
103     void ReloadProvider();
104 
105 private:
106     std::deque<RefPtr<AssetProvider>> providers_;
107     std::vector<std::string> packagePath_;
108     std::string appLibPathKey_;
109 };
110 
111 } // namespace OHOS::Ace
112 
113 #endif // FOUNDATION_ACE_FRAMEWORKS_COMMON_FLUTTER_FLUTTER_ASSET_MANAGER_H
114