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 #ifndef DLP_VISIT_RECORD_JSON_MANAGER_H
17 #define DLP_VISIT_RECORD_JSON_MANAGER_H
18 
19 #include <string>
20 #include <list>
21 #include <mutex>
22 #include "access_token.h"
23 #include "i_json_operator.h"
24 #include "nlohmann/json.hpp"
25 #include "visited_dlp_file_info.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace DlpPermission {
30 using namespace Security::AccessToken;
31 
32 struct VisitRecordInfo {
33     std::string bundleName = "";
34     std::string docUri = "";
35     int32_t userId = -1;
36     int64_t timestamp = -1;
37     AccessTokenID originalTokenId = 0;
38 };
39 
40 class VisitRecordJsonManager : public IJsonOperator {
41 public:
42     VisitRecordJsonManager();
43     ~VisitRecordJsonManager();
44 
45     int32_t AddVisitRecord(const std::string& bundleName, const int32_t& userId, const std::string& docUri);
46     int32_t GetVisitRecordList(const std::string& bundleName, const int32_t& userId,
47         std::vector<VisitedDLPFileInfo>& infoVec);
48 
49     Json ToJson() const override;
50     void FromJson(const Json& jsonObject) override;
51     std::string ToString() const override;
52 
53 private:
54     int32_t AddVisitRecord(const std::string& bundleName, const int32_t& userId, const std::string& docUri,
55         const int64_t timestamp, const AccessTokenID originalTokenId);
56     void VisitRecordInfoToJson(Json& json, const VisitRecordInfo& info) const;
57     bool VisitRecordInfoFromJson(const Json& json, VisitRecordInfo& info) const;
58     mutable std::mutex mutex_;
59     std::list<VisitRecordInfo> infoList_;
60 };
61 } // namespace DlpPermission
62 } // namespace Security
63 } // namespace OHOS
64 #endif // DLP_VISIT_RECORD_JSON_MANAGER_H
65