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) {
64 LOGW("JSPersistent: Fail to set persistent data, args too few");
65 return;
66 }
67 std::string key = args[0]->ToString();
68 std::string value = args[1]->ToString();
69 auto container = Container::Current();
70 if (!container) {
71 LOGW("container is null");
72 return;
73 }
74 auto executor = container->GetTaskExecutor();
75 if(!StorageProxy::GetInstance()->GetStorage(executor)) {
76 return;
77 }
78 StorageProxy::GetInstance()->GetStorage(executor)->SetString(key, value);
79 LOGD("cross window notify, containerId=%{private}d", container->GetInstanceId());
80 AceEngine::Get().NotifyContainers(
81 [currInstanceId = container->GetInstanceId(), key, value](const RefPtr<Container>& container) {
82 if (container && container->GetInstanceId() != currInstanceId) {
83 container->NotifyAppStorage(key, value);
84 }
85 });
86 }
87
Get(const JSCallbackInfo & args)88 void JSPersistent::Get(const JSCallbackInfo& args)
89 {
90 #if defined(PREVIEW)
91 LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
92 "emulator or a real device instead.");
93 return;
94 #endif
95 if (args.Length() < 1) {
96 LOGW("JSPersistent: Failed to Get persistent data, args too few");
97 return;
98 }
99 std::string key = args[0]->ToString();
100 auto container = Container::Current();
101 if (!container) {
102 LOGW("container is null");
103 return;
104 }
105 auto executor = container->GetTaskExecutor();
106 std::string value = StorageProxy::GetInstance()->GetStorage(executor)->GetString(key);
107 auto returnValue = JSVal(ToJSValue(value));
108 auto returnPtr = JSRef<JSVal>::Make(returnValue);
109 args.SetReturnValue(returnPtr);
110 }
111
Delete(const JSCallbackInfo & args)112 void JSPersistent::Delete(const JSCallbackInfo& args)
113 {
114 #if defined(PREVIEW)
115 LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
116 "emulator or a real device instead.");
117 return;
118 #endif
119 if (args.Length() < 1) {
120 LOGW("JSPersistent: Fail to Delete persistent data, args too few");
121 return;
122 }
123 std::string key = args[0]->ToString();
124 auto container = Container::Current();
125 if (!container) {
126 LOGW("container is null");
127 return;
128 }
129 auto executor = container->GetTaskExecutor();
130 StorageProxy::GetInstance()->GetStorage(executor)->Delete(key);
131 }
132
Clear(const JSCallbackInfo & args)133 void JSPersistent::Clear(const JSCallbackInfo& args)
134 {
135 #if defined(PREVIEW)
136 LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
137 "emulator or a real device instead.");
138 return;
139 #endif
140 auto container = Container::Current();
141 if (!container) {
142 LOGW("container is null");
143 return;
144 }
145 auto executor = container->GetTaskExecutor();
146 StorageProxy::GetInstance()->GetStorage(executor)->Clear();
147 }
148
149 } // namespace OHOS::Ace::Framework