1 /*
2  * Copyright (c) 2021-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 "bridge/declarative_frontend/jsview/js_persistent.h"
17 
18 #include "base/memory/referenced.h"
19 #include "core/common/ace_engine.h"
20 #include "core/common/container.h"
21 #include "core/common/storage/storage_proxy.h"
22 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
23 #include "frameworks/bridge/declarative_frontend/jsview/js_container_base.h"
24 
25 namespace OHOS::Ace::Framework {
26 constexpr int32_t DATA_REQUIRED_ARGS = 2;
27 
JSBind(BindingTarget globalObj)28 void JSPersistent::JSBind(BindingTarget globalObj)
29 {
30     JSClass<JSPersistent>::Declare("Storage");
31     JSClass<JSPersistent>::CustomMethod("set", &JSPersistent::Set);
32     JSClass<JSPersistent>::CustomMethod("get", &JSPersistent::Get);
33     JSClass<JSPersistent>::CustomMethod("has", &JSPersistent::Has);
34     JSClass<JSPersistent>::CustomMethod("clear", &JSPersistent::Clear);
35     JSClass<JSPersistent>::CustomMethod("delete", &JSPersistent::Delete);
36     JSClass<JSPersistent>::Bind(globalObj, JSPersistent::ConstructorCallback, JSPersistent::DestructorCallback);
37 }
38 
ConstructorCallback(const JSCallbackInfo & args)39 void JSPersistent::ConstructorCallback(const JSCallbackInfo& args)
40 {
41     bool needCrossThread = false;
42     if (args.Length() > 0 && args[0]->IsBoolean()) {
43         needCrossThread = args[0]->ToBoolean();
44     }
45     std::string fileName;
46     auto persistent = Referenced::MakeRefPtr<JSPersistent>(needCrossThread, fileName);
47     persistent->IncRefCount();
48     args.SetReturnValue(Referenced::RawPtr(persistent));
49 }
50 
DestructorCallback(JSPersistent * persistent)51 void JSPersistent::DestructorCallback(JSPersistent* persistent)
52 {
53     if (persistent != nullptr) {
54         persistent->DecRefCount();
55     }
56 }
57 
Set(const JSCallbackInfo & args)58 void JSPersistent::Set(const JSCallbackInfo& args)
59 {
60 #if defined(PREVIEW)
61     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
62         "emulator or a real device instead.");
63     return;
64 #endif
65     if (args.Length() < DATA_REQUIRED_ARGS || !args[0]->IsString()) {
66         LOGW("JSPersistent: Fail to set persistent data, args too few or key type is not a string");
67         return;
68     }
69     std::string key = args[0]->ToString();
70     auto serializedValue = JSON::Stringify(args.GetVm(), args[1].Get().GetLocalHandle());
71     std::string value = serializedValue->ToString(args.GetVm())->ToString(args.GetVm());
72     if (!StorageProxy::GetInstance()->GetStorage()) {
73         LOGW("no storage available");
74         return;
75     }
76     StorageProxy::GetInstance()->GetStorage()->SetString(key, value);
77 }
78 
Get(const JSCallbackInfo & args)79 void JSPersistent::Get(const JSCallbackInfo& args)
80 {
81 #if defined(PREVIEW)
82     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
83         "emulator or a real device instead.");
84     return;
85 #endif
86     if (args.Length() < 1 || !args[0]->IsString()) {
87         return;
88     }
89     auto storage = StorageProxy::GetInstance()->GetStorage();
90     if (!storage) {
91         LOGW("no storage available");
92         return;
93     }
94     std::string key = args[0]->ToString();
95     std::string value = storage->GetString(key);
96     if (value.empty() || value == "undefined") {
97         args.SetReturnValue(JSVal::Undefined());
98         return;
99     }
100     JSRef<JSObject> obj = JSRef<JSObject>::New();
101     JSRef<JSVal> ret = obj->ToJsonObject(value.c_str());
102     args.SetReturnValue(ret);
103 }
104 
Has(const JSCallbackInfo & args)105 void JSPersistent::Has(const JSCallbackInfo& args)
106 {
107 #if defined(PREVIEW)
108     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
109         "emulator or a real device instead.");
110     return;
111 #endif
112     if (args.Length() < 1 || !args[0]->IsString()) {
113         LOGW("JSPersistent: Failed to Get persistent data, args too few");
114         return;
115     }
116     std::string key = args[0]->ToString();
117     if (!StorageProxy::GetInstance()->GetStorage()) {
118         LOGW("no storage available");
119         return;
120     }
121     std::string value = StorageProxy::GetInstance()->GetStorage()->GetString(key);
122     args.SetReturnValue(value.empty()? JSVal::False() : JSVal::True());
123 }
124 
Delete(const JSCallbackInfo & args)125 void JSPersistent::Delete(const JSCallbackInfo& args)
126 {
127 #if defined(PREVIEW)
128     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
129         "emulator or a real device instead.");
130     return;
131 #endif
132     if (args.Length() < 1 || !args[0]->IsString()) {
133         return;
134     }
135     std::string key = args[0]->ToString();
136     if (!StorageProxy::GetInstance()->GetStorage()) {
137         LOGW("no storage available");
138         return;
139     }
140     StorageProxy::GetInstance()->GetStorage()->Delete(key);
141 }
142 
Clear(const JSCallbackInfo & args)143 void JSPersistent::Clear(const JSCallbackInfo& args)
144 {
145 #if defined(PREVIEW)
146     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
147         "emulator or a real device instead.");
148     return;
149 #endif
150     if (!StorageProxy::GetInstance()->GetStorage()) {
151         LOGW("no storage available");
152         return;
153     }
154     StorageProxy::GetInstance()->GetStorage()->Clear();
155 }
156 
157 } // namespace OHOS::Ace::Framework