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