• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H
17 #define FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H
18 
19 #include "shell_command.h"
20 #include "bundle_mgr_interface.h"
21 #include "bundle_installer_interface.h"
22 
23 namespace OHOS {
24 namespace AppExecFwk {
25 namespace {
26 const std::string TOOL_NAME = "bm";
27 
28 const std::string HELP_MSG = "usage: bm <command> <options>\n"
29                              "These are common bm commands list:\n"
30                              "  help         list available commands\n"
31                              "  install      install a bundle with options\n"
32                              "  uninstall    uninstall a bundle with options\n"
33                              "  dump         dump the bundle info\n"
34                              "  clean        clean the bundle data\n"
35                              "  enable       enable the bundle\n"
36                              "  disable      disable the bundle\n"
37                              "  get          obtain device udid\n";
38 
39 const std::string HELP_MSG_INSTALL =
40     "usage: bm install <options>\n"
41     "options list:\n"
42     "  -h, --help                                                 list available commands\n"
43     "  -p, --bundle-path <hap-file-path>                          install a hap by a specified path\n"
44     "  -p, --bundle-path <hap-file-path> <hap-file-path> ...      install some haps of one bundle by some hap paths\n"
45     "  -p, --bundle-path <bundle-direction>                       install one bundle by a direction,\n"
46     "                                                                     under which are some hap files\n"
47     "  -r -p <bundle-file-path>                                   replace an existing bundle\n"
48     "  -r --bundle-path <bundle-file-path>                        replace an existing bundle\n"
49     "  -u, --user-id <user-id>                                    specify a user id\n";
50 
51 const std::string HELP_MSG_UNINSTALL =
52     "usage: bm uninstall <options>\n"
53     "options list:\n"
54     "  -h, --help                           list available commands\n"
55     "  -n, --bundle-name <bundle-name>      uninstall a bundle by bundle name\n"
56     "  -m, --module-name <module-name>      uninstall a module by module name\n"
57     "  -u, --user-id <user-id>              specify a user id\n";
58 
59 const std::string HELP_MSG_DUMP =
60     "usage: bm dump <options>\n"
61     "options list:\n"
62     "  -h, --help                           list available commands\n"
63     "  -a, --all                            list all bundles in system\n"
64     "  -i, --bundle-info                    list all bundles info in system\n"
65     "  -n, --bundle-name <bundle-name>      list the bundle info by a bundle name\n"
66     "  -s, --shortcut-info                  list the shortcut info\n"
67     "  -d, --device-id <device-id>          specify a device id\n"
68     "  -u, --user-id <user-id>              specify a user id\n";
69 
70 const std::string HELP_MSG_CLEAN =
71     "usage: bm clean <options>\n"
72     "options list:\n"
73     "  -h, --help                                      list available commands\n"
74     "  -n, --bundle-name  <bundle-name>                bundle name\n"
75     "  -c, --cache                                     clean bundle cache files by bundle name\n"
76     "  -d, --data                                      clean bundle data files by bundle name\n"
77     "  -u, --user-id <user-id>                         specify a user id\n";
78 
79 const std::string HELP_MSG_ENABLE =
80     "usage: bm enable <options>\n"
81     "options list:\n"
82     "  -h, --help                             list available commands\n"
83     "  -n, --bundle-name  <bundle-name>       enable bundle by bundle name\n"
84     "  -a, --ability-name <ability-name>      enable ability by ability name\n"
85     "  -u, --user-id <user-id>                specify a user id\n";
86 
87 const std::string HELP_MSG_DISABLE =
88     "usage: bm disable <options>\n"
89     "options list:\n"
90     "  -h, --help                             list available commands\n"
91     "  -n, --bundle-name  <bundle-name>       disable bundle by bundle name\n"
92     "  -a, --ability-name <ability-name>      disable ability by ability name\n"
93     "  -u, --user-id <user-id>                specify a user id\n";
94 
95 const std::string HELP_MSG_GET =
96     "usage: bm get <options>\n"
97     "options list:\n"
98     "  -u, --udid                             obtain udid of the current device\n";
99 
100 const std::string STRING_INCORRECT_OPTION = "error: incorrect option";
101 const std::string HELP_MSG_NO_BUNDLE_PATH_OPTION =
102     "error: you must specify a bundle path with '-p' or '--bundle-path'.";
103 
104 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION =
105     "error: you must specify a bundle name with '-n' or '--bundle-name'.";
106 
107 const std::string STRING_INSTALL_BUNDLE_OK = "install bundle successfully.";
108 const std::string STRING_INSTALL_BUNDLE_NG = "error: failed to install bundle.";
109 
110 const std::string STRING_UNINSTALL_BUNDLE_OK = "uninstall bundle successfully.";
111 const std::string STRING_UNINSTALL_BUNDLE_NG = "error: failed to uninstall bundle.";
112 
113 const std::string HELP_MSG_NO_DATA_OR_CACHE_OPTION =
114     "error: you must specify '-c' or '-d' for 'bm clean' option.";
115 const std::string STRING_CLEAN_CACHE_BUNDLE_OK = "clean bundle cache files successfully.";
116 const std::string STRING_CLEAN_CACHE_BUNDLE_NG = "error: failed to clean bundle cache files.";
117 
118 const std::string STRING_CLEAN_DATA_BUNDLE_OK = "clean bundle data files successfully.";
119 const std::string STRING_CLEAN_DATA_BUNDLE_NG = "error: failed to clean bundle data files.";
120 
121 const std::string STRING_ENABLE_BUNDLE_OK = "enable bundle successfully.";
122 const std::string STRING_ENABLE_BUNDLE_NG = "error: failed to enable bundle.";
123 
124 const std::string STRING_DISABLE_BUNDLE_OK = "disable bundle successfully.";
125 const std::string STRING_DISABLE_BUNDLE_NG = "error: failed to disable bundle.";
126 
127 const std::string STRING_GET_UDID_OK = "udid of current device is :";
128 const std::string STRING_GET_UDID_NG = "error: failed to get udid";
129 
130 const std::string HELP_MSG_DUMP_FAILED = "error: failed to get information and the parameters may be wrong.";
131 } // namespace
132 
133 class BundleManagerShellCommand : public ShellCommand {
134 public:
135     BundleManagerShellCommand(int argc, char *argv[]);
~BundleManagerShellCommand()136     ~BundleManagerShellCommand() override
137     {}
138 
139 private:
140     ErrCode CreateCommandMap() override;
141     ErrCode CreateMessageMap() override;
142     ErrCode init() override;
143 
144     sptr<IBundleMgr> GetBundleMgrProxy() const;
145     sptr<IBundleInstaller> GetInstallerProxy() const;
146 
147     ErrCode RunAsHelpCommand();
148     ErrCode RunAsInstallCommand();
149     ErrCode RunAsUninstallCommand();
150     ErrCode RunAsDumpCommand();
151     ErrCode RunAsCleanCommand();
152     ErrCode RunAsEnableCommand();
153     ErrCode RunAsDisableCommand();
154     ErrCode RunAsGetCommand();
155 
156     std::string DumpBundleList(int32_t userId) const;
157     std::string DumpBundleInfo(const std::string &bundleName, int32_t userId) const;
158     std::string DumpBundleInfos(int32_t userId) const;
159     std::string DumpShortcutInfos(const std::string &bundleName, int32_t userId) const;
160     std::string DumpDistributedBundleInfo(const std::string &deviceId, int32_t userId, const std::string &bundleName);
161 
162     int32_t InstallOperation(const std::vector<std::string> &bundlePaths, InstallParam &installParam) const;
163     int32_t UninstallOperation(const std::string &bundleName, const std::string &moduleName,
164                                InstallParam &installParam) const;
165     std::string GetUdid() const;
166 
167     ErrCode GetBundlePath(const std::string& param, std::vector<std::string>& bundlePaths) const;
168 
169     bool CleanBundleCacheFilesOperation(const std::string &bundleName, int32_t userId) const;
170     bool CleanBundleDataFilesOperation(const std::string &bundleName, int32_t userId) const;
171 
172     bool SetApplicationEnabledOperation(const AbilityInfo &abilityInfo, bool isEnable, int32_t userId) const;
173 
174     int32_t GetCurrentUserId(int32_t userId) const;
175 
176     sptr<IBundleMgr> bundleMgrProxy_;
177     sptr<IBundleInstaller> bundleInstallerProxy_;
178 };
179 }  // namespace AppExecFwk
180 }  // namespace OHOS
181 
182 #endif  // FOUNDATION_APPEXECFWK_STANDARD_TOOLS_BM_INCLUDE_BUNDLE_COMMAND_H