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 NAPI_RDB_OBSERVER_H 17 #define NAPI_RDB_OBSERVER_H 18 19 #include <uv.h> 20 21 #include "datashare_template.h" 22 #include "napi/native_api.h" 23 #include "napi/native_common.h" 24 #include "napi/native_node_api.h" 25 #include "dataproxy_handle_common.h" 26 27 namespace OHOS { 28 namespace DataShare { 29 class NapiObserver : public std::enable_shared_from_this<NapiObserver> { 30 public: 31 NapiObserver(napi_env env, napi_value callback); 32 void RegisterEnvCleanHook(); 33 virtual ~NapiObserver(); 34 virtual bool operator==(const NapiObserver &rhs) const; 35 virtual bool operator!=(const NapiObserver &rhs) const; 36 NapiObserver& operator=(NapiObserver &&rhs) = default; 37 protected: 38 struct ObserverWorker { 39 std::weak_ptr<NapiObserver> observer_; 40 std::function<napi_value(napi_env)> getParam; ObserverWorkerObserverWorker41 explicit ObserverWorker(std::shared_ptr<NapiObserver> observerIn) : observer_(observerIn) {} 42 }; 43 struct ObserverEnvHookWorker { 44 std::weak_ptr<NapiObserver> observer_; ObserverEnvHookWorkerObserverEnvHookWorker45 ObserverEnvHookWorker(std::shared_ptr<NapiObserver> observerIn): observer_(observerIn) {} 46 }; 47 static void CallbackFunc(ObserverWorker *observerWorker); 48 static void CleanEnv(void *obj); 49 napi_env env_ = nullptr; 50 napi_ref ref_ = nullptr; 51 uv_loop_s *loop_ = nullptr; 52 std::unique_ptr<std::mutex> envMutexPtr_; 53 ObserverEnvHookWorker* observerEnvHookWorker_ = nullptr; 54 }; 55 56 class NapiRdbObserver final: public NapiObserver { 57 public: NapiRdbObserver(napi_env env,napi_value callback)58 NapiRdbObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {}; 59 void OnChange(const RdbChangeNode &changeNode); 60 }; 61 62 class NapiPublishedObserver final: public NapiObserver { 63 public: NapiPublishedObserver(napi_env env,napi_value callback)64 NapiPublishedObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {}; 65 void OnChange(PublishedDataChangeNode &changeNode); 66 }; 67 68 class NapiProxyDataObserver final: public NapiObserver { 69 public: NapiProxyDataObserver(napi_env env,napi_value callback)70 NapiProxyDataObserver(napi_env env, napi_value callback) : NapiObserver(env, callback) {}; 71 void OnChange(const std::vector<DataProxyChangeInfo> &changeNode); 72 }; 73 } // namespace DataShare 74 } // namespace OHOS 75 #endif //NAPI_RDB_OBSERVER_H 76