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_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 namespace {
24 constexpr int32_t CYCLE_LIMIT_MIN = 0;
25 constexpr int32_t CYCLE_LIMIT_MAX = 1000;
26 }
AbilityDebugResponseProxy(const sptr<IRemoteObject> & impl)27 AbilityDebugResponseProxy::AbilityDebugResponseProxy(
28     const sptr<IRemoteObject> &impl) : IRemoteProxy<IAbilityDebugResponse>(impl)
29 {}
30 
WriteInterfaceToken(MessageParcel & data)31 bool AbilityDebugResponseProxy::WriteInterfaceToken(MessageParcel &data)
32 {
33     if (!data.WriteInterfaceToken(AbilityDebugResponseProxy::GetDescriptor())) {
34         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
35         return false;
36     }
37     return true;
38 }
39 
OnAbilitysDebugStarted(const std::vector<sptr<IRemoteObject>> & tokens)40 void AbilityDebugResponseProxy::OnAbilitysDebugStarted(const std::vector<sptr<IRemoteObject>> &tokens)
41 {
42     TAG_LOGD(AAFwkTag::APPMGR, "called");
43     SendRequest(IAbilityDebugResponse::Message::ON_ABILITYS_DEBUG_STARTED, tokens);
44 }
45 
OnAbilitysDebugStoped(const std::vector<sptr<IRemoteObject>> & tokens)46 void AbilityDebugResponseProxy::OnAbilitysDebugStoped(const std::vector<sptr<IRemoteObject>> &tokens)
47 {
48     TAG_LOGD(AAFwkTag::APPMGR, "called");
49     SendRequest(IAbilityDebugResponse::Message::ON_ABILITYS_DEBUG_STOPED, tokens);
50 }
51 
OnAbilitysAssertDebugChange(const std::vector<sptr<IRemoteObject>> & tokens,bool isAssertDebug)52 void AbilityDebugResponseProxy::OnAbilitysAssertDebugChange(
53     const std::vector<sptr<IRemoteObject>> &tokens, bool isAssertDebug)
54 {
55     TAG_LOGD(AAFwkTag::APPMGR, "called");
56     MessageParcel data;
57     if (!WriteInterfaceToken(data)) {
58         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
59         return;
60     }
61 
62     if (tokens.size() <= CYCLE_LIMIT_MIN || tokens.size() > CYCLE_LIMIT_MAX ||
63         !data.WriteInt32(tokens.size())) {
64         TAG_LOGE(AAFwkTag::APPMGR, "Write data size failed");
65         return;
66     }
67 
68     for (const auto &item : tokens) {
69         if (!data.WriteRemoteObject(item)) {
70             TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
71             return;
72         }
73     }
74 
75     if (!data.WriteBool(isAssertDebug)) {
76         TAG_LOGE(AAFwkTag::APPMGR, "Write flag failed");
77         return;
78     }
79 
80     sptr<IRemoteObject> remote = Remote();
81     if (remote == nullptr) {
82         TAG_LOGE(AAFwkTag::APPMGR, "null remote");
83         return;
84     }
85 
86     MessageParcel reply;
87     MessageOption option;
88     auto ret = remote->SendRequest(static_cast<uint32_t>(Message::ON_ABILITYS_ASSERT_DEBUG), data, reply, option);
89     if (ret != NO_ERROR) {
90         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
91     }
92 }
93 
SendRequest(const IAbilityDebugResponse::Message & message,const std::vector<sptr<IRemoteObject>> & tokens)94 void AbilityDebugResponseProxy::SendRequest(
95     const IAbilityDebugResponse::Message &message, const std::vector<sptr<IRemoteObject>> &tokens)
96 {
97     TAG_LOGD(AAFwkTag::APPMGR, "called");
98     MessageParcel data;
99     if (!WriteInterfaceToken(data)) {
100         TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
101         return;
102     }
103 
104     if (tokens.size() <= CYCLE_LIMIT_MIN || tokens.size() > CYCLE_LIMIT_MAX ||
105         !data.WriteInt32(tokens.size())) {
106         TAG_LOGE(AAFwkTag::APPMGR, "Write data size failed");
107         return;
108     }
109 
110     for (auto iter = tokens.begin(); iter != tokens.end(); iter++) {
111         if (!data.WriteRemoteObject(iter->GetRefPtr())) {
112             TAG_LOGE(AAFwkTag::APPMGR, "Write token failed");
113             return;
114         }
115     }
116 
117     sptr<IRemoteObject> remote = Remote();
118     if (remote == nullptr) {
119         TAG_LOGE(AAFwkTag::APPMGR, "null remote");
120         return;
121     }
122 
123     MessageParcel reply;
124     MessageOption option(MessageOption::TF_SYNC);
125     auto ret = remote->SendRequest(static_cast<uint32_t>(message), data, reply, option);
126     if (ret != NO_ERROR) {
127         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
128     }
129 }
130 } // namespace AppExecFwk
131 } // namespace OHOS
132