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 "to_string.h"
17
18 #include "constant_common.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace AccessToken {
DetailUsedRecordToString(bool isAccessDetail,const std::vector<UsedRecordDetail> & detailRecord,std::string & infos)23 void ToString::DetailUsedRecordToString(
24 bool isAccessDetail, const std::vector<UsedRecordDetail>& detailRecord, std::string& infos)
25 {
26 if (isAccessDetail) {
27 infos.append(R"( "accessRecords": [)");
28 } else {
29 infos.append(R"( "rejectRecords": [)");
30 }
31 infos.append("\n");
32 for (const auto& detail : detailRecord) {
33 infos.append(" {");
34 infos.append("\n");
35 infos.append(R"( "status": ")" + std::to_string(detail.status) + R"(")" + ",\n");
36 infos.append(R"( "lockScreenStatus": ")" +
37 std::to_string(detail.lockScreenStatus) + R"(")" + ",\n");
38 infos.append(R"( "timestamp": ")" + std::to_string(detail.timestamp) + R"(")" + ",\n");
39 infos.append(R"( "duration": )" + std::to_string(detail.accessDuration) + ",\n");
40 infos.append(R"( "count": )" + std::to_string(detail.count) + ",\n");
41 infos.append(" },");
42 infos.append("\n");
43 }
44
45 infos.append(" ]");
46 infos.append("\n");
47 }
48
PermissionUsedRecordToString(const std::vector<PermissionUsedRecord> & permissionRecords,std::string & infos)49 void ToString::PermissionUsedRecordToString(
50 const std::vector<PermissionUsedRecord>& permissionRecords, std::string& infos)
51 {
52 infos.append(R"( "permissionRecords": [)");
53 infos.append("\n");
54
55 for (const auto& perm : permissionRecords) {
56 infos.append(" {");
57 infos.append("\n");
58 infos.append(R"( "permissionName": ")" + perm.permissionName + R"(")" + ",\n");
59 infos.append(R"( "accessCount": ")" + std::to_string(perm.accessCount) + R"(")" + ",\n");
60 infos.append(R"( "rejectCount": )" + std::to_string(perm.rejectCount) + ",\n");
61 infos.append(R"( "lastAccessTime": )" + std::to_string(perm.lastAccessTime) + ",\n");
62 infos.append(R"( "lastRejectTime": )" + std::to_string(perm.lastRejectTime) + ",\n");
63 infos.append(R"( "lastAccessDuration": )" + std::to_string(perm.lastAccessDuration) + ",\n");
64 ToString::DetailUsedRecordToString(true, perm.accessRecords, infos);
65 ToString::DetailUsedRecordToString(false, perm.rejectRecords, infos);
66 infos.append(" },");
67 infos.append("\n");
68 }
69
70 infos.append(" ]");
71 infos.append("\n");
72 }
73
BundleUsedRecordToString(const BundleUsedRecord & bundleRecord,std::string & infos)74 void ToString::BundleUsedRecordToString(const BundleUsedRecord& bundleRecord, std::string& infos)
75 {
76 infos.append("{");
77 infos.append("\n");
78 infos.append(R"( "tokenId": )" + std::to_string(bundleRecord.tokenId) + ",\n");
79 infos.append(R"( "isRemote": )" + std::to_string(bundleRecord.isRemote) + ",\n");
80 infos.append(R"( "bundleName": )" + bundleRecord.bundleName + ",\n");
81 infos.append(R"( "deviceId": )" + ConstantCommon::EncryptDevId(bundleRecord.deviceId) + ",\n");
82
83 ToString::PermissionUsedRecordToString(bundleRecord.permissionRecords, infos);
84
85 infos.append("}");
86 infos.append("\n");
87 }
88
PermissionUsedResultToString(const PermissionUsedResult & result,std::string & infos)89 void ToString::PermissionUsedResultToString(const PermissionUsedResult& result, std::string& infos)
90 {
91 if (result.bundleRecords.empty()) {
92 return;
93 }
94 infos.append(R"("beginTime": )" + std::to_string(result.beginTimeMillis) + ",\n");
95 infos.append(R"("endTime": )" + std::to_string(result.endTimeMillis) + ",\n");
96
97 for (const auto& res : result.bundleRecords) {
98 ToString::BundleUsedRecordToString(res, infos);
99 }
100 }
101 } // namespace AccessToken
102 } // namespace Security
103 } // namespace OHOS