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_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 }
AppDebugListenerProxy(const sptr<IRemoteObject> & impl)27 AppDebugListenerProxy::AppDebugListenerProxy(
28     const sptr<IRemoteObject> &impl) : IRemoteProxy<IAppDebugListener>(impl)
29 {}
30 
WriteInterfaceToken(MessageParcel & data)31 bool AppDebugListenerProxy::WriteInterfaceToken(MessageParcel &data)
32 {
33     if (!data.WriteInterfaceToken(AppDebugListenerProxy::GetDescriptor())) {
34         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
35         return false;
36     }
37     return true;
38 }
39 
OnAppDebugStarted(const std::vector<AppDebugInfo> & debugInfos)40 void AppDebugListenerProxy::OnAppDebugStarted(const std::vector<AppDebugInfo> &debugInfos)
41 {
42     TAG_LOGD(AAFwkTag::APPMGR, "called");
43     SendRequest(IAppDebugListener::Message::ON_APP_DEBUG_STARTED, debugInfos);
44 }
45 
OnAppDebugStoped(const std::vector<AppDebugInfo> & debugInfos)46 void AppDebugListenerProxy::OnAppDebugStoped(const std::vector<AppDebugInfo> &debugInfos)
47 {
48     TAG_LOGD(AAFwkTag::APPMGR, "called");
49     SendRequest(IAppDebugListener::Message::ON_APP_DEBUG_STOPED, debugInfos);
50 }
51 
SendRequest(const IAppDebugListener::Message & message,const std::vector<AppDebugInfo> & debugInfos)52 void AppDebugListenerProxy::SendRequest(
53     const IAppDebugListener::Message &message, const std::vector<AppDebugInfo> &debugInfos)
54 {
55     MessageParcel data;
56     if (!WriteInterfaceToken(data)) {
57         TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed.");
58         return;
59     }
60 
61     if (debugInfos.size() <= CYCLE_LIMIT_MIN || debugInfos.size() > CYCLE_LIMIT_MAX ||
62         !data.WriteInt32(debugInfos.size())) {
63         TAG_LOGE(AAFwkTag::APPMGR, "Write debug info size failed.");
64         return;
65     }
66     for (auto &debugInfo : debugInfos) {
67         if (!data.WriteParcelable(&debugInfo)) {
68             TAG_LOGE(AAFwkTag::APPMGR, "Write debug info failed.");
69             return;
70         }
71     };
72 
73     sptr<IRemoteObject> remote = Remote();
74     if (remote == nullptr) {
75         TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
76         return;
77     }
78 
79     MessageParcel reply;
80     MessageOption option(MessageOption::TF_ASYNC);
81     int32_t ret = remote->SendRequest(static_cast<uint32_t>(message), data, reply, option);
82     if (ret != NO_ERROR) {
83         TAG_LOGE(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
84     }
85 }
86 } // namespace AppExecFwk
87 } // namespace OHOS
88