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_screen_manager_service.h"
17
18 #include <system_ability_definition.h>
19 #include <csignal>
20 #include <string_ex.h>
21 #include <unique_fd.h>
22
23 #include "scene_board_judgement.h"
24 #include "session_manager_service_interface.h"
25 #include "screen_session_manager_interface.h"
26 #include "window_manager_hilog.h"
27
28 namespace OHOS {
29 namespace Rosen {
30 namespace {
31 constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, HILOG_DOMAIN_DISPLAY, "MockScreenManagerService" };
32 const std::string ARG_DUMP_HELP = "-h";
33 const std::string ARG_DUMP_ALL = "-a";
34 const std::string ARG_DUMP_SCREEN = "-s";
35 }
36
37 WM_IMPLEMENT_SINGLE_INSTANCE(MockScreenManagerService)
38 bool registerResult = !SceneBoardJudgement::IsSceneBoardEnabled() ? false :
39 SystemAbility::MakeAndRegisterAbility(&SingletonContainer::Get<MockScreenManagerService>());
40
MockScreenManagerService()41 MockScreenManagerService::MockScreenManagerService() : SystemAbility(DISPLAY_MANAGER_SERVICE_SA_ID, true)
42 {
43 }
44
OnStart()45 void MockScreenManagerService::OnStart()
46 {
47 WLOGFI("OnStart begin");
48 RegisterMockScreenManagerService();
49 }
50
GetScreenDumpInfo(const std::vector<std::string> & params,std::string & info)51 void MockScreenManagerService::GetScreenDumpInfo(const std::vector<std::string>& params, std::string& info)
52 {
53 WLOGFD("GetScreenDumpInfo begin");
54 }
55
ShowHelpInfo(std::string & dumpInfo)56 void MockScreenManagerService::ShowHelpInfo(std::string& dumpInfo)
57 {
58 dumpInfo.append("Usage:\n")
59 .append(" -h ")
60 .append("|help text for the tool\n")
61 .append(" -a ")
62 .append("|dump all screen information in the system\n")
63 .append(" -s {screen id}")
64 .append("|dump specified screen information\n");
65 }
66
ShowIllegalArgsInfo(std::string & dumpInfo)67 void MockScreenManagerService::ShowIllegalArgsInfo(std::string& dumpInfo)
68 {
69 dumpInfo.append("The arguments are illegal and you can enter '-h' for help.");
70 }
71
IsValidDigitString(const std::string & idStr) const72 bool MockScreenManagerService::IsValidDigitString(const std::string& idStr) const
73 {
74 if (idStr.empty()) {
75 return false;
76 }
77 for (char ch : idStr) {
78 if ((ch >= '0' && ch <= '9')) {
79 continue;
80 }
81 WLOGFE("invalid id");
82 return false;
83 }
84 return true;
85 }
86
DumpScreenInfo(const std::vector<std::string> & args,std::string & dumpInfo)87 int MockScreenManagerService::DumpScreenInfo(const std::vector<std::string>& args, std::string& dumpInfo)
88 {
89 if (args.empty()) {
90 return -1;
91 }
92 if (args.size() == 1 && args[0] == ARG_DUMP_ALL) { // 1: params num
93 return DumpAllScreenInfo(dumpInfo);
94 } else if (args[0] == ARG_DUMP_SCREEN && IsValidDigitString(args[1])) {
95 ScreenId screenId = std::stoull(args[1]);
96 return DumpSpecifiedScreenInfo(screenId, dumpInfo);
97 } else {
98 return -1;
99 }
100 }
101
DumpAllScreenInfo(std::string & dumpInfo)102 int MockScreenManagerService::DumpAllScreenInfo(std::string& dumpInfo)
103 {
104 sptr<IScreenSessionManager> screenSessionManagerProxy = iface_cast<IScreenSessionManager>(screenSessionManager_);
105 screenSessionManagerProxy->DumpAllScreensInfo(dumpInfo);
106 return 0;
107 }
108
DumpSpecifiedScreenInfo(ScreenId screenId,std::string & dumpInfo)109 int MockScreenManagerService::DumpSpecifiedScreenInfo(ScreenId screenId, std::string& dumpInfo)
110 {
111 sptr<IScreenSessionManager> screenSessionManagerProxy = iface_cast<IScreenSessionManager>(screenSessionManager_);
112 screenSessionManagerProxy->DumpSpecialScreenInfo(screenId, dumpInfo);
113 return 0;
114 }
115
Dump(int fd,const std::vector<std::u16string> & args)116 int MockScreenManagerService::Dump(int fd, const std::vector<std::u16string>& args)
117 {
118 WLOGFI("Dump begin");
119 if (fd < 0) {
120 return -1;
121 }
122 InitScreenSessionManager();
123 if (!screenSessionManager_) {
124 WLOGFE("dump ipc not ready");
125 return -1;
126 }
127 (void) signal(SIGPIPE, SIG_IGN); // ignore SIGPIPE crash
128 UniqueFd ufd = UniqueFd(fd); // auto close
129 fd = ufd.Get();
130 std::vector<std::string> params;
131 for (auto& arg : args) {
132 params.emplace_back(Str16ToStr8(arg));
133 }
134 std::string dumpInfo;
135 if (params.empty()) {
136 ShowHelpInfo(dumpInfo);
137 } else if (params.size() == 1 && params[0] == ARG_DUMP_HELP) { // 1: params num
138 ShowHelpInfo(dumpInfo);
139 } else {
140 int errCode = DumpScreenInfo(params, dumpInfo);
141 if (errCode != 0) {
142 ShowIllegalArgsInfo(dumpInfo);
143 }
144 }
145 int ret = dprintf(fd, "%s\n", dumpInfo.c_str());
146 if (ret < 0) {
147 WLOGFE("dprintf error");
148 return -1; // WMError::WM_ERROR_INVALID_OPERATION;
149 }
150 WLOGI("dump end");
151 return 0;
152 }
153
InitScreenSessionManager()154 void MockScreenManagerService::InitScreenSessionManager()
155 {
156 if (!sessionManagerService_) {
157 WLOGFE("sessionManagerService is nullptr");
158 return;
159 }
160 if (screenSessionManager_) {
161 return;
162 }
163 sptr<ISessionManagerService> sessionManagerServiceProxy =
164 iface_cast<ISessionManagerService>(sessionManagerService_);
165 sptr<IRemoteObject> remoteObject = sessionManagerServiceProxy->GetScreenSessionManagerService();
166 if (!remoteObject) {
167 WLOGFW("Get scene session manager proxy failed, scene session manager service is null");
168 return;
169 }
170 screenSessionManager_ = remoteObject;
171 }
172
RegisterMockScreenManagerService()173 bool MockScreenManagerService::RegisterMockScreenManagerService()
174 {
175 WLOGFI("registerResult %{public}d", registerResult);
176 if (!registerResult) {
177 !SceneBoardJudgement::IsSceneBoardEnabled() ? false :
178 SystemAbility::MakeAndRegisterAbility(&SingletonContainer::Get<MockScreenManagerService>());
179 }
180 if (!Publish(this)) {
181 WLOGFE("Publish failed");
182 return false;
183 }
184 WLOGFI("Publish mock screen manager service success");
185 return true;
186 }
187
SetSessionManagerService(const sptr<IRemoteObject> & sessionManagerService)188 void MockScreenManagerService::SetSessionManagerService(const sptr<IRemoteObject>& sessionManagerService)
189 {
190 sessionManagerService_ = sessionManagerService;
191 }
192
193 } // namespace Rosen
194 } // namespace OHOS