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 
16 #ifndef UPDATER_UI_CALLBACK_MANAGER_H
17 #define UPDATER_UI_CALLBACK_MANAGER_H
18 
19 #include <functional>
20 #include <unordered_map>
21 #include <utility>
22 #include <vector>
23 #include "event_listener.h"
24 #include "json_node.h"
25 #include "updater_ui_traits.h"
26 
27 namespace Updater {
28 class CallbackManager {
29 public:
30     static void Init(bool hasFocus);
31     static void Register(const CallbackCfg &cbCfg);
32     static bool LoadCallbacks(const JsonNode &node);
33     static bool RegisterFunc(const std::string &name, Callback cb);
34 private:
35     static std::unordered_map<std::string, Callback> &GetFuncs();
36     static std::unordered_map<std::string, EventType> evtTypes_;
37     static std::vector<CallbackCfg> callbackCfgs_;
38 };
39 
40 #define DEFINE_CALLBACK(name, async)                                   \
41     void name(OHOS::UIView &);                                         \
42     static void __attribute((constructor)) RegisterCallback##name()    \
43     {                                                                  \
44         LOG(INFO) << "register callback " << (#name);                  \
45         CallbackManager::RegisterFunc(#name, Callback{&name, async});  \
46     }                                                                  \
47     void name(OHOS::UIView &)
48 
49 #define DEFINE_ASYN_CALLBACK(name) DEFINE_CALLBACK(name, true)
50 
51 #define DEFINE_SYNC_CALLBACK(name) DEFINE_CALLBACK(name, false)
52 }
53 #endif
54