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 }
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)36 int32_t BundleActiveStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel &reply,
37 MessageOption &option)
38 {
39 if (data.ReadInterfaceToken() != GetDescriptor()) {
40 return -1;
41 }
42 switch (code) {
43 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::REPORT_EVENT): {
44 int32_t userId = data.ReadInt32();
45 std::shared_ptr<BundleActiveEvent> tmpEvent = BundleActiveEvent::UnMarshalling(data);
46 if (!tmpEvent) {
47 return -1;
48 }
49 int32_t result = ReportEvent(*tmpEvent, userId);
50 return reply.WriteInt32(result);
51 }
52 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::IS_BUNDLE_IDLE): {
53 bool isBundleIdle = false;
54 std::string bundleName = data.ReadString();
55 int32_t userId = data.ReadInt32();
56 ErrCode errCode = IsBundleIdle(isBundleIdle, bundleName, userId);
57 reply.WriteInt32(isBundleIdle);
58 return reply.WriteInt32(errCode);
59 }
60 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFO_BY_INTERVAL): {
61 std::vector<BundleActivePackageStats> result;
62 int32_t intervalType = data.ReadInt32();
63 BUNDLE_ACTIVE_LOGI("OnRemoteRequest intervaltype is %{public}d", intervalType);
64 int64_t beginTime = data.ReadInt64();
65 int64_t endTime = data.ReadInt64();
66 int32_t userId = data.ReadInt32();
67 ErrCode errCode = QueryBundleStatsInfoByInterval(result, intervalType, beginTime, endTime, userId);
68 int32_t size = static_cast<int32_t>(result.size());
69 if (size > PACKAGE_MAX_SIZE) {
70 errCode = ERR_QUERY_RESULT_TOO_LARGE;
71 reply.WriteInt32(errCode);
72 return -1;
73 }
74 BUNDLE_ACTIVE_LOGI("OnRemoteRequest result size is %{public}d", size);
75 reply.WriteInt32(errCode);
76 reply.WriteInt32(size);
77 for (int32_t i = 0; i < size; i++) {
78 bool tmp = result[i].Marshalling(reply);
79 if (tmp == false) {
80 return 1;
81 }
82 }
83 return size == 0;
84 }
85 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_EVENTS): {
86 std::vector<BundleActiveEvent> result;
87 int64_t beginTime = data.ReadInt64();
88 int64_t endTime = data.ReadInt64();
89 int32_t userId = data.ReadInt32();
90 ErrCode errCode = QueryBundleEvents(result, beginTime, endTime, userId);
91 int32_t size = static_cast<int32_t>(result.size());
92 if (size > EVENT_MAX_SIZE) {
93 errCode = ERR_QUERY_RESULT_TOO_LARGE;
94 reply.WriteInt32(errCode);
95 return -1;
96 }
97 reply.WriteInt32(errCode);
98 reply.WriteInt32(size);
99 for (int32_t i = 0; i < size; i++) {
100 bool tmp = result[i].Marshalling(reply);
101 if (tmp == false) {
102 return 1;
103 }
104 }
105 return size == 0;
106 }
107 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::SET_APP_GROUP): {
108 std::string bundleName = data.ReadString();
109 int32_t newGroup = data.ReadInt32();
110 int32_t userId = data.ReadInt32();
111 ErrCode errCode = SetAppGroup(bundleName, newGroup, userId);
112 return reply.WriteInt32(errCode);
113 }
114 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_BUNDLE_STATS_INFOS): {
115 std::vector<BundleActivePackageStats> result;
116 int32_t intervalType = data.ReadInt32();
117 BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS intervaltype is %{public}d", intervalType);
118 int64_t beginTime = data.ReadInt64();
119 int64_t endTime = data.ReadInt64();
120 ErrCode errCode = QueryBundleStatsInfos(result, intervalType, beginTime, endTime);
121 int32_t size = static_cast<int32_t>(result.size());
122 if (size > PACKAGE_MAX_SIZE) {
123 errCode = ERR_QUERY_RESULT_TOO_LARGE;
124 reply.WriteInt32(errCode);
125 return -1;
126 }
127 BUNDLE_ACTIVE_LOGI("OnRemoteRequest QUERY_BUNDLE_STATS_INFOS result size is %{public}d", size);
128 reply.WriteInt32(errCode);
129 reply.WriteInt32(size);
130 for (int32_t i = 0; i < size; i++) {
131 bool tmp = result[i].Marshalling(reply);
132 if (tmp == false) {
133 return 1;
134 }
135 }
136 return size == 0;
137 }
138 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_CURRENT_BUNDLE_EVENTS): {
139 std::vector<BundleActiveEvent> result;
140 int64_t beginTime = data.ReadInt64();
141 int64_t endTime = data.ReadInt64();
142 ErrCode errCode = QueryCurrentBundleEvents(result, beginTime, endTime);
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 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_APP_GROUP): {
160 int32_t appGroup = -1;
161 std::string bundleName = data.ReadString();
162 int32_t userId = data.ReadInt32();
163 ErrCode errCode = QueryAppGroup(appGroup, bundleName, userId);
164 reply.WriteInt32(appGroup);
165 return reply.WriteInt32(errCode);
166 }
167 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_MODULE_USAGE_RECORDS): {
168 std::vector<BundleActiveModuleRecord> results;
169 int32_t maxNum = data.ReadInt32();
170 int32_t userId = data.ReadInt32();
171 ErrCode errCode = QueryModuleUsageRecords(maxNum, results, userId);
172 int32_t size = static_cast<int32_t>(results.size());
173 if (size > PACKAGE_MAX_SIZE) {
174 errCode = ERR_QUERY_RESULT_TOO_LARGE;
175 reply.WriteInt32(errCode);
176 return -1;
177 }
178 reply.WriteInt32(errCode);
179 reply.WriteInt32(size);
180 for (int32_t i = 0; i < size; i++) {
181 bool tmp = results[i].Marshalling(reply);
182 if (tmp == false) {
183 return 1;
184 }
185 }
186 return size == 0;
187 }
188 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::REGISTER_APP_GROUP_CALLBACK): {
189 auto observer = iface_cast<IAppGroupCallback>(data.ReadRemoteObject());
190 if (!observer) {
191 BUNDLE_ACTIVE_LOGE("RegisterAppGroupCallBack observer is null, return");
192 return false;
193 }
194 BUNDLE_ACTIVE_LOGI("RegisterAppGroupCallBack observer is ok");
195 ErrCode errCode = RegisterAppGroupCallBack(observer);
196 return reply.WriteInt32(errCode);
197 }
198 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::UNREGISTER_APP_GROUP_CALLBACK): {
199 auto observer = iface_cast<IAppGroupCallback>(data.ReadRemoteObject());
200 if (!observer) {
201 BUNDLE_ACTIVE_LOGE("UnRegisterAppGroupCallBack observer is null, return");
202 return false;
203 }
204 ErrCode errCode = UnRegisterAppGroupCallBack(observer);
205 return reply.WriteInt32(errCode);
206 }
207 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_DEVICE_EVENT_STATES): {
208 std::vector<BundleActiveEventStats> result;
209 int64_t beginTime = data.ReadInt64();
210 int64_t endTime = data.ReadInt64();
211 int32_t userId = data.ReadInt32();
212 ErrCode errCode = QueryDeviceEventStats(beginTime, endTime, result, userId);
213 int32_t size = static_cast<int32_t>(result.size());
214 if (size > EVENT_MAX_SIZE) {
215 errCode = ERR_QUERY_RESULT_TOO_LARGE;
216 reply.WriteInt32(errCode);
217 return -1;
218 }
219 reply.WriteInt32(errCode);
220 reply.WriteInt32(size);
221 for (int32_t i = 0; i < size; i++) {
222 bool tmp = result[i].Marshalling(reply);
223 if (!tmp) {
224 return 1;
225 }
226 }
227 return size == 0;
228 }
229 case static_cast<uint32_t>(IBundleActiveServiceInterfaceCode::QUERY_NOTIFICATION_NUMBER): {
230 std::vector<BundleActiveEventStats> result;
231 int64_t beginTime = data.ReadInt64();
232 int64_t endTime = data.ReadInt64();
233 int32_t userId = data.ReadInt32();
234 ErrCode errCode = QueryNotificationEventStats(beginTime, endTime, result, userId);
235 int32_t size = static_cast<int32_t>(result.size());
236 if (size > PACKAGE_MAX_SIZE) {
237 errCode = ERR_QUERY_RESULT_TOO_LARGE;
238 reply.WriteInt32(errCode);
239 return -1;
240 }
241 reply.WriteInt32(errCode);
242 reply.WriteInt32(size);
243 for (int32_t i = 0; i < size; i++) {
244 bool tmp = result[i].Marshalling(reply);
245 if (!tmp) {
246 return 1;
247 }
248 }
249 return size == 0;
250 }
251 default:
252 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
253 }
254 return ERR_OK;
255 }
256 } // namespace DeviceUsageStats
257 } // namespace OHOS
258
259