• 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     WantAgentHelper::TriggerWantAgent(wantAgent, triggerObj, triggerInfo);
90 }
91 
UnWrapTriggerInfoParam(CJTriggerInfo cjTriggerInfo,std::function<void (CJCompleteData)> callback,std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> & wantAgent,TriggerInfo & triggerInfo,std::shared_ptr<CJTriggerCompleteCallBack> & triggerObj)92 int32_t CJWantAgent::UnWrapTriggerInfoParam(CJTriggerInfo cjTriggerInfo, std::function<void(CJCompleteData)> callback,
93     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent>& wantAgent, TriggerInfo& triggerInfo,
94     std::shared_ptr<CJTriggerCompleteCallBack>& triggerObj)
95 {
96     wantAgent = wantAgent_;
97     std::shared_ptr<AAFwk::Want> want = nullptr;
98     if (cjTriggerInfo.hasWant) {
99         auto actualWant = reinterpret_cast<AAFwk::Want*>(cjTriggerInfo.want);
100         want = std::make_shared<AAFwk::Want>(*actualWant);
101     }
102     std::string permission = std::string(cjTriggerInfo.permission);
103     std::shared_ptr<AAFwk::WantParams> extraInfo = nullptr;
104     if (cjTriggerInfo.extraInfos != nullptr) {
105         extraInfo = std::make_shared<AAFwk::WantParams>(
106             OHOS::AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(cjTriggerInfo.extraInfos));
107     }
108     std::shared_ptr<AAFwk::StartOptions> startOptions = std::make_shared<AAFwk::StartOptions>();
109 
110     TriggerInfo triggerInfoData(permission, extraInfo, want, startOptions, cjTriggerInfo.code);
111     triggerInfo = triggerInfoData;
112     if (triggerObj != nullptr) {
113         triggerObj->SetCallbackInfo(callback);
114         triggerObj->SetWantAgentInstance(GetID());
115     }
116     return BUSINESS_ERROR_CODE_OK;
117 }
118 
OnGetOperationType(int32_t * errCode)119 int32_t CJWantAgent::OnGetOperationType(int32_t* errCode)
120 {
121     int32_t operType;
122     *errCode = WantAgentHelper::GetType(wantAgent_, operType);
123     return operType;
124 }
125 
OnEqual(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> second,int32_t * errCode)126 bool CJWantAgent::OnEqual(std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> second, int32_t* errCode)
127 {
128     *errCode = WantAgentHelper::IsEquals(wantAgent_, second);
129     if (*errCode == BUSINESS_ERROR_CODE_OK) {
130         return true;
131     } else if (*errCode == NOTEQ) {
132         *errCode = BUSINESS_ERROR_CODE_OK;
133     }
134     return false;
135 }
136 
137 extern "C" {
FfiWantAgentGetWantAgent(CJWantAgentInfo info,int32_t * errCode)138 int64_t FfiWantAgentGetWantAgent(CJWantAgentInfo info, int32_t* errCode)
139 {
140     std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>(
141         OHOS::AAFwk::WantParamWrapper::ParseWantParamsWithBrackets(info.extraInfos));
142     std::vector<WantAgentConstant::Flags> wantAgentFlags;
143     for (int64_t i = 0; i < info.actionFlags.size; i++) {
144         wantAgentFlags.emplace_back(static_cast<WantAgentConstant::Flags>(info.actionFlags.head[i]));
145     }
146     std::vector<std::shared_ptr<AAFwk::Want>> wants;
147     for (int64_t i = 0; i < info.wants.size; i++) {
148         auto actualWant = reinterpret_cast<AAFwk::Want*>(info.wants.head[i]);
149         wants.emplace_back(std::make_shared<AAFwk::Want>(*actualWant));
150     }
151     WantAgentInfo wantAgentInfo(info.requestCode, static_cast<WantAgentConstant::OperationType>(info.actionType),
152         wantAgentFlags, wants, extraInfo);
153     auto context = Context::GetApplicationContext();
154     std::shared_ptr<OHOS::AbilityRuntime::WantAgent::WantAgent> wantAgent = nullptr;
155     ErrCode result = WantAgentHelper::GetWantAgent(context, wantAgentInfo, wantAgent);
156     if (result != NO_ERROR) {
157         *errCode = result;
158         return -1;
159     } else {
160         if (wantAgent == nullptr) {
161             *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
162             return -1;
163         }
164         auto nativeWantAgent = OHOS::FFI::FFIData::Create<CJWantAgent>(
165             std::make_shared<OHOS::AbilityRuntime::WantAgent::WantAgent>(wantAgent->GetPendingWant()));
166         return nativeWantAgent->GetID();
167     }
168 }
169 
FfiWantAgentGetBoundleName(int64_t cjWantAgent,int32_t * errCode)170 char* FfiWantAgentGetBoundleName(int64_t cjWantAgent, int32_t* errCode)
171 {
172     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
173     if (nativeWantAgent == nullptr) {
174         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
175         return nullptr;
176     }
177     std::string bundleName = nativeWantAgent->OnGetBundleName(errCode);
178     if (*errCode != NO_ERROR) {
179         return nullptr;
180     }
181     return CreateCStringFromString(bundleName);
182 }
183 
FfiWantAgentGetUid(int64_t cjWantAgent,int32_t * errCode)184 int32_t FfiWantAgentGetUid(int64_t cjWantAgent, int32_t* errCode)
185 {
186     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
187     if (nativeWantAgent == nullptr) {
188         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
189         return -1;
190     }
191     return nativeWantAgent->OnGetUid(errCode);
192 }
193 
FfiWantAgentCancel(int64_t cjWantAgent,int32_t * errCode)194 void FfiWantAgentCancel(int64_t cjWantAgent, int32_t* errCode)
195 {
196     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
197     if (nativeWantAgent == nullptr) {
198         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
199         return;
200     }
201     return nativeWantAgent->OnCancel(errCode);
202 }
203 
FfiWantAgentTrigger(int64_t cjWantAgent,CJTriggerInfo triggerInfo,void (* callback)(CJCompleteData),int32_t * errCode)204 void FfiWantAgentTrigger(
205     int64_t cjWantAgent, CJTriggerInfo triggerInfo, void (*callback)(CJCompleteData), int32_t* errCode)
206 {
207     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
208     if (nativeWantAgent == nullptr) {
209         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
210         return;
211     }
212     return nativeWantAgent->OnTrigger(triggerInfo, CJLambda::Create(callback), errCode);
213 }
214 
FfiWantAgentGetOperationType(int64_t cjWantAgent,int32_t * errCode)215 int32_t FfiWantAgentGetOperationType(int64_t cjWantAgent, int32_t* errCode)
216 {
217     auto nativeWantAgent = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgent);
218     if (nativeWantAgent == nullptr) {
219         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
220         return -1;
221     }
222     return nativeWantAgent->OnGetOperationType(errCode);
223 }
224 
FfiWantAgentEqual(int64_t cjWantAgentFirst,int64_t cjWantAgentSecond,int32_t * errCode)225 bool FfiWantAgentEqual(int64_t cjWantAgentFirst, int64_t cjWantAgentSecond, int32_t* errCode)
226 {
227     auto nativeWantAgentFirst = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgentFirst);
228     auto nativeWantAgentSecond = OHOS::FFI::FFIData::GetData<CJWantAgent>(cjWantAgentSecond);
229     if (nativeWantAgentFirst == nullptr || nativeWantAgentSecond == nullptr) {
230         *errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
231         return false;
232     }
233     return nativeWantAgentFirst->OnEqual(nativeWantAgentSecond->wantAgent_, errCode);
234 }
235 }
236 } // namespace FfiWantAgent
237 } // namespace OHOS