• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 #ifndef OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
17 #define OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
18 
19 #include <regex>
20 
21 #include "shell_command.h"
22 #include "ability_manager_interface.h"
23 
24 namespace OHOS {
25 namespace AAFwk {
26 namespace {
27 const std::string TOOL_NAME = "aa";
28 
29 const std::string HELP_MSG = "usage: aa <command> <options>\n"
30     "These are common aa commands list:\n"
31     "  help                        list available commands\n"
32     "  start                       start ability with options\n"
33     "  stop-service                stop service with options\n"
34     "  dump                        dump the ability info\n"
35     "  force-stop <bundle-name>    force stop the process with bundle name\n"
36     "  process                     debug ability with options\n"
37 #ifdef ABILITY_COMMAND_FOR_TEST
38     "  test                        start the test framework with options\n"
39     "  ApplicationNotResponding     Pass in pid with options\n"
40     "  block-ability <ability-record-id>       block ability with ability record id\n"
41     "  block-ams-service                       block ams service\n"
42     "  block-app-service                       block app service\n";
43 #else
44     "  test                        start the test framework with options\n";
45 #endif
46 
47 const std::string HELP_MSG_SCREEN =
48     "usage: aa screen <options>\n"
49     "options list:\n"
50     "  -h, --help                                                   list available commands\n"
51     "  -p, --power <state>                                          power on or off with a state name\n";
52 
53 const std::string HELP_ApplicationNotResponding =
54     "usage: aa ApplicationNotResponding <options>\n"
55     "options list:\n"
56     "  -h, --help                   list available commands\n"
57     "  -p, --pid                    Pass in pid with option\n";
58 
59 const std::string HELP_MSG_START =
60     "usage: aa start <options>\n"
61     "options list:\n"
62     "  -h, --help                                                   list available commands\n"
63     "  [-d <device-id>] -a <ability-name> -b <bundle-name> [-m <module-name>] [-p <perf-cmd>] [-D] [-S] [-N] "
64     "  start ability with an element name\n";
65 
66 const std::string HELP_MSG_STOP_SERVICE =
67     "usage: aa stop-service <options>\n"
68     "options list:\n"
69     "  -h, --help                                                   list available commands\n"
70     "  [-d <device-id>] -a <ability-name> -b <bundle-name> [-m <module-name>] "
71     "  stop service with an element name\n";
72 
73 const std::string HELP_MSG_DUMPSYS = "usage: aa dump <options>\n"
74     "options list:\n"
75     "  -h, --help                   list available commands\n"
76     "  -a, --all                    dump all abilities\n"
77     "  -l, --mission-list           dump mission list\n"
78     "  -i, --ability                dump abilityRecordId\n"
79     "  -e, --extension              dump elementName (FA: serviceAbilityRecords,"
80     "Stage: ExtensionRecords)\n"
81     "  -p, --pending                dump pendingWantRecordId\n"
82     "  -r, --process                dump process\n"
83     "  -d, --data                   dump the data abilities\n"
84     "  -u, --userId                 userId\n"
85     "  -c, --client                 client\n"
86     "  -c, -u are auxiliary parameters and cannot be used alone\n"
87     "  The original -s parameter is invalid\n"
88     "  The original -m parameter is invalid\n";
89 
90 const std::string HELP_MSG_PROCESS = "usage: aa process <options>\n"
91     "options list:\n"
92     "  -h, --help                   list available commands\n"
93     "  -a <ability-name> -b <bundle-name> [-m <module-name>]\n"
94     "  -p <perf-cmd>                performance optimization command. Either -p or -D must be selected, "
95     "-p takes precedence.\n"
96     "  -D <debug-cmd>               debug command. Either -p or -D must be selected, -p takes precedence.\n"
97     "  [-S]\n"
98     "  debug ability with an element name\n";
99 
100 const std::string HELP_MSG_TEST =
101     "usage: aa test <options>\n"
102     "options list:\n"
103     "  -h, --help                                             list available commands\n"
104     "  -b <bundle-name> -s unittest <test-runner>             start the test framework with options\n"
105     "                  [-p <package-name>]                    the name of package with test-runner, "
106     "required for the FA model\n"
107     "                  [-m <module-name>]                     the name of module with test-runner, "
108     "required for the STAGE model\n"
109     "                  [-s class <test-class>]\n"
110     "                  [-s level <test-level>]\n"
111     "                  [-s size <test-size>]\n"
112     "                  [-s testType <test-testType>]\n"
113     "                  [-s timeout <test-timeout>]\n"
114     "                  [-s <any-key> <any-value>]\n"
115     "                  [-w <wait-time>]\n"
116     "                  [-D]\n";
117 
118 const std::string HELP_MSG_FORCE_STOP = "usage: aa force-stop <bundle-name>\n";
119 const std::string HELP_MSG_BLOCK_ABILITY = "usage: aa block-ability <abilityrecordid>\n";
120 const std::string HELP_MSG_FORCE_TIMEOUT =
121     "usage: aa force-timeout <ability-name> <INITIAL|INACTIVE|COMMAND|FOREGROUND|BACKGROUND|TERMINATING>\n"
122     "usage: aa force-timeout clean.";
123 const std::string HELP_MSG_FORCE_TIMEOUT_CLEAN = "clean";
124 
125 const std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
126 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
127 
128 const std::string STRING_START_ABILITY_OK = "start ability successfully.";
129 const std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
130 
131 const std::string STRING_STOP_SERVICE_ABILITY_OK = "stop service ability successfully.";
132 const std::string STRING_STOP_SERVICE_ABILITY_NG = "error: failed to stop service ability.";
133 
134 const std::string STRING_FORCE_STOP_OK = "force stop process successfully.";
135 const std::string STRING_FORCE_STOP_NG = "error: failed to force stop process.";
136 
137 const std::string STRING_START_USER_TEST_NG = "error: failed to start user test.";
138 const std::string STRING_USER_TEST_STARTED = "user test started.";
139 const std::string STRING_USER_TEST_FINISHED = "user test finished.";
140 
141 const std::string STRING_BLOCK_ABILITY_OK = "block ability successfully.";
142 const std::string STRING_BLOCK_ABILITY_NG = "error: failed to block stop ability.";
143 
144 const std::string STRING_BLOCK_AMS_SERVICE_OK = "block ams service successfully.";
145 const std::string STRING_BLOCK_AMS_SERVICE_NG = "error: failed to block ams service.";
146 
147 const std::string STRING_BLOCK_APP_SERVICE_OK = "block app service successfully.";
148 const std::string STRING_BLOCK_APP_SERVICE_NG = "error: failed to block app service.";
149 
150 const std::string STRING_START_NATIVE_PROCESS_OK = "start native process successfully.";
151 const std::string STRING_START_NATIVE_PROCESS_NG = "error: failed to start native process.";
152 
153 const int USER_TEST_COMMAND_START_INDEX = 2;
154 const int USER_TEST_COMMAND_PARAMS_NUM = 2;
155 const int TIME_RATE_MS = 1000;
156 const std::string STRING_FORCE_TIMEOUT_OK = "force ability timeout successfully.";
157 const std::string STRING_FORCE_TIMEOUT_NG = "error: failed to force ability timeout.";
158 
159 const int NUMBER_TWO = 2;
160 const int NUMBER_ONE = 1;
161 
162 const std::string DEBUG_VALUE = "true";
163 
164 const std::string PERFCMD_FIRST_PROFILE = "profile";
165 const std::string PERFCMD_FIRST_DUMPHEAP = "dumpheap";
166 
167 const std::string STRING_TEST_REGEX_INTEGER_NUMBERS = "^(0|[1-9][0-9]*|-[1-9][0-9]*)$";
168 }  // namespace
169 
170 class AbilityManagerShellCommand : public ShellCommand {
171 public:
172     AbilityManagerShellCommand(int argc, char* argv[]);
~AbilityManagerShellCommand()173     ~AbilityManagerShellCommand() override
174     {}
175 
176     ErrCode CreateMessageMap() override;
177     bool IsTestCommandIntegrity(const std::map<std::string, std::string>& params);
178     ErrCode StartUserTest(const std::map<std::string, std::string>& params);
179 
180 private:
181     ErrCode CreateCommandMap() override;
182     ErrCode init() override;
183 
184     ErrCode RunAsHelpCommand();
185     ErrCode RunAsScreenCommand();
186     ErrCode RunAsStartAbility();
187     ErrCode RunAsStopService();
188     ErrCode RunAsDumpsysCommand();
189     ErrCode RunAsForceStop();
190     ErrCode RunAsProcessCommand();
191 #ifdef ABILITY_COMMAND_FOR_TEST
192     ErrCode RunForceTimeoutForTest();
193     ErrCode RunAsSendAppNotRespondingProcessID();
194     ErrCode RunAsBlockAbilityCommand();
195     ErrCode RunAsBlockAmsServiceCommand();
196     ErrCode RunAsBlockAppServiceCommand();
197     ErrCode RunAsSendAppNotRespondingWithUnknownOption();
198     ErrCode RunAsSendAppNotRespondingWithOption(int32_t option, std::string& pid);
199 #endif
200 #ifdef ABILITY_FAULT_AND_EXIT_TEST
201     ErrCode RunAsForceExitAppCommand();
202     ErrCode RunAsNotifyAppFaultCommand();
203 #endif
204     sptr<IAbilityManager> GetAbilityManagerService();
205 
206     ErrCode MakeWantFromCmd(Want& want, std::string& windowMode);
207     ErrCode MakeWantForProcess(Want& want);
208     ErrCode RunAsTestCommand();
209     ErrCode TestCommandError(const std::string& info);
210     bool MatchOrderString(const std::regex &r, const std::string &orderCmd);
211     bool CheckPerfCmdString(const char* optarg, const size_t paramLength, std::string &perfCmd);
212 };
213 }  // namespace AAFwk
214 }  // namespace OHOS
215 
216 #endif  // OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
217