1 /*
2  * Copyright (c) 2023-2024 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 "app_debug_listener_stub.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20 #include "iremote_object.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 constexpr int32_t CYCLE_LIMIT_MIN = 0;
26 constexpr int32_t CYCLE_LIMIT_MAX = 1000;
27 }
AppDebugListenerStub()28 AppDebugListenerStub::AppDebugListenerStub() {}
29 
~AppDebugListenerStub()30 AppDebugListenerStub::~AppDebugListenerStub() {}
31 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int AppDebugListenerStub::OnRemoteRequest(
33     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
34 {
35     TAG_LOGD(AAFwkTag::APPMGR, "code = %{public}u, flags= %{public}d", code, option.GetFlags());
36     std::u16string descriptor = AppDebugListenerStub::GetDescriptor();
37     std::u16string remoteDescriptor = data.ReadInterfaceToken();
38     if (descriptor != remoteDescriptor) {
39         TAG_LOGE(AAFwkTag::APPMGR, "Local descriptor is not equal to remote.");
40         return ERR_INVALID_STATE;
41     }
42 
43     switch (code) {
44         case static_cast<uint32_t>(IAppDebugListener::Message::ON_APP_DEBUG_STARTED):
45             return HandleOnAppDebugStarted(data, reply);
46         case static_cast<uint32_t>(IAppDebugListener::Message::ON_APP_DEBUG_STOPED):
47             return HandleOnAppDebugStoped(data, reply);
48     }
49 
50     TAG_LOGD(AAFwkTag::APPMGR, "AppDebugListenerStub::OnRemoteRequest end");
51     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53 
HandleOnAppDebugStarted(MessageParcel & data,MessageParcel & reply)54 int32_t AppDebugListenerStub::HandleOnAppDebugStarted(MessageParcel &data, MessageParcel &reply)
55 {
56     auto infoSize = data.ReadInt32();
57     if (infoSize <= CYCLE_LIMIT_MIN || infoSize > CYCLE_LIMIT_MAX) {
58         TAG_LOGE(AAFwkTag::APPMGR, "Token size exceeds limit.");
59         return ERR_INVALID_DATA;
60     }
61 
62     std::vector<AppDebugInfo> appDebugInfos;
63     for (int32_t index = 0; index < infoSize; index++) {
64         std::unique_ptr<AppDebugInfo> appDebugInfo(data.ReadParcelable<AppDebugInfo>());
65         if (appDebugInfo == nullptr) {
66             TAG_LOGE(AAFwkTag::APPMGR, "Read app debug infos failed.");
67             return ERR_INVALID_DATA;
68         }
69         appDebugInfos.emplace_back(*appDebugInfo);
70     }
71 
72     OnAppDebugStarted(appDebugInfos);
73     return NO_ERROR;
74 }
75 
HandleOnAppDebugStoped(MessageParcel & data,MessageParcel & reply)76 int32_t AppDebugListenerStub::HandleOnAppDebugStoped(MessageParcel &data, MessageParcel &reply)
77 {
78     auto infoSize = data.ReadInt32();
79     if (infoSize <= CYCLE_LIMIT_MIN || infoSize > CYCLE_LIMIT_MAX) {
80         TAG_LOGE(AAFwkTag::APPMGR, "Token size exceeds limit.");
81         return ERR_INVALID_DATA;
82     }
83 
84     std::vector<AppDebugInfo> appDebugInfos;
85     for (int32_t index = 0; index < infoSize; index++) {
86         std::unique_ptr<AppDebugInfo> appDebugInfo(data.ReadParcelable<AppDebugInfo>());
87         if (appDebugInfo == nullptr) {
88             TAG_LOGE(AAFwkTag::APPMGR, "Read app debug infos failed.");
89             return ERR_INVALID_DATA;
90         }
91         appDebugInfos.emplace_back(*appDebugInfo);
92     }
93 
94     OnAppDebugStoped(appDebugInfos);
95     return NO_ERROR;
96 }
97 } // namespace AppExecFwk
98 } // namespace OHOS
99