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 IMPL_CLASS_H
17 #define IMPL_CLASS_H
18 
19 #include <map>
20 #include <mutex>
21 #include <set>
22 #include <string>
23 #include "json.hpp"
24 #include "attr_data.h"
25 #include "capability.h"
26 #include "impl_class_key.h"
27 #include "plugin_errors.h"
28 
29 namespace OHOS {
30 namespace MultimediaPlugin {
31 class Plugin;
32 class PluginClassBase;
33 
34 enum class ClassState : int32_t {
35     CLASS_STATE_UNREGISTER = 0,
36     CLASS_STATE_REGISTERED
37 };
38 
39 class ImplClass final {
40 public:
41     ImplClass();
42     ~ImplClass() = default;
MakeServiceFlag(uint16_t interfaceID,uint16_t serviceType)43     static uint32_t MakeServiceFlag(uint16_t interfaceID, uint16_t serviceType)
44     {
45         return (((static_cast<uint32_t>(interfaceID)) << SERVICETYPE_BIT_NUM) | serviceType);
46     }
MakeIID(uint32_t serviceFlag)47     static uint16_t MakeIID(uint32_t serviceFlag)
48     {
49         return ((serviceFlag >> SERVICETYPE_BIT_NUM) & IID_MASK);
50     }
51     uint32_t Register(const std::weak_ptr<Plugin> &plugin, const nlohmann::json &classInfo);
52     PluginClassBase *CreateObject(uint32_t &errorCode);
53     std::weak_ptr<Plugin> GetPluginRef() const;
54     const std::string &GetClassName() const;
55     const std::string &GetPackageName() const;
56     bool IsSupport(uint16_t interfaceID) const;
57     void OnObjectDestroy();
58     const std::set<uint32_t> &GetServices() const;
59     bool IsCompatible(const std::map<std::string, AttrData> &caps) const;
60 
GetPriority()61     uint16_t GetPriority() const
62     {
63         return priority_;
64     }
65 
66     const AttrData *GetCapability(const std::string &key) const;
67     const std::map<std::string, AttrData> &GetCapability() const;
68 
69     static constexpr uint8_t SERVICETYPE_BIT_NUM = 16;
70     static constexpr uint32_t IID_MASK = 0xFFFF;
71 
72 private:
73     bool AnalysisServices(const nlohmann::json &classInfo);
74     bool AnalysisMaxInstance(const nlohmann::json &classInfo);
75     PluginClassBase *DoCreateObject(std::shared_ptr<Plugin> &plugin);
76     static constexpr uint16_t INSTANCE_NO_LIMIT_NUM = 0;
77     static std::string emptyString_;
78     // dynDataLock_:
79     // for data that only changes in the register, we don't call it dynamic data.
80     // non-dynamic data are protected by other means, that is: mutual exclusion between
81     // the register and createObject processes.
82     // current dynamic data includes:
83     // instanceNum_.
84     std::recursive_mutex dynDataLock_;
85     ClassState state_ = ClassState::CLASS_STATE_UNREGISTER;
86     std::string className_;
87     std::set<uint32_t> services_;
88     uint16_t priority_ = 0;
89     uint16_t maxInstance_ = 0;
90     Capability capability_;
91     std::weak_ptr<Plugin> pluginRef_;
92     ImplClassKey selfKey_;
93     uint16_t instanceNum_ = 0;
94 };
95 } // namespace MultimediaPlugin
96 } // namespace OHOS
97 
98 #endif // IMPL_CLASS_H
99