1 /*
2 * Copyright (c) 2022 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 <string>
17
18 #include <uv.h>
19 #include "securec.h"
20
21 #include "bundle_active_log.h"
22 #include "bundle_state_common.h"
23 #include "bundle_state_data.h"
24 #include "bundle_state_inner_errors.h"
25 #include "app_group_callback_info.h"
26
27 #include "app_group_observer_napi.h"
28
29 namespace OHOS {
30 namespace DeviceUsageStats {
~AppGroupObserver()31 AppGroupObserver::~AppGroupObserver()
32 {
33 if (bundleGroupCallbackInfo_.ref) {
34 napi_delete_reference(bundleGroupCallbackInfo_.env, bundleGroupCallbackInfo_.ref);
35 }
36 }
37
SetCallbackInfo(const napi_env & env,const napi_ref & ref)38 void AppGroupObserver::SetCallbackInfo(const napi_env &env, const napi_ref &ref)
39 {
40 bundleGroupCallbackInfo_.env = env;
41 bundleGroupCallbackInfo_.ref = ref;
42 }
43
SetBundleGroupChangedData(const CallbackReceiveDataWorker * commonEventDataWorkerData,napi_value & result)44 napi_value SetBundleGroupChangedData(const CallbackReceiveDataWorker *commonEventDataWorkerData, napi_value &result)
45 {
46 if (!commonEventDataWorkerData) {
47 BUNDLE_ACTIVE_LOGE("commonEventDataWorkerData is null");
48 return nullptr;
49 }
50 napi_value value = nullptr;
51
52 // oldGroup
53 napi_create_int32(commonEventDataWorkerData->env, commonEventDataWorkerData->oldGroup, &value);
54 napi_set_named_property(commonEventDataWorkerData->env, result, "appOldGroup", value);
55
56 // newGroup
57 napi_create_int32(commonEventDataWorkerData->env, commonEventDataWorkerData->newGroup, &value);
58 napi_set_named_property(commonEventDataWorkerData->env, result, "appNewGroup", value);
59
60 // userId
61 napi_create_int32(commonEventDataWorkerData->env, commonEventDataWorkerData->userId, &value);
62 napi_set_named_property(commonEventDataWorkerData->env, result, "userId", value);
63
64 // changeReason
65 napi_create_uint32(commonEventDataWorkerData->env, commonEventDataWorkerData->changeReason, &value);
66 napi_set_named_property(commonEventDataWorkerData->env, result, "changeReason", value);
67 // bundleName
68 napi_create_string_utf8(
69 commonEventDataWorkerData->env, commonEventDataWorkerData->bundleName.c_str(), NAPI_AUTO_LENGTH, &value);
70 napi_set_named_property(commonEventDataWorkerData->env, result, "bundleName", value);
71 BUNDLE_ACTIVE_LOGD(
72 "RegisterGroupCallBack appOldGroup = %{public}d, appNewGroup = %{public}d, userId=%{public}d, "
73 "changeReason=%{public}d, bundleName=%{public}s",
74 commonEventDataWorkerData->oldGroup, commonEventDataWorkerData->newGroup, commonEventDataWorkerData->userId,
75 commonEventDataWorkerData->changeReason, commonEventDataWorkerData->bundleName.c_str());
76
77 return BundleStateCommon::NapiGetNull(commonEventDataWorkerData->env);
78 }
79
UvQueueWorkOnAppGroupChanged(uv_work_t * work,int status)80 void UvQueueWorkOnAppGroupChanged(uv_work_t *work, int status)
81 {
82 BUNDLE_ACTIVE_LOGD("OnAppGroupChanged uv_work_t start");
83 if (!work) {
84 return;
85 }
86 CallbackReceiveDataWorker *callbackReceiveDataWorkerData = (CallbackReceiveDataWorker *)work->data;
87 if (!callbackReceiveDataWorkerData || !callbackReceiveDataWorkerData->ref) {
88 BUNDLE_ACTIVE_LOGE("OnAppGroupChanged commonEventDataWorkerData or ref is null");
89 delete work;
90 work = nullptr;
91 return;
92 }
93 napi_handle_scope scope = nullptr;
94 napi_open_handle_scope(callbackReceiveDataWorkerData->env, &scope);
95 if (scope == nullptr) {
96 delete work;
97 work = nullptr;
98 return;
99 }
100
101 napi_value result = nullptr;
102 napi_create_object(callbackReceiveDataWorkerData->env, &result);
103 if (!SetBundleGroupChangedData(callbackReceiveDataWorkerData, result)) {
104 delete work;
105 work = nullptr;
106 napi_close_handle_scope(callbackReceiveDataWorkerData->env, scope);
107 delete callbackReceiveDataWorkerData;
108 callbackReceiveDataWorkerData = nullptr;
109 return;
110 }
111
112 napi_value undefined = nullptr;
113 napi_get_undefined(callbackReceiveDataWorkerData->env, &undefined);
114
115 napi_value callback = nullptr;
116 napi_value resultout = nullptr;
117 napi_get_reference_value(callbackReceiveDataWorkerData->env, callbackReceiveDataWorkerData->ref, &callback);
118
119 napi_value results[ARGS_ONE] = {nullptr};
120 results[PARAM_FIRST] = result;
121 NAPI_CALL_RETURN_VOID(callbackReceiveDataWorkerData->env, napi_call_function(callbackReceiveDataWorkerData->env,
122 undefined, callback, ARGS_ONE, &results[PARAM_FIRST], &resultout));
123
124 napi_close_handle_scope(callbackReceiveDataWorkerData->env, scope);
125 delete callbackReceiveDataWorkerData;
126 callbackReceiveDataWorkerData = nullptr;
127 delete work;
128 work = nullptr;
129 }
130
131 /*
132 * observer callback when group change
133 */
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)134 void AppGroupObserver::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
135 {
136 BUNDLE_ACTIVE_LOGD("OnAppGroupChanged start");
137 uv_loop_s *loop = nullptr;
138 napi_get_uv_event_loop(bundleGroupCallbackInfo_.env, &loop);
139 if (!loop) {
140 BUNDLE_ACTIVE_LOGE("loop instance is nullptr");
141 return;
142 }
143 uv_work_t* work = new (std::nothrow) uv_work_t;
144 if (!work) {
145 BUNDLE_ACTIVE_LOGE("work is null");
146 return;
147 }
148 CallbackReceiveDataWorker* callbackReceiveDataWorker = new (std::nothrow) CallbackReceiveDataWorker();
149 if (!callbackReceiveDataWorker) {
150 BUNDLE_ACTIVE_LOGE("callbackReceiveDataWorker is null");
151 delete work;
152 work = nullptr;
153 return;
154 }
155 MessageParcel data;
156 if (!appGroupCallbackInfo.Marshalling(data)) {
157 BUNDLE_ACTIVE_LOGE("Marshalling fail");
158 }
159 AppGroupCallbackInfo* callBackInfo = appGroupCallbackInfo.Unmarshalling(data);
160 callbackReceiveDataWorker->oldGroup = callBackInfo->GetOldGroup();
161 callbackReceiveDataWorker->newGroup = callBackInfo->GetNewGroup();
162 callbackReceiveDataWorker->changeReason = callBackInfo->GetChangeReason();
163 callbackReceiveDataWorker->userId = callBackInfo->GetUserId();
164 callbackReceiveDataWorker->bundleName = callBackInfo->GetBundleName();
165 callbackReceiveDataWorker->env = bundleGroupCallbackInfo_.env;
166 callbackReceiveDataWorker->ref = bundleGroupCallbackInfo_.ref;
167 delete callBackInfo;
168
169 work->data = static_cast<void*>(callbackReceiveDataWorker);
170 int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAppGroupChanged);
171 if (ret != 0) {
172 delete callbackReceiveDataWorker;
173 callbackReceiveDataWorker = nullptr;
174 delete work;
175 work = nullptr;
176 }
177 }
178 } // namespace DeviceUsageStats
179 } // namespace OHOS