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 #ifndef DEVICE_INFO_REPOSITORY_H
17 #define DEVICE_INFO_REPOSITORY_H
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 #include <mutex>
23 
24 #include "constant.h"
25 #include "device_info.h"
26 
27 namespace OHOS {
28 namespace Security {
29 namespace AccessToken {
30 class DeviceInfoRepository {
31 public:
32     static DeviceInfoRepository &GetInstance();
33 
34     std::vector<DeviceInfo> ListDeviceInfo();
35 
36     bool FindDeviceInfo(const std::string &nodeId, DeviceIdType type, DeviceInfo &deviceInfo);
37 
38     void DeleteAllDeviceInfoExceptOne(const DeviceInfo deviceInfo);
39 
40     void SaveDeviceInfo(const DeviceInfo deviceInfo);
41 
42     void SaveDeviceInfo(const DeviceId deviceId, const std::string &deviceName, const std::string &deviceType);
43 
44     void SaveDeviceInfo(const std::string &networkId, const std::string &universallyUniqueId,
45         const std::string &uniqueDeviceId, const std::string &deviceName, const std::string &deviceType);
46 
47     void DeleteDeviceInfo(const std::string &nodeId, const DeviceIdType type);
48 
49     void Clear();
50 
51 private:
52     bool FindDeviceIdByNodeIdLocked(const std::string &nodeId, const DeviceIdType type, DeviceId &deviceId) const;
53 
54     bool FindDeviceInfoByDeviceIdLocked(const DeviceId deviceId, DeviceInfo &deviceInfo) const;
55 
56     bool FindDeviceIdByNetworkIdLocked(const std::string &networkId, DeviceId &deviceId) const;
57 
58     bool FindDeviceIdByUniversallyUniqueIdLocked(const std::string &universallyUniqueId, DeviceId &deviceId) const;
59 
60     bool FindDeviceIdByUniqueDeviceIdLocked(const std::string &uniqueDeviceId, DeviceId &deviceId) const;
61 
62     void DeleteDeviceInfoByDeviceIdLocked(const DeviceId deviceId);
63 
64     std::map<std::string, DeviceId> deviceIdMapByNetworkId_;
65 
66     std::map<std::string, DeviceId> deviceIdMapByUniversallyUniqueId_;
67 
68     std::map<std::string, DeviceId> deviceIdMapByUniqueDeviceId_;
69 
70     std::map<std::string, DeviceInfo> deviceInfoMap_;
71 
72     std::recursive_mutex stackLock_;
73 };
74 } // namespace AccessToken
75 } // namespace Security
76 } // namespace OHOS
77 #endif // DEVICE_INFO_REPOSITORY_H
78