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 "napi_want_agent.h"
17
18 #include <cstdio>
19 #include <cstring>
20 #include <pthread.h>
21 #include <unistd.h>
22
23 #include "ability_runtime_error_util.h"
24 #include "hilog_wrapper.h"
25 #include "ipc_skeleton.h"
26 #include "js_runtime_utils.h"
27 #include "napi_common.h"
28 #include "want_agent_helper.h"
29 #include "tokenid_kit.h"
30
31 using namespace OHOS::AbilityRuntime;
32 namespace OHOS {
33 #define NAPI_ASSERT_RETURN_NULL(env, assertion, message) \
34 do { \
35 if (!(assertion)) { \
36 HILOG_INFO(message); \
37 return nullptr; \
38 } \
39 } while (0)
40 namespace {
41 constexpr size_t ARGC_ZERO = 0;
42 constexpr size_t ARGC_ONE = 1;
43 constexpr size_t ARGC_TWO = 2;
44 constexpr size_t ARGC_THREE = 3;
45 constexpr uint8_t INDEX_ONE = 1;
46 constexpr uint8_t INDEX_TWO = 2;
47 constexpr int32_t ERR_NOT_OK = -1;
48 constexpr int32_t BUSINESS_ERROR_CODE_OK = 0;
49 constexpr int32_t PARAMETER_ERROR = -1;
50 } // namespace
51
52
TriggerCompleteCallBack()53 TriggerCompleteCallBack::TriggerCompleteCallBack()
54 {}
55
~TriggerCompleteCallBack()56 TriggerCompleteCallBack::~TriggerCompleteCallBack()
57 {}
58
SetCallbackInfo(NativeEngine & engine,NativeReference * ref)59 void TriggerCompleteCallBack::SetCallbackInfo(NativeEngine &engine, NativeReference* ref)
60 {
61 triggerCompleteInfo_.engine = &engine;
62 triggerCompleteInfo_.nativeRef.reset(ref);
63 }
64
SetWantAgentInstance(WantAgent * wantAgent)65 void TriggerCompleteCallBack::SetWantAgentInstance(WantAgent* wantAgent)
66 {
67 triggerCompleteInfo_.wantAgent = wantAgent;
68 }
69
RetErrMsg(NativeEngine & engine,NativeValue * lastParam,int32_t errorCode)70 NativeValue* RetErrMsg(NativeEngine &engine, NativeValue* lastParam, int32_t errorCode)
71 {
72 HILOG_DEBUG("RetErrMsg called");
73 auto complete = [err = errorCode] (NativeEngine &engine, AsyncTask &task, int32_t status) {
74 HILOG_DEBUG("error information output");
75 task.Reject(engine, CreateJsError(engine, err, "PARAMETER_ERROR"));
76 };
77
78 NativeValue* result = nullptr;
79 AsyncTask::ScheduleHighQos("RetErrMsg",
80 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
81
82 return result;
83 }
84
CreateErrorValue(napi_env env,int32_t errCode)85 napi_value CreateErrorValue(napi_env env, int32_t errCode)
86 {
87 HILOG_INFO("enter, errorCode[%{public}d]", errCode);
88 napi_value error = NapiGetNull(env);
89 if (errCode == NO_ERROR) {
90 return error;
91 }
92
93 napi_value code = nullptr;
94 napi_create_int32(env, errCode, &code);
95
96 std::string errMsg = AbilityRuntimeErrorUtil::GetErrMessage(errCode);
97 napi_value message = nullptr;
98 napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &message);
99
100 napi_create_error(env, nullptr, message, &error);
101 napi_set_named_property(env, error, "code", code);
102 return error;
103 }
104
NapiThrow(napi_env env,int32_t errCode)105 void NapiThrow(napi_env env, int32_t errCode)
106 {
107 HILOG_INFO("enter");
108
109 napi_throw(env, CreateErrorValue(env, errCode));
110 }
111
__anonc469c4f60302(uv_work_t* work, int status) 112 auto OnSendFinishedUvAfterWorkCallback = [](uv_work_t* work, int status) {
113 HILOG_INFO("TriggerCompleteCallBack::OnSendFinishedUvAfterWorkCallback:status = %{public}d", status);
114
115 TriggerReceiveDataWorker* dataWorkerData = static_cast<TriggerReceiveDataWorker *>(work->data);
116 if (dataWorkerData == nullptr) {
117 HILOG_ERROR("TriggerReceiveDataWorker instance(uv_work_t) is nullptr");
118 delete work;
119 return;
120 }
121 NativeValue* args[ARGC_TWO] = {0};
122 NativeValue* objValueFirst = dataWorkerData->engine->CreateObject();
123 NativeObject* objectFirst = ConvertNativeValueTo<NativeObject>(objValueFirst);
124
125 if (objectFirst == nullptr) {
126 HILOG_ERROR("Failed to get objectFirst");
127 delete dataWorkerData;
128 dataWorkerData = nullptr;
129 delete work;
130 return;
131 }
132 NativeValue* objValueSecond = dataWorkerData->engine->CreateObject();
133 NativeObject* objectSecond = ConvertNativeValueTo<NativeObject>(objValueSecond);
134
135 if (objectSecond == nullptr) {
136 HILOG_ERROR("Failed to get objectSecond");
137 delete dataWorkerData;
138 dataWorkerData = nullptr;
139 delete work;
140 return;
141 }
142
143 #ifdef ENABLE_ERRCODE
144 objValueFirst = dataWorkerData->engine->CreateUndefined();
145 objectFirst = ConvertNativeValueTo<NativeObject>(objValueFirst);
146 #else
147 objectFirst->SetProperty("code", CreateJsValue(*(dataWorkerData->engine), BUSINESS_ERROR_CODE_OK));
148 #endif
149 objectSecond->SetProperty("wantAgent", JsWantAgent::WrapWantAgent(*(dataWorkerData->engine),
150 dataWorkerData->wantAgent));
151 objectSecond->SetProperty("want", CreateJsWant(*(dataWorkerData->engine), dataWorkerData->want));
152 objectSecond->SetProperty("finalCode", CreateJsValue(*(dataWorkerData->engine), dataWorkerData->resultCode));
153 objectSecond->SetProperty("finalData", CreateJsValue(*(dataWorkerData->engine), dataWorkerData->resultData));
154 objectSecond->SetProperty("extraInfo", CreateJsWantParams(*(dataWorkerData->engine), dataWorkerData->resultExtras));
155 args[ARGC_ZERO] = objValueFirst;
156 args[ARGC_ONE] = objValueSecond;
157
158 NativeValue* value = dataWorkerData->nativeRef->Get();
159 NativeValue* callback = dataWorkerData->nativeRef->Get();
160 dataWorkerData->engine->CallFunction(value, callback, args, ARGC_TWO);
161 delete dataWorkerData;
162 dataWorkerData = nullptr;
163 delete work;
164 };
165
OnSendFinished(const AAFwk::Want & want,int resultCode,const std::string & resultData,const AAFwk::WantParams & resultExtras)166 void TriggerCompleteCallBack::OnSendFinished(
167 const AAFwk::Want &want, int resultCode, const std::string &resultData, const AAFwk::WantParams &resultExtras)
168 {
169 HILOG_INFO("TriggerCompleteCallBack::OnSendFinished start");
170 if (triggerCompleteInfo_.nativeRef == nullptr) {
171 HILOG_INFO("triggerCompleteInfo_ CallBack is nullptr");
172 return;
173 }
174
175 uv_loop_t* loop = nullptr;
176 #if NAPI_VERSION >= NUMBER_OF_PARAMETERS_TWO
177 loop = triggerCompleteInfo_.engine->GetUVLoop();
178 #endif // NAPI_VERSION >= 2
179 if (loop == nullptr) {
180 HILOG_INFO("loop instance is nullptr");
181 return;
182 }
183
184 uv_work_t* work = new (std::nothrow) uv_work_t;
185 if (work == nullptr) {
186 HILOG_INFO("uv_work_t instance is nullptr");
187 return;
188 }
189 TriggerReceiveDataWorker* dataWorker = new (std::nothrow) TriggerReceiveDataWorker();
190 if (dataWorker == nullptr) {
191 HILOG_INFO("TriggerReceiveDataWorker instance is nullptr");
192 delete work;
193 work = nullptr;
194 return;
195 }
196 dataWorker->want = want;
197 dataWorker->resultCode = resultCode;
198 dataWorker->resultData = resultData;
199 dataWorker->resultExtras = resultExtras;
200 dataWorker->engine = triggerCompleteInfo_.engine;
201 dataWorker->nativeRef = std::move(triggerCompleteInfo_.nativeRef);
202 dataWorker->wantAgent = triggerCompleteInfo_.wantAgent;
203 work->data = static_cast<void *>(dataWorker);
204 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, OnSendFinishedUvAfterWorkCallback);
205 if (ret != 0) {
206 delete dataWorker;
207 dataWorker = nullptr;
208 delete work;
209 work = nullptr;
210 }
211
212 HILOG_INFO("TriggerCompleteCallBack::OnSendFinished end");
213 }
214
Finalizer(NativeEngine * engine,void * data,void * hint)215 void JsWantAgent::Finalizer(NativeEngine* engine, void* data, void* hint)
216 {
217 HILOG_INFO("JsWantAgent::Finalizer is called");
218 std::unique_ptr<JsWantAgent>(static_cast<JsWantAgent*>(data));
219 }
220
Equal(NativeEngine * engine,NativeCallbackInfo * info)221 NativeValue* JsWantAgent::Equal(NativeEngine* engine, NativeCallbackInfo* info)
222 {
223 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
224 return (me != nullptr) ? me->OnEqual(*engine, *info) : nullptr;
225 };
226
GetWant(NativeEngine * engine,NativeCallbackInfo * info)227 NativeValue* JsWantAgent::GetWant(NativeEngine* engine, NativeCallbackInfo* info)
228 {
229 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
230 return (me != nullptr) ? me->OnGetWant(*engine, *info) : nullptr;
231 };
232
GetOperationType(NativeEngine * engine,NativeCallbackInfo * info)233 NativeValue* JsWantAgent::GetOperationType(NativeEngine* engine, NativeCallbackInfo* info)
234 {
235 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
236 return (me != nullptr) ? me->OnGetOperationType(*engine, *info) : nullptr;
237 };
238
GetBundleName(NativeEngine * engine,NativeCallbackInfo * info)239 NativeValue* JsWantAgent::GetBundleName(NativeEngine* engine, NativeCallbackInfo* info)
240 {
241 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
242 return (me != nullptr) ? me->OnGetBundleName(*engine, *info) : nullptr;
243 };
244
GetUid(NativeEngine * engine,NativeCallbackInfo * info)245 NativeValue* JsWantAgent::GetUid(NativeEngine* engine, NativeCallbackInfo* info)
246 {
247 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
248 return (me != nullptr) ? me->OnGetUid(*engine, *info) : nullptr;
249 };
250
Cancel(NativeEngine * engine,NativeCallbackInfo * info)251 NativeValue* JsWantAgent::Cancel(NativeEngine* engine, NativeCallbackInfo* info)
252 {
253 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
254 return (me != nullptr) ? me->OnCancel(*engine, *info) : nullptr;
255 };
256
Trigger(NativeEngine * engine,NativeCallbackInfo * info)257 NativeValue* JsWantAgent::Trigger(NativeEngine* engine, NativeCallbackInfo* info)
258 {
259 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
260 return (me != nullptr) ? me->OnTrigger(*engine, *info) : nullptr;
261 };
262
GetWantAgent(NativeEngine * engine,NativeCallbackInfo * info)263 NativeValue* JsWantAgent::GetWantAgent(NativeEngine* engine, NativeCallbackInfo* info)
264 {
265 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
266 return (me != nullptr) ? me->OnGetWantAgent(*engine, *info) : nullptr;
267 };
268
NapiGetWant(NativeEngine * engine,NativeCallbackInfo * info)269 NativeValue* JsWantAgent::NapiGetWant(NativeEngine* engine, NativeCallbackInfo* info)
270 {
271 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
272 return (me != nullptr) ? me->OnNapiGetWant(*engine, *info) : nullptr;
273 };
274
NapiTrigger(NativeEngine * engine,NativeCallbackInfo * info)275 NativeValue* JsWantAgent::NapiTrigger(NativeEngine* engine, NativeCallbackInfo* info)
276 {
277 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
278 return (me != nullptr) ? me->OnNapiTrigger(*engine, *info) : nullptr;
279 };
280
NapiGetWantAgent(NativeEngine * engine,NativeCallbackInfo * info)281 NativeValue* JsWantAgent::NapiGetWantAgent(NativeEngine* engine, NativeCallbackInfo* info)
282 {
283 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
284 return (me != nullptr) ? me->OnNapiGetWantAgent(*engine, *info) : nullptr;
285 };
286
NapiGetOperationType(NativeEngine * engine,NativeCallbackInfo * info)287 NativeValue* JsWantAgent::NapiGetOperationType(NativeEngine* engine, NativeCallbackInfo* info)
288 {
289 JsWantAgent* me = CheckParamsAndGetThis<JsWantAgent>(engine, info);
290 return (me != nullptr) ? me->OnNapiGetOperationType(*engine, *info) : nullptr;
291 };
292
OnEqual(NativeEngine & engine,NativeCallbackInfo & info)293 NativeValue* JsWantAgent::OnEqual(NativeEngine &engine, NativeCallbackInfo &info)
294 {
295 HILOG_DEBUG("enter, argc = %{public}d", static_cast<int32_t>(info.argc));
296 int32_t errCode = BUSINESS_ERROR_CODE_OK;
297 WantAgent* pWantAgentFirst = nullptr;
298 WantAgent* pWantAgentSecond = nullptr;
299 if (info.argc < ARGC_TWO || info.argc > ARGC_THREE) {
300 HILOG_ERROR("Not enough params");
301 #ifdef ENABLE_ERRCODE
302 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
303 #endif
304 return engine.CreateUndefined();
305 }
306
307 NativeValue* lastParam = (info.argc >= ARGC_THREE) ? info.argv[INDEX_TWO] : nullptr;
308 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
309 HILOG_ERROR("Wrong argument type. Object expected.");
310 #ifdef ENABLE_ERRCODE
311 errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
312 AbilityRuntimeErrorUtil::Throw(engine, errCode);
313 return engine.CreateUndefined();
314 #else
315 errCode = ERR_NOT_OK;
316 return RetErrMsg(engine, lastParam, errCode);
317 #endif
318 }
319
320 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgentFirst));
321 if (pWantAgentFirst == nullptr) {
322 HILOG_ERROR("Parse pWantAgentFirst failed");
323 #ifdef ENABLE_ERRCODE
324 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
325 return engine.CreateUndefined();
326 #else
327 errCode = ERR_NOT_OK;
328 return RetErrMsg(engine, lastParam, errCode);
329 #endif
330 }
331
332 if (info.argv[1]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
333 HILOG_ERROR("Wrong argument type. Object expected.");
334 #ifdef ENABLE_ERRCODE
335 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
336 return engine.CreateUndefined();
337 #else
338 errCode = ERR_NOT_OK;
339 return RetErrMsg(engine, lastParam, errCode);
340 #endif
341 }
342
343 UnwrapWantAgent(engine, info.argv[1], reinterpret_cast<void **>(&pWantAgentSecond));
344 if (pWantAgentSecond == nullptr) {
345 HILOG_ERROR("Parse pWantAgentSceond failed");
346 #ifdef ENABLE_ERRCODE
347 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
348 return engine.CreateUndefined();
349 #else
350 errCode = ERR_NOT_OK;
351 return RetErrMsg(engine, lastParam, errCode);
352 #endif
353 }
354
355 std::shared_ptr<WantAgent> wantAgentFirst = std::make_shared<WantAgent>(*pWantAgentFirst);
356 std::shared_ptr<WantAgent> wantAgentSecond = std::make_shared<WantAgent>(*pWantAgentSecond);
357 AsyncTask::CompleteCallback complete =
358 [wantAgentFirst, wantAgentSecond](NativeEngine &engine, AsyncTask &task, int32_t status) {
359 HILOG_DEBUG("OnEqual AsyncTask is called");
360 bool ret = false;
361 ErrCode retCode = WantAgentHelper::IsEquals(wantAgentFirst, wantAgentSecond);
362 #ifdef ENABLE_ERRCODE
363 if (retCode == ERR_NOT_OK) {
364 ret = false;
365 task.ResolveWithNoError(engine, CreateJsValue(engine, ret));
366 } else if (retCode == ERR_OK) {
367 ret = true;
368 task.ResolveWithNoError(engine, CreateJsValue(engine, ret));
369 } else {
370 task.Reject(engine, CreateJsError(engine, retCode, AbilityRuntimeErrorUtil::GetErrMessage(retCode)));
371 }
372 #else
373 if (retCode != ERR_OK) {
374 ret = false;
375 task.Resolve(engine, CreateJsValue(engine, ret));
376 } else {
377 ret = true;
378 task.Resolve(engine, CreateJsValue(engine, ret));
379 }
380 #endif
381 };
382
383 NativeValue* result = nullptr;
384 AsyncTask::ScheduleHighQos("JsWantAgent::OnEqual",
385 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
386 return result;
387 }
388
OnGetWant(NativeEngine & engine,NativeCallbackInfo & info)389 NativeValue* JsWantAgent::OnGetWant(NativeEngine &engine, NativeCallbackInfo &info)
390 {
391 HILOG_DEBUG("enter, argc = %{public}d", static_cast<int32_t>(info.argc));
392
393 int32_t errCode = BUSINESS_ERROR_CODE_OK;
394 WantAgent* pWantAgent = nullptr;
395 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
396 HILOG_ERROR("Not enough params");
397 return engine.CreateUndefined();
398 }
399
400 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
401 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
402 HILOG_ERROR("Wrong argument type. Object expected.");
403 errCode = ERR_NOT_OK;
404 return RetErrMsg(engine, lastParam, errCode);
405 }
406
407 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
408 if (pWantAgent == nullptr) {
409 HILOG_ERROR("Parse pWantAgent failed");
410 errCode = ERR_NOT_OK;
411 return RetErrMsg(engine, lastParam, errCode);
412 }
413
414 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
415 AsyncTask::CompleteCallback complete = [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
416 HILOG_DEBUG("OnGetWant AsyncTask is called");
417 std::shared_ptr<Want> want = WantAgentHelper::GetWant(wantAgent);
418 if (want == nullptr) {
419 task.Reject(engine, CreateJsError(engine, ERR_NOT_OK, "WantAgentHelper::GetWant result nullptr."));
420 return;
421 }
422 task.Resolve(engine, CreateJsWant(engine, *(want)));
423 };
424
425 NativeValue* result = nullptr;
426 AsyncTask::ScheduleHighQos("JsWantAgent::OnGetWant",
427 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
428 return result;
429 }
430
OnGetOperationType(NativeEngine & engine,NativeCallbackInfo & info)431 NativeValue* JsWantAgent::OnGetOperationType(NativeEngine &engine, NativeCallbackInfo &info)
432 {
433 HILOG_DEBUG("JsWantAgent::OnGetOperationType enter, argc = %{public}d", static_cast<int32_t>(info.argc));
434 int32_t errCode = BUSINESS_ERROR_CODE_OK;
435 WantAgent* pWantAgent = nullptr;
436 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
437 HILOG_ERROR("Not enough params");
438 return engine.CreateUndefined();
439 }
440
441 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
442 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
443 HILOG_ERROR("Wrong argument type. Object expected.");
444 errCode = ERR_NOT_OK;
445 return RetErrMsg(engine, lastParam, errCode);
446 }
447
448 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
449 if (pWantAgent == nullptr) {
450 HILOG_ERROR("Parse pWantAgent failed");
451 errCode = ERR_NOT_OK;
452 return RetErrMsg(engine, lastParam, errCode);
453 }
454
455 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
456 AsyncTask::CompleteCallback complete = [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
457 HILOG_DEBUG("OnGetOperationType AsyncTask is called");
458 auto ret = WantAgentHelper::GetType(wantAgent);
459 task.Resolve(engine, CreateJsValue(engine, ret));
460 };
461
462 NativeValue* result = nullptr;
463 AsyncTask::ScheduleHighQos("JsWantAgent::OnGetOperationType",
464 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
465 return result;
466 }
467
OnGetBundleName(NativeEngine & engine,NativeCallbackInfo & info)468 NativeValue* JsWantAgent::OnGetBundleName(NativeEngine &engine, NativeCallbackInfo &info)
469 {
470 HILOG_DEBUG("JsWantAgent::OnGetBundleName enter, argc = %{public}d", static_cast<int32_t>(info.argc));
471 int32_t errCode = BUSINESS_ERROR_CODE_OK;
472 WantAgent* pWantAgent = nullptr;
473 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
474 HILOG_ERROR("Not enough params");
475 #ifdef ENABLE_ERRCODE
476 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
477 #endif
478 return engine.CreateUndefined();
479 }
480
481 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
482 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
483 HILOG_ERROR("Wrong argument type. Object expected.");
484 #ifdef ENABLE_ERRCODE
485 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
486 return engine.CreateUndefined();
487 #else
488 errCode = ERR_NOT_OK;
489 return RetErrMsg(engine, lastParam, errCode);
490 #endif
491 }
492
493 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
494 if (pWantAgent == nullptr) {
495 HILOG_ERROR("Parse pWantAgent failed");
496 #ifdef ENABLE_ERRCODE
497 errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
498 AbilityRuntimeErrorUtil::Throw(engine, errCode);
499 return engine.CreateUndefined();
500 #else
501 errCode = ERR_NOT_OK;
502 return RetErrMsg(engine, lastParam, errCode);
503 #endif
504 }
505
506 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
507 AsyncTask::CompleteCallback complete = [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
508 HILOG_DEBUG("OnGetBundleName AsyncTask is called");
509 std::string bundleName = "";
510 #ifdef ENABLE_ERRCODE
511 ErrCode result = WantAgentHelper::GetBundleName(wantAgent, bundleName);
512 if (result != NO_ERROR) {
513 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
514 } else {
515 task.ResolveWithNoError(engine, CreateJsValue(engine, bundleName));
516 }
517 #else
518 WantAgentHelper::GetBundleName(wantAgent, bundleName);
519 task.Resolve(engine, CreateJsValue(engine, bundleName));
520 #endif
521 };
522
523 NativeValue* result = nullptr;
524 AsyncTask::ScheduleHighQos("JsWantAgent::OnGetBundleName",
525 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
526 return result;
527 }
528
OnGetUid(NativeEngine & engine,NativeCallbackInfo & info)529 NativeValue* JsWantAgent::OnGetUid(NativeEngine &engine, NativeCallbackInfo &info)
530 {
531 HILOG_DEBUG("JsWantAgent::OnGetUid enter, argc = %{public}d", static_cast<int32_t>(info.argc));
532 int32_t errCode = BUSINESS_ERROR_CODE_OK;
533 WantAgent* pWantAgent = nullptr;
534 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
535 HILOG_ERROR("Not enough params");
536 #ifdef ENABLE_ERRCODE
537 errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
538 AbilityRuntimeErrorUtil::Throw(engine, errCode);
539 #endif
540 return engine.CreateUndefined();
541 }
542
543 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
544 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
545 HILOG_ERROR("Wrong argument type. Object expected.");
546 #ifdef ENABLE_ERRCODE
547 errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
548 AbilityRuntimeErrorUtil::Throw(engine, errCode);
549 return engine.CreateUndefined();
550 #else
551 errCode = ERR_NOT_OK;
552 return RetErrMsg(engine, lastParam, errCode);
553 #endif
554 }
555
556 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
557 if (pWantAgent == nullptr) {
558 HILOG_ERROR("Parse pWantAgent failed");
559 #ifdef ENABLE_ERRCODE
560 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
561 return engine.CreateUndefined();
562 #else
563 errCode = ERR_NOT_OK;
564 return RetErrMsg(engine, lastParam, errCode);
565 #endif
566 }
567
568 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
569 AsyncTask::CompleteCallback complete = [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
570 HILOG_DEBUG("OnGetUid AsyncTask is called");
571 int uid = -1;
572 #ifdef ENABLE_ERRCODE
573 ErrCode result = WantAgentHelper::GetUid(wantAgent, uid);
574 if (result != NO_ERROR) {
575 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
576 } else {
577 task.ResolveWithNoError(engine, CreateJsValue(engine, uid));
578 }
579 #else
580 WantAgentHelper::GetUid(wantAgent, uid);
581 task.Resolve(engine, CreateJsValue(engine, uid));
582 #endif
583 };
584 NativeValue* result = nullptr;
585 AsyncTask::ScheduleHighQos("JsWantAgent::OnGetUid",
586 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
587 return result;
588 }
589
OnCancel(NativeEngine & engine,NativeCallbackInfo & info)590 NativeValue* JsWantAgent::OnCancel(NativeEngine &engine, NativeCallbackInfo &info)
591 {
592 HILOG_DEBUG("%{public}s is called", __FUNCTION__);
593 int32_t errCode = BUSINESS_ERROR_CODE_OK;
594 WantAgent* pWantAgent = nullptr;
595 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
596 HILOG_ERROR("Not enough params");
597 #ifdef ENABLE_ERRCODE
598 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
599 #endif
600 return engine.CreateUndefined();
601 }
602
603 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
604 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
605 HILOG_ERROR("Wrong argument type. Object expected.");
606 #ifdef ENABLE_ERRCODE
607 errCode = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
608 AbilityRuntimeErrorUtil::Throw(engine, errCode);
609 return engine.CreateUndefined();
610 #else
611 errCode = ERR_NOT_OK;
612 return RetErrMsg(engine, lastParam, errCode);
613 #endif
614 }
615
616 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
617 if (pWantAgent == nullptr) {
618 HILOG_ERROR("Parse pWantAgent failed");
619 #ifdef ENABLE_ERRCODE
620 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
621 return engine.CreateUndefined();
622 #else
623 errCode = ERR_NOT_OK;
624 return RetErrMsg(engine, lastParam, errCode);
625 #endif
626 }
627
628 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
629 AsyncTask::CompleteCallback complete =
630 [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
631 HILOG_DEBUG("OnCancel AsyncTask is called");
632 #ifdef ENABLE_ERRCODE
633 ErrCode result = WantAgentHelper::Cancel(wantAgent);
634 if (result != NO_ERROR) {
635 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
636 } else {
637 task.ResolveWithNoError(engine, engine.CreateUndefined());
638 }
639 #else
640 WantAgentHelper::Cancel(wantAgent);
641 task.Resolve(engine, engine.CreateUndefined());
642 #endif
643 };
644
645 NativeValue* result = nullptr;
646 AsyncTask::Schedule("JsWantAgent::OnCancel",
647 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
648 return result;
649 }
650
OnTrigger(NativeEngine & engine,NativeCallbackInfo & info)651 NativeValue* JsWantAgent::OnTrigger(NativeEngine &engine, NativeCallbackInfo &info)
652 {
653 HILOG_DEBUG("%{public}s is called", __FUNCTION__);
654 if (info.argc != ARGC_THREE) {
655 HILOG_ERROR("Not enough params");
656 return engine.CreateUndefined();
657 }
658
659 std::shared_ptr<WantAgent> wantAgent = nullptr;
660 TriggerInfo triggerInfo;
661 auto triggerObj = std::make_shared<TriggerCompleteCallBack>();
662 int32_t errCode = UnWrapTriggerInfoParam(engine, info, wantAgent, triggerInfo, triggerObj);
663 if (errCode != BUSINESS_ERROR_CODE_OK) {
664 return RetErrMsg(engine, info.argv[ARGC_TWO], errCode);
665 }
666
667 WantAgentHelper::TriggerWantAgent(wantAgent, triggerObj, triggerInfo);
668 return engine.CreateNull();
669 }
670
UnWrapTriggerInfoParam(NativeEngine & engine,NativeCallbackInfo & info,std::shared_ptr<WantAgent> & wantAgent,TriggerInfo & triggerInfo,std::shared_ptr<TriggerCompleteCallBack> & triggerObj)671 int32_t JsWantAgent::UnWrapTriggerInfoParam(NativeEngine &engine, NativeCallbackInfo &info,
672 std::shared_ptr<WantAgent> &wantAgent, TriggerInfo &triggerInfo,
673 std::shared_ptr<TriggerCompleteCallBack> &triggerObj)
674 {
675 if (info.argc != ARGC_THREE) {
676 HILOG_ERROR("Not enough params");
677 return ERR_NOT_OK;
678 }
679
680 if (info.argv[ARGC_ZERO]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
681 HILOG_ERROR("Wrong argument type. Object expected.");
682 return ERR_NOT_OK;
683 }
684 WantAgent* pWantAgent = nullptr;
685 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
686
687 if (pWantAgent == nullptr) {
688 HILOG_ERROR("Parse pWantAgent failed");
689 return ERR_NOT_OK;
690 }
691 wantAgent = std::make_shared<WantAgent>(*pWantAgent);
692
693 int32_t ret = GetTriggerInfo(engine, info.argv[ARGC_ONE], triggerInfo);
694 if (ret != BUSINESS_ERROR_CODE_OK) {
695 HILOG_ERROR("Get trigger info error");
696 return ret;
697 }
698
699 NativeReference* ref = engine.CreateReference(info.argv[ARGC_TWO], 1);
700 triggerObj->SetCallbackInfo(engine, ref);
701 triggerObj->SetWantAgentInstance(pWantAgent);
702
703 return BUSINESS_ERROR_CODE_OK;
704 }
705
GetTriggerInfo(NativeEngine & engine,NativeValue * param,TriggerInfo & triggerInfo)706 int32_t JsWantAgent::GetTriggerInfo(NativeEngine &engine, NativeValue* param, TriggerInfo &triggerInfo)
707 {
708 HILOG_DEBUG("GetTriggerInfo called.");
709 if (param->TypeOf() != NativeValueType::NATIVE_OBJECT) {
710 HILOG_ERROR("param type mismatch!");
711 return ERR_NOT_OK;
712 }
713
714 NativeObject* objectParam = ConvertNativeValueTo<NativeObject>(param);
715
716 int32_t code = -1;
717 NativeValue* jsCode = objectParam->GetProperty("code");
718 if (!ConvertFromJsValue(engine, jsCode, code)) {
719 HILOG_ERROR("GetTriggerInfo convert code error!");
720 return ERR_NOT_OK;
721 }
722
723 std::shared_ptr<AAFwk::Want> want = nullptr;
724 if (objectParam->HasProperty("want")) {
725 NativeValue* jsWant = objectParam->GetProperty("want");
726 want = std::make_shared<AAFwk::Want>();
727 if (!UnwrapWant(reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(jsWant), *want)) {
728 HILOG_ERROR("GetTriggerInfo convert want error!");
729 return ERR_NOT_OK;
730 }
731 }
732
733 std::string permission = "";
734 if (objectParam->HasProperty("permission")) {
735 NativeValue* jsPermission = objectParam->GetProperty("permission");
736 if (!ConvertFromJsValue(engine, jsPermission, permission)) {
737 HILOG_ERROR("GetTriggerInfo convert permission error!");
738 return ERR_NOT_OK;
739 }
740 }
741
742 std::shared_ptr<AAFwk::WantParams> extraInfo = nullptr;
743 if (objectParam->HasProperty("extraInfo")) {
744 NativeValue* jsExtraInfo = objectParam->GetProperty("extraInfo");
745 extraInfo = std::make_shared<AAFwk::WantParams>();
746 if (!UnwrapWantParams(reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(jsExtraInfo),
747 *extraInfo)) {
748 HILOG_ERROR("GetTriggerInfo convert extraInfo error!");
749 return ERR_NOT_OK;
750 }
751 }
752
753 TriggerInfo triggerInfoData(permission, extraInfo, want, code);
754 triggerInfo = triggerInfoData;
755 return BUSINESS_ERROR_CODE_OK;
756 }
757
GetWantAgentParam(NativeEngine & engine,NativeCallbackInfo & info,WantAgentWantsParas & paras)758 int32_t JsWantAgent::GetWantAgentParam(NativeEngine &engine, NativeCallbackInfo &info, WantAgentWantsParas ¶s)
759 {
760 HILOG_DEBUG("GetWantAgentParam called.");
761 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
762 HILOG_ERROR("param type mismatch!");
763 return PARAMETER_ERROR;
764 }
765 NativeObject* paramObject = ConvertNativeValueTo<NativeObject>(info.argv[0]);
766
767 NativeValue* jsWants = paramObject->GetProperty("wants");
768 if (!jsWants->IsArray()) {
769 HILOG_ERROR("wants is not array!");
770 return PARAMETER_ERROR;
771 }
772
773 NativeArray* jsWantsArray = ConvertNativeValueTo<NativeArray>(jsWants);
774 for (uint32_t i = 0; i < jsWantsArray->GetLength(); i++) {
775 std::shared_ptr<AAFwk::Want> want = std::make_shared<AAFwk::Want>();
776 NativeValue* jsWant = jsWantsArray->GetElement(i);
777 if (!UnwrapWant(reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(jsWant), *want)) {
778 HILOG_ERROR("UnwrapWant failed!");
779 return PARAMETER_ERROR;
780 }
781 paras.wants.emplace_back(want);
782 }
783
784 NativeValue* jsOperationType = paramObject->GetProperty("operationType");
785 if (!ConvertFromJsValue(engine, jsOperationType, paras.operationType)) {
786 HILOG_ERROR("Convert operationType failed!");
787 return PARAMETER_ERROR;
788 }
789
790 NativeValue* jsRequestCode = paramObject->GetProperty("requestCode");
791 if (!ConvertFromJsValue(engine, jsRequestCode, paras.requestCode)) {
792 HILOG_ERROR("Convert requestCode failed!");
793 return PARAMETER_ERROR;
794 }
795
796 if (paramObject->HasProperty("wantAgentFlags")) {
797 NativeValue* jsWantAgentFlags = paramObject->GetProperty("wantAgentFlags");
798 if (!jsWantAgentFlags->IsArray()) {
799 HILOG_ERROR("wantAgentFlags is not array!");
800 return PARAMETER_ERROR;
801 }
802
803 NativeArray* jsWantAgentFlagsArray = ConvertNativeValueTo<NativeArray>(jsWantAgentFlags);
804 for (uint32_t i = 0; i < jsWantAgentFlagsArray->GetLength(); i++) {
805 NativeValue* jsWantAgentFlag = jsWantAgentFlagsArray->GetElement(i);
806 if (jsWantAgentFlag->TypeOf() != NativeValueType::NATIVE_NUMBER) {
807 HILOG_ERROR("WantAgentFlag type error!");
808 return PARAMETER_ERROR;
809 }
810 int32_t wantAgentFlag = 0;
811 if (!ConvertFromJsValue(engine, jsWantAgentFlag, wantAgentFlag)) {
812 HILOG_ERROR("Convert WantAgentFlag failed!");
813 return PARAMETER_ERROR;
814 }
815 paras.wantAgentFlags.emplace_back(static_cast<WantAgentConstant::Flags>(wantAgentFlag));
816 }
817 }
818
819 if (paramObject->HasProperty("extraInfo")) {
820 NativeValue* jsExtraInfo = paramObject->GetProperty("extraInfo");
821 if (jsExtraInfo->TypeOf() != NativeValueType::NATIVE_OBJECT) {
822 HILOG_ERROR("ExtraInfo type error!");
823 return PARAMETER_ERROR;
824 }
825 if (!UnwrapWantParams(reinterpret_cast<napi_env>(&engine), reinterpret_cast<napi_value>(jsExtraInfo),
826 paras.extraInfo)) {
827 HILOG_ERROR("Convert extraInfo failed!");
828 return PARAMETER_ERROR;
829 }
830 }
831 return BUSINESS_ERROR_CODE_OK;
832 }
833
WrapWantAgent(NativeEngine & engine,WantAgent * wantAgent)834 NativeValue* JsWantAgent::WrapWantAgent(NativeEngine &engine, WantAgent* wantAgent)
835 {
836 HILOG_DEBUG("WrapWantAgent called.");
837 NativeCallback callback = [](NativeEngine* engine, NativeCallbackInfo* info) -> NativeValue* {
838 return info->thisVar;
839 };
840
841 NativeValue* wantAgentClass = engine.DefineClass("WantAgentClass", callback, nullptr, nullptr, 0);
842 NativeValue* result = engine.CreateInstance(wantAgentClass, nullptr, 0);
843 if (result == nullptr) {
844 HILOG_ERROR("create instance failed.");
845 return nullptr;
846 }
847
848 NativeObject* nativeObject = reinterpret_cast<NativeObject*>(result->GetInterface(NativeObject::INTERFACE_ID));
849 NativeFinalize nativeFinalize = [](NativeEngine* engine, void* data, void* hint) {
850 HILOG_DEBUG("delete wantAgent.");
851 auto agent = static_cast<WantAgent*>(data);
852 delete agent;
853 agent = nullptr;
854 };
855
856 nativeObject->SetNativePointer(reinterpret_cast<void*>(wantAgent), nativeFinalize, nullptr);
857 return result;
858 }
859
UnwrapWantAgent(NativeEngine & engine,NativeValue * jsParam,void ** result)860 void JsWantAgent::UnwrapWantAgent(NativeEngine &engine, NativeValue* jsParam, void** result)
861 {
862 HILOG_DEBUG("UnwrapWantAgent called.");
863 if (jsParam == nullptr) {
864 HILOG_ERROR("UnwrapWantAgent jsParam is nullptr!");
865 return;
866 }
867
868 if (jsParam->TypeOf() != NATIVE_OBJECT) {
869 HILOG_ERROR("UnwrapWantAgent jsParam type error!");
870 return;
871 }
872
873 NativeObject *nativeObject = reinterpret_cast<NativeObject*>(jsParam->GetInterface(NativeObject::INTERFACE_ID));
874 *result = nativeObject->GetNativePointer();
875 }
876
OnGetWantAgent(NativeEngine & engine,NativeCallbackInfo & info)877 NativeValue* JsWantAgent::OnGetWantAgent(NativeEngine &engine, NativeCallbackInfo &info)
878 {
879 HILOG_DEBUG("enter, argc = %{public}d", static_cast<int32_t>(info.argc));
880 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
881 HILOG_ERROR("Not enough params");
882 return engine.CreateUndefined();
883 }
884
885 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
886 std::shared_ptr<WantAgentWantsParas> spParas = std::make_shared<WantAgentWantsParas>();
887 int32_t ret = GetWantAgentParam(engine, info, *spParas);
888 if (ret != 0) {
889 HILOG_ERROR("Failed to get wantAgent param.");
890 return RetErrMsg(engine, lastParam, ret);
891 }
892
893 AsyncTask::CompleteCallback complete = [weak = weak_from_this(), parasobj = spParas](NativeEngine &engine,
894 AsyncTask &task, int32_t status) {
895 HILOG_DEBUG("OnGetWantAgent AsyncTask is called");
896 auto self = weak.lock();
897 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>(parasobj->extraInfo);
898 WantAgentInfo wantAgentInfo(parasobj->requestCode,
899 static_cast<WantAgentConstant::OperationType>(parasobj->operationType),
900 parasobj->wantAgentFlags,
901 parasobj->wants,
902 extraInfo);
903
904 auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
905 std::shared_ptr<WantAgent> wantAgent = nullptr;
906 WantAgentHelper::GetWantAgent(context, wantAgentInfo, wantAgent);
907 WantAgent* pWantAgent = nullptr;
908 if (wantAgent == nullptr) {
909 HILOG_INFO("wantAgent is nullptr...");
910 } else {
911 pWantAgent = new WantAgent(wantAgent->GetPendingWant());
912 }
913
914 if (pWantAgent == nullptr) {
915 HILOG_INFO("pWantAgent is nullptr...");
916 }
917 task.Resolve(engine, self->WrapWantAgent(engine, pWantAgent));
918 };
919
920 NativeValue* result = nullptr;
921 AsyncTask::ScheduleHighQos("JsWantAgent::OnGetWantAgent",
922 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
923 return result;
924 }
925
OnNapiGetWant(NativeEngine & engine,NativeCallbackInfo & info)926 NativeValue* JsWantAgent::OnNapiGetWant(NativeEngine &engine, NativeCallbackInfo &info)
927 {
928 HILOG_DEBUG("enter, argc = %{public}d", static_cast<int32_t>(info.argc));
929
930 WantAgent* pWantAgent = nullptr;
931 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
932 HILOG_ERROR("Not enough params");
933 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
934 return engine.CreateUndefined();
935 }
936 auto selfToken = IPCSkeleton::GetSelfTokenID();
937 if (!Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(selfToken)) {
938 HILOG_ERROR("This application is not system-app, can not use system-api");
939 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_NOT_SYSTEM_APP);
940 return engine.CreateUndefined();
941 }
942
943 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
944 HILOG_ERROR("Wrong argument type. Object expected.");
945 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
946 return engine.CreateUndefined();
947 }
948
949 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
950 if (pWantAgent == nullptr) {
951 HILOG_ERROR("Parse pWantAgent failed");
952 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
953 return engine.CreateUndefined();
954 }
955
956 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
957 AsyncTask::CompleteCallback complete = [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
958 HILOG_DEBUG("OnNapiGetWant AsyncTask is called");
959 std::shared_ptr<Want> want = std::make_shared<Want>();
960 ErrCode result = WantAgentHelper::GetWant(wantAgent, want);
961 if (result != NO_ERROR) {
962 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
963 return;
964 }
965 task.ResolveWithNoError(engine, CreateJsWant(engine, *(want)));
966 };
967 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
968 NativeValue* result = nullptr;
969 AsyncTask::ScheduleHighQos("JsWantAgent::OnNapiGetWant",
970 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
971 return result;
972 }
973
OnNapiTrigger(NativeEngine & engine,NativeCallbackInfo & info)974 NativeValue* JsWantAgent::OnNapiTrigger(NativeEngine &engine, NativeCallbackInfo &info)
975 {
976 HILOG_DEBUG("%{public}s is called", __FUNCTION__);
977 if (info.argc != ARGC_THREE) {
978 HILOG_ERROR("Not enough params");
979 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
980 return engine.CreateUndefined();
981 }
982
983 std::shared_ptr<WantAgent> wantAgent = nullptr;
984 TriggerInfo triggerInfo;
985 auto triggerObj = std::make_shared<TriggerCompleteCallBack>();
986 int32_t errCode = UnWrapTriggerInfoParam(engine, info, wantAgent, triggerInfo, triggerObj);
987 if (errCode != NO_ERROR) {
988 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
989 return engine.CreateUndefined();
990 }
991 ErrCode result = WantAgentHelper::TriggerWantAgent(wantAgent, triggerObj, triggerInfo);
992 if (result != ERR_OK) {
993 return CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result));
994 }
995 return engine.CreateNull();
996 }
997
OnNapiGetWantAgent(NativeEngine & engine,NativeCallbackInfo & info)998 NativeValue* JsWantAgent::OnNapiGetWantAgent(NativeEngine &engine, NativeCallbackInfo &info)
999 {
1000 HILOG_DEBUG("enter, argc = %{public}d", static_cast<int32_t>(info.argc));
1001 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
1002 HILOG_ERROR("Not enough params");
1003 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
1004 return engine.CreateUndefined();
1005 }
1006
1007 std::shared_ptr<WantAgentWantsParas> spParas = std::make_shared<WantAgentWantsParas>();
1008 int32_t ret = GetWantAgentParam(engine, info, *spParas);
1009 if (ret != 0) {
1010 HILOG_ERROR("Failed to get wantAgent param.");
1011 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
1012 return engine.CreateUndefined();
1013 }
1014
1015 AsyncTask::CompleteCallback complete = [weak = weak_from_this(), parasobj = spParas](NativeEngine &engine,
1016 AsyncTask &task, int32_t status) {
1017 HILOG_DEBUG("OnNapiGetWantAgent AsyncTask is called");
1018 auto self = weak.lock();
1019 std::shared_ptr<AAFwk::WantParams> extraInfo = std::make_shared<AAFwk::WantParams>(parasobj->extraInfo);
1020 WantAgentInfo wantAgentInfo(parasobj->requestCode,
1021 static_cast<WantAgentConstant::OperationType>(parasobj->operationType),
1022 parasobj->wantAgentFlags,
1023 parasobj->wants,
1024 extraInfo);
1025
1026 auto context = OHOS::AbilityRuntime::Context::GetApplicationContext();
1027 std::shared_ptr<WantAgent> wantAgent = nullptr;
1028 ErrCode result = WantAgentHelper::GetWantAgent(context, wantAgentInfo, wantAgent);
1029 if (result != NO_ERROR) {
1030 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
1031 } else {
1032 WantAgent* pWantAgent = nullptr;
1033 if (wantAgent == nullptr) {
1034 result = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
1035 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
1036 return;
1037 } else {
1038 pWantAgent = new (std::nothrow) WantAgent(wantAgent->GetPendingWant());
1039 }
1040
1041 if (pWantAgent == nullptr) {
1042 HILOG_ERROR("pWantAgent is nullptr...");
1043 result = ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER;
1044 task.Reject(engine, CreateJsError(engine, result, AbilityRuntimeErrorUtil::GetErrMessage(result)));
1045 } else {
1046 task.ResolveWithNoError(engine, self->WrapWantAgent(engine, pWantAgent));
1047 }
1048 }
1049 };
1050
1051 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
1052 NativeValue* result = nullptr;
1053 AsyncTask::ScheduleHighQos("JsWantAgent::OnNapiGetWantAgent",
1054 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1055 return result;
1056 }
1057
OnNapiGetOperationType(NativeEngine & engine,NativeCallbackInfo & info)1058 NativeValue* JsWantAgent::OnNapiGetOperationType(NativeEngine &engine, NativeCallbackInfo &info)
1059 {
1060 HILOG_DEBUG("JsWantAgent::OnNapiGetOperationType enter, argc = %{public}d", static_cast<int32_t>(info.argc));
1061 WantAgent* pWantAgent = nullptr;
1062 if (info.argc > ARGC_TWO || info.argc < ARGC_ONE) {
1063 HILOG_ERROR("Not enough params");
1064 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
1065 return engine.CreateUndefined();
1066 }
1067
1068 if (info.argv[0]->TypeOf() != NativeValueType::NATIVE_OBJECT) {
1069 HILOG_ERROR("Wrong argument type. Object expected.");
1070 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
1071 return engine.CreateUndefined();
1072 }
1073
1074 UnwrapWantAgent(engine, info.argv[0], reinterpret_cast<void **>(&pWantAgent));
1075
1076 if (pWantAgent == nullptr) {
1077 HILOG_ERROR("Parse pWantAgent failed");
1078 AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER);
1079 return engine.CreateUndefined();
1080 }
1081
1082 std::shared_ptr<WantAgent> wantAgent = std::make_shared<WantAgent>(*pWantAgent);
1083 AsyncTask::CompleteCallback complete = [wantAgent](NativeEngine &engine, AsyncTask &task, int32_t status) {
1084 HILOG_DEBUG("OnNapiGetOperationType AsyncTask is called");
1085 int32_t operType;
1086 ErrCode ret = WantAgentHelper::GetType(wantAgent, operType);
1087 if (ret != NO_ERROR) {
1088 task.Reject(engine, CreateJsError(engine, ret, AbilityRuntimeErrorUtil::GetErrMessage(ret)));
1089 } else {
1090 task.ResolveWithNoError(engine, CreateJsValue(engine, operType));
1091 }
1092 };
1093
1094 NativeValue* lastParam = (info.argc >= ARGC_TWO) ? info.argv[INDEX_ONE] : nullptr;
1095 NativeValue* result = nullptr;
1096 AsyncTask::ScheduleHighQos("JsWantAgent::OnNapiGetOperationType",
1097 engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result));
1098 return result;
1099 }
1100
WantAgentFlagsInit(NativeEngine * engine)1101 NativeValue* WantAgentFlagsInit(NativeEngine* engine)
1102 {
1103 HILOG_INFO("enter");
1104
1105 if (engine == nullptr) {
1106 HILOG_ERROR("Invalid input parameters");
1107 return nullptr;
1108 }
1109
1110 NativeValue* objValue = engine->CreateObject();
1111 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
1112
1113 if (object == nullptr) {
1114 HILOG_ERROR("Failed to get object");
1115 return nullptr;
1116 }
1117
1118 object->SetProperty("ONE_TIME_FLAG", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_ZERO)));
1119 object->SetProperty("NO_BUILD_FLAG", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_ONE)));
1120 object->SetProperty("CANCEL_PRESENT_FLAG", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_TWO)));
1121 object->SetProperty("UPDATE_PRESENT_FLAG", CreateJsValue(*engine,
1122 static_cast<int32_t>(NUMBER_OF_PARAMETERS_THREE)));
1123 object->SetProperty("CONSTANT_FLAG", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_FOUR)));
1124 object->SetProperty("REPLACE_ELEMENT", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_FIVE)));
1125 object->SetProperty("REPLACE_ACTION", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_SIX)));
1126 object->SetProperty("REPLACE_URI", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_SEVEN)));
1127 object->SetProperty("REPLACE_ENTITIES", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_EIGHT)));
1128 object->SetProperty("REPLACE_BUNDLE", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_NINE)));
1129
1130 return objValue;
1131 }
1132
WantAgentOperationTypeInit(NativeEngine * engine)1133 NativeValue* WantAgentOperationTypeInit(NativeEngine* engine)
1134 {
1135 HILOG_INFO("enter");
1136
1137 if (engine == nullptr) {
1138 HILOG_ERROR("Invalid input parameters");
1139 return nullptr;
1140 }
1141
1142 NativeValue* objValue = engine->CreateObject();
1143 NativeObject* object = ConvertNativeValueTo<NativeObject>(objValue);
1144
1145 if (object == nullptr) {
1146 HILOG_ERROR("Failed to get object");
1147 return nullptr;
1148 }
1149
1150 object->SetProperty("UNKNOWN_TYPE", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_ZERO)));
1151 object->SetProperty("START_ABILITY", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_ONE)));
1152 object->SetProperty("START_ABILITIES", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_TWO)));
1153 object->SetProperty("START_SERVICE", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_THREE)));
1154 object->SetProperty("SEND_COMMON_EVENT", CreateJsValue(*engine, static_cast<int32_t>(NUMBER_OF_PARAMETERS_FOUR)));
1155 object->SetProperty("START_FOREGROUND_SERVICE", CreateJsValue(*engine,
1156 static_cast<int32_t>(NUMBER_OF_PARAMETERS_FIVE)));
1157
1158 return objValue;
1159 }
1160
JsWantAgentInit(NativeEngine * engine,NativeValue * exportObj)1161 NativeValue* JsWantAgentInit(NativeEngine* engine, NativeValue* exportObj)
1162 {
1163 HILOG_DEBUG("JsWantAgentInit is called");
1164
1165 if (engine == nullptr || exportObj == nullptr) {
1166 HILOG_ERROR("engine or exportObj null");
1167 return nullptr;
1168 }
1169
1170 NativeObject* object = ConvertNativeValueTo<NativeObject>(exportObj);
1171 if (object == nullptr) {
1172 HILOG_ERROR("object null");
1173 return nullptr;
1174 }
1175
1176 std::unique_ptr<JsWantAgent> jsWantAgent = std::make_unique<JsWantAgent>();
1177 object->SetNativePointer(jsWantAgent.release(), JsWantAgent::Finalizer, nullptr);
1178
1179 object->SetProperty("WantAgentFlags", WantAgentFlagsInit(engine));
1180 object->SetProperty("OperationType", WantAgentOperationTypeInit(engine));
1181
1182 HILOG_DEBUG("JsWantAgentInit BindNativeFunction called");
1183 const char* moduleName = "JsWantAgent";
1184 BindNativeFunction(*engine, *object, "equal", moduleName, JsWantAgent::Equal);
1185 BindNativeFunction(*engine, *object, "getWant", moduleName, JsWantAgent::GetWant);
1186 BindNativeFunction(*engine, *object, "getOperationType", moduleName, JsWantAgent::GetOperationType);
1187 BindNativeFunction(*engine, *object, "getBundleName", moduleName, JsWantAgent::GetBundleName);
1188 BindNativeFunction(*engine, *object, "getUid", moduleName, JsWantAgent::GetUid);
1189 BindNativeFunction(*engine, *object, "cancel", moduleName, JsWantAgent::Cancel);
1190 BindNativeFunction(*engine, *object, "trigger", moduleName, JsWantAgent::Trigger);
1191 BindNativeFunction(*engine, *object, "getWantAgent", moduleName, JsWantAgent::GetWantAgent);
1192 HILOG_DEBUG("JsWantAgentInit end");
1193 return engine->CreateUndefined();
1194 }
1195
NapiGetNull(napi_env env)1196 napi_value NapiGetNull(napi_env env)
1197 {
1198 napi_value result = nullptr;
1199 napi_get_null(env, &result);
1200 return result;
1201 }
1202 } // namespace OHOS
1203