1 /*
2 * Copyright (c) 2023 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 "visit_record_file_manager.h"
17 #include "dlp_permission.h"
18 #include "dlp_permission_log.h"
19
20 namespace OHOS {
21 namespace Security {
22 namespace DlpPermission {
23 namespace {
24 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, SECURITY_DOMAIN_DLP_PERMISSION, "VisitRecordFileManager" };
25 const std::string PATH_SEPARATOR = "/";
26 const std::string USER_INFO_BASE = "/data/service/el1/public/dlp_permission_service";
27 const std::string DLP_VISIT_RECORD_JSON_PATH = USER_INFO_BASE + PATH_SEPARATOR + "dlp_file_visit_record_info.json";
28 }
29
VisitRecordFileManager()30 VisitRecordFileManager::VisitRecordFileManager()
31 : fileOperator_(std::make_shared<FileOperator>()),
32 visitRecordJsonManager_(std::make_shared<VisitRecordJsonManager>())
33 {
34 Init();
35 }
36
~VisitRecordFileManager()37 VisitRecordFileManager::~VisitRecordFileManager() {}
38
GetInstance()39 VisitRecordFileManager& VisitRecordFileManager::GetInstance()
40 {
41 static VisitRecordFileManager instance;
42 return instance;
43 }
44
Init()45 bool VisitRecordFileManager::Init()
46 {
47 std::lock_guard<std::recursive_mutex> lock(mutex_);
48 if (hasInit_) {
49 return true;
50 }
51 if (fileOperator_->IsExistFile(DLP_VISIT_RECORD_JSON_PATH)) {
52 std::string constraintsConfigStr;
53 if (fileOperator_->GetFileContentByPath(DLP_VISIT_RECORD_JSON_PATH, constraintsConfigStr) != DLP_OK) {
54 return false;
55 }
56 if (!constraintsConfigStr.empty()) {
57 Json callbackInfoJson = Json::parse(constraintsConfigStr, nullptr, false);
58 if (callbackInfoJson.is_discarded()) {
59 DLP_LOG_ERROR(LABEL, "callbackInfoJson is discarded");
60 return false;
61 }
62 visitRecordJsonManager_->FromJson(callbackInfoJson);
63 }
64 }
65 hasInit_ = true;
66 return true;
67 }
68
UpdateFile(const int32_t & jsonRes)69 int32_t VisitRecordFileManager::UpdateFile(const int32_t& jsonRes)
70 {
71 std::lock_guard<std::recursive_mutex> lock(mutex_);
72 if (jsonRes == DLP_FILE_NO_NEED_UPDATE) {
73 return DLP_OK;
74 }
75 if (jsonRes != DLP_OK) {
76 return jsonRes;
77 }
78 std::string jsonStr = visitRecordJsonManager_->ToString();
79 if (fileOperator_->InputFileByPathAndContent(DLP_VISIT_RECORD_JSON_PATH, jsonStr) != DLP_OK) {
80 DLP_LOG_ERROR(LABEL, "InputFileByPathAndContent failed!");
81 return DLP_INSERT_FILE_ERROR;
82 }
83 return DLP_OK;
84 }
85
AddVisitRecord(const std::string & bundleName,const int32_t & userId,const std::string & docUri)86 int32_t VisitRecordFileManager::AddVisitRecord(const std::string& bundleName, const int32_t& userId,
87 const std::string& docUri)
88 {
89 if (!hasInit_ && !Init()) {
90 DLP_LOG_ERROR(LABEL, "Init failed!");
91 return DLP_RETENTION_UPDATE_ERROR;
92 }
93 int32_t res = visitRecordJsonManager_->AddVisitRecord(bundleName, userId, docUri);
94 return UpdateFile(res);
95 }
96
GetVisitRecordList(const std::string & bundleName,const int32_t & userId,std::vector<VisitedDLPFileInfo> & infoVec)97 int32_t VisitRecordFileManager::GetVisitRecordList(const std::string& bundleName, const int32_t& userId,
98 std::vector<VisitedDLPFileInfo>& infoVec)
99 {
100 if (!hasInit_ && !Init()) {
101 DLP_LOG_ERROR(LABEL, "Init failed!");
102 return DLP_RETENTION_UPDATE_ERROR;
103 }
104 int32_t res = visitRecordJsonManager_->GetVisitRecordList(bundleName, userId, infoVec);
105 return UpdateFile(res);
106 }
107 } // namespace DlpPermission
108 } // namespace Security
109 } // namespace OHOS
110