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 "mock_session_manager_service.h"
17
18 #include <unistd.h>
19
20 #include <system_ability_definition.h>
21 #include <cinttypes>
22 #include <csignal>
23 #include <iomanip>
24 #include <map>
25 #include <sstream>
26
27 #include "mock_screen_manager_service.h"
28 #include "window_manager_hilog.h"
29 #include "unique_fd.h"
30 #include "root_scene.h"
31 #include "string_ex.h"
32 #include "wm_common.h"
33 #include "ws_common.h"
34 #include "session_manager_service_interface.h"
35 #include "scene_session_manager_interface.h"
36
37 namespace OHOS {
38 namespace Rosen {
39 namespace {
40 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_WINDOW, "MockSessionManagerService" };
41
42 const std::u16string DEFAULT_USTRING = u"error";
43 const char DEFAULT_STRING[] = "error";
44 const std::string ARG_DUMP_HELP = "-h";
45 const std::string ARG_DUMP_ALL = "-a";
46 const std::string ARG_DUMP_WINDOW = "-w";
47 } // namespace
48
WM_IMPLEMENT_SINGLE_INSTANCE(MockSessionManagerService)49 WM_IMPLEMENT_SINGLE_INSTANCE(MockSessionManagerService)
50
51 void MockSessionManagerService::SMSDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& object)
52 {
53 auto sessionManagerService = object.promote();
54 if (!sessionManagerService) {
55 WLOGFE("sessionManagerService is null");
56 return;
57 }
58 WLOGFI("SessionManagerService died, restart foundation now!");
59 _exit(0);
60 }
61
MockSessionManagerService()62 MockSessionManagerService::MockSessionManagerService() : SystemAbility(WINDOW_MANAGER_SERVICE_ID, true)
63 {
64 }
65
RegisterMockSessionManagerService()66 bool MockSessionManagerService::RegisterMockSessionManagerService()
67 {
68 bool res = SystemAbility::MakeAndRegisterAbility(this);
69 if (!res) {
70 WLOGFE("register failed");
71 }
72 if (!Publish(this)) {
73 WLOGFE("Publish failed");
74 }
75 WLOGFI("Publish mock session manager service success");
76 MockScreenManagerService::GetInstance().RegisterMockScreenManagerService();
77 return true;
78 }
79
OnStart()80 void MockSessionManagerService::OnStart()
81 {
82 WLOGFD("OnStart begin");
83 }
84
Str16ToStr8(const std::u16string & str)85 static std::string Str16ToStr8(const std::u16string& str)
86 {
87 if (str == DEFAULT_USTRING) {
88 return DEFAULT_STRING;
89 }
90 std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert(DEFAULT_STRING);
91 std::string result = convert.to_bytes(str);
92 return result == DEFAULT_STRING ? "" : result;
93 }
94
Dump(int fd,const std::vector<std::u16string> & args)95 int MockSessionManagerService::Dump(int fd, const std::vector<std::u16string> &args)
96 {
97 WLOGI("dump begin fd: %{public}d", fd);
98 if (fd < 0) {
99 return -1;
100 }
101 (void) signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE crash
102 UniqueFd ufd = UniqueFd(fd); // auto close
103 fd = ufd.Get();
104 std::vector<std::string> params;
105 for (auto& arg : args) {
106 params.emplace_back(Str16ToStr8(arg));
107 }
108
109 std::string dumpInfo;
110 if (params.empty()) {
111 ShowHelpInfo(dumpInfo);
112 } else if (params.size() == 1 && params[0] == ARG_DUMP_HELP) { // 1: params num
113 ShowHelpInfo(dumpInfo);
114 } else {
115 int errCode = DumpSessionInfo(params, dumpInfo);
116 if (errCode != 0) {
117 ShowIllegalArgsInfo(dumpInfo);
118 }
119 }
120 int ret = dprintf(fd, "%s\n", dumpInfo.c_str());
121 if (ret < 0) {
122 WLOGFE("dprintf error");
123 return -1; // WMError::WM_ERROR_INVALID_OPERATION;
124 }
125 WLOGI("dump end");
126 return 0;
127 }
128
SetSessionManagerService(const sptr<IRemoteObject> & sessionManagerService)129 bool MockSessionManagerService::SetSessionManagerService(const sptr<IRemoteObject>& sessionManagerService)
130 {
131 if (!sessionManagerService) {
132 WLOGFE("sessionManagerService is nullptr");
133 return false;
134 }
135 sessionManagerService_ = sessionManagerService;
136
137 smsDeathRecipient_ = new SMSDeathRecipient();
138 if (sessionManagerService_->IsProxyObject() && !sessionManagerService_->AddDeathRecipient(smsDeathRecipient_)) {
139 WLOGFE("Failed to add death recipient");
140 return false;
141 }
142
143 RegisterMockSessionManagerService();
144 MockScreenManagerService::GetInstance().SetSessionManagerService(sessionManagerService);
145 WLOGFI("sessionManagerService set success!");
146 return true;
147 }
148
GetSessionManagerService()149 sptr<IRemoteObject> MockSessionManagerService::GetSessionManagerService()
150 {
151 if (!sessionManagerService_) {
152 WLOGFE("sessionManagerService is nullptr");
153 return nullptr;
154 }
155 WLOGFD("Get session manager service success");
156 return sessionManagerService_;
157 }
158
ShowIllegalArgsInfo(std::string & dumpInfo)159 void MockSessionManagerService::ShowIllegalArgsInfo(std::string& dumpInfo)
160 {
161 dumpInfo.append("The arguments are illegal and you can enter '-h' for help.");
162 }
163
GetSceneSessionManager()164 sptr<IRemoteObject> MockSessionManagerService::GetSceneSessionManager()
165 {
166 sptr<ISessionManagerService> sessionManagerServiceProxy =
167 iface_cast<ISessionManagerService>(sessionManagerService_);
168 sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy->GetSceneSessionManager();
169 if (!remoteObject) {
170 WLOGFW("Get scene session manager proxy failed, scene session manager service is null");
171 return sptr<IRemoteObject>(nullptr);
172 }
173 sceneSessionManager_ = remoteObject;
174 return sceneSessionManager_;
175 }
176
DumpSessionInfo(const std::vector<std::string> & args,std::string & dumpInfo)177 int MockSessionManagerService::DumpSessionInfo(const std::vector<std::string>& args, std::string& dumpInfo)
178 {
179 if (args.empty()) {
180 return -1; // WMError::WM_ERROR_INVALID_PARAM;
181 }
182 if (!sessionManagerService_) {
183 WLOGFE("sessionManagerService is nullptr");
184 return -1;
185 }
186 if (!sceneSessionManager_) {
187 WLOGFW("Get scene session manager ...");
188 GetSceneSessionManager();
189 if (!sceneSessionManager_) {
190 WLOGFW("Get scene session manager proxy failed, nullptr");
191 return -1;
192 }
193 }
194 sptr<ISceneSessionManager> sceneSessionManagerProxy = iface_cast<ISceneSessionManager>(sceneSessionManager_);
195 WSError ret = sceneSessionManagerProxy->GetSessionDumpInfo(args, dumpInfo);
196 if (ret != WSError::WS_OK) {
197 WLOGFD("sessionManagerService set success!");
198 return -1;
199 }
200 return 0; // WMError::WM_OK;
201 }
202
203
ShowHelpInfo(std::string & dumpInfo)204 void MockSessionManagerService::ShowHelpInfo(std::string& dumpInfo)
205 {
206 dumpInfo.append("Usage:\n")
207 .append(" -h ")
208 .append("|help text for the tool\n")
209 .append(" -a ")
210 .append("|dump all window information in the system\n")
211 .append(" -w {window id} [ArkUI Option] ")
212 .append("|dump specified window information\n")
213 .append(" ------------------------------------[ArkUI Option]------------------------------------ \n");
214 ShowAceDumpHelp(dumpInfo);
215 }
216
ShowAceDumpHelp(std::string & dumpInfo)217 void MockSessionManagerService::ShowAceDumpHelp(std::string& dumpInfo)
218 {
219 }
220 } // namespace Rosen
221 } // namespace OHOS