• 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 #ifndef FOUNDATION_MODULE_MANAGER_H
17 #define FOUNDATION_MODULE_MANAGER_H
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include "third_party/v8/include/v8.h"
23 
24 #include "base/utils/noncopyable.h"
25 
26 template<typename T>
27 using CopyablePersistent = v8::Persistent<T, v8::CopyablePersistentTraits<T>>;
28 
29 namespace OHOS::Ace::Framework {
30 class ModuleManager {
31 public:
32     ModuleManager() = default;
33     ~ModuleManager() = default;
34 
35     static ModuleManager* GetInstance();
36     bool InitModule(v8::Local<v8::Object> moduleObj, const std::string& moduleName, v8::Isolate* isolate);
37 
38     static void InitTimerModule(v8::Local<v8::Context>& localContext);
39     void SetWaitTimer(const v8::FunctionCallbackInfo<v8::Value>& args, bool isInterval);
40     void ClearWaitTimer(const v8::FunctionCallbackInfo<v8::Value>& args, bool isInterval);
41     uint32_t AddCallback(CopyablePersistent<v8::Function> callbackFunc,
42         std::vector<CopyablePersistent<v8::Value>> callbackArray, v8::Isolate* callbackIsolateMap, bool isInterval);
43     void RemoveCallbackFunc(uint32_t callbackId, bool isInterval);
44     CopyablePersistent<v8::Function> GetCallbackFunc(uint32_t callbackId, bool isInterval);
45     std::vector<CopyablePersistent<v8::Value>> GetCallbackArray(uint32_t callbackId, bool isInterval);
46     v8::Isolate* GetCallbackIsolate(uint32_t callbackId, bool isInterval);
47     void ClearTimerIsolate(v8::Isolate* isolate);
48 
49 private:
50     uint32_t AddCallback(std::unordered_map<uint32_t, CopyablePersistent<v8::Function>>& callbackFuncMap,
51         std::unordered_map<uint32_t, std::vector<CopyablePersistent<v8::Value>>>& callbackArrayMap,
52         std::unordered_map<uint32_t, v8::Isolate*>& callbackIsolateMap, CopyablePersistent<v8::Function> callbackFunc,
53         std::vector<CopyablePersistent<v8::Value>> callbackArray, v8::Isolate* isolate);
54 
55     void RemoveCallbackFunc(std::unordered_map<uint32_t, CopyablePersistent<v8::Function>>& callbackFuncMap,
56         std::unordered_map<uint32_t, std::vector<CopyablePersistent<v8::Value>>>& callbackArrayMap,
57         std::unordered_map<uint32_t, v8::Isolate*>& callbackIsolateMap, uint32_t callbackId);
58 
59     std::vector<CopyablePersistent<v8::Value>> GetCallbackArray(
60         std::unordered_map<uint32_t, std::vector<CopyablePersistent<v8::Value>>>& callbackArrayMap,
61         uint32_t callbackId);
62 
63     std::unordered_map<uint32_t, CopyablePersistent<v8::Function>> callbackFuncMap_;
64     std::unordered_map<uint32_t, CopyablePersistent<v8::Function>> intervalCallbackFuncMap_;
65     std::unordered_map<uint32_t, std::vector<CopyablePersistent<v8::Value>>> callbackArrayMap_;
66     std::unordered_map<uint32_t, std::vector<CopyablePersistent<v8::Value>>> intervalCallbackArrayMap_;
67     std::unordered_map<uint32_t, v8::Isolate*> callbackIsolateMap_;
68     std::unordered_map<uint32_t, v8::Isolate*> intervalCallbackIsolateMap_;
69     uint32_t callbackId_ = 0;
70 
71     ACE_DISALLOW_COPY_AND_MOVE(ModuleManager);
72 };
73 } // namespace OHOS::Ace::Framework
74 
75 #endif // FOUNDATION_MODULE_MANAGER_H
76