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