• 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 "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,GenericValues & orGenericValues)25 int32_t DataTranslator::TranslationIntoGenericValues(const PermissionUsedRequest& request,
26     GenericValues& andGenericValues, GenericValues& orGenericValues)
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 
51     for (const auto& perm : request.permissionList) {
52         int32_t opCode;
53         if (Constant::TransferPermissionToOpcode(perm, opCode)) {
54             orGenericValues.Put(PrivacyFiledConst::FIELD_OP_CODE, opCode);
55         } else {
56             orGenericValues.Put(PrivacyFiledConst::FIELD_OP_CODE, Constant::OP_INVALID);
57         }
58     }
59     return Constant::SUCCESS;
60 }
61 
TranslationGenericValuesIntoPermissionUsedRecord(const GenericValues & inGenericValues,PermissionUsedRecord & permissionRecord)62 int32_t DataTranslator::TranslationGenericValuesIntoPermissionUsedRecord(const GenericValues& inGenericValues,
63     PermissionUsedRecord& permissionRecord)
64 {
65     std::string permission;
66     int32_t opCode = inGenericValues.GetInt(PrivacyFiledConst::FIELD_OP_CODE);
67     if (!Constant::TransferOpcodeToPermission(opCode, permission)) {
68         return Constant::FAILURE;
69     }
70 
71     int64_t timestamp = inGenericValues.GetInt64(PrivacyFiledConst::FIELD_TIMESTAMP);
72     permissionRecord.permissionName = permission;
73 
74     if (inGenericValues.GetInt(PrivacyFiledConst::FIELD_ACCESS_COUNT) != 0) {
75         permissionRecord.accessCount = inGenericValues.GetInt(PrivacyFiledConst::FIELD_ACCESS_COUNT);
76         permissionRecord.lastAccessTime = timestamp;
77         permissionRecord.lastAccessDuration = inGenericValues.GetInt64(PrivacyFiledConst::FIELD_ACCESS_DURATION);
78     }
79 
80     if (inGenericValues.GetInt(PrivacyFiledConst::FIELD_REJECT_COUNT) != 0) {
81         permissionRecord.rejectCount = inGenericValues.GetInt(PrivacyFiledConst::FIELD_REJECT_COUNT);
82         permissionRecord.lastRejectTime = timestamp;
83     }
84 
85     if (inGenericValues.GetInt(PrivacyFiledConst::FIELD_FLAG) == 0) {
86         return Constant::SUCCESS;
87     }
88 
89     UsedRecordDetail detail;
90     detail.status = inGenericValues.GetInt(PrivacyFiledConst::FIELD_STATUS);
91     if (permissionRecord.lastAccessTime > 0) {
92         detail.timestamp = permissionRecord.lastAccessTime;
93         detail.accessDuration = inGenericValues.GetInt64(PrivacyFiledConst::FIELD_ACCESS_DURATION);
94         permissionRecord.accessRecords.emplace_back(detail);
95     }
96     if (permissionRecord.lastRejectTime > 0) {
97         detail.timestamp = permissionRecord.lastRejectTime;
98         permissionRecord.rejectRecords.emplace_back(detail);
99     }
100     return Constant::SUCCESS;
101 }
102 } // namespace AccessToken
103 } // namespace Security
104 } // namespace OHOS