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 "bundle_active_stub.h"
17
18 #include "ipc_object_stub.h"
19 #include "iremote_broker.h"
20
21 #include "bundle_active_event.h"
22 #include "bundle_active_event_stats.h"
23 #include "bundle_state_inner_errors.h"
24 #include "bundle_active_log.h"
25 #include "bundle_active_module_record.h"
26 #include "bundle_active_package_stats.h"
27 #include "iapp_group_callback.h"
28 #include "ibundle_active_service_ipc_interface_code.h"
29
30 namespace OHOS {
31 namespace DeviceUsageStats {
32 namespace {
33 constexpr int32_t EVENT_MAX_SIZE = 100000;
34 constexpr int32_t PACKAGE_MAX_SIZE = 1000;
35 }
36
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply,
38 MessageOption &option)
39 {
40 if (data.ReadInterfaceToken() != GetDescriptor()) {
41 return -1;
42 }
43 switch (code) {
44 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::REPORT_EVENT):
45 return HandleReportEvent(data, reply);
46 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::IS_BUNDLE_IDLE):
47 return HandleIsBundleIdle(data, reply);
48 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFO_BY_INTERVAL):
49 return HandleQueryBundleStatsInfoByInterval(data, reply);
50 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_EVENTS):
51 return HandleQueryBundleEvents(data, reply);
52 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::SET_APP_GROUP):
53 return HandleSetAppGroup(data, reply);
54 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFOS):
55 return HandleQueryBundleStatsInfos(data, reply);
56 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_CURRENT_BUNDLE_EVENTS):
57 return HandleQueryCurrentBundleEvents(data, reply);
58 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_APP_GROUP):
59 return HandleQueryAppGroup(data, reply);
60 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_MODULE_USAGE_RECORDS):
61 return HandleQueryModuleUsageRecords(data, reply);
62 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::REGISTER_APP_GROUP_CALLBACK):
63 return HandleRegisterAppGroupCallBack(data, reply);
64 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::UNREGISTER_APP_GROUP_CALLBACK):
65 return HandleUnRegisterAppGroupCallBack(data, reply);
66 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_DEVICE_EVENT_STATES):
67 return HandleQueryDeviceEventStats(data, reply);
68 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_NOTIFICATION_NUMBER):
69 return HandleQueryNotificationEventStats(data, reply);
70 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::IS_BUNDLE_USE_PERIOD):
71 return HandleIsBundleUsePeriod(data, reply);
72 default:
73 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75 return ERR_OK;
76 }
77
HandleReportEvent(MessageParcel & data,MessageParcel & reply)78 ErrCode BundleActiveStub::HandleReportEvent(MessageParcel& data, MessageParcel& reply)
79 {
80 int32_t userId = data.ReadInt32();
81 std::shared_ptr<BundleActiveEvent> tmpEvent = BundleActiveEvent::UnMarshalling(data);
82 if (!tmpEvent) {
83 return -1;
84 }
85 int32_t result = ReportEvent(*tmpEvent, userId);
86 return reply.WriteInt32(result);
87 }
88
HandleIsBundleIdle(MessageParcel & data,MessageParcel & reply)89 ErrCode BundleActiveStub::HandleIsBundleIdle(MessageParcel& data, MessageParcel& reply)
90 {
91 bool isBundleIdle = false;
92 std::string bundleName = data.ReadString();
93 int32_t userId = data.ReadInt32();
94 ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId);
95 reply.WriteInt32(isBundleIdle);
96 return reply.WriteInt32(errCode);
97 }
98
HandleIsBundleUsePeriod(MessageParcel & data,MessageParcel & reply)99 ErrCode BundleActiveStub::HandleIsBundleUsePeriod(MessageParcel& data, MessageParcel& reply)
100 {
101 bool IsUsePeriod = false;
102 std::string bundleName = data.ReadString();
103 int32_t userId = data.ReadInt32();
104 ErrCode errCode = IsBundleUsePeriod(IsUsePeriod, bundleName, userId);
105 reply.WriteInt32(IsUsePeriod);
106 return reply.WriteInt32(errCode);
107 }
108
HandleQueryBundleStatsInfoByInterval(MessageParcel & data,MessageParcel & reply)109 ErrCode BundleActiveStub::HandleQueryBundleStatsInfoByInterval(MessageParcel& data, MessageParcel& reply)
110 {
111 std::vector<BundleActivePackageStats> result;
112 int32_t intervalType = data.ReadInt32();
113 BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType);
114 int64_t beginTime = data.ReadInt64();
115 int64_t endTime = data.ReadInt64();
116 int32_t userId = data.ReadInt32();
117 ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId);
118 int32_t size = static_cast<int32_t>(result.size());
119 if (size > PACKAGE_MAX_SIZE) {
120 errCode = ERR_QUERY_RESULT_TOO_LARGE;
121 reply.WriteInt32(errCode);
122 return -1;
123 }
124 BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size);
125 reply.WriteInt32(errCode);
126 reply.WriteInt32(size);
127 for (int32_t i = 0; i < size; i++) {
128 bool tmp = result[i].Marshalling(reply);
129 if (tmp == false) {
130 return 1;
131 }
132 }
133 return size == 0;
134 }
135
HandleQueryBundleEvents(MessageParcel & data,MessageParcel & reply)136 ErrCode BundleActiveStub::HandleQueryBundleEvents(MessageParcel& data, MessageParcel& reply)
137 {
138 std::vector<BundleActiveEvent> result;
139 int64_t beginTime = data.ReadInt64();
140 int64_t endTime = data.ReadInt64();
141 int32_t userId = data.ReadInt32();
142 ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId);
143 int32_t size = static_cast<int32_t>(result.size());
144 if (size > EVENT_MAX_SIZE) {
145 errCode = ERR_QUERY_RESULT_TOO_LARGE;
146 reply.WriteInt32(errCode);
147 return -1;
148 }
149 reply.WriteInt32(errCode);
150 reply.WriteInt32(size);
151 for (int32_t i = 0; i < size; i++) {
152 bool tmp = result[i].Marshalling(reply);
153 if (tmp == false) {
154 return 1;
155 }
156 }
157 return size == 0;
158 }
159
HandleQueryBundleStatsInfos(MessageParcel & data,MessageParcel & reply)160 ErrCode BundleActiveStub::HandleQueryBundleStatsInfos(MessageParcel& data, MessageParcel& reply)
161 {
162 std::vector<BundleActivePackageStats> result;
163 int32_t intervalType = data.ReadInt32();
164 BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType);
165 int64_t beginTime = data.ReadInt64();
166 int64_t endTime = data.ReadInt64();
167 ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime);
168 int32_t size = static_cast<int32_t>(result.size());
169 if (size > PACKAGE_MAX_SIZE) {
170 errCode = ERR_QUERY_RESULT_TOO_LARGE;
171 reply.WriteInt32(errCode);
172 return -1;
173 }
174 BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size);
175 reply.WriteInt32(errCode);
176 reply.WriteInt32(size);
177 for (int32_t i = 0; i < size; i++) {
178 bool tmp = result[i].Marshalling(reply);
179 if (tmp == false) {
180 return 1;
181 }
182 }
183 return size == 0;
184 }
HandleSetAppGroup(MessageParcel & data,MessageParcel & reply)185 ErrCode BundleActiveStub::HandleSetAppGroup(MessageParcel &data, MessageParcel &reply)
186 {
187 std::string bundleName = data.ReadString();
188 int32_t newGroup = data.ReadInt32();
189 int32_t userId = data.ReadInt32();
190 ErrCode errCode = SetAppGroup(bundleName, newGroup, userId);
191 return reply.WriteInt32(errCode);
192 }
193
HandleQueryCurrentBundleEvents(MessageParcel & data,MessageParcel & reply)194 ErrCode BundleActiveStub::HandleQueryCurrentBundleEvents(MessageParcel &data, MessageParcel &reply)
195 {
196 std::vector<BundleActiveEvent> result;
197 int64_t beginTime = data.ReadInt64();
198 int64_t endTime = data.ReadInt64();
199 ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime);
200 int32_t size = static_cast<int32_t>(result.size());
201 if (size > EVENT_MAX_SIZE) {
202 errCode = ERR_QUERY_RESULT_TOO_LARGE;
203 reply.WriteInt32(errCode);
204 return -1;
205 }
206 reply.WriteInt32(errCode);
207 reply.WriteInt32(size);
208 for (int32_t i = 0; i < size; i++) {
209 bool tmp = result[i].Marshalling(reply);
210 if (tmp == false) {
211 return 1;
212 }
213 }
214 return size == 0;
215 }
216
HandleQueryModuleUsageRecords(MessageParcel & data,MessageParcel & reply)217 ErrCode BundleActiveStub::HandleQueryModuleUsageRecords(MessageParcel &data, MessageParcel &reply)
218 {
219 std::vector<BundleActiveModuleRecord> results;
220 int32_t maxNum = data.ReadInt32();
221 int32_t userId = data.ReadInt32();
222 ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId);
223 int32_t size = static_cast<int32_t>(results.size());
224 if (size > PACKAGE_MAX_SIZE) {
225 errCode = ERR_QUERY_RESULT_TOO_LARGE;
226 reply.WriteInt32(errCode);
227 return -1;
228 }
229 reply.WriteInt32(errCode);
230 reply.WriteInt32(size);
231 for (int32_t i = 0; i < size; i++) {
232 bool tmp = results[i].Marshalling(reply);
233 if (tmp == false) {
234 return 1;
235 }
236 }
237 return size == 0;
238 }
239
HandleQueryAppGroup(MessageParcel & data,MessageParcel & reply)240 ErrCode BundleActiveStub::HandleQueryAppGroup(MessageParcel& data, MessageParcel& reply)
241 {
242 int32_t appGroup = -1;
243 std::string bundleName = data.ReadString();
244 int32_t userId = data.ReadInt32();
245 ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId);
246 reply.WriteInt32(appGroup);
247 return reply.WriteInt32(errCode);
248 }
249
HandleRegisterAppGroupCallBack(MessageParcel & data,MessageParcel & reply)250 ErrCode BundleActiveStub::HandleRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply)
251 {
252 auto observer = iface_cast<IAppGroupCallback>(data.ReadRemoteObject());
253 if (!observer) {
254 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return");
255 return false;
256 }
257 BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok");
258 ErrCode errCode = RegisterAppGroupCallBack(observer);
259 return reply.WriteInt32(errCode);
260 }
261
HandleUnRegisterAppGroupCallBack(MessageParcel & data,MessageParcel & reply)262 ErrCode BundleActiveStub::HandleUnRegisterAppGroupCallBack(MessageParcel& data, MessageParcel& reply)
263 {
264 auto observer = iface_cast<IAppGroupCallback>(data.ReadRemoteObject());
265 if (!observer) {
266 BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return");
267 return false;
268 }
269 ErrCode errCode = UnRegisterAppGroupCallBack(observer);
270 return reply.WriteInt32(errCode);
271 }
272
HandleQueryDeviceEventStats(MessageParcel & data,MessageParcel & reply)273 ErrCode BundleActiveStub::HandleQueryDeviceEventStats(MessageParcel& data, MessageParcel& reply)
274 {
275 std::vector<BundleActiveEventStats> result;
276 int64_t beginTime = data.ReadInt64();
277 int64_t endTime = data.ReadInt64();
278 int32_t userId = data.ReadInt32();
279 ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId);
280 int32_t size = static_cast<int32_t>(result.size());
281 if (size > EVENT_MAX_SIZE) {
282 errCode = ERR_QUERY_RESULT_TOO_LARGE;
283 reply.WriteInt32(errCode);
284 return -1;
285 }
286 reply.WriteInt32(errCode);
287 reply.WriteInt32(size);
288 for (int32_t i = 0; i < size; i++) {
289 bool tmp = result[i].Marshalling(reply);
290 if (!tmp) {
291 return 1;
292 }
293 }
294 return size == 0;
295 }
296
HandleQueryNotificationEventStats(MessageParcel & data,MessageParcel & reply)297 ErrCode BundleActiveStub::HandleQueryNotificationEventStats(MessageParcel& data, MessageParcel& reply)
298 {
299 std::vector<BundleActiveEventStats> result;
300 int64_t beginTime = data.ReadInt64();
301 int64_t endTime = data.ReadInt64();
302 int32_t userId = data.ReadInt32();
303 ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId);
304 int32_t size = static_cast<int32_t>(result.size());
305 if (size > PACKAGE_MAX_SIZE) {
306 errCode = ERR_QUERY_RESULT_TOO_LARGE;
307 reply.WriteInt32(errCode);
308 return -1;
309 }
310 reply.WriteInt32(errCode);
311 reply.WriteInt32(size);
312 for (int32_t i = 0; i < size; i++) {
313 bool tmp = result[i].Marshalling(reply);
314 if (!tmp) {
315 return 1;
316 }
317 }
318 return size == 0;
319 }
320 } // namespace DeviceUsageStats
321 } // namespace OHOS
322
323