• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "napi_common_execute_result.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "insight_intent_execute_result.h"
20 #include "napi_common_util.h"
21 #include "napi_common_want.h"
22 #include "want_params.h"
23 #include <memory>
24 
25 namespace OHOS {
26 namespace AbilityRuntime {
27 using namespace OHOS::AppExecFwk;
UnwrapExecuteResult(napi_env env,napi_value param,InsightIntentExecuteResult & executeResult)28 bool UnwrapExecuteResult(napi_env env, napi_value param, InsightIntentExecuteResult &executeResult)
29 {
30     TAG_LOGD(AAFwkTag::JSNAPI, "called");
31 
32     if (!IsTypeForNapiValue(env, param, napi_valuetype::napi_object)) {
33         TAG_LOGE(AAFwkTag::JSNAPI, "UnwrapExecuteResult not object");
34         return false;
35     }
36     int32_t code = 0;
37     if (!UnwrapInt32ByPropertyName(env, param, "code", code)) {
38         TAG_LOGE(AAFwkTag::JSNAPI, "parse code fail");
39         return false;
40     }
41     executeResult.code = code;
42 
43     if (IsExistsByPropertyName(env, param, "result")) {
44         napi_value result = nullptr;
45         napi_get_named_property(env, param, "result", &result);
46         if (result != nullptr) {
47             napi_valuetype valueType = napi_undefined;
48             napi_typeof(env, result, &valueType);
49             if (valueType != napi_object) {
50                 TAG_LOGE(AAFwkTag::JSNAPI, "type not function");
51                 return false;
52             }
53             auto wp = std::make_shared<AAFwk::WantParams>();
54             if (!AppExecFwk::UnwrapWantParams(env, result, *wp)) {
55                 TAG_LOGE(AAFwkTag::JSNAPI, "unwrap want failed");
56                 return false;
57             }
58             if (!executeResult.CheckResult(wp)) {
59                 TAG_LOGE(AAFwkTag::JSNAPI, "Check wp fail");
60                 return false;
61             }
62             executeResult.result = wp;
63         }
64     }
65 
66     return true;
67 }
68 }  // namespace AbilityRuntime
69 }  // namespace OHOS
70