• 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;
UnwrapResultOfExecuteResult(napi_env env,napi_value param,InsightIntentExecuteResult & executeResult)28 bool UnwrapResultOfExecuteResult(napi_env env, napi_value param, InsightIntentExecuteResult &executeResult)
29 {
30     napi_value result = nullptr;
31     napi_get_named_property(env, param, "result", &result);
32     if (result != nullptr) {
33         napi_valuetype valueType = napi_undefined;
34         napi_typeof(env, result, &valueType);
35         if (valueType != napi_object) {
36             TAG_LOGE(AAFwkTag::JSNAPI, "type not function");
37             return false;
38         }
39         auto wp = std::make_shared<AAFwk::WantParams>();
40         if (!AppExecFwk::UnwrapWantParams(env, result, *wp)) {
41             TAG_LOGE(AAFwkTag::JSNAPI, "unwrap want failed");
42             return false;
43         }
44         if (!executeResult.CheckResult(wp)) {
45             TAG_LOGE(AAFwkTag::JSNAPI, "Check wp fail");
46             return false;
47         }
48         executeResult.result = wp;
49     }
50     return true;
51 }
52 
UnwrapExecuteResult(napi_env env,napi_value param,InsightIntentExecuteResult & executeResult)53 bool UnwrapExecuteResult(napi_env env, napi_value param, InsightIntentExecuteResult &executeResult)
54 {
55     TAG_LOGD(AAFwkTag::JSNAPI, "called");
56 
57     if (!IsTypeForNapiValue(env, param, napi_valuetype::napi_object)) {
58         TAG_LOGE(AAFwkTag::JSNAPI, "UnwrapExecuteResult not object");
59         return false;
60     }
61     int32_t code = 0;
62     if (!UnwrapInt32ByPropertyName(env, param, "code", code)) {
63         TAG_LOGE(AAFwkTag::JSNAPI, "parse code fail");
64         return false;
65     }
66     executeResult.code = code;
67 
68     if (IsExistsByPropertyName(env, param, "result")) {
69         if (!UnwrapResultOfExecuteResult(env, param, executeResult)) {
70             TAG_LOGE(AAFwkTag::JSNAPI, "unwrap result fail");
71             return false;
72         }
73     }
74 
75     if (IsExistsByPropertyName(env, param, "uris")) {
76         std::vector<std::string> uris;
77         if (!UnwrapStringArrayByPropertyName(env, param, "uris", uris)) {
78             TAG_LOGE(AAFwkTag::JSNAPI, "unwrap uris is null");
79             return false;
80         }
81         executeResult.uris = uris;
82     }
83 
84     if (IsExistsByPropertyName(env, param, "flags")) {
85         int32_t flags = 0;
86         if (!UnwrapInt32ByPropertyName(env, param, "flags", flags)) {
87             TAG_LOGE(AAFwkTag::JSNAPI, "unwrap flags is null");
88             return false;
89         }
90         executeResult.flags = flags;
91     }
92 
93     return true;
94 }
95 }  // namespace AbilityRuntime
96 }  // namespace OHOS
97