1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "distributed_ability_manager_dumper.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include "accesstoken_kit.h"
19686862fbSopenharmony_ci#include "base/continuationmgr_log.h"
20686862fbSopenharmony_ci#include "distributed_ability_manager_service.h"
21686862fbSopenharmony_ci#include "ipc_skeleton.h"
22686862fbSopenharmony_ci
23686862fbSopenharmony_cinamespace OHOS {
24686862fbSopenharmony_cinamespace DistributedSchedule {
25686862fbSopenharmony_cinamespace {
26686862fbSopenharmony_ciconst std::string TAG = "ContinuationManagerDumper";
27686862fbSopenharmony_ciconst std::string HIDUMPER_PROCESS_NAME = "hidumper_service";
28686862fbSopenharmony_ciconst std::string ARGS_HELP = "-h";
29686862fbSopenharmony_ciconst std::string ARGS_APP_REGISTER_INFO = "-register";
30686862fbSopenharmony_ciconstexpr size_t MIN_ARGS_SIZE = 1;
31686862fbSopenharmony_ci}
32686862fbSopenharmony_ci
33686862fbSopenharmony_cibool DistributedAbilityManagerDumper::Dump(const std::vector<std::string>& args, std::string& result)
34686862fbSopenharmony_ci{
35686862fbSopenharmony_ci    result.clear();
36686862fbSopenharmony_ci    if (!CanDump()) {
37686862fbSopenharmony_ci        result.append("Dump failed, not allowed");
38686862fbSopenharmony_ci        return false;
39686862fbSopenharmony_ci    }
40686862fbSopenharmony_ci    if (args.size() < MIN_ARGS_SIZE) {
41686862fbSopenharmony_ci        return DumpDefault(result);
42686862fbSopenharmony_ci    }
43686862fbSopenharmony_ci    if (args.size() == MIN_ARGS_SIZE) {
44686862fbSopenharmony_ci        // -h
45686862fbSopenharmony_ci        if (args[0] == ARGS_HELP) {
46686862fbSopenharmony_ci            ShowHelp(result);
47686862fbSopenharmony_ci            return true;
48686862fbSopenharmony_ci        }
49686862fbSopenharmony_ci        // -register
50686862fbSopenharmony_ci        if (args[0] == ARGS_APP_REGISTER_INFO) {
51686862fbSopenharmony_ci            ShowAppRegisterInfo(result);
52686862fbSopenharmony_ci            return true;
53686862fbSopenharmony_ci        }
54686862fbSopenharmony_ci    }
55686862fbSopenharmony_ci    IllegalInput(result);
56686862fbSopenharmony_ci    return false;
57686862fbSopenharmony_ci}
58686862fbSopenharmony_ci
59686862fbSopenharmony_cibool DistributedAbilityManagerDumper::CanDump()
60686862fbSopenharmony_ci{
61686862fbSopenharmony_ci    uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
62686862fbSopenharmony_ci    Security::AccessToken::NativeTokenInfo nativeTokenInfo;
63686862fbSopenharmony_ci    int32_t result = Security::AccessToken::AccessTokenKit::GetNativeTokenInfo(accessToken, nativeTokenInfo);
64686862fbSopenharmony_ci    if (result == ERR_OK && nativeTokenInfo.processName == HIDUMPER_PROCESS_NAME) {
65686862fbSopenharmony_ci        return true;
66686862fbSopenharmony_ci    }
67686862fbSopenharmony_ci    return false;
68686862fbSopenharmony_ci}
69686862fbSopenharmony_ci
70686862fbSopenharmony_cibool DistributedAbilityManagerDumper::DumpDefault(std::string& result)
71686862fbSopenharmony_ci{
72686862fbSopenharmony_ci    result.append("ContinuationManagerService Dump\n")
73686862fbSopenharmony_ci        .append("\n");
74686862fbSopenharmony_ci    ShowAppRegisterInfo(result);
75686862fbSopenharmony_ci    return true;
76686862fbSopenharmony_ci}
77686862fbSopenharmony_ci
78686862fbSopenharmony_civoid DistributedAbilityManagerDumper::ShowAppRegisterInfo(std::string& result)
79686862fbSopenharmony_ci{
80686862fbSopenharmony_ci    DistributedAbilityManagerService::GetInstance().DumpAppRegisterInfo(result);
81686862fbSopenharmony_ci}
82686862fbSopenharmony_ci
83686862fbSopenharmony_civoid DistributedAbilityManagerDumper::ShowHelp(std::string& result)
84686862fbSopenharmony_ci{
85686862fbSopenharmony_ci    result.append("ContinuationManagerService Dump options:\n")
86686862fbSopenharmony_ci        .append("  [-h] [cmd]...\n")
87686862fbSopenharmony_ci        .append("cmd maybe one of:\n")
88686862fbSopenharmony_ci        .append("  -register: show all application register infos.\n");
89686862fbSopenharmony_ci}
90686862fbSopenharmony_ci
91686862fbSopenharmony_civoid DistributedAbilityManagerDumper::IllegalInput(std::string& result)
92686862fbSopenharmony_ci{
93686862fbSopenharmony_ci    result.append("The arguments are illegal and you can enter '-h' for help.\n");
94686862fbSopenharmony_ci}
95686862fbSopenharmony_ci} // namespace DistributedSchedule
96686862fbSopenharmony_ci} // namespace OHOS