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 JSWATCHER_H 17 #define JSWATCHER_H 18 19 #include "distributed_objectstore.h" 20 #include "flat_object_store.h" 21 #include "napi/native_api.h" 22 #include "napi/native_node_api.h" 23 #include "uv_queue.h" 24 25 namespace OHOS::ObjectStore { 26 class JSWatcher; 27 struct EventHandler { 28 napi_ref callbackRef = nullptr; 29 EventHandler *next = nullptr; 30 }; 31 32 class EventListener { 33 public: EventListener()34 EventListener() : handlers_(nullptr) 35 { 36 } 37 ~EventListener()38 virtual ~EventListener() 39 { 40 } 41 42 virtual bool Add(napi_env env, napi_value handler); 43 44 virtual bool Del(napi_env env, napi_value handler); 45 46 virtual void Clear(napi_env env); 47 48 EventHandler *Find(napi_env env, napi_value handler); 49 EventHandler *handlers_; 50 }; 51 52 class ChangeEventListener : public EventListener { 53 public: 54 ChangeEventListener(JSWatcher *watcher, DistributedObjectStore *objectStore, DistributedObject *object); 55 56 bool Add(napi_env env, napi_value handler) override; 57 58 bool Del(napi_env env, napi_value handler) override; 59 60 void Clear(napi_env env) override; 61 62 private: 63 bool isWatched_ = false; 64 DistributedObjectStore *objectStore_; 65 DistributedObject *object_; 66 JSWatcher *watcher_; 67 }; 68 69 class StatusEventListener : public EventListener { 70 public: 71 StatusEventListener(JSWatcher *watcher, const std::string &sessionId); 72 bool Add(napi_env env, napi_value handler) override; 73 74 bool Del(napi_env env, napi_value handler) override; 75 76 void Clear(napi_env env) override; 77 78 private: 79 JSWatcher *watcher_; 80 std::string sessionId_; 81 }; 82 83 class JSWatcher : public UvQueue { 84 public: 85 JSWatcher(const napi_env env, DistributedObjectStore *objectStore, DistributedObject *object); 86 87 ~JSWatcher(); 88 89 bool On(const char *type, napi_value handler); 90 91 void Off(const char *type, napi_value handler = nullptr); 92 93 void Emit(const char *type, const std::string &sessionId, const std::vector<std::string> &changeData); 94 95 void Emit(const char *type, const std::string &sessionId, const std::string &networkId, const std::string &status); 96 97 private: 98 struct ChangeArgs { 99 ChangeArgs(const napi_ref callback, const std::string &sessionId, const std::vector<std::string> &changeData); 100 napi_ref callback_; 101 const std::string sessionId_; 102 const std::vector<std::string> changeData_; 103 }; 104 struct StatusArgs { 105 StatusArgs(const napi_ref callback, const std::string &sessionId, const std::string &networkId, 106 const std::string &status); 107 napi_ref callback_; 108 const std::string sessionId_; 109 const std::string networkId_; 110 const std::string status_; 111 }; 112 EventListener *Find(const char *type); 113 static void ProcessChange(napi_env env, std::list<void *> &args); 114 static void ProcessStatus(napi_env env, std::list<void *> &args); 115 napi_env env_; 116 ChangeEventListener *changeEventListener_; 117 StatusEventListener *statusEventListener_; 118 }; 119 120 class WatcherImpl : public ObjectWatcher { 121 public: WatcherImpl(JSWatcher * watcher)122 WatcherImpl(JSWatcher *watcher) : watcher_(watcher) 123 { 124 } 125 126 virtual ~WatcherImpl(); 127 128 void OnChanged(const std::string &sessionid, const std::vector<std::string> &changedData) override; 129 130 private: 131 JSWatcher *watcher_ = nullptr; 132 }; 133 } // namespace OHOS::ObjectStore 134 135 #endif // JSWATCHER_H 136