• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_MODULES_QJS_MODULE_MANAGER_H
17 #define FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_MODULES_QJS_MODULE_MANAGER_H
18 
19 #include <string>
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "third_party/quickjs/quickjs.h"
24 
25 #include "base/utils/noncopyable.h"
26 
27 namespace OHOS::Ace::Framework {
28 class ModuleManager {
29 public:
30     ModuleManager() = default;
31     ~ModuleManager() = default;
32 
33     static ModuleManager* GetInstance();
34     bool InitModule(JSContext* ctx, const std::string& moduleName, JSValue& jsObject);
35     void InitTimerModule(JSContext* ctx);
36     void InitSyscapModule(JSContext* ctx);
37 
38     JSValue SetWaitTimer(JSContext* ctx, int32_t argc, JSValueConst* argv, bool isInterval);
39     JSValue ClearWaitTimer(JSContext* ctx, int32_t argc, JSValueConst* argv, bool isInterval);
40     uint32_t AddCallback(JSValue callbackFunc, std::vector<JSValue> callbackArray, bool isInterval);
41     JSValue GetCallbackFunc(uint32_t callbackId, bool isInterval);
42     std::vector<JSValue> GetCallbackArray(uint32_t callbackId, bool isInterval);
43     void RemoveCallbackFunc(JSContext* ctx, uint32_t callbackId, bool isInterval);
44 
45 private:
46     uint32_t AddCallback(std::unordered_map<uint32_t, JSValue>& callbackFuncMap,
47         std::unordered_map<uint32_t, std::vector<JSValue>>& callbackArrayMap, JSValue callbackFunc,
48         std::vector<JSValue> callbackArray);
49 
50     std::vector<JSValue> GetCallbackArray(
51         std::unordered_map<uint32_t, std::vector<JSValue>>& callbackArrayMap, uint32_t callbackId);
52 
53     void RemoveCallbackFunc(JSContext* ctx, std::unordered_map<uint32_t, JSValue>& callbackFuncMap,
54         std::unordered_map<uint32_t, std::vector<JSValue>>& callbackArrayMap, uint32_t callbackId);
55 
56     std::unordered_map<uint32_t, JSValue> callbackFuncMap_;
57     std::unordered_map<uint32_t, JSValue> intervalCallbackFuncMap_;
58     std::unordered_map<uint32_t, std::vector<JSValue>> callbackArrayMap_;
59     std::unordered_map<uint32_t, std::vector<JSValue>> intervalCallbackArrayMap_;
60     uint32_t callbackId_ = 0;
61 
62     ACE_DISALLOW_COPY_AND_MOVE(ModuleManager);
63 };
64 } // namespace OHOS::Ace::Framework
65 
66 #endif // FRAMEWORKS_BRIDGE_DECLARATIVE_FRONTEND_ENGINE_QUICKJS_MODULES_QJS_MODULE_MANAGER_H
67