• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 KV_STORE_DELEGATE_MANAGER_H
17 #define KV_STORE_DELEGATE_MANAGER_H
18 
19 #include <string>
20 #include <functional>
21 #include <mutex>
22 #include <memory>
23 
24 
25 #include "auto_launch_export.h"
26 #include "iprocess_communicator.h"
27 #include "iprocess_system_api_adapter.h"
28 #include "kv_store_delegate.h"
29 #include "kv_store_nb_delegate.h"
30 #include "store_types.h"
31 
32 namespace DistributedDB {
33 class KvStoreDelegateManager final {
34 public:
35     DB_API KvStoreDelegateManager(const std::string &appId, const std::string &userId, int32_t instanceId = 0);
36     DB_API ~KvStoreDelegateManager();
37 
38     KvStoreDelegateManager(const KvStoreDelegateManager &) = delete;
39     KvStoreDelegateManager(KvStoreDelegateManager &&) = delete;
40     KvStoreDelegateManager &operator=(const KvStoreDelegateManager &) = delete;
41     KvStoreDelegateManager &operator=(KvStoreDelegateManager &&) = delete;
42 
43     // Used to set global config of the KvStores, such dataDir, return OK if set config success.
44     DB_API DBStatus SetKvStoreConfig(const KvStoreConfig &kvStoreConfig);
45 
46     // Used to open or create a KvStore.
47     // Return OK and a KvStoreDelegate* if there is no error. else return ERROR and nullptr;
48     DB_API void GetKvStore(const std::string &storeId, const KvStoreDelegate::Option &option,
49         const std::function<void(DBStatus, KvStoreDelegate *)> &callback);
50 
51     // Used to open or create a KvStore(Natural store).
52     // Suggest: Not to use encrypted database in S3 SECE access controlled;
53     // Warning: Access controlled prevents access to files so cannot verify passwords,
54     // So that a cacheDb with incorrect passwd will be created or opened and lose these data.
55     DB_API void GetKvStore(const std::string &storeId, const KvStoreNbDelegate::Option &option,
56         const std::function<void(DBStatus, KvStoreNbDelegate *)> &callback);
57 
58     // Close a KvStore, return OK if close success.
59     DB_API DBStatus CloseKvStore(KvStoreDelegate *kvStore);
60 
61     DB_API DBStatus CloseKvStore(KvStoreNbDelegate *kvStore);
62 
63     // Used to delete a KvStore, return OK if delete success.
64     DB_API DBStatus DeleteKvStore(const std::string &storeId);
65 
66     // Get the database size.
67     DB_API DBStatus GetKvStoreDiskSize(const std::string &storeId, uint64_t &size);
68 
69     // Used to set the process userid and appId
70     DB_API static DBStatus SetProcessLabel(const std::string &appId, const std::string &userId);
71 
72     // Set process communicator.
73     DB_API static DBStatus SetProcessCommunicator(const std::shared_ptr<IProcessCommunicator> &inCommunicator);
74 
75     DB_API static void SetKvStoreCorruptionHandler(const KvStoreCorruptionHandler &handler);
76 
77     // Get database directory by storeId + appId + userId
78     DB_API static DBStatus GetDatabaseDir(const std::string &storeId, const std::string &appId,
79         const std::string &userId, std::string &directory);
80 
81     // Get database directory by storeId
82     DB_API static DBStatus GetDatabaseDir(const std::string &storeId, std::string &directory);
83 
84     DB_API static DBStatus SetPermissionCheckCallback(const PermissionCheckCallback &callback);
85 
86     DB_API static DBStatus SetPermissionCheckCallback(const PermissionCheckCallbackV2 &callback);
87 
88     DB_API static DBStatus EnableKvStoreAutoLaunch(const std::string &userId, const std::string &appId,
89         const std::string &storeId, const AutoLaunchOption &option, const AutoLaunchNotifier &notifier);
90 
91     DB_API static DBStatus DisableKvStoreAutoLaunch(const std::string &userId, const std::string &appId,
92         const std::string &storeId);
93 
94     // deprecated
95     DB_API static void SetAutoLaunchRequestCallback(const AutoLaunchRequestCallback &callback);
96 
97     // deprecated
98     DB_API static std::string GetKvStoreIdentifier(const std::string &userId, const std::string &appId,
99         const std::string &storeId, bool syncDualTupleMode = false);
100 
101     DB_API static DBStatus SetProcessSystemAPIAdapter(const std::shared_ptr<IProcessSystemApiAdapter> &adapter);
102 
103     DB_API static bool IsProcessSystemApiAdapterValid();
104 
105     DB_API static void SetStoreStatusNotifier(const StoreStatusNotifier &notifier);
106 
107     DB_API static DBStatus SetSyncActivationCheckCallback(const SyncActivationCheckCallback &callback);
108 
109     DB_API static DBStatus NotifyUserChanged();
110 private:
111 
112     // Check if the dataDir is safe arg.
113     bool IsDataDirSafe(const std::string &dataDir, std::string &canonicalDir) const;
114     bool GetKvStoreParamCheck(const std::string &storeId, const KvStoreNbDelegate::Option &option,
115         const std::function<void(DBStatus, KvStoreNbDelegate *)> &callback) const;
116     DBStatus SetObserverNotifier(KvStoreNbDelegate *kvStore, const KvStoreNbDelegate::Option &option);
117 
118     const std::string &GetKvStorePath() const;
119     static const std::string DEFAULT_PROCESS_APP_ID;
120     static std::mutex communicatorMutex_;
121     static std::shared_ptr<IProcessCommunicator> processCommunicator_;
122     static std::mutex multiUserMutex_;
123 
124     KvStoreConfig kvStoreConfig_;
125     std::string appId_;
126     std::string userId_;
127     int32_t instanceId_;
128 
129     mutable std::mutex mutex_;
130 };
131 } // namespace DistributedDB
132 
133 #endif // KV_STORE_DELEGATE_MANAGER_H
134