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 "data_translator.h"
17
18 #include "constant.h"
19 #include "privacy_field_const.h"
20 #include "time_util.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
TranslationIntoGenericValues(const PermissionUsedRequest & request,GenericValues & andGenericValues)25 int32_t DataTranslator::TranslationIntoGenericValues(const PermissionUsedRequest& request,
26 GenericValues& andGenericValues)
27 {
28 int64_t begin = request.beginTimeMillis;
29 int64_t end = request.endTimeMillis;
30 if ((begin < 0) || (end < 0) || (begin > end)) {
31 return Constant::FAILURE;
32 }
33
34 if (request.flag != FLAG_PERMISSION_USAGE_SUMMARY && request.flag != FLAG_PERMISSION_USAGE_DETAIL) {
35 return Constant::FAILURE;
36 }
37
38 if (begin == 0 && end == 0) {
39 int64_t beginTime = TimeUtil::GetCurrentTimestamp() - Constant::LATEST_RECORD_TIME;
40 begin = (beginTime < 0) ? 0 : beginTime;
41 end = TimeUtil::GetCurrentTimestamp();
42 }
43
44 if (begin != 0) {
45 andGenericValues.Put(PrivacyFiledConst::FIELD_TIMESTAMP_BEGIN, begin);
46 }
47 if (end != 0) {
48 andGenericValues.Put(PrivacyFiledConst::FIELD_TIMESTAMP_END, end);
49 }
50 return Constant::SUCCESS;
51 }
52
TranslationGenericValuesIntoPermissionUsedRecord(const GenericValues & inGenericValues,PermissionUsedRecord & permissionRecord)53 int32_t DataTranslator::TranslationGenericValuesIntoPermissionUsedRecord(const GenericValues& inGenericValues,
54 PermissionUsedRecord& permissionRecord)
55 {
56 std::string permission;
57 int32_t opCode = inGenericValues.GetInt(PrivacyFiledConst::FIELD_OP_CODE);
58 if (!Constant::TransferOpcodeToPermission(opCode, permission)) {
59 return Constant::FAILURE;
60 }
61
62 int64_t timestamp = inGenericValues.GetInt64(PrivacyFiledConst::FIELD_TIMESTAMP);
63 permissionRecord.permissionName = permission;
64
65 if (inGenericValues.GetInt(PrivacyFiledConst::FIELD_ACCESS_COUNT) != 0) {
66 permissionRecord.accessCount = inGenericValues.GetInt(PrivacyFiledConst::FIELD_ACCESS_COUNT);
67 permissionRecord.lastAccessTime = timestamp;
68 permissionRecord.lastAccessDuration = inGenericValues.GetInt64(PrivacyFiledConst::FIELD_ACCESS_DURATION);
69 }
70
71 if (inGenericValues.GetInt(PrivacyFiledConst::FIELD_REJECT_COUNT) != 0) {
72 permissionRecord.rejectCount = inGenericValues.GetInt(PrivacyFiledConst::FIELD_REJECT_COUNT);
73 permissionRecord.lastRejectTime = timestamp;
74 }
75
76 if (inGenericValues.GetInt(PrivacyFiledConst::FIELD_FLAG) == 0) {
77 return Constant::SUCCESS;
78 }
79
80 UsedRecordDetail detail;
81 detail.status = inGenericValues.GetInt(PrivacyFiledConst::FIELD_STATUS);
82 if (permissionRecord.lastAccessTime > 0) {
83 detail.timestamp = permissionRecord.lastAccessTime;
84 detail.accessDuration = inGenericValues.GetInt64(PrivacyFiledConst::FIELD_ACCESS_DURATION);
85 permissionRecord.accessRecords.emplace_back(detail);
86 }
87 if (permissionRecord.lastRejectTime > 0) {
88 detail.timestamp = permissionRecord.lastRejectTime;
89 permissionRecord.rejectRecords.emplace_back(detail);
90 }
91 return Constant::SUCCESS;
92 }
93 } // namespace AccessToken
94 } // namespace Security
95 } // namespace OHOS