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