1 /*
2  * Copyright (c) 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 "status_bar_delegate_proxy.h"
17 
18 #include "ability_manager_errors.h"
19 #include "hilog_tag_wrapper.h"
20 #include "message_parcel.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
24 
StatusBarDelegateProxy(const sptr<IRemoteObject> & impl)25 StatusBarDelegateProxy::StatusBarDelegateProxy(const sptr<IRemoteObject> &impl)
26     : IRemoteProxy<IStatusBarDelegate>(impl) {}
27 
CheckIfStatusBarItemExists(uint32_t accessTokenId,bool & isExist)28 int32_t StatusBarDelegateProxy::CheckIfStatusBarItemExists(uint32_t accessTokenId, bool& isExist)
29 {
30     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
31     MessageParcel data;
32     MessageParcel reply;
33     MessageOption option;
34     if (!data.WriteInterfaceToken(IStatusBarDelegate::GetDescriptor())) {
35         TAG_LOGE(AAFwkTag::ABILITYMGR, "write token failed");
36         return AAFwk::ERR_NATIVE_IPC_PARCEL_FAILED;
37     }
38     if (!data.WriteUint32(accessTokenId)) {
39         TAG_LOGE(AAFwkTag::ABILITYMGR, "accessTokenId write failed");
40         return AAFwk::ERR_NATIVE_IPC_PARCEL_FAILED;
41     }
42     auto ret = SendRequest(StatusBarDelegateCmd::CHECK_IF_STATUS_BAR_ITEM_EXISTS, data, reply, option);
43     if (ret != NO_ERROR) {
44         TAG_LOGE(AAFwkTag::ABILITYMGR, "Send request error: %{public}d", ret);
45         return ret;
46     }
47     ret = reply.ReadInt32();
48     isExist = reply.ReadBool();
49     return ret;
50 }
51 
AttachPidToStatusBarItem(uint32_t accessTokenId,int32_t pid)52 int32_t StatusBarDelegateProxy::AttachPidToStatusBarItem(uint32_t accessTokenId, int32_t pid)
53 {
54     TAG_LOGI(AAFwkTag::ABILITYMGR, "call");
55     MessageParcel data;
56     MessageParcel reply;
57     MessageOption option;
58     if (!data.WriteInterfaceToken(IStatusBarDelegate::GetDescriptor())) {
59         TAG_LOGE(AAFwkTag::ABILITYMGR, "write token failed");
60         return AAFwk::ERR_NATIVE_IPC_PARCEL_FAILED;
61     }
62     if (!data.WriteUint32(accessTokenId)) {
63         TAG_LOGE(AAFwkTag::ABILITYMGR, "write accessTokenId failed");
64         return AAFwk::ERR_NATIVE_IPC_PARCEL_FAILED;
65     }
66     if (!data.WriteInt32(pid)) {
67         TAG_LOGE(AAFwkTag::ABILITYMGR, "write pid failed");
68         return AAFwk::ERR_NATIVE_IPC_PARCEL_FAILED;
69     }
70     auto ret = SendRequest(StatusBarDelegateCmd::ATTACH_PID_TO_STATUS_BAR_ITEM, data, reply, option);
71     if (ret != NO_ERROR) {
72         TAG_LOGE(AAFwkTag::ABILITYMGR, "Send request error: %{public}d", ret);
73         return ret;
74     }
75     return reply.ReadInt32();
76 }
77 
SendRequest(StatusBarDelegateCmd code,MessageParcel & data,MessageParcel & reply,MessageOption & option)78 int32_t StatusBarDelegateProxy::SendRequest(
79     StatusBarDelegateCmd code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
80 {
81     sptr<IRemoteObject> remote = Remote();
82     if (remote == nullptr) {
83         TAG_LOGE(AAFwkTag::ABILITYMGR, "null remote");
84         return ERR_NULL_OBJECT;
85     }
86     return remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
87 }
88 } // namespace AbilityRuntime
89 } // namespace OHOS