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