• 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)80 void UvQueueWorkOnAppGroupChanged(uv_work_t *work)
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_status status = napi_call_function(callbackReceiveDataWorkerData->env,
120         undefined, callback, ARGS_ONE, &results[PARAM_FIRST], &resultout);
121     if (status != napi_ok) {
122         GET_AND_THROW_LAST_ERROR(callbackReceiveDataWorkerData->env);
123     }
124 
125     napi_close_handle_scope(callbackReceiveDataWorkerData->env, scope);
126     delete callbackReceiveDataWorkerData;
127     callbackReceiveDataWorkerData = nullptr;
128     delete work;
129     work = nullptr;
130 }
131 
132 /*
133 * observer callback when group change
134 */
OnAppGroupChanged(const AppGroupCallbackInfo & appGroupCallbackInfo)135 ErrCode AppGroupObserver::OnAppGroupChanged(const AppGroupCallbackInfo &appGroupCallbackInfo)
136 {
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 ERR_OK;
142     }
143     uv_work_t* work = new (std::nothrow) uv_work_t;
144     if (!work) {
145         BUNDLE_ACTIVE_LOGE("work is null");
146         return ERR_OK;
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 ERR_OK;
154     }
155     MessageParcel data;
156     appGroupCallbackInfo.Marshalling(data);
157     AppGroupCallbackInfo* callBackInfo = appGroupCallbackInfo.Unmarshalling(data);
158     if (callBackInfo == nullptr) {
159         delete callbackReceiveDataWorker;
160         callbackReceiveDataWorker = nullptr;
161         delete work;
162         work = nullptr;
163         return ERR_OK;
164     }
165     callbackReceiveDataWorker->oldGroup     = callBackInfo->GetOldGroup();
166     callbackReceiveDataWorker->newGroup     = callBackInfo->GetNewGroup();
167     callbackReceiveDataWorker->changeReason = callBackInfo->GetChangeReason();
168     callbackReceiveDataWorker->userId       = callBackInfo->GetUserId();
169     callbackReceiveDataWorker->bundleName   = callBackInfo->GetBundleName();
170     callbackReceiveDataWorker->env = bundleGroupCallbackInfo_.env;
171     callbackReceiveDataWorker->ref = bundleGroupCallbackInfo_.ref;
172     delete callBackInfo;
173     work->data = static_cast<void*>(callbackReceiveDataWorker);
174     auto task = [work]() {
175         UvQueueWorkOnAppGroupChanged(work);
176     };
177     if (napi_status::napi_ok != napi_send_event(callbackReceiveDataWorker->env, task, napi_eprio_high)) {
178         BUNDLE_ACTIVE_LOGE("failed to napi_send_event");
179         delete callbackReceiveDataWorker;
180         callbackReceiveDataWorker = nullptr;
181         delete work;
182         work = nullptr;
183     }
184     return ERR_OK;
185 }
186 }  // namespace DeviceUsageStats
187 }  // namespace OHOS