• 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 #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         return;
65     }
66     std::string key = args[0]->ToString();
67     auto serializedValue = JSON::Stringify(args.GetVm(), args[1].Get().GetLocalHandle());
68     std::string value = serializedValue->ToString(args.GetVm())->ToString();
69     auto container = Container::Current();
70     if (!container) {
71         return;
72     }
73     auto executor = container->GetTaskExecutor();
74     if(!StorageProxy::GetInstance()->GetStorage(executor)) {
75         LOGW("no storage available");
76         return;
77     }
78     StorageProxy::GetInstance()->GetStorage(executor)->SetString(key, value);
79     AceEngine::Get().NotifyContainers(
80         [currInstanceId = container->GetInstanceId(), key, value](const RefPtr<Container>& container) {
81         if (container && container->GetInstanceId() != currInstanceId) {
82             container->NotifyAppStorage(key, value);
83         }
84     });
85 }
86 
Get(const JSCallbackInfo & args)87 void JSPersistent::Get(const JSCallbackInfo& args)
88 {
89 #if defined(PREVIEW)
90     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
91         "emulator or a real device instead.");
92     return;
93 #endif
94     if (args.Length() < 1 || !args[0]->IsString()) {
95         return;
96     }
97     std::string key = args[0]->ToString();
98     auto container = Container::Current();
99     if (!container) {
100         return;
101     }
102     auto executor = container->GetTaskExecutor();
103     std::string value = StorageProxy::GetInstance()->GetStorage(executor)->GetString(key);
104     if (value.empty()) {
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 
Delete(const JSCallbackInfo & args)113 void JSPersistent::Delete(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         return;
122     }
123     std::string key = args[0]->ToString();
124     auto container = Container::Current();
125     if (!container) {
126         return;
127     }
128     auto executor = container->GetTaskExecutor();
129     StorageProxy::GetInstance()->GetStorage(executor)->Delete(key);
130 }
131 
Clear(const JSCallbackInfo & args)132 void JSPersistent::Clear(const JSCallbackInfo& args)
133 {
134 #if defined(PREVIEW)
135     LOGW("[Engine Log] Unable to use the PersistentStorage in the Previewer. Perform this operation on the "
136         "emulator or a real device instead.");
137     return;
138 #endif
139     auto container = Container::Current();
140     if (!container) {
141         return;
142     }
143     auto executor = container->GetTaskExecutor();
144     StorageProxy::GetInstance()->GetStorage(executor)->Clear();
145 }
146 
147 } // namespace OHOS::Ace::Framework