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 #ifndef OHOS_KV_MANAGER_H
16 #define OHOS_KV_MANAGER_H
17 #include <mutex>
18 #include "distributed_kv_data_manager.h"
19 #include "kvstore_death_recipient.h"
20 #include "napi_queue.h"
21 #include "uv_queue.h"
22 #include "js_observer.h"
23 
24 namespace OHOS::DistributedKVStore {
25 struct ContextParam {
26     std::string baseDir = "";
27     std::string hapName = "";
28     int32_t area = DistributedKv::Area::EL1;
29     bool isSystemApp = false;
30 };
31 class JsKVManager {
32 public:
33     JsKVManager(const std::string &bundleName, napi_env env, ContextParam param);
34     ~JsKVManager();
35 
36     static napi_value CreateKVManager(napi_env env, napi_callback_info info);
37 
38     static napi_value Constructor(napi_env env);
39 
40     static napi_value New(napi_env env, napi_callback_info info);
41 
42 private:
43     static napi_value GetKVStore(napi_env env, napi_callback_info info);
44     static napi_value CloseKVStore(napi_env env, napi_callback_info info);
45     static napi_value DeleteKVStore(napi_env env, napi_callback_info info);
46     static napi_value GetAllKVStoreId(napi_env env, napi_callback_info info);
47     static napi_value On(napi_env env, napi_callback_info info);
48     static napi_value Off(napi_env env, napi_callback_info info);
49 
50 private:
51     class DeathRecipient : public DistributedKv::KvStoreDeathRecipient, public JSObserver {
52     public:
DeathRecipient(std::shared_ptr<UvQueue> uvQueue,napi_value callback)53         DeathRecipient(std::shared_ptr<UvQueue> uvQueue, napi_value callback) : JSObserver(uvQueue, callback) {};
54         virtual ~DeathRecipient() = default;
55         void OnRemoteDied() override;
56     };
57 
58     DistributedKv::DistributedKvDataManager kvDataManager_ {};
59     std::string bundleName_ {};
60     std::mutex deathMutex_ {};
61     std::list<std::shared_ptr<DeathRecipient>> deathRecipient_ {};
62     std::shared_ptr<UvQueue> uvQueue_;
63     std::shared_ptr<ContextParam> param_;
64     static constexpr int MAX_APP_ID_LEN = 256;
65 };
66 } // namespace OHOS::DistributedKVStore
67 #endif // OHOS_KV_MANAGER_H
68