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 "privacy_scene_session_manager_lite_proxy.h"
17 
18 #include "accesstoken_log.h"
19 #include "privacy_error.h"
20 
21 namespace OHOS {
22 namespace Security {
23 namespace AccessToken {
24 namespace {
25 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_PRIVACY, "PrivacySceneSessionManagerLiteProxy"};
26 }
27 
RegisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)28 int32_t PrivacySceneSessionManagerLiteProxy::RegisterWindowManagerAgent(WindowManagerAgentType type,
29     const sptr<IWindowManagerAgent>& windowManagerAgent)
30 {
31     MessageOption option;
32     MessageParcel reply;
33     MessageParcel data;
34     if (!data.WriteInterfaceToken(GetDescriptor())) {
35         ACCESSTOKEN_LOG_ERROR(LABEL, "Write InterfaceToken failed");
36         return ERR_WRITE_PARCEL_FAILED;
37     }
38 
39     if (!data.WriteUint32(static_cast<uint32_t>(type))) {
40         ACCESSTOKEN_LOG_ERROR(LABEL, "Write type failed");
41         return ERR_WRITE_PARCEL_FAILED;
42     }
43 
44     if (!data.WriteRemoteObject(windowManagerAgent->AsObject())) {
45         ACCESSTOKEN_LOG_ERROR(LABEL, "Write IWindowManagerAgent failed");
46         return ERR_WRITE_PARCEL_FAILED;
47     }
48 
49     sptr<IRemoteObject> remote = Remote();
50     if (remote == nullptr) {
51         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
52         return ERR_REMOTE_CONNECTION;
53     }
54     int32_t error = remote->SendRequest(static_cast<uint32_t>(
55         SceneSessionManagerLiteMessage::TRANS_ID_REGISTER_WINDOW_MANAGER_AGENT),
56         data, reply, option);
57     if (error != ERR_NONE) {
58         ACCESSTOKEN_LOG_ERROR(LABEL, "SendRequest failed, err=%{public}d.", error);
59         return error;
60     }
61 
62     return reply.ReadInt32();
63 }
64 
UnregisterWindowManagerAgent(WindowManagerAgentType type,const sptr<IWindowManagerAgent> & windowManagerAgent)65 int32_t PrivacySceneSessionManagerLiteProxy::UnregisterWindowManagerAgent(WindowManagerAgentType type,
66     const sptr<IWindowManagerAgent>& windowManagerAgent)
67 {
68     MessageParcel reply;
69     MessageOption option;
70     MessageParcel data;
71     if (!data.WriteInterfaceToken(GetDescriptor())) {
72         ACCESSTOKEN_LOG_ERROR(LABEL, "Write InterfaceToken failed");
73         return ERR_WRITE_PARCEL_FAILED;
74     }
75 
76     if (!data.WriteUint32(static_cast<uint32_t>(type))) {
77         ACCESSTOKEN_LOG_ERROR(LABEL, "Write type failed");
78         return ERR_WRITE_PARCEL_FAILED;
79     }
80 
81     if (!data.WriteRemoteObject(windowManagerAgent->AsObject())) {
82         ACCESSTOKEN_LOG_ERROR(LABEL, "Write IWindowManagerAgent failed");
83         return ERR_WRITE_PARCEL_FAILED;
84     }
85 
86     sptr<IRemoteObject> remote = Remote();
87     if (remote == nullptr) {
88         ACCESSTOKEN_LOG_ERROR(LABEL, "Remote service is null.");
89         return ERR_REMOTE_CONNECTION;
90     }
91     int32_t error = remote->SendRequest(static_cast<uint32_t>(
92         SceneSessionManagerLiteMessage::TRANS_ID_UNREGISTER_WINDOW_MANAGER_AGENT),
93         data, reply, option);
94     if (error != ERR_NONE) {
95         ACCESSTOKEN_LOG_ERROR(LABEL, "SendRequest failed, err=%{public}d.", error);
96         return error;
97     }
98 
99     return reply.ReadInt32();
100 }
101 }
102 }
103 }