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 "ability_debug_response_stub.h"
17 
18 #include "appexecfwk_errors.h"
19 #include "hilog_tag_wrapper.h"
20 #include "ipc_types.h"
21 #include "iremote_object.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 constexpr int32_t CYCLE_LIMIT_MIN = 0;
27 constexpr int32_t CYCLE_LIMIT_MAX = 1000;
28 }
AbilityDebugResponseStub()29 AbilityDebugResponseStub::AbilityDebugResponseStub() {}
30 
~AbilityDebugResponseStub()31 AbilityDebugResponseStub::~AbilityDebugResponseStub() {}
32 
HandleOnAbilitysDebugStarted(MessageParcel & data,MessageParcel & reply)33 int32_t AbilityDebugResponseStub::HandleOnAbilitysDebugStarted(MessageParcel &data, MessageParcel &reply)
34 {
35     auto tokenSize = data.ReadInt32();
36     if (tokenSize <= CYCLE_LIMIT_MIN || tokenSize > CYCLE_LIMIT_MAX) {
37         TAG_LOGE(AAFwkTag::APPMGR, "Token size exceeds limit");
38         return ERR_INVALID_DATA;
39     }
40 
41     std::vector<sptr<IRemoteObject>> tokens;
42     for (int32_t index = 0; index < tokenSize; index++) {
43         auto token = data.ReadRemoteObject();
44         if (token == nullptr) {
45             TAG_LOGE(AAFwkTag::APPMGR, "null token");
46             return ERR_INVALID_DATA;
47         }
48         tokens.push_back(token);
49     }
50     OnAbilitysDebugStarted(tokens);
51     return NO_ERROR;
52 }
53 
HandleOnAbilitysDebugStoped(MessageParcel & data,MessageParcel & reply)54 int32_t AbilityDebugResponseStub::HandleOnAbilitysDebugStoped(MessageParcel &data, MessageParcel &reply)
55 {
56     auto tokenSize = data.ReadInt32();
57     if (tokenSize <= CYCLE_LIMIT_MIN || tokenSize > CYCLE_LIMIT_MAX) {
58         TAG_LOGE(AAFwkTag::APPMGR, "Token size exceeds limit");
59         return ERR_INVALID_DATA;
60     }
61 
62     std::vector<sptr<IRemoteObject>> tokens;
63     for (int32_t index = 0; index < tokenSize; index++) {
64         auto token = data.ReadRemoteObject();
65         if (token == nullptr) {
66             TAG_LOGE(AAFwkTag::APPMGR, "null token");
67             return ERR_INVALID_DATA;
68         }
69         tokens.push_back(token);
70     }
71     OnAbilitysDebugStoped(tokens);
72     return NO_ERROR;
73 }
74 
HandleOnAbilitysAssertDebugChange(MessageParcel & data,MessageParcel & reply)75 int32_t AbilityDebugResponseStub::HandleOnAbilitysAssertDebugChange(MessageParcel &data, MessageParcel &reply)
76 {
77     auto tokenSize = data.ReadInt32();
78     if (tokenSize <= CYCLE_LIMIT_MIN || tokenSize > CYCLE_LIMIT_MAX) {
79         TAG_LOGE(AAFwkTag::APPMGR, "Token size exceeds limit");
80         return ERR_INVALID_DATA;
81     }
82 
83     std::vector<sptr<IRemoteObject>> tokens;
84     for (int32_t index = 0; index < tokenSize; index++) {
85         auto token = data.ReadRemoteObject();
86         if (token == nullptr) {
87             TAG_LOGE(AAFwkTag::APPMGR, "null token");
88             return ERR_INVALID_DATA;
89         }
90         tokens.push_back(token);
91     }
92     auto isAssertDebug = data.ReadBool();
93     OnAbilitysAssertDebugChange(tokens, isAssertDebug);
94     return NO_ERROR;
95 }
96 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)97 int AbilityDebugResponseStub::OnRemoteRequest(
98     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
99 {
100     TAG_LOGD(AAFwkTag::APPMGR, "code: %{public}u, flags: %{public}d", code, option.GetFlags());
101     std::u16string descriptor = AbilityDebugResponseStub::GetDescriptor();
102     std::u16string remoteDescriptor = data.ReadInterfaceToken();
103     if (descriptor != remoteDescriptor) {
104         TAG_LOGE(AAFwkTag::APPMGR, "invalid descriptor");
105         return ERR_INVALID_STATE;
106     }
107 
108     switch (code) {
109         case static_cast<uint32_t>(IAbilityDebugResponse::Message::ON_ABILITYS_DEBUG_STARTED):
110             return HandleOnAbilitysDebugStarted(data, reply);
111         case static_cast<uint32_t>(IAbilityDebugResponse::Message::ON_ABILITYS_DEBUG_STOPED):
112             return HandleOnAbilitysDebugStoped(data, reply);
113     }
114 
115     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
116 }
117 } // namespace AppExecFwk
118 } // namespace OHOS
119