• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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_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 {
31     responseFuncMap_[static_cast<uint32_t>(IAbilityDebugResponse::Message::ON_ABILITYS_DEBUG_STARTED)] =
32         &AbilityDebugResponseStub::HandleOnAbilitysDebugStarted;
33     responseFuncMap_[static_cast<uint32_t>(IAbilityDebugResponse::Message::ON_ABILITYS_DEBUG_STOPED)] =
34         &AbilityDebugResponseStub::HandleOnAbilitysDebugStoped;
35 }
36 
~AbilityDebugResponseStub()37 AbilityDebugResponseStub::~AbilityDebugResponseStub()
38 {
39     responseFuncMap_.clear();
40 }
41 
HandleOnAbilitysDebugStarted(MessageParcel & data,MessageParcel & reply)42 int32_t AbilityDebugResponseStub::HandleOnAbilitysDebugStarted(MessageParcel &data, MessageParcel &reply)
43 {
44     auto tokenSize = data.ReadInt32();
45     if (tokenSize <= CYCLE_LIMIT_MIN || tokenSize > CYCLE_LIMIT_MAX) {
46         HILOG_ERROR("Token size exceeds limit.");
47         return ERR_INVALID_DATA;
48     }
49 
50     std::vector<sptr<IRemoteObject>> tokens;
51     for (int32_t index = 0; index < tokenSize; index++) {
52         auto token = data.ReadRemoteObject();
53         if (token == nullptr) {
54             HILOG_ERROR("Token is nullptr.");
55             return ERR_INVALID_DATA;
56         }
57         tokens.push_back(token);
58     }
59     OnAbilitysDebugStarted(tokens);
60     return NO_ERROR;
61 }
62 
HandleOnAbilitysDebugStoped(MessageParcel & data,MessageParcel & reply)63 int32_t AbilityDebugResponseStub::HandleOnAbilitysDebugStoped(MessageParcel &data, MessageParcel &reply)
64 {
65     auto tokenSize = data.ReadInt32();
66     if (tokenSize <= CYCLE_LIMIT_MIN || tokenSize > CYCLE_LIMIT_MAX) {
67         HILOG_ERROR("Token size exceeds limit.");
68         return ERR_INVALID_DATA;
69     }
70 
71     std::vector<sptr<IRemoteObject>> tokens;
72     for (int32_t index = 0; index < tokenSize; index++) {
73         auto token = data.ReadRemoteObject();
74         if (token == nullptr) {
75             HILOG_ERROR("Token is nullptr.");
76             return ERR_INVALID_DATA;
77         }
78         tokens.push_back(token);
79     }
80     OnAbilitysDebugStoped(tokens);
81     return NO_ERROR;
82 }
83 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)84 int AbilityDebugResponseStub::OnRemoteRequest(
85     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
86 {
87     HILOG_DEBUG("code = %{public}u, flags= %{public}d", code, option.GetFlags());
88     std::u16string descriptor = AbilityDebugResponseStub::GetDescriptor();
89     std::u16string remoteDescriptor = data.ReadInterfaceToken();
90     if (descriptor != remoteDescriptor) {
91         HILOG_ERROR("Local descriptor is not equal to remote.");
92         return ERR_INVALID_STATE;
93     }
94 
95     auto itFunc = responseFuncMap_.find(code);
96     if (itFunc != responseFuncMap_.end()) {
97         auto responseFunc = itFunc->second;
98         if (responseFunc != nullptr) {
99             return (this->*responseFunc)(data, reply);
100         }
101     }
102     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
103 }
104 } // namespace AppExecFwk
105 } // namespace OHOS
106