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 std::mutex g_instanceMutex;
29 }
30
VisitRecordFileManager()31 VisitRecordFileManager::VisitRecordFileManager()
32 : fileOperator_(std::make_shared<FileOperator>()),
33 visitRecordJsonManager_(std::make_shared<VisitRecordJsonManager>())
34 {
35 Init();
36 }
37
~VisitRecordFileManager()38 VisitRecordFileManager::~VisitRecordFileManager() {}
39
GetInstance()40 VisitRecordFileManager& VisitRecordFileManager::GetInstance()
41 {
42 static VisitRecordFileManager* instance = nullptr;
43 if (instance == nullptr) {
44 std::lock_guard<std::mutex> lock(g_instanceMutex);
45 if (instance == nullptr) {
46 instance = new VisitRecordFileManager();
47 }
48 }
49 return *instance;
50 }
51
Init()52 bool VisitRecordFileManager::Init()
53 {
54 std::lock_guard<std::recursive_mutex> lock(mutex_);
55 if (hasInit_) {
56 return true;
57 }
58 if (fileOperator_->IsExistFile(DLP_VISIT_RECORD_JSON_PATH)) {
59 std::string constraintsConfigStr;
60 if (fileOperator_->GetFileContentByPath(DLP_VISIT_RECORD_JSON_PATH, constraintsConfigStr) != DLP_OK) {
61 return false;
62 }
63 if (!constraintsConfigStr.empty()) {
64 Json callbackInfoJson = Json::parse(constraintsConfigStr, nullptr, false);
65 if (callbackInfoJson.is_discarded()) {
66 DLP_LOG_ERROR(LABEL, "callbackInfoJson is discarded");
67 return false;
68 }
69 visitRecordJsonManager_->FromJson(callbackInfoJson);
70 }
71 }
72 hasInit_ = true;
73 return true;
74 }
75
UpdateFile(const int32_t & jsonRes)76 int32_t VisitRecordFileManager::UpdateFile(const int32_t& jsonRes)
77 {
78 std::lock_guard<std::recursive_mutex> lock(mutex_);
79 if (jsonRes == DLP_FILE_NO_NEED_UPDATE) {
80 return DLP_OK;
81 }
82 if (jsonRes != DLP_OK) {
83 return jsonRes;
84 }
85 std::string jsonStr = visitRecordJsonManager_->ToString();
86 if (fileOperator_->InputFileByPathAndContent(DLP_VISIT_RECORD_JSON_PATH, jsonStr) != DLP_OK) {
87 DLP_LOG_ERROR(LABEL, "InputFileByPathAndContent failed!");
88 return DLP_INSERT_FILE_ERROR;
89 }
90 return DLP_OK;
91 }
92
AddVisitRecord(const std::string & bundleName,const int32_t & userId,const std::string & docUri)93 int32_t VisitRecordFileManager::AddVisitRecord(const std::string& bundleName, const int32_t& userId,
94 const std::string& docUri)
95 {
96 if (!Init()) {
97 DLP_LOG_ERROR(LABEL, "Init failed!");
98 return DLP_RETENTION_UPDATE_ERROR;
99 }
100 int32_t res = visitRecordJsonManager_->AddVisitRecord(bundleName, userId, docUri);
101 return UpdateFile(res);
102 }
103
GetVisitRecordList(const std::string & bundleName,const int32_t & userId,std::vector<VisitedDLPFileInfo> & infoVec)104 int32_t VisitRecordFileManager::GetVisitRecordList(const std::string& bundleName, const int32_t& userId,
105 std::vector<VisitedDLPFileInfo>& infoVec)
106 {
107 if (!Init()) {
108 DLP_LOG_ERROR(LABEL, "Init failed!");
109 return DLP_RETENTION_UPDATE_ERROR;
110 }
111 int32_t res = visitRecordJsonManager_->GetVisitRecordList(bundleName, userId, infoVec);
112 return UpdateFile(res);
113 }
114 } // namespace DlpPermission
115 } // namespace Security
116 } // namespace OHOS
117