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