• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
24 namespace OHOS::Ace::Framework {
25 constexpr int32_t DATA_REQUIRED_ARGS = 2;
26 constexpr int32_t ARGS_AREAMODE = 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     int32_t areaMode = -1;
72     if (args.Length() > DATA_REQUIRED_ARGS && args[ARGS_AREAMODE]->IsNumber()) {
73         areaMode = args[ARGS_AREAMODE]->ToNumber<int32_t>();
74     }
75     std::string value = serializedValue->ToString(args.GetVm())->ToString(args.GetVm());
76     if (!StorageProxy::GetInstance()->GetStorage(areaMode)) {
77         LOGW("no storage available");
78         return;
79     }
80     StorageProxy::GetInstance()->GetStorage(areaMode)->SetString(key, value);
81 }
82 
Get(const JSCallbackInfo & args)83 void JSPersistent::Get(const JSCallbackInfo& args)
84 {
85 #if defined(PREVIEW)
86     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
87         "emulator or a real device instead.");
88     return;
89 #endif
90     if (args.Length() < 1 || !args[0]->IsString()) {
91         return;
92     }
93     int32_t areaMode = -1;
94     if (args.Length() > 1 && args[1]->IsNumber()) {
95         areaMode = args[1]->ToNumber<int32_t>();
96     }
97     auto storage = StorageProxy::GetInstance()->GetStorage(areaMode);
98     if (!storage) {
99         LOGW("no storage available");
100         return;
101     }
102     std::string key = args[0]->ToString();
103     std::string value = storage->GetString(key);
104     if (value.empty() || value == "undefined") {
105         args.SetReturnValue(JSVal::Undefined());
106         return;
107     }
108     JSRef<JSObject> obj = JSRef<JSObject>::New();
109     JSRef<JSVal> ret = obj->ToJsonObject(value.c_str());
110     args.SetReturnValue(ret);
111 }
112 
Has(const JSCallbackInfo & args)113 void JSPersistent::Has(const JSCallbackInfo& args)
114 {
115 #if defined(PREVIEW)
116     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
117         "emulator or a real device instead.");
118     return;
119 #endif
120     if (args.Length() < 1 || !args[0]->IsString()) {
121         LOGW("JSPersistent: Failed to Get persistent data, args too few");
122         return;
123     }
124     int32_t areaMode = -1;
125     if (args.Length() > 1 && args[1]->IsNumber()) {
126         areaMode = args[1]->ToNumber<int32_t>();
127     }
128     std::string key = args[0]->ToString();
129     if (!StorageProxy::GetInstance()->GetStorage(areaMode)) {
130         LOGW("no storage available");
131         return;
132     }
133     std::string value = StorageProxy::GetInstance()->GetStorage(areaMode)->GetString(key);
134     args.SetReturnValue(value.empty()? JSVal::False() : JSVal::True());
135 }
136 
Delete(const JSCallbackInfo & args)137 void JSPersistent::Delete(const JSCallbackInfo& args)
138 {
139 #if defined(PREVIEW)
140     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
141         "emulator or a real device instead.");
142     return;
143 #endif
144     if (args.Length() < 1 || !args[0]->IsString()) {
145         return;
146     }
147     std::string key = args[0]->ToString();
148     int32_t areaMode = -1;
149     if (args.Length() > 1 && args[1]->IsNumber()) {
150         areaMode = args[1]->ToNumber<int32_t>();
151     }
152 
153     if (!StorageProxy::GetInstance()->GetStorage(areaMode)) {
154         LOGW("no storage available");
155         return;
156     }
157     StorageProxy::GetInstance()->GetStorage(areaMode)->Delete(key);
158 }
159 
Clear(const JSCallbackInfo & args)160 void JSPersistent::Clear(const JSCallbackInfo& args)
161 {
162 #if defined(PREVIEW)
163     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
164         "emulator or a real device instead.");
165     return;
166 #endif
167     if (!StorageProxy::GetInstance()->GetStorage()) {
168         LOGW("no storage available");
169         return;
170     }
171     StorageProxy::GetInstance()->GetStorage()->Clear();
172 }
173 
174 } // namespace OHOS::Ace::Framework