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_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_COMPONENT_MANAGER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_COMPONENT_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <unordered_set>
22 
23 #include "bundlemgr/bundle_mgr_interface.h"
24 #include "ui_service_interface.h"
25 #include "ui_service_stub.h"
26 #include "want.h"
27 
28 #include "base/json/json_util.h"
29 #include "base/memory/ace_type.h"
30 #include "core/components/common/layout/grid_column_info.h"
31 #include "core/components/plugin/plugin_component_callback.h"
32 #include "core/components/plugin/plugin_component_template.h"
33 
34 namespace OHOS::Ace {
35 class ACE_FORCE_EXPORT PluginComponentManager final {
36 public:
PluginComponentManager()37     PluginComponentManager()
38     {
39         if (!listener_) {
40             listener_ = new (std::nothrow) UIServiceListener();
41         }
42     }
43     ~PluginComponentManager() = default;
44 
45     ACE_EXPORT static std::shared_ptr<PluginComponentManager> GetInstance();
46 
47     int Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath, const std::string& data,
48         const std::string& extraData);
49     int Request(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath, const std::string& data);
50     int ReturnRequest(
51         const AAFwk::Want& want, const std::string& pluginName, const std::string& data, const std::string& extraData);
52     void RegisterCallBack(
53         const AAFwk::Want& want, const std::shared_ptr<PluginComponentCallBack>& callback, CallBackType callBackType);
54     void UnregisterCallBack(const std::shared_ptr<PluginComponentCallBack>& callback);
55     void UnregisterCallBack(const AAFwk::Want& want);
56 
57     bool GetTemplatePathFromJsonFile(const std::string& packagePathStr, const std::string& srcPath,
58         const std::string& jsonPath, std::string& jsonStr);
59     std::string GetPackagePath(const AAFwk::Want& want) const;
60 
61     sptr<AppExecFwk::IBundleMgr> GetBundleManager();
62 
63     class UIServiceListener final : public Ace::UIServiceStub {
64     public:
65         UIServiceListener() = default;
~UIServiceListener()66         ~UIServiceListener()
67         {
68             std::lock_guard<std::recursive_mutex> lock(mutex_);
69             callbackVec_.clear();
70         };
71 
72         void ResgisterListener(const std::shared_ptr<PluginComponentCallBack>& callback, CallBackType callBackType);
73         void UnresgisterListener(const std::shared_ptr<PluginComponentCallBack>& callback);
74         void OnPushCallBack(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
75             const std::string& data, const std::string& extraData) override;
76         void OnRequestCallBack(const AAFwk::Want& want, const std::string& name, const std::string& data) override;
77         void OnReturnRequest(const AAFwk::Want& want, const std::string& source, const std::string& data,
78             const std::string& extraData) override;
79         void RequestByJsonPath(const PluginComponentTemplate& pluginTemplate, const std::string& data);
80 
81     private:
82         std::recursive_mutex mutex_;
83         std::map<std::shared_ptr<PluginComponentCallBack>, CallBackType> callbackVec_;
84         std::unordered_set<std::shared_ptr<PluginComponentCallBack>> callbacks_;
85     };
86 
87 private:
88     static std::mutex mutex_;
89     static std::shared_ptr<PluginComponentManager> instance_;
90     sptr<UIServiceListener> listener_ = nullptr;
91 
92     ACE_DISALLOW_COPY_AND_MOVE(PluginComponentManager);
93 };
94 } // namespace OHOS::Ace
95 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_PLUGIN_COMPONENT_MANAGER_H
96