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
~JSLocalStorage()27 JSLocalStorage::~JSLocalStorage() {}
28
JSBind(BindingTarget globalObj)29 void JSLocalStorage::JSBind(BindingTarget globalObj)
30 {
31 JSClass<JSLocalStorage>::Declare("NativeLocalStorage");
32 JSClass<JSLocalStorage>::StaticMethod("GetShared", JSLocalStorage::GetShared);
33 JSClass<JSLocalStorage>::Bind(globalObj, ConstructorCallback, DestructorCallback);
34 }
35
ConstructorCallback(const JSCallbackInfo & info)36 void JSLocalStorage::ConstructorCallback(const JSCallbackInfo& info)
37 {
38 auto* instance = new JSLocalStorage();
39 instance->IncRefCount();
40 info.SetReturnValue(instance);
41 }
42
DestructorCallback(JSLocalStorage * instance)43 void JSLocalStorage::DestructorCallback(JSLocalStorage* instance)
44 {
45 instance->DecRefCount();
46 }
47
AddStorage(int32_t key,const JSRef<JSObject> & value)48 void JSLocalStorage::AddStorage(int32_t key, const JSRef<JSObject>& value)
49 {
50 if (storages_.find(key) != storages_.end()) {
51 LOGW("A local storage already exists for key %d", key);
52 return;
53 }
54
55 storages_.emplace(key, value);
56 }
57
RemoveStorage(int32_t key)58 void JSLocalStorage::RemoveStorage(int32_t key)
59 {
60 auto it = storages_.find(key);
61 if (it != storages_.end()) {
62 storages_.erase(it);
63 } else {
64 LOGW("A local storage with key %{public}d does not exist!", key);
65 }
66 }
67
GetShared(const JSCallbackInfo & info)68 void JSLocalStorage::GetShared(const JSCallbackInfo& info)
69 {
70 int32_t currentInstance = Container::CurrentIdSafely();
71 if (currentInstance >= MIN_SUBCONTAINER_ID && currentInstance < MIN_PLUGIN_SUBCONTAINER_ID) {
72 currentInstance = SubwindowManager::GetInstance()->GetParentContainerId(currentInstance);
73 }
74 auto it = storages_.find(currentInstance);
75 if (it == storages_.end()) {
76 LOGW("LocalStorage with ID %{public}d not found!", currentInstance);
77 return;
78 }
79 info.SetReturnValue(it->second);
80 }
81
82 } // namespace OHOS::Ace::Framework
83