• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "ani_push_callback.h"
17 #include "ans_log_wrapper.h"
18 #include "sts_notification_manager.h"
19 #include "sts_common.h"
20 
21 namespace OHOS {
22 namespace NotificationManagerSts {
23 using namespace OHOS::Notification;
24 using namespace OHOS::NotificationSts;
StsPushCallBack(ani_env * env)25 StsPushCallBack::StsPushCallBack(ani_env *env)
26 {
27     if (env == nullptr || env->GetVM(&vm_) != ANI_OK) {
28         ANS_LOGE("InvalidParam 'env'");
29     }
30 }
31 
~StsPushCallBack()32 StsPushCallBack::~StsPushCallBack()
33 {
34 }
35 
OnCheckNotification(const std::string & notificationData,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)36 int32_t StsPushCallBack::OnCheckNotification(
37     const std::string &notificationData, const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
38 {
39     ANS_LOGD("enter");
40     std::lock_guard<std::mutex> l(mutexlock);
41     if (vm_ == nullptr || pushCallBackParam == nullptr) {
42         ANS_LOGE("InvalidParam");
43         return ERR_INVALID_STATE;
44     }
45     ani_env* env;
46     ani_status aniResult = ANI_ERROR;
47     if (ANI_OK != (aniResult = vm_->GetEnv(ANI_VERSION_1, &env))) {
48         ANS_LOGD("GetEnv error. result: %{public}d.", aniResult);
49         return ERR_INVALID_STATE;
50     }
51     return CheckNotification(env, notificationData, pushCallBackParam);
52 }
53 
SetJsPushCallBackObject(ani_env * env,NotificationConstant::SlotType slotType,ani_ref pushCallBackObject)54 void StsPushCallBack::SetJsPushCallBackObject(
55     ani_env *env, NotificationConstant::SlotType slotType, ani_ref pushCallBackObject)
56 {
57     ANS_LOGD("enter");
58     if (env == nullptr || pushCallBackObject == nullptr) {
59         ANS_LOGE("InvalidParam");
60         return;
61     }
62     ani_ref pushCheckObject;
63     ani_status status = ANI_OK;
64     if (ANI_OK != (status = env->GlobalReference_Create(pushCallBackObject, &pushCheckObject))) {
65         ANS_LOGE("GlobalReference_Create pushCallBackObject faild. status %{public}d", status);
66         return;
67     }
68     pushCallBackObjects_.insert_or_assign(slotType, pushCheckObject);
69 }
70 
HandleCheckCallback(ani_env * env,ani_fn_object fn,ani_object value,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)71 void StsPushCallBack::HandleCheckCallback(
72     ani_env *env, ani_fn_object fn, ani_object value, const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
73 {
74     ANS_LOGD("enter");
75     if (env == nullptr || fn == nullptr || value == nullptr || pushCallBackParam == nullptr) {
76         ANS_LOGE("pushCallBackObjects is nullptr");
77         return;
78     }
79     std::vector<ani_ref> vec;
80     vec.push_back(value);
81     ani_ref funcResult;
82     ani_status status = ANI_OK;
83     if (ANI_OK != (status = env->FunctionalObject_Call(fn, vec.size(), vec.data(), &funcResult))) {
84         ANS_LOGE("FunctionalObject_Call faild. status %{public}d", status);
85         return;
86     }
87     ResultParam result;
88     if (!WarpFunctionResult(env, static_cast<ani_object>(funcResult), result)) {
89         ANS_LOGE("WarpFunctionResult faild");
90         return;
91     }
92     std::unique_lock<ffrt::mutex> uniqueLock(pushCallBackParam->callBackMutex);
93     pushCallBackParam->result = result.code;
94     pushCallBackParam->ready = true;
95     pushCallBackParam->callBackCondition.notify_all();
96     ANS_LOGD("done");
97 }
98 
CheckNotification(ani_env * env,const std::string & notificationData,const std::shared_ptr<PushCallBackParam> & pushCallBackParam)99 int32_t StsPushCallBack::CheckNotification(
100     ani_env *env,
101     const std::string &notificationData,
102     const std::shared_ptr<PushCallBackParam> &pushCallBackParam)
103 {
104     ANS_LOGD("enter");
105     auto checkInfo = std::make_shared<NotificationCheckInfo>();
106     checkInfo->ConvertJsonStringToValue(notificationData);
107     NotificationConstant::SlotType outSlotType = static_cast<NotificationConstant::SlotType>(checkInfo->GetSlotType());
108     if (pushCallBackObjects_.find(outSlotType) == pushCallBackObjects_.end()) {
109         ANS_LOGE("pushCallBackObjects is nullptr");
110         return ERR_INVALID_STATE;
111     }
112     ani_object checkInfoObj;
113     if (!WarpNotificationCheckInfo(env, checkInfo, checkInfoObj) || checkInfoObj == nullptr) {
114         ANS_LOGE("WarpNotificationCheckInfo faild");
115         return ERR_INVALID_STATE;
116     }
117     HandleCheckCallback(
118         env, static_cast<ani_fn_object>(pushCallBackObjects_[outSlotType]), checkInfoObj, pushCallBackParam);
119     return ERR_OK;
120 }
121 
WarpFunctionResult(ani_env * env,ani_object obj,ResultParam & result)122 bool StsPushCallBack::WarpFunctionResult(ani_env *env, ani_object obj, ResultParam &result)
123 {
124     ANS_LOGD("enter");
125     if (env == nullptr || obj == nullptr) return false;
126     ani_status status = ANI_OK;
127     ani_double code;
128     ani_ref msg;
129     std::string message = "";
130     if (ANI_OK != (status = env->Object_GetPropertyByName_Double(obj, "code", &code))) {
131         ANS_LOGE("WarpFunctionResult. code faild. status %{public}d", status);
132         return false;
133     }
134     if (ANI_OK != (status = env->Object_GetPropertyByName_Ref(obj, "message", &msg))) {
135         ANS_LOGE("WarpFunctionResult. message faild. status %{public}d", status);
136         return false;
137     }
138     if (ANI_OK != (status = GetStringByAniString(env, static_cast<ani_string>(msg), message))) {
139         ANS_LOGE("GetStringByAniString faild. status %{public}d", status);
140         return false;
141     }
142     result.code = code;
143     result.msg = message;
144     ANS_LOGD("WarpFunctionResult: code %{public}d message %{public}s", result.code, result.msg.c_str());
145     return true;
146 }
147 }
148 }