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 #include "plugin_fw.h"
17 #include "image_log.h"
18 #include "singleton.h"
19 #include "impl_class_mgr.h"
20 #include "plugin_info_lock.h"
21 #include "plugin_mgr.h"
22 
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
25 
26 #undef LOG_TAG
27 #define LOG_TAG "PluginFw"
28 
29 namespace OHOS {
30 namespace MultimediaPlugin {
31 using std::map;
32 using std::string;
33 using std::vector;
34 using OHOS::Utils::RWLock;
35 using OHOS::Utils::UniqueReadGuard;
36 using OHOS::Utils::UniqueWriteGuard;
37 
Register(const vector<string> & canonicalPaths)38 uint32_t PluginFw::Register(const vector<string> &canonicalPaths)
39 {
40     IMAGE_LOGD("plugin register.");
41     // Use the read-write lock to mutually exclusive write plugin information and read plugin information operations,
42     // where Register() plays the write role.
43     UniqueWriteGuard<RWLock> lk(DelayedRefSingleton<PluginInfoLock>::GetInstance().rwLock_);
44     return pluginMgr_.Register(canonicalPaths);
45 }
46 
CreateObject(uint16_t interfaceID,const string & className,uint32_t & errorCode)47 PluginClassBase *PluginFw::CreateObject(uint16_t interfaceID, const string &className, uint32_t &errorCode)
48 {
49     // Use the read-write lock to mutually exclusive write plugin information and read plugin information operations,
50     // where CreateObject() plays the read role.
51     UniqueReadGuard<RWLock> lk(DelayedRefSingleton<PluginInfoLock>::GetInstance().rwLock_);
52     return implClassMgr_.CreateObject(interfaceID, className, errorCode);
53 }
54 
CreateObject(uint16_t interfaceID,uint16_t serviceType,const map<string,AttrData> & capabilities,const PriorityScheme & priorityScheme,uint32_t & errorCode)55 PluginClassBase *PluginFw::CreateObject(uint16_t interfaceID, uint16_t serviceType,
56                                         const map<string, AttrData> &capabilities,
57                                         const PriorityScheme &priorityScheme, uint32_t &errorCode)
58 {
59     // Use the read-write lock to mutually exclusive write plugin information and read plugin information operations,
60     // where CreateObject() plays the read role.
61     UniqueReadGuard<RWLock> lk(DelayedRefSingleton<PluginInfoLock>::GetInstance().rwLock_);
62     return implClassMgr_.CreateObject(interfaceID, serviceType, capabilities, priorityScheme, errorCode);
63 }
64 
PluginFwGetClassInfo(uint16_t interfaceID,uint16_t serviceType,const map<std::string,AttrData> & capabilities,vector<ClassInfo> & classesInfo)65 uint32_t PluginFw::PluginFwGetClassInfo(uint16_t interfaceID, uint16_t serviceType,
66                                         const map<std::string, AttrData> &capabilities,
67                                         vector<ClassInfo> &classesInfo)
68 {
69     // Use the read-write lock to mutually exclusive write plugin information and read plugin information operations,
70     // where GetClassInfo() plays the read role.
71     UniqueReadGuard<RWLock> lk(DelayedRefSingleton<PluginInfoLock>::GetInstance().rwLock_);
72     return implClassMgr_.ImplClassMgrGetClassInfo(interfaceID, serviceType, capabilities, classesInfo);
73 }
74 
75 // ------------------------------- private method -------------------------------
PluginFw()76 PluginFw::PluginFw()
77     : pluginMgr_(DelayedRefSingleton<PluginMgr>::GetInstance()),
78       implClassMgr_(DelayedRefSingleton<ImplClassMgr>::GetInstance()) {}
79 
~PluginFw()80 PluginFw::~PluginFw() {}
81 } // namespace MultimediaPlugin
82 } // namespace OHOS
83