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 <cinttypes>
17 #include "zidl/mock_session_manager_service_proxy.h"
18 #include "window_manager_hilog.h"
19 
20 namespace OHOS {
21 namespace Rosen {
22 namespace {
23 constexpr HiviewDFX::HiLogLabel LABEL = {LOG_CORE, HILOG_DOMAIN_WINDOW, "MockSessionManagerServiceProxy"};
24 }
25 
GetSessionManagerService()26 sptr<IRemoteObject> MockSessionManagerServiceProxy::GetSessionManagerService()
27 {
28     MessageParcel data;
29     MessageParcel reply;
30     MessageOption option;
31     if (!data.WriteInterfaceToken(GetDescriptor())) {
32         WLOGFE("WriteInterfaceToken failed");
33         return nullptr;
34     }
35     sptr<IRemoteObject> remote = Remote();
36     if (remote == nullptr) {
37         WLOGFE("remote is null");
38         return nullptr;
39     }
40     if (remote->SendRequest(static_cast<uint32_t>(
41         MockSessionManagerServiceMessage::TRANS_ID_GET_SESSION_MANAGER_SERVICE),
42         data, reply, option) != ERR_NONE) {
43         WLOGFE("SendRequest failed");
44         return nullptr;
45     }
46     sptr<IRemoteObject> remoteObject = reply.ReadRemoteObject();
47     return remoteObject;
48 }
49 
50 
GetScreenSessionManagerLite()51 sptr<IRemoteObject> MockSessionManagerServiceProxy::GetScreenSessionManagerLite()
52 {
53     MessageParcel data;
54     MessageParcel reply;
55     MessageOption option;
56     if (!data.WriteInterfaceToken(GetDescriptor())) {
57         WLOGFE("WriteInterfaceToken failed");
58         return nullptr;
59     }
60     sptr<IRemoteObject> remote = Remote();
61     if (remote == nullptr) {
62         WLOGFE("remote is null");
63         return nullptr;
64     }
65     if (remote->SendRequest(static_cast<uint32_t>(
66         MockSessionManagerServiceMessage::TRANS_ID_GET_SCREEN_SESSION_MANAGER),
67         data, reply, option) != ERR_NONE) {
68         return nullptr;
69     }
70     sptr<IRemoteObject> remoteObject = reply.ReadRemoteObject();
71     return remoteObject;
72 }
73 
NotifySceneBoardAvailable()74 void MockSessionManagerServiceProxy::NotifySceneBoardAvailable()
75 {
76     MessageParcel data;
77     MessageParcel reply;
78     MessageOption option = { MessageOption::TF_ASYNC };
79     if (!data.WriteInterfaceToken(GetDescriptor())) {
80         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
81         return;
82     }
83 
84     sptr<IRemoteObject> remote = Remote();
85     if (remote == nullptr) {
86         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
87         return;
88     }
89     if (remote->SendRequest(static_cast<uint32_t>(
90         MockSessionManagerServiceMessage::TRANS_ID_NOTIFY_SCENE_BOARD_AVAILABLE),
91         data, reply, option) != ERR_NONE) {
92         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
93         return;
94     }
95 }
96 
RegisterSMSRecoverListener(const sptr<IRemoteObject>& listener)97 void MockSessionManagerServiceProxy::RegisterSMSRecoverListener(const sptr<IRemoteObject>& listener)
98 {
99     TLOGD(WmsLogTag::WMS_RECOVER, "Register");
100     MessageParcel data;
101     MessageParcel reply;
102     MessageOption option;
103     if (!data.WriteInterfaceToken(GetDescriptor())) {
104         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
105         return;
106     }
107 
108     if (!data.WriteRemoteObject(listener)) {
109         TLOGE(WmsLogTag::WMS_RECOVER, "WriteRemoteObject listener failed");
110         return;
111     }
112 
113     sptr<IRemoteObject> remote = Remote();
114     if (remote == nullptr) {
115         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
116         return;
117     }
118     if (remote->SendRequest(static_cast<uint32_t>(
119         MockSessionManagerServiceMessage::TRANS_ID_REGISTER_SMS_RECOVER_LISTENER),
120         data, reply, option) != ERR_NONE) {
121         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
122         return;
123     }
124 }
125 
UnregisterSMSRecoverListener()126 void MockSessionManagerServiceProxy::UnregisterSMSRecoverListener()
127 {
128     TLOGD(WmsLogTag::WMS_RECOVER, "UnRegister");
129     MessageParcel data;
130     MessageParcel reply;
131     MessageOption option;
132     if (!data.WriteInterfaceToken(GetDescriptor())) {
133         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
134     }
135     sptr<IRemoteObject> remote = Remote();
136     if (remote == nullptr) {
137         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
138         return;
139     }
140     if (remote->SendRequest(static_cast<uint32_t>(
141         MockSessionManagerServiceMessage::TRANS_ID_UNREGISTER_SMS_RECOVER_LISTENER),
142         data, reply, option) != ERR_NONE) {
143         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
144         return;
145     }
146 }
147 
RegisterSMSLiteRecoverListener(const sptr<IRemoteObject>& listener)148 void MockSessionManagerServiceProxy::RegisterSMSLiteRecoverListener(const sptr<IRemoteObject>& listener)
149 {
150     TLOGD(WmsLogTag::WMS_RECOVER, "Register");
151     MessageParcel data;
152     MessageParcel reply;
153     MessageOption option;
154     if (!data.WriteInterfaceToken(GetDescriptor())) {
155         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
156         return;
157     }
158 
159     if (!data.WriteRemoteObject(listener)) {
160         TLOGE(WmsLogTag::WMS_RECOVER, "WriteRemoteObject listener failed");
161         return;
162     }
163 
164     sptr<IRemoteObject> remote = Remote();
165     if (remote == nullptr) {
166         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
167         return;
168     }
169     if (remote->SendRequest(static_cast<uint32_t>(
170         MockSessionManagerServiceMessage::TRANS_ID_REGISTER_SMS_LITE_RECOVER_LISTENER),
171         data, reply, option) != ERR_NONE) {
172         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
173         return;
174     }
175 }
176 
UnregisterSMSLiteRecoverListener()177 void MockSessionManagerServiceProxy::UnregisterSMSLiteRecoverListener()
178 {
179     TLOGD(WmsLogTag::WMS_RECOVER, "Unregister");
180     MessageParcel data;
181     MessageParcel reply;
182     MessageOption option;
183     if (!data.WriteInterfaceToken(GetDescriptor())) {
184         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
185         return;
186     }
187     sptr<IRemoteObject> remote = Remote();
188     if (remote == nullptr) {
189         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
190         return;
191     }
192     if (remote->SendRequest(static_cast<uint32_t>(
193         MockSessionManagerServiceMessage::TRANS_ID_UNREGISTER_SMS_LITE_RECOVER_LISTENER),
194         data, reply, option) != ERR_NONE) {
195         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
196         return;
197     }
198 }
199 
SetSnapshotSkipByUserIdAndBundleNames(int32_t userId, const std::vector<std::string>& bundleNameList)200 int32_t MockSessionManagerServiceProxy::SetSnapshotSkipByUserIdAndBundleNames(int32_t userId,
201     const std::vector<std::string>& bundleNameList)
202 {
203     MessageParcel data;
204     MessageParcel reply;
205     MessageOption option;
206     if (!data.WriteInterfaceToken(GetDescriptor())) {
207         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
208         return ERR_TRANSACTION_FAILED;
209     }
210     if (!data.WriteInt32(userId)) {
211         TLOGE(WmsLogTag::DEFAULT, "Write userId failed");
212         return ERR_INVALID_DATA;
213     }
214     if (!data.WriteStringVector(bundleNameList)) {
215         TLOGE(WmsLogTag::DEFAULT, "Write bundleNameList failed");
216         return ERR_INVALID_DATA;
217     }
218     sptr<IRemoteObject> remote = Remote();
219     if (remote == nullptr) {
220         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
221         return ERR_NULL_OBJECT;
222     }
223     if (remote->SendRequest(static_cast<uint32_t>(
224         MockSessionManagerServiceMessage::TRANS_ID_SET_SNAPSHOT_SKIP_BY_USERID_AND_BUNDLENAMES),
225         data, reply, option) != ERR_NONE) {
226         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
227         return ERR_TRANSACTION_FAILED;
228     }
229     return reply.ReadInt32();
230 }
231 
SetSnapshotSkipByIdNamesMap( const std::unordered_map<int32_t, std::vector<std::string>>& userIdAndBunldeNames)232 int32_t MockSessionManagerServiceProxy::SetSnapshotSkipByIdNamesMap(
233     const std::unordered_map<int32_t, std::vector<std::string>>& userIdAndBunldeNames)
234 {
235     MessageParcel data;
236     MessageParcel reply;
237     MessageOption option;
238     if (!data.WriteInterfaceToken(GetDescriptor())) {
239         TLOGE(WmsLogTag::WMS_RECOVER, "WriteInterfaceToken failed");
240         return ERR_TRANSACTION_FAILED;
241     }
242     data.WriteInt32(userIdAndBunldeNames.size());
243     for (auto it = userIdAndBunldeNames.begin(); it != userIdAndBunldeNames.end(); ++it) {
244         if (!data.WriteInt32(it->first)) {
245             TLOGE(WmsLogTag::DEFAULT, "Write [it->first] failed");
246             return ERR_INVALID_DATA;
247         }
248         if (!data.WriteStringVector(it->second)) {
249             TLOGE(WmsLogTag::DEFAULT, "Write [it->second] failed");
250             return ERR_INVALID_DATA;
251         }
252     }
253     sptr<IRemoteObject> remote = Remote();
254     if (remote == nullptr) {
255         TLOGE(WmsLogTag::WMS_RECOVER, "remote is null");
256         return ERR_NULL_OBJECT;
257     }
258     if (remote->SendRequest(static_cast<uint32_t>(
259         MockSessionManagerServiceMessage::TRANS_ID_SET_SNAPSHOT_SKIP_BY_MAP),
260         data, reply, option) != ERR_NONE) {
261         TLOGE(WmsLogTag::WMS_RECOVER, "SendRequest failed");
262         return ERR_TRANSACTION_FAILED;
263     }
264     return reply.ReadInt32();
265 }
266 
267 } // namespace Rosen
268 } // namespace OHOS