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 "core/pipeline_ng/pipeline_context.h"
19 #include "frameworks/bridge/common/utils/utils.h"
20 #include "frameworks/core/components_ng/base/inspector.h"
21
22 namespace OHOS::Ace::Framework {
23 namespace {
24
25 const char SLASH = '/';
26 const char SLASHSTR[] = "/";
27 const char SUPDIRECTORY[] = "../";
28
GetFileInfo(const std::string & fileUri,std::string & fileName,std::string & filePath)29 void GetFileInfo(const std::string& fileUri, std::string& fileName, std::string& filePath)
30 {
31 size_t slashPos = fileUri.find_last_of(SLASH);
32 if (slashPos == std::string::npos) {
33 fileName = fileUri;
34 } else {
35 fileName = fileUri.substr(slashPos + 1);
36 filePath = fileUri.substr(0, slashPos + 1);
37 }
38
39 if (StartWith(filePath, SLASHSTR)) {
40 filePath = filePath.substr(1);
41 }
42 }
43
ParseWorkerUri(const RefPtr<AssetManager> & assetManager,const std::string & fileUri,std::string & assetsFilePath)44 bool ParseWorkerUri(const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath)
45 {
46 if (!assetManager || fileUri.empty() || (fileUri.length() > PATH_MAX)) {
47 return false;
48 }
49
50 std::string fileName;
51 std::string filePath;
52 GetFileInfo(fileUri, fileName, filePath);
53 if (StartWith(filePath, SUPDIRECTORY)) {
54 filePath = filePath.substr(3); // 3 : offset of filePath
55 }
56 std::vector<std::string> files;
57 assetManager->GetAssetList(filePath, files);
58
59 bool fileExist = false;
60 for (const auto& file : files) {
61 size_t filePos = file.find_last_of(SLASH);
62 if (filePos != std::string::npos) {
63 if (file.substr(filePos + 1) == fileName) {
64 assetsFilePath = filePath + fileName;
65 fileExist = true;
66 break;
67 }
68 }
69 }
70 return fileExist;
71 }
72
ParseFileUri(const RefPtr<AssetManager> & assetManager,const std::string & fileUri,std::string & assetsFilePath)73 bool ParseFileUri(const RefPtr<AssetManager>& assetManager, const std::string& fileUri, std::string& assetsFilePath)
74 {
75 if (!assetManager || fileUri.empty() || (fileUri.length() > PATH_MAX)) {
76 return false;
77 }
78
79 std::string fileName;
80 std::string filePath;
81 GetFileInfo(fileUri, fileName, filePath);
82 std::vector<std::string> files;
83 assetManager->GetAssetList(filePath, files);
84
85 bool fileExist = false;
86 for (const auto& file : files) {
87 bool startWithSlash = StartWith(file, SLASHSTR);
88 if ((startWithSlash && (file.substr(1) == fileName)) || (!startWithSlash && (file == fileName))) {
89 assetsFilePath = filePath + file;
90 fileExist = true;
91 break;
92 }
93 }
94
95 return fileExist;
96 }
97
98 } // namespace
99
100 template<typename T>
GetResourceData(const std::string & fileUri,T & content)101 bool FrontendDelegate::GetResourceData(const std::string& fileUri, T& content)
102 {
103 std::string targetFilePath;
104 if (!ParseFileUri(assetManager_, fileUri, targetFilePath)) {
105 LOGE("GetResourceData parse file uri failed.");
106 return false;
107 }
108 if (!GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
109 LOGE("GetResourceData GetAssetContent failed.");
110 return false;
111 }
112
113 return true;
114 }
115
GetRectangleById(const std::string & key,NG::Rectangle & rectangle)116 void FrontendDelegate::GetRectangleById(const std::string& key, NG::Rectangle& rectangle)
117 {
118 NG::Inspector::GetRectangleById(key, rectangle);
119 }
120
ResetFocus()121 void FrontendDelegate::ResetFocus()
122 {
123 TAG_LOGI(AceLogTag::ACE_FOCUS, "Lost focus to view root scope by user");
124 NG::FocusHub::LostFocusToViewRoot();
125 }
126
RequestFocus(const std::string & value,bool isSyncRequest)127 bool FrontendDelegate::RequestFocus(const std::string& value, bool isSyncRequest)
128 {
129 auto pipeline = NG::PipelineContext::GetCurrentContext();
130 CHECK_NULL_RETURN(pipeline, false);
131 return pipeline->RequestFocus(value, isSyncRequest);
132 }
133
SetRequestFocusCallback(std::function<void (NG::RequestFocusResult result)> callback)134 void FrontendDelegate::SetRequestFocusCallback(
135 std::function<void(NG::RequestFocusResult result)> callback)
136 {
137 auto pipeline = NG::PipelineContext::GetCurrentContext();
138 CHECK_NULL_VOID(pipeline);
139 auto focusManager = pipeline->GetOrCreateFocusManager();
140 CHECK_NULL_VOID(focusManager);
141 focusManager->SetRequestFocusCallback(callback);
142 }
143
ResetRequestFocusCallback()144 void FrontendDelegate::ResetRequestFocusCallback()
145 {
146 auto pipeline = NG::PipelineContext::GetCurrentContext();
147 CHECK_NULL_VOID(pipeline);
148 auto focusManager = pipeline->GetOrCreateFocusManager();
149 CHECK_NULL_VOID(focusManager);
150 focusManager->ResetRequestFocusCallback();
151 }
152
SetAutoFocusTransfer(bool isAutoFocusTransfer)153 void FrontendDelegate::SetAutoFocusTransfer(bool isAutoFocusTransfer)
154 {
155 auto pipeline = NG::PipelineContext::GetCurrentContext();
156 CHECK_NULL_VOID(pipeline);
157 auto focusManager = pipeline->GetOrCreateFocusManager();
158 CHECK_NULL_VOID(focusManager);
159 focusManager->SetIsAutoFocusTransfer(isAutoFocusTransfer);
160 }
161
SetKeyProcessingMode(int32_t keyProcessingMode)162 void FrontendDelegate::SetKeyProcessingMode(int32_t keyProcessingMode)
163 {
164 auto pipeline = NG::PipelineContext::GetCurrentContext();
165 CHECK_NULL_VOID(pipeline);
166 auto focusManager = pipeline->GetOrCreateFocusManager();
167 CHECK_NULL_VOID(focusManager);
168 focusManager->SetKeyProcessingMode(static_cast<NG::KeyProcessingMode>(keyProcessingMode));
169 }
170
Activate(bool isActive,bool autoInactive)171 bool FrontendDelegate::Activate(bool isActive, bool autoInactive)
172 {
173 auto pipeline = NG::PipelineContext::GetCurrentContext();
174 CHECK_NULL_RETURN(pipeline, false);
175 return pipeline->SetIsFocusActive(isActive, NG::FocusActiveReason::USE_API, autoInactive);
176 }
177
178 template<typename T>
GetResourceData(const std::string & fileUri,T & content,std::string & ami)179 bool FrontendDelegate::GetResourceData(const std::string& fileUri, T& content, std::string& ami)
180 {
181 std::string targetFilePath;
182 if (!ParseWorkerUri(assetManager_, fileUri, targetFilePath)) {
183 LOGE("GetResourceData parse file uri failed.");
184 return false;
185 }
186 ami = assetManager_->GetAssetPath(targetFilePath, true) + targetFilePath;
187 LOGI("GetResourceData ami: %{private}s.", ami.c_str());
188 if (!GetAssetContentAllowEmpty(assetManager_, targetFilePath, content)) {
189 LOGE("GetResourceData GetAssetContent failed.");
190 return false;
191 }
192 return true;
193 }
194
195 template<typename T>
GetResourceData(const std::string & fileUri,const RefPtr<AssetManager> & assetManager,T & content)196 bool FrontendDelegate::GetResourceData(const std::string& fileUri, const RefPtr<AssetManager>& assetManager, T& content)
197 {
198 std::string targetFilePath;
199 if (!ParseFileUri(assetManager, fileUri, targetFilePath)) {
200 LOGE("GetResourceData parse file uri failed.");
201 return false;
202 }
203 if (!GetAssetContentAllowEmpty(assetManager, targetFilePath, content)) {
204 LOGE("GetResourceData GetAssetContent failed.");
205 return false;
206 }
207
208 return true;
209 }
210
211 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::string& content);
212 template bool FrontendDelegate::GetResourceData(const std::string& fileUri, std::vector<uint8_t>& content);
213 template bool FrontendDelegate::GetResourceData(
214 const std::string& fileUri, std::vector<uint8_t>& content, std::string& ami);
215 template bool FrontendDelegate::GetResourceData(
216 const std::string& fileUri, const RefPtr<AssetManager>& assetManager, std::vector<uint8_t>& content);
217
218 } // namespace OHOS::Ace::Framework
219