• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2025 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 #ifndef PLUGINS_ETS_RUNTIME_NAMESPACE_MANAGER_IMPL_H
16 #define PLUGINS_ETS_RUNTIME_NAMESPACE_MANAGER_IMPL_H
17 
18 #include <map>
19 #include "libpandabase/os/mutex.h"
20 #include <string>
21 #include <cstddef>
22 #include <libpandabase/macros.h>
23 #include <functional>
24 #if defined(PANDA_TARGET_OHOS) && !defined(PANDA_CMAKE_SDK)
25 #include <dlfcn.h>
26 #endif
27 #include "os/library_loader.h"
28 #include "plugins/ets/runtime/ets_native_library.h"
29 using CreateNamespaceCallback = std::function<bool(const std::string &bundleModuleName, std::string &namespaceName)>;
30 using ExtensionApiCheckCallback = std::function<bool(const std::string &className, const std::string &fileName)>;
31 namespace ark::ets {
32 class EtsNamespaceManagerImpl {
33 public:
34     static EtsNamespaceManagerImpl &GetInstance();
35 
36     Expected<EtsNativeLibrary, os::Error> LoadNativeLibraryFromNs(const std::string &pathKey, const char *name);
37     void RegisterNamespaceName(const std::string &key, const std::string &value);
38 
SetExtensionApiCheckCallback(const ExtensionApiCheckCallback & cb)39     void SetExtensionApiCheckCallback(const ExtensionApiCheckCallback &cb)
40     {
41         os::memory::WriteLockHolder wlh(lock_);
42         checkLibraryPermissionCallback_ = cb;
43     }
44 
GetExtensionApiCheckCallback()45     ExtensionApiCheckCallback GetExtensionApiCheckCallback() const
46     {
47         os::memory::ReadLockHolder rlh(lock_);
48         return checkLibraryPermissionCallback_;
49     }
50 
51     virtual ~EtsNamespaceManagerImpl();
52 
53     NO_COPY_SEMANTIC(EtsNamespaceManagerImpl);
54 
55     NO_MOVE_SEMANTIC(EtsNamespaceManagerImpl);
56 
57 private:
58     EtsNamespaceManagerImpl() = default;
59 
60     mutable os::memory::RWLock lock_;
61     std::map<std::string, std::string> namespaceNames_ GUARDED_BY(lock_);
62     ExtensionApiCheckCallback checkLibraryPermissionCallback_ {nullptr};
63 };
64 }  // namespace ark::ets
65 #endif  // !PLUGINS_ETS_RUNTIME_NAMESPACE_MANAGER_IMPL_H