• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 "cj_want_agent_ffi.h"
17 
18 #include "cj_ability_runtime_error.h"
19 #include "cj_lambda.h"
20 #include "cj_utils_ffi.h"
21 #include "hilog_tag_wrapper.h"
22 #include "start_options.h"
23 #include "want_agent_helper.h"
24 #include "want_params_wrapper.h"
25 
26 namespace OHOS {
27 namespace FfiWantAgent {
28 
29 using namespace OHOS::AbilityRuntime;
30 
31 constexpr int32_t BUSINESS_ERROR_CODE_OK = 0;
32 constexpr int32_t NOTEQ = -1;
33 
CJTriggerCompleteCallBack()34 CJTriggerCompleteCallBack::CJTriggerCompleteCallBack() {}
35 
~CJTriggerCompleteCallBack()36 CJTriggerCompleteCallBack::~CJTriggerCompleteCallBack() {}
37 
SetCallbackInfo(std::function<void (CJCompleteData)> callback)38 void CJTriggerCompleteCallBack::SetCallbackInfo(std::function<void(CJCompleteData)> callback)
39 {
40     callback_ = callback;
41 }
42 
SetWantAgentInstance(int64_t wantAgent)43 void CJTriggerCompleteCallBack::SetWantAgentInstance(int64_t wantAgent)
44 {
45     wantAgent_ = wantAgent;
46 }
47 
OnSendFinished(const AAFwk::Want & want,int resultCode,const std::string & resultData,const AAFwk::WantParams & resultExtras)48 void CJTriggerCompleteCallBack::OnSendFinished(
49     const AAFwk::Want& want, int resultCode, const std::string& resultData, const AAFwk::WantParams& resultExtras)
50 {
51     CJCompleteData data = { .info = wantAgent_,
52         .want = new (std::nothrow) AAFwk::Want(want),
53         .finalCode = resultCode,
54         .finalData = CreateCStringFromString(resultData),
55         .extraInfo = CreateCStringFromString(OHOS::AAFwk::WantParamWrapper(resultExtras).ToString()) };
56     callback_(data);
57 }
58 
OnGetBundleName(int32_t * errCode)59 std::string CJWantAgent::OnGetBundleName(int32_t* errCode)
60 {
61     std::string bundleName = "";
62     *errCode = WantAgentHelper::GetBundleName(wantAgent_, bundleName);
63     return bundleName;
64 }
65 
OnGetUid(int32_t * errCode)66 int32_t CJWantAgent::OnGetUid(int32_t* errCode)
67 {
68     int uid = -1;
69     *errCode = WantAgentHelper::GetUid(wantAgent_, uid);
70     return uid;
71 }
72 
OnCancel(int32_t * errCode)73 void CJWantAgent::OnCancel(int32_t* errCode)
74 {
75     *errCode = WantAgentHelper::Cancel(wantAgent_);
76 }
77 
OnTrigger(CJTriggerInfo cjTriggerInfo,std::function<void (CJCompleteData)> callback,int32_t * errCode)78 void CJWantAgent::OnTrigger(CJTriggerInfo cjTriggerInfo, std::function<void(CJCompleteData)> callback, int32_t* errCode)
79 {
80     TAG_LOGD(AAFwkTag::WANTAGENT, "called");
81     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
82     TriggerInfo triggerInfo;
83     auto triggerObj = std::make_shared<CJTriggerCompleteCallBack>();
84     *errCode = UnWrapTriggerInfoParam(cjTriggerInfo, callback, wantAgent, triggerInfo, triggerObj);
85     if (*errCode != NO_ERROR) {
86         return;
87     }
88     TAG_LOGD(AAFwkTag::WANTAGENT, "called");
89     sptr<CompletedDispatcher> completedData;
90     WantAgentHelper::TriggerWantAgent(wantAgent, triggerObj, triggerInfo, completedData, nullptr);
91 }
92 
UnWrapTriggerInfoParam(CJTriggerInfo cjTriggerInfo,std::function<void (CJCompleteData)> callback,std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> & wantAgent,TriggerInfo & triggerInfo,std::shared_ptr<CJTriggerCompleteCallBack> & triggerObj)93 int32_t CJWantAgent::UnWrapTriggerInfoParam(CJTriggerInfo cjTriggerInfo, std::function<void(CJCompleteData)> callback,
94     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>& wantAgent, TriggerInfo& triggerInfo,
95     std::shared_ptr<CJTriggerCompleteCallBack>& triggerObj)
96 {
97     wantAgent = wantAgent_;
98     std::shared_ptr<AAFwk::Want> want = nullptr;
99     if (cjTriggerInfo.hasWant) {
100         auto actualWant = reinterpret_cast<AAFwk::Want*>(cjTriggerInfo.want);
101         want = std::make_shared<AAFwk::Want>(*actualWant);
102     }
103     std::string permission = std::string(cjTriggerInfo.permission);
104     std::shared_ptr<AAFwk::WantParams> extraInfo = nullptr;
105     if (cjTriggerInfo.extraInfos != nullptr) {
106         extraInfo = std::make_shared<AAFwk::WantParams>(
107             OHOS::AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(cjTriggerInfo.extraInfos));
108     }
109     std::shared_ptr<AAFwk::StartOptions> startOptions = std::make_shared<AAFwk::StartOptions>();
110 
111     TriggerInfo triggerInfoData(permission, extraInfo, want, startOptions, cjTriggerInfo.code);
112     triggerInfo = triggerInfoData;
113     if (triggerObj != nullptr) {
114         triggerObj->SetCallbackInfo(callback);
115         triggerObj->SetWantAgentInstance(GetID());
116     }
117     return BUSINESS_ERROR_CODE_OK;
118 }
119 
OnGetOperationType(int32_t * errCode)120 int32_t CJWantAgent::OnGetOperationType(int32_t* errCode)
121 {
122     int32_t operType;
123     *errCode = WantAgentHelper::GetType(wantAgent_, operType);
124     return operType;
125 }
126 
OnEqual(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> second,int32_t * errCode)127 bool CJWantAgent::OnEqual(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> second, int32_t* errCode)
128 {
129     *errCode = WantAgentHelper::IsEquals(wantAgent_, second);
130     if (*errCode == BUSINESS_ERROR_CODE_OK) {
131         return true;
132     } else if (*errCode == NOTEQ) {
133         *errCode = BUSINESS_ERROR_CODE_OK;
134     }
135     return false;
136 }
137 
138 extern "C" {
FfiWantAgentGetWantAgent(CJWantAgentInfo info,int32_t * errCode)139 int64_t FfiWantAgentGetWantAgent(CJWantAgentInfo info, int32_t* errCode)
140 {
141     std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>(
142         OHOS::AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(info.extraInfos));
143     std::vector<WantAgentConstant::Flags> wantAgentFlags;
144     for (int64_t i = 0; i < info.actionFlags.size; i++) {
145         wantAgentFlags.emplace_back(static_cast<WantAgentConstant::Flags>(info.actionFlags.head[i]));
146     }
147     std::vector<std::shared_ptr<AAFwk::Want>> wants;
148     for (int64_t i = 0; i < info.wants.size; i++) {
149         auto actualWant = reinterpret_cast<AAFwk::Want*>(info.wants.head[i]);
150         wants.emplace_back(std::make_shared<AAFwk::Want>(*actualWant));
151     }
152     WantAgentInfo wantAgentInfo(info.requestCode, static_cast<WantAgentConstant::OperationType>(info.actionType),
153         wantAgentFlags, wants, extraInfo);
154     auto context = Context::GetApplicationContext();
155     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
156     ErrCode result = WantAgentHelper::GetWantAgent(context, wantAgentInfo, wantAgent);
157     if (result != NO_ERROR) {
158         *errCode = result;
159         return -1;
160     } else {
161         if (wantAgent == nullptr) {
162             *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
163             return -1;
164         }
165         auto nativeWantAgent = OHOS::FFI::FFIData::Create<CJWantAgent>(
166             std::make_shared<OHOS::AbilityRuntime::WantAgent::WantAgent>(wantAgent->GetPendingWant()));
167         return nativeWantAgent->GetID();
168     }
169 }
170 
FfiWantAgentGetBoundleName(int64_t cjWantAgent,int32_t * errCode)171 char* FfiWantAgentGetBoundleName(int64_t cjWantAgent, int32_t* errCode)
172 {
173     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
174     if (nativeWantAgent == nullptr) {
175         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
176         return nullptr;
177     }
178     std::string bundleName = nativeWantAgent->OnGetBundleName(errCode);
179     if (*errCode != NO_ERROR) {
180         return nullptr;
181     }
182     return CreateCStringFromString(bundleName);
183 }
184 
FfiWantAgentGetUid(int64_t cjWantAgent,int32_t * errCode)185 int32_t FfiWantAgentGetUid(int64_t cjWantAgent, int32_t* errCode)
186 {
187     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
188     if (nativeWantAgent == nullptr) {
189         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
190         return -1;
191     }
192     return nativeWantAgent->OnGetUid(errCode);
193 }
194 
FfiWantAgentCancel(int64_t cjWantAgent,int32_t * errCode)195 void FfiWantAgentCancel(int64_t cjWantAgent, int32_t* errCode)
196 {
197     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
198     if (nativeWantAgent == nullptr) {
199         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
200         return;
201     }
202     return nativeWantAgent->OnCancel(errCode);
203 }
204 
FfiWantAgentTrigger(int64_t cjWantAgent,CJTriggerInfo triggerInfo,void (* callback)(CJCompleteData),int32_t * errCode)205 void FfiWantAgentTrigger(
206     int64_t cjWantAgent, CJTriggerInfo triggerInfo, void (*callback)(CJCompleteData), int32_t* errCode)
207 {
208     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
209     if (nativeWantAgent == nullptr) {
210         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
211         return;
212     }
213     return nativeWantAgent->OnTrigger(triggerInfo, CJLambda::Create(callback), errCode);
214 }
215 
FfiWantAgentGetOperationType(int64_t cjWantAgent,int32_t * errCode)216 int32_t FfiWantAgentGetOperationType(int64_t cjWantAgent, int32_t* errCode)
217 {
218     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
219     if (nativeWantAgent == nullptr) {
220         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
221         return -1;
222     }
223     return nativeWantAgent->OnGetOperationType(errCode);
224 }
225 
FfiWantAgentEqual(int64_t cjWantAgentFirst,int64_t cjWantAgentSecond,int32_t * errCode)226 bool FfiWantAgentEqual(int64_t cjWantAgentFirst, int64_t cjWantAgentSecond, int32_t* errCode)
227 {
228     auto nativeWantAgentFirst = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgentFirst);
229     auto nativeWantAgentSecond = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgentSecond);
230     if (nativeWantAgentFirst == nullptr || nativeWantAgentSecond == nullptr) {
231         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
232         return false;
233     }
234     return nativeWantAgentFirst->OnEqual(nativeWantAgentSecond->wantAgent_, errCode);
235 }
236 }
237 } // namespace FfiWantAgent
238 } // namespace OHOS