• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_TWO] = {nullptr};
118     results[PARAM_FIRST] = BundleStateCommon::GetErrorValue(callbackReceiveDataWorkerData->env, NO_ERROR);
119     results[PARAM_SECOND] = result;
120     NAPI_CALL_RETURN_VOID(callbackReceiveDataWorkerData->env, napi_call_function(callbackReceiveDataWorkerData->env,
121         undefined, callback, ARGS_TWO, &results[PARAM_FIRST], &resultout));
122 
123     napi_close_handle_scope(callbackReceiveDataWorkerData->env, scope);
124     delete callbackReceiveDataWorkerData;
125     callbackReceiveDataWorkerData = nullptr;
126     delete work;
127     work = nullptr;
128 }
129 
130 /*
131 * observer callback when group change
132 */
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)133 void AppGroupObserver::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
134 {
135     BUNDLE_ACTIVE_LOGD("OnAppGroupChanged start");
136     uv_loop_s *loop = nullptr;
137     napi_get_uv_event_loop(bundleGroupCallbackInfo_.env, &loop);
138     if (!loop) {
139         BUNDLE_ACTIVE_LOGE("loop instance is nullptr");
140         return;
141     }
142     uv_work_t* work = new (std::nothrow) uv_work_t;
143     if (!work) {
144         BUNDLE_ACTIVE_LOGE("work is null");
145         return;
146     }
147     CallbackReceiveDataWorker* callbackReceiveDataWorker = new (std::nothrow) CallbackReceiveDataWorker();
148     if (!callbackReceiveDataWorker) {
149         BUNDLE_ACTIVE_LOGE("callbackReceiveDataWorker is null");
150         delete work;
151         work = nullptr;
152         return;
153     }
154     MessageParcel data;
155     if (!appGroupCallbackInfo.Marshalling(data)) {
156         BUNDLE_ACTIVE_LOGE("Marshalling fail");
157     }
158     AppGroupCallbackInfo* callBackInfo = appGroupCallbackInfo.Unmarshalling(data);
159     callbackReceiveDataWorker->oldGroup     = callBackInfo->GetOldGroup();
160     callbackReceiveDataWorker->newGroup     = callBackInfo->GetNewGroup();
161     callbackReceiveDataWorker->changeReason = callBackInfo->GetChangeReason();
162     callbackReceiveDataWorker->userId       = callBackInfo->GetUserId();
163     callbackReceiveDataWorker->bundleName   = callBackInfo->GetBundleName();
164     callbackReceiveDataWorker->env = bundleGroupCallbackInfo_.env;
165     callbackReceiveDataWorker->ref = bundleGroupCallbackInfo_.ref;
166     delete callBackInfo;
167 
168     work->data = static_cast<void*>(callbackReceiveDataWorker);
169     int ret = uv_queue_work(loop, work, [](uv_work_t *work) {}, UvQueueWorkOnAppGroupChanged);
170     if (ret != 0) {
171         delete callbackReceiveDataWorker;
172         callbackReceiveDataWorker = nullptr;
173         delete work;
174         work = nullptr;
175     }
176 }
177 }  // namespace DeviceUsageStats
178 }  // namespace OHOS