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 "core/components/form/resource/form_manager_resource.h"
17 
18 namespace OHOS::Ace {
19 
20 const char FORM_MANAGER_PARAM_NONE[] = "";
21 const char FORM_MANAGER_PARAM_AND[] = "#HWJS-&-#";
22 const char FORM_MANAGER_PARAM_VALUE[] = "value";
23 const char FORM_MANAGER_PARAM_EQUALS[] = "#HWJS-=-#";
24 const char FORM_MANAGER_PARAM_BEGIN[] = "#HWJS-?-#";
25 const char FORM_MANAGER_METHOD[] = "method";
26 const char FORM_MANAGER_EVENT[] = "event";
27 const char FORM_MANAGER_RESULT_FAIL[] = "fail";
28 
Release(const std::function<void (bool)> & onRelease)29 void FormManagerResource::Release(const std::function<void(bool)>& onRelease)
30 {
31     auto context = context_.Upgrade();
32     if (!context) {
33         LOGE("fail to release resource due to context is null");
34         return;
35     }
36 
37     auto resRegister = context->GetPlatformResRegister();
38     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
39     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
40     auto releaseTask = [weak = WeakClaim(this), weakRes, onRelease] {
41         auto resource = weak.Upgrade();
42         if (!resource || resource->id_ == INVALID_ID) {
43             LOGW("form manager resource has released");
44             return;
45         }
46 
47         auto resRegister = weakRes.Upgrade();
48         if (resRegister) {
49             bool ret = resRegister->ReleaseResource(resource->GetHashCode());
50             if (ret) {
51                 resource->Reset();
52             }
53             if (onRelease) {
54                 onRelease(ret);
55             }
56         }
57     };
58     if (platformTaskExecutor.IsRunOnCurrentThread()) {
59         releaseTask();
60     } else {
61         platformTaskExecutor.PostTask(releaseTask, "ArkUIFormResourceRelease");
62     }
63 }
64 
CallResRegisterMethod(const std::string & method,const std::string & param,const std::function<void (std::string &)> & callback)65 void FormManagerResource::CallResRegisterMethod(
66     const std::string& method, const std::string& param, const std::function<void(std::string&)>& callback)
67 {
68     if (method.empty()) {
69         return;
70     }
71 
72     auto context = context_.Upgrade();
73     if (!context) {
74         LOGE("fail to get context to call res register method");
75         return;
76     }
77 
78     auto resRegister = context->GetPlatformResRegister();
79     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
80 
81     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
82     platformTaskExecutor.PostTask([method, param, weakRes, callback] {
83         auto resRegister = weakRes.Upgrade();
84         if (resRegister == nullptr) {
85             LOGE("resRegister is nullptr");
86             return;
87         }
88         std::string result;
89         resRegister->OnMethodCall(method, param, result);
90         if (callback) {
91             callback(result);
92         }
93     }, "ArkUIFormCallResRegisterMethod");
94 }
95 
GetIntParam(const std::string & param,const std::string & name) const96 int32_t FormManagerResource::GetIntParam(const std::string& param, const std::string& name) const
97 {
98     size_t len = name.length();
99     size_t pos = param.find(name);
100     int32_t result = 0;
101 
102     if (pos != std::string::npos) {
103         std::stringstream ss;
104 
105         ss << param.substr(pos + 1 + len);
106         ss >> result;
107     }
108 
109     return result;
110 }
111 
ParseMapFromString(const std::string & param)112 std::map<std::string, std::string> FormManagerResource::ParseMapFromString(const std::string& param)
113 {
114     size_t equalsLen = sizeof(FORM_MANAGER_PARAM_EQUALS) - 1;
115     size_t andLen = sizeof(FORM_MANAGER_PARAM_EQUALS) - 1;
116     size_t totalLen = param.length();
117     size_t index = 0;
118     std::map<std::string, std::string> result;
119     while (index < totalLen) {
120         size_t end = param.find(FORM_MANAGER_PARAM_AND, index);
121         if (end == std::string::npos) {
122             end = totalLen;
123         }
124 
125         size_t mid = param.find(FORM_MANAGER_PARAM_EQUALS, index);
126         if (mid == std::string::npos) {
127             index = end + andLen;
128             continue;
129         }
130         std::string key = param.substr(index, mid - index);
131         std::string value = param.substr(mid + equalsLen, end - mid - equalsLen);
132         result[key] = value;
133         index = end + andLen;
134     }
135     return result;
136 }
137 
MakeResourceHash() const138 std::string FormManagerResource::MakeResourceHash() const
139 {
140     std::stringstream hashCode;
141     hashCode << type_ << "@" << id_;
142 
143     return hashCode.str();
144 }
145 
MakeEventHash(const std::string & event) const146 std::string FormManagerResource::MakeEventHash(const std::string& event) const
147 {
148     std::string eventHash = hash_;
149 
150     eventHash += std::string(FORM_MANAGER_EVENT);
151     eventHash += std::string(FORM_MANAGER_PARAM_EQUALS);
152     eventHash += event;
153     eventHash += std::string(FORM_MANAGER_PARAM_BEGIN);
154     return eventHash;
155 }
156 
MakeMethodHash(const std::string & method) const157 std::string FormManagerResource::MakeMethodHash(const std::string& method) const
158 {
159     std::string methodHash = hash_;
160 
161     methodHash += std::string(FORM_MANAGER_METHOD);
162     methodHash += std::string(FORM_MANAGER_PARAM_EQUALS);
163     methodHash += method;
164     methodHash += std::string(FORM_MANAGER_PARAM_BEGIN);
165 
166     return methodHash;
167 }
168 
GetStringParam(const std::string & param,const std::string & name) const169 std::string FormManagerResource::GetStringParam(const std::string& param, const std::string& name) const
170 {
171     size_t len = name.length();
172     size_t pos = param.find(name);
173     std::string result;
174 
175     if (pos != std::string::npos) {
176         std::stringstream ss;
177 
178         ss << param.substr(pos + 1 + len);
179         ss >> result;
180     }
181     return result;
182 }
183 
184 } // namespace OHOS::Ace