• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #include "device_profile_dumper.h"
17 
18 #include "distributed_device_profile_log.h"
19 #include "ipc_skeleton.h"
20 
21 namespace OHOS {
22 namespace DistributedDeviceProfile {
23 namespace {
24 constexpr size_t MIN_ARGS_SIZE = 1;
25 const std::string ARGS_HELP = "-h";
26 const std::string TAG = "DeviceProfileDumper";
27 constexpr int32_t UID_HIDUMPER = 1212;
28 }
29 
DumpDefault(std::string & result)30 bool DeviceProfileDumper::DumpDefault(std::string& result)
31 {
32     result.append("DeviceProfile Dump:\n");
33 //    DeviceProfileStorageManager::GetInstance().DumpLocalProfile(result);
34     return true;
35 }
36 
Dump(const std::vector<std::string> & args,std::string & result)37 bool DeviceProfileDumper::Dump(const std::vector<std::string>& args, std::string& result)
38 {
39     result.clear();
40     if (!CanDump()) {
41         result.append("Dump failed, not allowed");
42         return false;
43     }
44 
45     if (args.size() < MIN_ARGS_SIZE) {
46         return DumpDefault(result);
47     }
48 
49     if (args.size() == MIN_ARGS_SIZE) {
50         // -h
51         if (args[0] == ARGS_HELP) {
52             ShowHelp(result);
53             return true;
54         }
55     }
56     IllegalInput(result);
57     return false;
58 }
59 
ShowHelp(std::string & result)60 void DeviceProfileDumper::ShowHelp(std::string& result)
61 {
62     result.append("DeviceProfile Dump options:\n")
63         .append("  [-h] [cmd]...\n");
64 }
65 
IllegalInput(std::string & result)66 void DeviceProfileDumper::IllegalInput(std::string& result)
67 {
68     result.append("The arguments are illegal and you can enter '-h' for help.\n");
69 }
70 
CanDump()71 bool DeviceProfileDumper::CanDump()
72 {
73     auto callingUid = IPCSkeleton::GetCallingUid();
74     HILOGI("calling uid = %{public}u", callingUid);
75     if (callingUid != UID_HIDUMPER) {
76         return false;
77     }
78     return true;
79 }
80 } // namespace DeviceProfile
81 } // namespace OHOS
82