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 SANDBOX_JSON_MANAGER_H
17 #define SANDBOX_JSON_MANAGER_H
18 
19 #include <string>
20 #include <vector>
21 #include <map>
22 #include <mutex>
23 #include <thread>
24 #include "accesstoken_kit.h"
25 #include "account_adapt.h"
26 #include "bundle_mgr_interface.h"
27 #include "i_json_operator.h"
28 #include "nlohmann/json.hpp"
29 #include "parcel.h"
30 #include "retention_sandbox_info.h"
31 #include "safe_map.h"
32 
33 namespace OHOS {
34 namespace Security {
35 namespace DlpPermission {
36 struct RetentionInfo {
37     int32_t appIndex = -1;
38     uint32_t tokenId = 0;
39     std::string bundleName = "";
40     DLPFileAccess dlpFileAccess = DLPFileAccess::NO_PERMISSION;
41     std::set<std::string> docUriSet;
42     int32_t userId = -1;
43     bool hasRead = false;
44 };
45 
46 class SandboxJsonManager : public IJsonOperator {
47 public:
48     SandboxJsonManager();
49     ~SandboxJsonManager();
50 
51     int32_t AddSandboxInfo(const RetentionInfo& retentionInfo);
52     int32_t DelSandboxInfo(const uint32_t& tokenId);
53     bool CanUninstall(const uint32_t& tokenId);
54     int32_t UpdateRetentionState(const std::set<std::string>& docUriSet, RetentionInfo& info, bool isRetention);
55     int32_t UpdateReadFlag(uint32_t tokenId);
56     int32_t RemoveRetentionState(const std::string& bundleName, const int32_t& appIndex);
57     bool HasRetentionSandboxInfo(const std::string& bundleName);
58     int32_t GetRetentionSandboxList(const std::string& bundleName,
59         std::vector<RetentionSandBoxInfo>& retentionSandBoxInfoVec, bool isRetention);
60     void RetentionInfoToJson(Json& json, const RetentionInfo& info) const;
61     int32_t ClearUnreservedSandbox();
62     Json ToJson() const override;
63     void FromJson(const Json& jsonObject) override;
64     std::string ToString() const override;
65     int32_t GetBundleNameSetByUserId(const int32_t userId, std::set<std::string>& bundleNameSet);
66     int32_t RemoveRetentionInfoByUserId(const int32_t userId, const std::set<std::string>& bundleNameSet);
67 
68 private:
69     bool InsertSandboxInfo(const RetentionInfo& info);
70     sptr<AppExecFwk::IBundleMgr> GetBundleMgr();
71     bool GetUserIdByUid(int32_t& userId);
72     bool CheckReInstall(const RetentionInfo& info, const int32_t userId);
73     static bool CompareByTokenId(const RetentionInfo& info1, const RetentionInfo& info2);
74     static bool CompareByBundleName(const RetentionInfo& info1, const RetentionInfo& info2);
75     static bool UpdateDocUriSetByUnion(RetentionInfo& info, const std::set<std::string>& newSet);
76     static bool ClearDocUriSet(RetentionInfo& info, const std::set<std::string>& newSet);
77     int32_t UpdateRetentionState(const std::set<std::string>& newSet, const RetentionInfo& info,
78         bool (*compare)(const RetentionInfo& info1, const RetentionInfo& info2),
79         bool (*update)(RetentionInfo& info, const std::set<std::string>& newSet));
80     mutable std::mutex mutex_;
81     std::vector<RetentionInfo> infoVec_;
82 };
83 } // namespace DlpPermission
84 } // namespace Security
85 } // namespace OHOS
86 #endif // SANDBOX_JSON_MANAGER_H
87