• 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 _PLUGIN_MANAGER_H_
17 #define _PLUGIN_MANAGER_H_
18 
19 #include <string>
20 #include <unordered_map>
21 
22 #include <native_interface_xcomponent.h>
23 #include <napi/native_api.h>
24 #include <uv.h>
25 
26 #include "common/native_common.h"
27 #include "render/plugin_render.h"
28 
29 class PluginManager {
30 public:
~PluginManager()31     ~PluginManager() {}
32 
GetInstance()33     static PluginManager* GetInstance()
34     {
35         return &PluginManager::manager_;
36     }
37 
38     static napi_value GetContext(napi_env env, napi_callback_info info);
39 
40     /******************************APP Lifecycle******************************/
41     static napi_value NapiOnCreate(napi_env env, napi_callback_info info);
42     static napi_value NapiOnShow(napi_env env, napi_callback_info info);
43     static napi_value NapiOnHide(napi_env env, napi_callback_info info);
44     static napi_value NapiOnDestroy(napi_env env, napi_callback_info info);
45 
46     void OnCreateNative(napi_env env, uv_loop_t* loop);
47     void OnShowNative();
48     void OnHideNative();
49     void OnDestroyNative();
50 
51     /******************************声明式范式******************************/
52     /**                      JS Page : Lifecycle                        **/
53     static napi_value NapiOnPageShow(napi_env env, napi_callback_info info);
54     static napi_value NapiOnPageHide(napi_env env, napi_callback_info info);
55     void OnPageShowNative();
56     void OnPageHideNative();
57 
58     OH_NativeXComponent* GetNativeXComponent(std::string& id);
59     void SetNativeXComponent(std::string& id, OH_NativeXComponent* nativeXComponent);
60     PluginRender* GetRender(std::string& id);
61 
62 public:
63     // Napi export
64     bool Export(napi_env env, napi_value exports);
65 
66 public:
67     napi_env mainEnv_ = nullptr;
68     uv_loop_t* mainLoop_ = nullptr;
69     uv_async_t mainOnMessageSignal_ {};
70 
71 private:
72     static void MainOnMessage(const uv_async_t* req);
73     static PluginManager manager_;
74 
75     std::string id_;
76     std::unordered_map<std::string, OH_NativeXComponent*> nativeXComponentMap_;
77     std::unordered_map<std::string, PluginRender*> pluginRenderMap_;
78 };
79 
80 #endif // _PLUGIN_MANAGER_H_