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         return;
97     }
98 
99     napi_value result = nullptr;
100     napi_create_object(callbackReceiveDataWorkerData->env, &result);
101     if (!SetBundleGroupChangedData(callbackReceiveDataWorkerData, result)) {
102         delete work;
103         work = nullptr;
104         napi_close_handle_scope(callbackReceiveDataWorkerData->env, scope);
105         delete callbackReceiveDataWorkerData;
106         callbackReceiveDataWorkerData = nullptr;
107         return;
108     }
109 
110     napi_value undefined = nullptr;
111     napi_get_undefined(callbackReceiveDataWorkerData->env, &undefined);
112 
113     napi_value callback = nullptr;
114     napi_value resultout = nullptr;
115     napi_get_reference_value(callbackReceiveDataWorkerData->env, callbackReceiveDataWorkerData->ref, &callback);
116 
117     napi_value results[ARGS_ONE] = {nullptr};
118     results[PARAM_FIRST] = result;
119     NAPI_CALL_RETURN_VOID(callbackReceiveDataWorkerData->env, napi_call_function(callbackReceiveDataWorkerData->env,
120         undefined, callback, ARGS_ONE, &results[PARAM_FIRST], &resultout));
121 
122     napi_close_handle_scope(callbackReceiveDataWorkerData->env, scope);
123     delete callbackReceiveDataWorkerData;
124     callbackReceiveDataWorkerData = nullptr;
125     delete work;
126     work = nullptr;
127 }
128 
129 /*
130 * observer callback when group change
131 */
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)132 void AppGroupObserver::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
133 {
134     BUNDLE_ACTIVE_LOGD("OnAppGroupChanged start");
135     uv_loop_s *loop = nullptr;
136     napi_get_uv_event_loop(bundleGroupCallbackInfo_.env, &loop);
137     if (!loop) {
138         BUNDLE_ACTIVE_LOGE("loop instance is nullptr");
139         return;
140     }
141     uv_work_t* work = new (std::nothrow) uv_work_t;
142     if (!work) {
143         BUNDLE_ACTIVE_LOGE("work is null");
144         return;
145     }
146     CallbackReceiveDataWorker* callbackReceiveDataWorker = new (std::nothrow) CallbackReceiveDataWorker();
147     if (!callbackReceiveDataWorker) {
148         BUNDLE_ACTIVE_LOGE("callbackReceiveDataWorker is null");
149         delete work;
150         work = nullptr;
151         return;
152     }
153     MessageParcel data;
154     if (!appGroupCallbackInfo.Marshalling(data)) {
155         BUNDLE_ACTIVE_LOGE("Marshalling fail");
156     }
157     AppGroupCallbackInfo* callBackInfo = appGroupCallbackInfo.Unmarshalling(data);
158     callbackReceiveDataWorker->oldGroup     = callBackInfo->GetOldGroup();
159     callbackReceiveDataWorker->newGroup     = callBackInfo->GetNewGroup();
160     callbackReceiveDataWorker->changeReason = callBackInfo->GetChangeReason();
161     callbackReceiveDataWorker->userId       = callBackInfo->GetUserId();
162     callbackReceiveDataWorker->bundleName   = callBackInfo->GetBundleName();
163     callbackReceiveDataWorker->env = bundleGroupCallbackInfo_.env;
164     callbackReceiveDataWorker->ref = bundleGroupCallbackInfo_.ref;
165     delete callBackInfo;
166 
167     work->data = static_cast<void*>(callbackReceiveDataWorker);
168     int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAppGroupChanged);
169     if (ret != 0) {
170         delete callbackReceiveDataWorker;
171         callbackReceiveDataWorker = nullptr;
172         delete work;
173         work = nullptr;
174     }
175 }
176 }  // namespace DeviceUsageStats
177 }  // namespace OHOS