1 /*
2 * Copyright (c) 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 #include "frameworks/bridge/declarative_frontend/jsview/js_local_storage.h"
16
17 #include "base/subwindow/subwindow_manager.h"
18 #include "frameworks/bridge/declarative_frontend/engine/bindings.h"
19 #include "frameworks/core/common/container.h"
20
21 namespace OHOS::Ace::Framework {
22
23 thread_local std::unordered_map<int32_t, JSRef<JSObject>> JSLocalStorage::storages_;
24
JSLocalStorage()25 JSLocalStorage::JSLocalStorage()
26 {
27 LOGD("JSLocalStorage: JSLocalStorage()");
28 }
29
~JSLocalStorage()30 JSLocalStorage::~JSLocalStorage()
31 {
32 LOGD("JSLocalStorage: ~JSLocalStorage()");
33 }
34
JSBind(BindingTarget globalObj)35 void JSLocalStorage::JSBind(BindingTarget globalObj)
36 {
37 JSClass<JSLocalStorage>::Declare("NativeLocalStorage");
38 JSClass<JSLocalStorage>::StaticMethod("GetShared", JSLocalStorage::GetShared);
39 JSClass<JSLocalStorage>::Bind(globalObj, ConstructorCallback, DestructorCallback);
40 }
41
ConstructorCallback(const JSCallbackInfo & info)42 void JSLocalStorage::ConstructorCallback(const JSCallbackInfo& info)
43 {
44 auto* instance = new JSLocalStorage();
45 instance->IncRefCount();
46 info.SetReturnValue(instance);
47 }
48
DestructorCallback(JSLocalStorage * instance)49 void JSLocalStorage::DestructorCallback(JSLocalStorage* instance)
50 {
51 instance->DecRefCount();
52 }
53
AddStorage(int32_t key,const JSRef<JSObject> & value)54 void JSLocalStorage::AddStorage(int32_t key, const JSRef<JSObject>& value)
55 {
56 if (storages_.find(key) != storages_.end()) {
57 LOGW("A local storage already exists for key %d", key);
58 return;
59 }
60
61 storages_.emplace(key, value);
62 }
63
RemoveStorage(int32_t key)64 void JSLocalStorage::RemoveStorage(int32_t key)
65 {
66 auto it = storages_.find(key);
67 if (it != storages_.end()) {
68 storages_.erase(it);
69 } else {
70 LOGW("A local storage with key %{public}d does not exist!", key);
71 }
72 }
73
GetShared(const JSCallbackInfo & info)74 void JSLocalStorage::GetShared(const JSCallbackInfo& info)
75 {
76 int32_t currentInstance = Container::CurrentId();
77 if (currentInstance >= MIN_SUBCONTAINER_ID && currentInstance < MIN_PLUGIN_SUBCONTAINER_ID) {
78 currentInstance = SubwindowManager::GetInstance()->GetParentContainerId(currentInstance);
79 }
80 LOGD("Current ID is %{public}d", currentInstance);
81 auto it = storages_.find(currentInstance);
82 if (it == storages_.end()) {
83 LOGW("LocalStorage with ID %{public}d not found!", currentInstance);
84 return;
85 }
86 LOGI("JSLocalStorage::GetShared find ID:%{public}d", currentInstance);
87 info.SetReturnValue(it->second);
88 }
89
90 } // namespace OHOS::Ace::Framework