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 #include "frameworks/bridge/js_frontend/frontend_delegate.h"
17
18 #include <string>
19
20 #include "core/common/ace_application_info.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 #include "frameworks/core/components_ng/base/inspector.h"
23
24 namespace OHOS::Ace::Framework {
25 namespace {
26
27 const char SLASH = '/';
28 const char SLASHSTR[] = "/";
29 const char SUPDIRECTORY[] = "../";
30
GetFileInfo(const std::string & fileUri,std::string & fileName,std::string & filePath)31 void GetFileInfo(const std::string& fileUri, std::string& fileName, std::string& filePath)
32 {
33 size_t slashPos = fileUri.find_last_of(SLASH);
34 if (slashPos == std::string::npos) {
35 fileName = fileUri;
36 } else {
37 fileName = fileUri.substr(slashPos + 1);
38 filePath = fileUri.substr(0, slashPos + 1);
39 }
40
41 if (StartWith(filePath, SLASHSTR)) {
42 filePath = filePath.substr(1);
43 }
44 }
45
ParseWorkerUri(const RefPtr<AssetManager> & assetManager,const std::string & fileUri,std::string & assetsFilePath)46 bool ParseWorkerUri(const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath)
47 {
48 if (!assetManager || fileUri.empty() || (fileUri.length() > PATH_MAX)) {
49 return false;
50 }
51
52 std::string fileName;
53 std::string filePath;
54 GetFileInfo(fileUri, fileName, filePath);
55 if (StartWith(filePath, SUPDIRECTORY)) {
56 filePath = filePath.substr(3); // 3 : offset of filePath
57 }
58 std::vector<std::string> files;
59 assetManager->GetAssetList(filePath, files);
60
61 bool fileExist = false;
62 for (const auto& file : files) {
63 size_t filePos = file.find_last_of(SLASH);
64 if (filePos != std::string::npos) {
65 if (file.substr(filePos + 1) == fileName) {
66 assetsFilePath = filePath + fileName;
67 fileExist = true;
68 break;
69 }
70 }
71 }
72 return fileExist;
73 }
74
ParseFileUri(const RefPtr<AssetManager> & assetManager,const std::string & fileUri,std::string & assetsFilePath)75 bool ParseFileUri(const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath)
76 {
77 if (!assetManager || fileUri.empty() || (fileUri.length() > PATH_MAX)) {
78 return false;
79 }
80
81 std::string fileName;
82 std::string filePath;
83 GetFileInfo(fileUri, fileName, filePath);
84 std::vector<std::string> files;
85 assetManager->GetAssetList(filePath, files);
86
87 bool fileExist = false;
88 for (const auto& file : files) {
89 bool startWithSlash = StartWith(file, SLASHSTR);
90 if ((startWithSlash && (file.substr(1) == fileName)) || (!startWithSlash && (file == fileName))) {
91 assetsFilePath = filePath + file;
92 fileExist = true;
93 break;
94 }
95 }
96
97 return fileExist;
98 }
99
100 } // namespace
101
102 template<typename T>
GetResourceData(const std::string & fileUri,T & content)103 bool FrontendDelegate::GetResourceData(const std::string& fileUri, T& content)
104 {
105 std::string targetFilePath;
106 if (!ParseFileUri(assetManager_, fileUri, targetFilePath)) {
107 LOGE("GetResourceData parse file uri failed.");
108 return false;
109 }
110 if (!GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
111 LOGE("GetResourceData GetAssetContent failed.");
112 return false;
113 }
114
115 return true;
116 }
117
GetRectangleById(const std::string & key,NG::Rectangle & rectangle)118 void FrontendDelegate::GetRectangleById(const std::string& key, NG::Rectangle& rectangle)
119 {
120 NG::Inspector::GetRectangleById(key, rectangle);
121 }
122
123 template<typename T>
GetResourceData(const std::string & fileUri,T & content,std::string & ami)124 bool FrontendDelegate::GetResourceData(const std::string& fileUri, T& content, std::string& ami)
125 {
126 std::string targetFilePath;
127 if (!ParseWorkerUri(assetManager_, fileUri, targetFilePath)) {
128 LOGE("GetResourceData parse file uri failed.");
129 return false;
130 }
131 ami = assetManager_->GetAssetPath(targetFilePath, true) + targetFilePath;
132 LOGI("GetResourceData ami: %{private}s.", ami.c_str());
133 if (!GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
134 LOGE("GetResourceData GetAssetContent failed.");
135 return false;
136 }
137 return true;
138 }
139
140 template<typename T>
GetResourceData(const std::string & fileUri,const RefPtr<AssetManager> & assetManager,T & content)141 bool FrontendDelegate::GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager, T& content)
142 {
143 std::string targetFilePath;
144 if (!ParseFileUri(assetManager, fileUri, targetFilePath)) {
145 LOGE("GetResourceData parse file uri failed.");
146 return false;
147 }
148 if (!GetAssetContentAllowEmpty(assetManager, targetFilePath, content)) {
149 LOGE("GetResourceData GetAssetContent failed.");
150 return false;
151 }
152
153 return true;
154 }
155
156 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::string& content);
157 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::vector<uint8_t>& content);
158 template bool FrontendDelegate::GetResourceData(
159 const std::string& fileUri, std::vector<uint8_t>& content, std::string& ami);
160 template bool FrontendDelegate::GetResourceData(
161 const std::string& fileUri, const RefPtr<AssetManager>& assetManager, std::vector<uint8_t>& content);
162
163 } // namespace OHOS::Ace::Framework
164