1 /*
2  * Copyright (c) 2022 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 OHOS_RESOURCE_ADAPTER_IMPL_H
17 #define OHOS_RESOURCE_ADAPTER_IMPL_H
18 
19 #include "extractor.h"
20 #include "file_mapper.h"
21 #include "ohos_resource_adapter.h"
22 
23 namespace OHOS::NWeb {
24 class OhosFileMapperImpl : public OhosFileMapper {
25 public:
26     OhosFileMapperImpl(std::unique_ptr<OHOS::AbilityBase::FileMapper> fileMap,
27         const std::shared_ptr<OHOS::AbilityBase::Extractor>& extractor);
28 
29     ~OhosFileMapperImpl() override = default;
30 
31     int32_t GetFd() override;
32 
33     int32_t GetOffset() override;
34 
35     std::string GetFileName() override;
36 
37     bool IsCompressed() override;
38 
39     void* GetDataPtr() override;
40 
41     size_t GetDataLen() override;
42 
43     bool UnzipData(uint8_t** dest, size_t& len) override;
44 
45 private:
46     std::shared_ptr<OHOS::AbilityBase::Extractor> extractor_ = nullptr;
47 
48     std::unique_ptr<OHOS::AbilityBase::FileMapper> fileMap_ = nullptr;
49 };
50 
51 class OhosResourceAdapterImpl : public OhosResourceAdapter {
52 public:
53     explicit OhosResourceAdapterImpl(const std::string& hapPath);
54 
55     ~OhosResourceAdapterImpl() override = default;
56 
57     bool GetRawFileData(const std::string& rawFile, size_t& len,
58         uint8_t** dest, bool isSys = false) override;
59 
60     std::shared_ptr<OhosFileMapper> GetRawFileMapper(const std::string& rawFile,
61         bool isSys = false) override;
62 
63     bool IsRawFileExist(const std::string& rawFile, bool isSys = false) override;
64 
65     bool GetRawFileLastModTime(const std::string& rawFile,
66         uint16_t& date, uint16_t& time, bool isSys = false) override;
67 
68     bool GetRawFileLastModTime(const std::string& rawFile, time_t& time, bool isSys = false) override;
69 
70     static bool GetResourceString(const std::string& bundleName, const std::string& moduleName,
71         const int32_t resId, std::string& result);
72 
73     static std::string GetArkWebVersion();
74 
75     static void SetArkWebCoreHapPathOverride(const std::string& hapPath);
76 
77     std::string GetSystemLanguage() override;
78 
79 private:
80     void Init(const std::string& hapPath);
81 
82     static std::string GetModuleName(const char *configStr, size_t len);
83 
84     static std::string ParseModuleName(const std::shared_ptr<OHOS::AbilityBase::Extractor> &manager);
85 
86     static bool GetRawFileData(const std::shared_ptr<OHOS::AbilityBase::Extractor>& manager,
87         const std::string& rawFile, size_t& len, std::unique_ptr<uint8_t[]>& dest);
88 
89     static bool HasEntry(const std::shared_ptr<OHOS::AbilityBase::Extractor>& manager,
90         const std::string& rawFile);
91 
92     static bool GetFileInfo(const std::shared_ptr<OHOS::AbilityBase::Extractor>& manager,
93         const std::string& rawFile, OHOS::AbilityBase::FileInfo& info);
94 
95     static std::shared_ptr<OhosFileMapper> GetRawFileMapper(
96         const std::shared_ptr<OHOS::AbilityBase::Extractor>& manager,
97         const std::string& rawFile);
98 
99     std::shared_ptr<OHOS::AbilityBase::Extractor> sysExtractor_;
100     std::shared_ptr<OHOS::AbilityBase::Extractor> extractor_;
101     static std::string arkWebCoreHapPathOverride_;
102 };
103 }  // namespace OHOS::NWeb
104 
105 #endif  // OHOS_RESOURCE_ADAPTER_IMPL_H
106