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