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 "uv_queue.h" 21 22 namespace OHOS::ObjectStore { 23 class JSWatcher; 24 struct EventHandler { 25 napi_ref callbackRef = nullptr; 26 EventHandler *next = nullptr; 27 }; 28 29 class EventListener { 30 public: EventListener()31 EventListener() : handlers_(nullptr) 32 { 33 } 34 ~EventListener()35 virtual ~EventListener() 36 { 37 } 38 IsEmpty()39 bool IsEmpty() 40 { 41 return handlers_ == nullptr; 42 } 43 44 virtual bool Add(napi_env env, napi_value handler); 45 46 virtual bool Del(napi_env env, napi_value handler); 47 48 virtual void Clear(napi_env env); 49 50 EventHandler *Find(napi_env env, napi_value handler); 51 EventHandler *handlers_ = nullptr; 52 }; 53 54 class ChangeEventListener : public EventListener { 55 public: 56 ChangeEventListener( 57 std::weak_ptr<JSWatcher> watcher, DistributedObjectStore *objectStore, DistributedObject *object); 58 59 bool Add(napi_env env, napi_value handler) override; 60 61 bool Del(napi_env env, napi_value handler) override; 62 63 void Clear(napi_env env) override; 64 private: 65 bool isWatched_ = false; 66 DistributedObjectStore *objectStore_ = nullptr; 67 DistributedObject *object_ = nullptr; 68 std::weak_ptr<JSWatcher> watcher_; 69 }; 70 71 class StatusEventListener : public EventListener { 72 public: 73 StatusEventListener(std::weak_ptr<JSWatcher> watcher, const std::string &sessionId); 74 bool Add(napi_env env, napi_value handler) override; 75 76 bool Del(napi_env env, napi_value handler) override; 77 78 void Clear(napi_env env) override; 79 private: 80 std::weak_ptr<JSWatcher> watcher_; 81 std::string sessionId_; 82 }; 83 84 class ProgressEventListener : public EventListener { 85 public: 86 ProgressEventListener(std::weak_ptr<JSWatcher> watcher, const std::string &sessionId); 87 bool Add(napi_env env, napi_value handler) override; 88 89 bool Del(napi_env env, napi_value handler) override; 90 91 void Clear(napi_env env) override; 92 93 private: 94 std::weak_ptr<JSWatcher> watcher_; 95 std::string sessionId_; 96 }; 97 98 class JSWatcher : public UvQueue { 99 public: 100 JSWatcher(const napi_env env, DistributedObjectStore *objectStore, DistributedObject *object); 101 102 ~JSWatcher(); 103 104 bool On(const char *type, napi_value handler); 105 106 void Off(const char *type, napi_value handler = nullptr); 107 108 void Emit(const char *type, const std::string &sessionId, const std::vector<std::string> &changeData); 109 110 void Emit(const char *type, const std::string &sessionId, const std::string &networkId, const std::string &status); 111 112 void Emit(const char *type, const std::string &sessionId, int32_t progress); 113 114 bool IsEmpty(); 115 116 void SetListener(ChangeEventListener *changeEventListener, StatusEventListener *statusEventListener, 117 ProgressEventListener *progressEventListener); 118 119 private: 120 struct ChangeArgs { 121 ChangeArgs(const napi_ref callback, const std::string &sessionId, const std::vector<std::string> &changeData); 122 napi_ref callback_; 123 const std::string sessionId_; 124 const std::vector<std::string> changeData_; 125 }; 126 struct StatusArgs { 127 StatusArgs(const napi_ref callback, const std::string &sessionId, const std::string &networkId, 128 const std::string &status); 129 napi_ref callback_; 130 const std::string sessionId_; 131 const std::string networkId_; 132 const std::string status_; 133 }; 134 struct ProgressArgs { 135 ProgressArgs(const napi_ref callback, const std::string &sessionId, int32_t progress); 136 napi_ref callback_; 137 const std::string sessionId_; 138 int32_t progress_ = 0; 139 }; 140 EventListener *Find(const char *type); 141 static void ProcessChange(napi_env env, std::list<void *> &args); 142 static void ProcessStatus(napi_env env, std::list<void *> &args); 143 static void ProcessProgress(napi_env env, std::list<void *> &args); 144 napi_env env_; 145 ChangeEventListener *changeEventListener_ = nullptr; 146 StatusEventListener *statusEventListener_ = nullptr; 147 ProgressEventListener *progressEventListener_ = nullptr; 148 }; 149 150 class WatcherImpl : public ObjectWatcher { 151 public: WatcherImpl(std::weak_ptr<JSWatcher> watcher)152 WatcherImpl(std::weak_ptr<JSWatcher> watcher) : watcher_(watcher) 153 { 154 } 155 156 virtual ~WatcherImpl(); 157 158 void OnChanged(const std::string &sessionid, const std::vector<std::string> &changedData) override; 159 160 private: 161 std::weak_ptr<JSWatcher> watcher_; 162 }; 163 } // namespace OHOS::ObjectStore 164 165 #endif // JSWATCHER_H 166