• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ability_component/resource/ability_component_resource.h"
17 
18 namespace OHOS::Ace {
19 
20 const char ABILITY_COMPONENT_PARAM_AND[] = "#HWJS-&-#";
21 const char ABILITY_COMPONENT_PARAM_EQUALS[] = "#HWJS-=-#";
22 const char ABILITY_COMPONENT_PARAM_BEGIN[] = "#HWJS-?-#";
23 const char ABILITY_COMPONENT_METHOD[] = "method";
24 const char ABILITY_COMPONENT_EVENT[] = "event";
25 
Release(const std::function<void (bool)> & onRelease)26 void AbilityComponentResource::Release(const std::function<void(bool)>& onRelease)
27 {
28     auto context = context_.Upgrade();
29     if (!context) {
30         LOGE("fail to release resource due to context is null");
31         return;
32     }
33 
34     auto resRegister = context->GetPlatformResRegister();
35     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
36     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
37     auto releaseTask = [id = id_, hashCode = GetHashCode(), weakRes, onRelease] {
38         if (id == INVALID_ID) {
39             LOGW("ability component resource has released");
40             return;
41         }
42 
43         auto resRegister = weakRes.Upgrade();
44         if (resRegister) {
45             bool ret = resRegister->ReleaseResource(hashCode);
46             if (onRelease) {
47                 onRelease(ret);
48             }
49         }
50     };
51     if (platformTaskExecutor.IsRunOnCurrentThread()) {
52         releaseTask();
53     } else {
54         platformTaskExecutor.PostTask(releaseTask, "ArkUIAbilityComponentRelease");
55     }
56 }
57 
MakeResourceHash() const58 std::string AbilityComponentResource::MakeResourceHash() const
59 {
60     std::stringstream hashCode;
61     hashCode << type_ << "@" << id_;
62 
63     return hashCode.str();
64 }
65 
MakeEventHash(const std::string & event) const66 std::string AbilityComponentResource::MakeEventHash(const std::string& event) const
67 {
68     std::string eventHash = hash_;
69 
70     eventHash += std::string(ABILITY_COMPONENT_EVENT);
71     eventHash += std::string(ABILITY_COMPONENT_PARAM_EQUALS);
72     eventHash += event;
73     eventHash += std::string(ABILITY_COMPONENT_PARAM_BEGIN);
74 
75     return eventHash;
76 }
77 
MakeMethodHash(const std::string & method) const78 std::string AbilityComponentResource::MakeMethodHash(const std::string& method) const
79 {
80     std::string methodHash = hash_;
81 
82     methodHash += std::string(ABILITY_COMPONENT_METHOD);
83     methodHash += std::string(ABILITY_COMPONENT_PARAM_EQUALS);
84     methodHash += method;
85     methodHash += std::string(ABILITY_COMPONENT_PARAM_BEGIN);
86 
87     return methodHash;
88 }
89 
CallResRegisterMethod(const std::string & method,const std::string & param,bool useSyncTask)90 std::string AbilityComponentResource::CallResRegisterMethod(
91     const std::string& method, const std::string& param, bool useSyncTask)
92 {
93     if (method.empty()) {
94         return "";
95     }
96 
97     auto context = context_.Upgrade();
98     if (!context) {
99         LOGE("fail to get context to call res register method");
100         return "";
101     }
102 
103     auto resRegister = context->GetPlatformResRegister();
104     auto platformTaskExecutor = SingleTaskExecutor::Make(context->GetTaskExecutor(), TaskExecutor::TaskType::PLATFORM);
105     auto weakRes = AceType::WeakClaim(AceType::RawPtr(resRegister));
106 
107     if (useSyncTask) {
108         std::string result;
109         platformTaskExecutor.PostSyncTask(
110             [method, param, weakRes, &result] {
111                 auto resRegister = weakRes.Upgrade();
112                 if (resRegister != nullptr) {
113                     resRegister->OnMethodCall(method, param, result);
114                 }
115             },
116             "ArkUIAbilityComponentCallResRegister");
117         return result;
118     } else {
119         platformTaskExecutor.PostTask(
120             [method, param, weakRes] {
121                 auto resRegister = weakRes.Upgrade();
122                 if (resRegister != nullptr) {
123                     std::string result;
124                     resRegister->OnMethodCall(method, param, result);
125                 }
126             },
127             "ArkUIAbilityComponentCallResRegister");
128     }
129     return "";
130 }
131 
132 } // namespace OHOS::Ace
133