1 /*
2  * Copyright (c) 2022-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 "distributed_ability_manager_dumper.h"
17 
18 #include "accesstoken_kit.h"
19 #include "base/continuationmgr_log.h"
20 #include "distributed_ability_manager_service.h"
21 #include "ipc_skeleton.h"
22 
23 namespace OHOS {
24 namespace DistributedSchedule {
25 namespace {
26 const std::string TAG = "ContinuationManagerDumper";
27 const std::string HIDUMPER_PROCESS_NAME = "hidumper_service";
28 const std::string ARGS_HELP = "-h";
29 const std::string ARGS_APP_REGISTER_INFO = "-register";
30 constexpr size_t MIN_ARGS_SIZE = 1;
31 }
32 
Dump(const std::vector<std::string> & args,std::string & result)33 bool DistributedAbilityManagerDumper::Dump(const std::vector<std::string>& args, std::string& result)
34 {
35     result.clear();
36     if (!CanDump()) {
37         result.append("Dump failed, not allowed");
38         return false;
39     }
40     if (args.size() < MIN_ARGS_SIZE) {
41         return DumpDefault(result);
42     }
43     if (args.size() == MIN_ARGS_SIZE) {
44         // -h
45         if (args[0] == ARGS_HELP) {
46             ShowHelp(result);
47             return true;
48         }
49         // -register
50         if (args[0] == ARGS_APP_REGISTER_INFO) {
51             ShowAppRegisterInfo(result);
52             return true;
53         }
54     }
55     IllegalInput(result);
56     return false;
57 }
58 
CanDump()59 bool DistributedAbilityManagerDumper::CanDump()
60 {
61     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
62     Security::AccessToken::NativeTokenInfo nativeTokenInfo;
63     int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
64     if (result == ERR_OK && nativeTokenInfo.processName == HIDUMPER_PROCESS_NAME) {
65         return true;
66     }
67     return false;
68 }
69 
DumpDefault(std::string & result)70 bool DistributedAbilityManagerDumper::DumpDefault(std::string& result)
71 {
72     result.append("ContinuationManagerService Dump\n")
73         .append("\n");
74     ShowAppRegisterInfo(result);
75     return true;
76 }
77 
ShowAppRegisterInfo(std::string & result)78 void DistributedAbilityManagerDumper::ShowAppRegisterInfo(std::string& result)
79 {
80     DistributedAbilityManagerService::GetInstance().DumpAppRegisterInfo(result);
81 }
82 
ShowHelp(std::string & result)83 void DistributedAbilityManagerDumper::ShowHelp(std::string& result)
84 {
85     result.append("ContinuationManagerService Dump options:\n")
86         .append("  [-h] [cmd]...\n")
87         .append("cmd maybe one of:\n")
88         .append("  -register: show all application register infos.\n");
89 }
90 
IllegalInput(std::string & result)91 void DistributedAbilityManagerDumper::IllegalInput(std::string& result)
92 {
93     result.append("The arguments are illegal and you can enter '-h' for help.\n");
94 }
95 } // namespace DistributedSchedule
96 } // namespace OHOS