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