1eace7efcSopenharmony_ci/*
2eace7efcSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3eace7efcSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4eace7efcSopenharmony_ci * you may not use this file except in compliance with the License.
5eace7efcSopenharmony_ci * You may obtain a copy of the License at
6eace7efcSopenharmony_ci *
7eace7efcSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8eace7efcSopenharmony_ci *
9eace7efcSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10eace7efcSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11eace7efcSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12eace7efcSopenharmony_ci * See the License for the specific language governing permissions and
13eace7efcSopenharmony_ci * limitations under the License.
14eace7efcSopenharmony_ci */
15eace7efcSopenharmony_ci
16eace7efcSopenharmony_ci#ifndef OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
17eace7efcSopenharmony_ci#define OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
18eace7efcSopenharmony_ci
19eace7efcSopenharmony_ci#include <regex>
20eace7efcSopenharmony_ci
21eace7efcSopenharmony_ci#include "ability_manager_interface.h"
22eace7efcSopenharmony_ci#include "shell_command.h"
23eace7efcSopenharmony_ci
24eace7efcSopenharmony_cinamespace OHOS {
25eace7efcSopenharmony_cinamespace AAFwk {
26eace7efcSopenharmony_cinamespace {
27eace7efcSopenharmony_ciusing ParametersInteger = std::map<std::string, int>;
28eace7efcSopenharmony_ciusing ParametersString = std::map<std::string, std::string>;
29eace7efcSopenharmony_ciusing ParametersBool = std::map<std::string, bool>;
30eace7efcSopenharmony_ci
31eace7efcSopenharmony_ciconst std::string TOOL_NAME = "aa";
32eace7efcSopenharmony_ci
33eace7efcSopenharmony_ci// not show in command
34eace7efcSopenharmony_ci// process -- debug ability with options
35eace7efcSopenharmony_ciconst std::string HELP_MSG = "usage: aa <command> <options>\n"
36eace7efcSopenharmony_ci    "These are common aa commands list:\n"
37eace7efcSopenharmony_ci    "  help                        list available commands\n"
38eace7efcSopenharmony_ci    "  start                       start ability with options\n"
39eace7efcSopenharmony_ci    "  stop-service                stop service with options\n"
40eace7efcSopenharmony_ci    "  dump                        dump the ability info\n"
41eace7efcSopenharmony_ci    "  force-stop <bundle-name>    force stop the process with bundle name\n"
42eace7efcSopenharmony_ci    "  attach                      attach application to enter debug mode\n"
43eace7efcSopenharmony_ci    "  detach                      detach application to exit debug mode\n"
44eace7efcSopenharmony_ci#ifdef ABILITY_COMMAND_FOR_TEST
45eace7efcSopenharmony_ci    "  test                        start the test framework with options\n"
46eace7efcSopenharmony_ci    "  ApplicationNotResponding     Pass in pid with options\n"
47eace7efcSopenharmony_ci#else
48eace7efcSopenharmony_ci    "  test                        start the test framework with options\n"
49eace7efcSopenharmony_ci    "  appdebug                    set / cancel / get waiting debug status\n"
50eace7efcSopenharmony_ci    "  process                     debug and tune\n";
51eace7efcSopenharmony_ci#endif
52eace7efcSopenharmony_ci
53eace7efcSopenharmony_ciconst std::string HELP_ApplicationNotResponding =
54eace7efcSopenharmony_ci    "usage: aa ApplicationNotResponding <options>\n"
55eace7efcSopenharmony_ci    "options list:\n"
56eace7efcSopenharmony_ci    "  -h, --help                   list available commands\n"
57eace7efcSopenharmony_ci    "  -p, --pid                    Pass in pid with option\n";
58eace7efcSopenharmony_ci
59eace7efcSopenharmony_ci// not show in command
60eace7efcSopenharmony_ci// [-N] [-p <perf-cmd>]
61eace7efcSopenharmony_ciconst std::string HELP_MSG_START =
62eace7efcSopenharmony_ci    "usage: aa start <options>\n"
63eace7efcSopenharmony_ci    "options list:\n"
64eace7efcSopenharmony_ci    "  -h, --help                                                   list available commands\n"
65eace7efcSopenharmony_ci    "  [-d <device-id>] [-a <ability-name> -b <bundle-name>] [-m <module-name>] [-p <perf-cmd>] [-D] [-E] [-S] [-N]"
66eace7efcSopenharmony_ci    "  [-R] [--ps <key> <string-value>] "
67eace7efcSopenharmony_ci    "  [--pi <key> <integer-value>] "
68eace7efcSopenharmony_ci    "  [--pb <key> <boolean-value>] "
69eace7efcSopenharmony_ci    "  [--psn <key>] "
70eace7efcSopenharmony_ci    "  [-A <action-name>] "
71eace7efcSopenharmony_ci    "  [-U <URI>] "
72eace7efcSopenharmony_ci    "  [-e <entity>] "
73eace7efcSopenharmony_ci    "  [-t <mime-type>] "
74eace7efcSopenharmony_ci    "  [--wl <window-left>] "
75eace7efcSopenharmony_ci    "  [--wt <window-top>] "
76eace7efcSopenharmony_ci    "  [--wh <window-height>] "
77eace7efcSopenharmony_ci    "  [--ww <window-width>] "
78eace7efcSopenharmony_ci    "  start ability with an element name\n";
79eace7efcSopenharmony_ci
80eace7efcSopenharmony_ciconst std::string HELP_MSG_STOP_SERVICE =
81eace7efcSopenharmony_ci    "usage: aa stop-service <options>\n"
82eace7efcSopenharmony_ci    "options list:\n"
83eace7efcSopenharmony_ci    "  -h, --help                                                   list available commands\n"
84eace7efcSopenharmony_ci    "  [-d <device-id>] -a <ability-name> -b <bundle-name> [-m <module-name>] "
85eace7efcSopenharmony_ci    "  stop service with an element name\n";
86eace7efcSopenharmony_ci
87eace7efcSopenharmony_ciconst std::string HELP_MSG_DUMPSYS = "usage: aa dump <options>\n"
88eace7efcSopenharmony_ci    "options list:\n"
89eace7efcSopenharmony_ci    "  -h, --help                   list available commands\n"
90eace7efcSopenharmony_ci    "  -a, --all                    dump all abilities\n"
91eace7efcSopenharmony_ci    "  -l, --mission-list           dump mission list\n"
92eace7efcSopenharmony_ci    "  -i, --ability                dump abilityRecordId\n"
93eace7efcSopenharmony_ci    "  -e, --extension              dump elementName (FA: serviceAbilityRecords,"
94eace7efcSopenharmony_ci    "Stage: ExtensionRecords)\n"
95eace7efcSopenharmony_ci    "  -p, --pending                dump pendingWantRecordId\n"
96eace7efcSopenharmony_ci    "  -r, --process                dump process\n"
97eace7efcSopenharmony_ci    "  -d, --data                   dump the data abilities\n"
98eace7efcSopenharmony_ci    "  -u, --userId                 userId\n"
99eace7efcSopenharmony_ci    "  -c, --client                 client\n"
100eace7efcSopenharmony_ci    "  -c, -u are auxiliary parameters and cannot be used alone\n"
101eace7efcSopenharmony_ci    "  The original -s parameter is invalid\n"
102eace7efcSopenharmony_ci    "  The original -m parameter is invalid\n";
103eace7efcSopenharmony_ci
104eace7efcSopenharmony_ciconst std::string HELP_MSG_PROCESS = "usage: aa process <options>\n"
105eace7efcSopenharmony_ci    "options list:\n"
106eace7efcSopenharmony_ci    "  -h, --help                   list available commands\n"
107eace7efcSopenharmony_ci    "  -a <ability-name> -b <bundle-name> [-m <module-name>]\n"
108eace7efcSopenharmony_ci    "  -p <perf-cmd>                performance optimization command. Either -p or -D must be selected, "
109eace7efcSopenharmony_ci    "-p takes precedence.\n"
110eace7efcSopenharmony_ci    "  -D <debug-cmd>               debug command. Either -p or -D must be selected, -p takes precedence.\n"
111eace7efcSopenharmony_ci    "  [-S]\n"
112eace7efcSopenharmony_ci    "  debug ability with an element name\n";
113eace7efcSopenharmony_ci
114eace7efcSopenharmony_ciconst std::string HELP_MSG_TEST =
115eace7efcSopenharmony_ci    "usage: aa test <options>\n"
116eace7efcSopenharmony_ci    "options list:\n"
117eace7efcSopenharmony_ci    "  -h, --help                                             list available commands\n"
118eace7efcSopenharmony_ci    "  -b <bundle-name> -s unittest <test-runner>             start the test framework with options\n"
119eace7efcSopenharmony_ci    "                  [-p <package-name>]                    the name of package with test-runner, "
120eace7efcSopenharmony_ci    "required for the FA model\n"
121eace7efcSopenharmony_ci    "                  [-m <module-name>]                     the name of module with test-runner, "
122eace7efcSopenharmony_ci    "required for the STAGE model\n"
123eace7efcSopenharmony_ci    "                  [-s class <test-class>]\n"
124eace7efcSopenharmony_ci    "                  [-s level <test-level>]\n"
125eace7efcSopenharmony_ci    "                  [-s size <test-size>]\n"
126eace7efcSopenharmony_ci    "                  [-s testType <test-testType>]\n"
127eace7efcSopenharmony_ci    "                  [-s timeout <test-timeout>]\n"
128eace7efcSopenharmony_ci    "                  [-s <any-key> <any-value>]\n"
129eace7efcSopenharmony_ci    "                  [-w <wait-time>]\n"
130eace7efcSopenharmony_ci    "                  [-D]\n";
131eace7efcSopenharmony_ci
132eace7efcSopenharmony_ciconst std::string HELP_MSG_ATTACH_APP_DEBUG =
133eace7efcSopenharmony_ci    "usage: aa attach <options>\n"
134eace7efcSopenharmony_ci    "options list:\n"
135eace7efcSopenharmony_ci    "  -h, --help                                             list available commands\n"
136eace7efcSopenharmony_ci    "  -b <bundle-name>                                       let application enter debug mode by bundle name\n";
137eace7efcSopenharmony_ciconst std::string HELP_MSG_DETACH_APP_DEBUG =
138eace7efcSopenharmony_ci    "usage: aa detach <options>\n"
139eace7efcSopenharmony_ci    "options list:\n"
140eace7efcSopenharmony_ci    "  -h, --help                                             list available commands\n"
141eace7efcSopenharmony_ci    "  -b <bundle-name>                                       let application exit debug mode by bundle name\n";
142eace7efcSopenharmony_ci
143eace7efcSopenharmony_ciconst std::string HELP_MSG_APPDEBUG_APP_DEBUG =
144eace7efcSopenharmony_ci    "usage: aa appdebug <options>\n"
145eace7efcSopenharmony_ci    "options list:\n"
146eace7efcSopenharmony_ci    "  -h, --help                                  list available commands\n"
147eace7efcSopenharmony_ci    "  -b, --bundlename <bundle-name>              let application set wait debug mode by bundle name with options\n"
148eace7efcSopenharmony_ci    "                  [-p, --persist]             option: persist flag\n"
149eace7efcSopenharmony_ci    "  -c, --cancel                                let application cancel wait debug\n"
150eace7efcSopenharmony_ci    "  -g, --get                                   get wait debug mode application bundle name and persist flag\n";
151eace7efcSopenharmony_ci
152eace7efcSopenharmony_ciconst std::string HELP_MSG_FORCE_STOP = "usage: aa force-stop <bundle-name> [-p pid] [-r kill-reason]\n";
153eace7efcSopenharmony_ciconst std::string HELP_MSG_FORCE_TIMEOUT =
154eace7efcSopenharmony_ci    "usage: aa force-timeout <ability-name> <INITIAL|INACTIVE|COMMAND|FOREGROUND|BACKGROUND|TERMINATING>\n"
155eace7efcSopenharmony_ci    "usage: aa force-timeout clean.";
156eace7efcSopenharmony_ciconst std::string HELP_MSG_FORCE_TIMEOUT_CLEAN = "clean";
157eace7efcSopenharmony_ci
158eace7efcSopenharmony_ciconst std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
159eace7efcSopenharmony_ciconst std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
160eace7efcSopenharmony_ci
161eace7efcSopenharmony_ciconst std::string STRING_START_ABILITY_OK = "start ability successfully.";
162eace7efcSopenharmony_ciconst std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
163eace7efcSopenharmony_ci
164eace7efcSopenharmony_ciconst std::string STRING_STOP_SERVICE_ABILITY_OK = "stop service ability successfully.";
165eace7efcSopenharmony_ciconst std::string STRING_STOP_SERVICE_ABILITY_NG = "error: failed to stop service ability.";
166eace7efcSopenharmony_ci
167eace7efcSopenharmony_ciconst std::string STRING_FORCE_STOP_OK = "force stop process successfully.";
168eace7efcSopenharmony_ciconst std::string STRING_FORCE_STOP_NG = "error: failed to force stop process.";
169eace7efcSopenharmony_ci
170eace7efcSopenharmony_ciconst std::string STRING_ATTACH_APP_DEBUG_OK = "attach app debug successfully.";
171eace7efcSopenharmony_ciconst std::string STRING_ATTACH_APP_DEBUG_NG = "error: failed to attach app debug.";
172eace7efcSopenharmony_ci
173eace7efcSopenharmony_ciconst std::string STRING_DETACH_APP_DEBUG_OK = "detach app debug successfully.";
174eace7efcSopenharmony_ciconst std::string STRING_DETACH_APP_DEBUG_NG = "error: failed to detach app debug.";
175eace7efcSopenharmony_ci
176eace7efcSopenharmony_ciconst std::string STRING_START_USER_TEST_NG = "error: failed to start user test.";
177eace7efcSopenharmony_ciconst std::string STRING_USER_TEST_STARTED = "user test started.";
178eace7efcSopenharmony_ciconst std::string STRING_USER_TEST_FINISHED = "user test finished.";
179eace7efcSopenharmony_ci
180eace7efcSopenharmony_ciconst std::string STRING_BLOCK_AMS_SERVICE_OK = "block ams service successfully.";
181eace7efcSopenharmony_ciconst std::string STRING_BLOCK_AMS_SERVICE_NG = "error: failed to block ams service.";
182eace7efcSopenharmony_ci
183eace7efcSopenharmony_ciconst std::string STRING_APP_DEBUG_OK = "app debug successfully.";
184eace7efcSopenharmony_ciconst std::string STRING_APP_DEBUG_NG = "error: failed to app debug.";
185eace7efcSopenharmony_ci
186eace7efcSopenharmony_ciconst std::string STRING_START_NATIVE_PROCESS_OK = "start native process successfully.";
187eace7efcSopenharmony_ciconst std::string STRING_START_NATIVE_PROCESS_NG = "error: failed to start native process.";
188eace7efcSopenharmony_ci
189eace7efcSopenharmony_ciconst int USER_TEST_COMMAND_START_INDEX = 2;
190eace7efcSopenharmony_ciconst int USER_TEST_COMMAND_PARAMS_NUM = 2;
191eace7efcSopenharmony_ciconst int TIME_RATE_MS = 1000;
192eace7efcSopenharmony_ciconst std::string STRING_FORCE_TIMEOUT_OK = "force ability timeout successfully.";
193eace7efcSopenharmony_ciconst std::string STRING_FORCE_TIMEOUT_NG = "error: failed to force ability timeout.";
194eace7efcSopenharmony_ci
195eace7efcSopenharmony_ciconst int NUMBER_TWO = 2;
196eace7efcSopenharmony_ciconst int NUMBER_ONE = 1;
197eace7efcSopenharmony_ci
198eace7efcSopenharmony_ciconst std::string DEBUG_VALUE = "true";
199eace7efcSopenharmony_ci
200eace7efcSopenharmony_ciconst std::string PERFCMD_FIRST_PROFILE = "profile";
201eace7efcSopenharmony_ciconst std::string PERFCMD_FIRST_DUMPHEAP = "dumpheap";
202eace7efcSopenharmony_ci
203eace7efcSopenharmony_ciconst std::string STRING_TEST_REGEX_INTEGER_NUMBERS = "^(0|[1-9][0-9]*|-[1-9][0-9]*)$";
204eace7efcSopenharmony_ciconst std::string STRING_REGEX_ALL_NUMBERS = "^(-)?([0-9]|[1-9][0-9]+)([\\.][0-9]+)?$";
205eace7efcSopenharmony_ci}  // namespace
206eace7efcSopenharmony_ci
207eace7efcSopenharmony_ciclass AbilityManagerShellCommand : public ShellCommand {
208eace7efcSopenharmony_cipublic:
209eace7efcSopenharmony_ci    AbilityManagerShellCommand(int argc, char* argv[]);
210eace7efcSopenharmony_ci    ~AbilityManagerShellCommand() override
211eace7efcSopenharmony_ci    {}
212eace7efcSopenharmony_ci
213eace7efcSopenharmony_ci    ErrCode CreateMessageMap() override;
214eace7efcSopenharmony_ci    bool IsTestCommandIntegrity(const std::map<std::string, std::string>& params);
215eace7efcSopenharmony_ci    ErrCode StartUserTest(const std::map<std::string, std::string>& params);
216eace7efcSopenharmony_ci
217eace7efcSopenharmony_ciprivate:
218eace7efcSopenharmony_ci    ErrCode CreateCommandMap() override;
219eace7efcSopenharmony_ci    ErrCode init() override;
220eace7efcSopenharmony_ci
221eace7efcSopenharmony_ci    ErrCode RunAsHelpCommand();
222eace7efcSopenharmony_ci    ErrCode RunAsStartAbility();
223eace7efcSopenharmony_ci    ErrCode RunAsStopService();
224eace7efcSopenharmony_ci    ErrCode RunAsDumpsysCommand();
225eace7efcSopenharmony_ci    ErrCode RunAsForceStop();
226eace7efcSopenharmony_ci    bool SwitchOptionForAppDebug(int32_t option, std::string &bundleName, bool &isPersist, bool &isCancel, bool &isGet);
227eace7efcSopenharmony_ci    bool ParseAppDebugParameter(std::string &bundleName, bool &isPersist, bool &isCancel, bool &isGet);
228eace7efcSopenharmony_ci    ErrCode RunAsAppDebugDebugCommand();
229eace7efcSopenharmony_ci    ErrCode RunAsProcessCommand();
230eace7efcSopenharmony_ci    ErrCode RunAsAttachDebugCommand();
231eace7efcSopenharmony_ci    ErrCode RunAsDetachDebugCommand();
232eace7efcSopenharmony_ci    bool CheckParameters(int target);
233eace7efcSopenharmony_ci    ErrCode ParseParam(ParametersInteger& pi);
234eace7efcSopenharmony_ci    ErrCode ParseParam(ParametersString& ps, bool isNull);
235eace7efcSopenharmony_ci    ErrCode ParseParam(ParametersBool& pb);
236eace7efcSopenharmony_ci    void SetParams(const ParametersInteger& pi, Want& want);
237eace7efcSopenharmony_ci    void SetParams(const ParametersString& ps, Want& want);
238eace7efcSopenharmony_ci    void SetParams(const ParametersBool& pb, Want& want);
239eace7efcSopenharmony_ci    Reason CovertExitReason(std::string& reasonStr);
240eace7efcSopenharmony_ci    pid_t ConvertPid(std::string& inputPid);
241eace7efcSopenharmony_ci
242eace7efcSopenharmony_ci#ifdef ABILITY_COMMAND_FOR_TEST
243eace7efcSopenharmony_ci    ErrCode RunForceTimeoutForTest();
244eace7efcSopenharmony_ci    ErrCode RunAsSendAppNotRespondingProcessID();
245eace7efcSopenharmony_ci    ErrCode RunAsSendAppNotRespondingWithUnknownOption();
246eace7efcSopenharmony_ci    ErrCode RunAsSendAppNotRespondingWithOption(int32_t option, std::string& pid);
247eace7efcSopenharmony_ci#endif
248eace7efcSopenharmony_ci#ifdef ABILITY_FAULT_AND_EXIT_TEST
249eace7efcSopenharmony_ci    ErrCode RunAsForceExitAppCommand();
250eace7efcSopenharmony_ci    ErrCode RunAsNotifyAppFaultCommand();
251eace7efcSopenharmony_ci#endif
252eace7efcSopenharmony_ci    sptr<IAbilityManager> GetAbilityManagerService();
253eace7efcSopenharmony_ci
254eace7efcSopenharmony_ci    ErrCode MakeWantFromCmd(Want& want, std::string& windowMode);
255eace7efcSopenharmony_ci    ErrCode MakeWantForProcess(Want& want);
256eace7efcSopenharmony_ci    ErrCode RunAsTestCommand();
257eace7efcSopenharmony_ci    ErrCode TestCommandError(const std::string& info);
258eace7efcSopenharmony_ci    bool MatchOrderString(const std::regex &r, const std::string &orderCmd);
259eace7efcSopenharmony_ci    bool CheckPerfCmdString(const char* optarg, const size_t paramLength, std::string &perfCmd);
260eace7efcSopenharmony_ci    void ParseBundleName(std::string &bundleName);
261eace7efcSopenharmony_ci};
262eace7efcSopenharmony_ci}  // namespace AAFwk
263eace7efcSopenharmony_ci}  // namespace OHOS
264eace7efcSopenharmony_ci
265eace7efcSopenharmony_ci#endif  // OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
266