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 #include "js_object_wrapper.h"
17
18 #include "logger.h"
19 namespace OHOS::ObjectStore {
JSObjectWrapper(DistributedObjectStore * objectStore,DistributedObject * object)20 JSObjectWrapper::JSObjectWrapper(DistributedObjectStore *objectStore, DistributedObject *object)
21 : objectStore_(objectStore), object_(object)
22 {
23 }
24
~JSObjectWrapper()25 JSObjectWrapper::~JSObjectWrapper()
26 {
27 LOG_INFO("JSObjectWrapper::~JSObjectWrapper");
28 std::unique_lock<std::shared_mutex> cacheLock(watchMutex_);
29 if (watcher_ != nullptr) {
30 watcher_ = nullptr;
31 }
32 LOG_INFO("JSObjectWrapper::~JSObjectWrapper end");
33 }
34
GetObject()35 DistributedObject *JSObjectWrapper::GetObject()
36 {
37 return object_;
38 }
39
AddWatch(napi_env env,const char * type,napi_value handler)40 bool JSObjectWrapper::AddWatch(napi_env env, const char *type, napi_value handler)
41 {
42 std::unique_lock<std::shared_mutex> cacheLock(watchMutex_);
43 if (watcher_ == nullptr) {
44 watcher_ = std::make_shared<JSWatcher>(env, objectStore_, object_);
45 if (watcher_ == nullptr) {
46 LOG_ERROR("JSObjectWrapper::new JSWatcher fail");
47 return false;
48 }
49 }
50 return watcher_->On(type, handler);
51 }
52
DeleteWatch(napi_env env,const char * type,napi_value handler)53 void JSObjectWrapper::DeleteWatch(napi_env env, const char *type, napi_value handler)
54 {
55 std::unique_lock<std::shared_mutex> cacheLock(watchMutex_);
56 if (watcher_ != nullptr) {
57 watcher_->Off(type, handler);
58 LOG_INFO("JSObjectWrapper::DeleteWatch %{public}s", type);
59 if (watcher_->IsEmpty()) {
60 LOG_DEBUG("delete JSWatcher");
61 watcher_ = nullptr;
62 }
63 } else {
64 LOG_ERROR("JSObjectWrapper::DeleteWatch watcher_ is null");
65 }
66 }
67
isUndefined(char * value)68 bool JSObjectWrapper::isUndefined(char *value)
69 {
70 std::string tmpStr = value;
71 auto it = std::find(undefinedProperties.begin(), undefinedProperties.end(), tmpStr);
72 if (it == undefinedProperties.end()) {
73 return false;
74 }
75 return true;
76 }
77
AddUndefined(char * value)78 void JSObjectWrapper::AddUndefined(char *value)
79 {
80 std::string tmpStr = value;
81 if (std::find(undefinedProperties.begin(), undefinedProperties.end(), tmpStr) == undefinedProperties.end()) {
82 undefinedProperties.push_back(tmpStr);
83 }
84 }
85
DeleteUndefined(char * value)86 void JSObjectWrapper::DeleteUndefined(char *value)
87 {
88 std::string tmpStr = value;
89 auto it = std::find(undefinedProperties.begin(), undefinedProperties.end(), tmpStr);
90 if (it != undefinedProperties.end()) {
91 undefinedProperties.erase(it);
92 }
93 }
94
DestroyObject()95 void JSObjectWrapper::DestroyObject()
96 {
97 object_ = nullptr;
98 }
99
SetObjectId(const std::string & objectId)100 void JSObjectWrapper::SetObjectId(const std::string &objectId)
101 {
102 objectId_ = objectId;
103 }
104
GetObjectId()105 std::string JSObjectWrapper::GetObjectId()
106 {
107 return objectId_;
108 }
109
110 } // namespace OHOS::ObjectStore