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_H
17 #define PLUGIN_H
18 
19 #include <iostream>
20 #include <sstream>
21 #include <string>
22 #include "nocopyable.h"
23 #include "plugin_errors.h"
24 #include "plugin_export.h"
25 
26 namespace OHOS {
27 namespace MultimediaPlugin {
28 enum class PluginState : int32_t {
29     PLUGIN_STATE_UNREGISTER = 0,
30     PLUGIN_STATE_REGISTERED,
31     PLUGIN_STATE_RESOLVED,
32     PLUGIN_STATE_STARTING,
33     PLUGIN_STATE_ACTIVE,
34     PLUGIN_STATE_STOPPING
35 };
36 
37 enum class VersionParseStep;
38 class ImplClassMgr;
39 class PlatformAdp;
40 struct VersionNum;
41 
42 class Plugin final : public NoCopyable {
43 public:
44     Plugin();
45     ~Plugin() override;
46     uint32_t Register(std::istream &metadata, std::string &&libraryPath, std::weak_ptr<Plugin> &plugin);
47     uint32_t Ref();
48     void DeRef();
49     void Block();
50     void Unblock();
51     PluginCreateFunc GetCreateFunc();
52     const std::string &GetLibraryPath() const;
53     const std::string &GetPackageName() const;
54 
55 private:
56     static constexpr uint8_t VERSION_MAJOR_INDEX = 0;
57     static constexpr uint8_t VERSION_MINOR_INDEX = 1;
58     static constexpr uint8_t VERSION_MICRO_INDEX = 2;
59     static constexpr uint8_t VERSION_NANO_INDEX = 3;
60     static constexpr uint8_t VERSION_ARRAY_SIZE = 4;
61     static constexpr uint8_t UINT16_MAX_DECIMAL_DIGITS = 5;  // uint16_t max number 65535, 5 digits.
62 
63     uint32_t ResolveLibrary();
64     void FreeLibrary();
65     uint32_t RegisterMetadata(std::istream &metadata, std::weak_ptr<Plugin> &plugin);
66     uint32_t CheckTargetVersion(const std::string &targetVersion);
67     uint32_t AnalyzeVersion(const std::string &versionInfo, VersionNum &versionNum);
68     uint32_t ExecuteVersionAnalysis(const std::string &input, VersionParseStep &step,
69                                     uint16_t (&versionNum)[VERSION_ARRAY_SIZE]);
70     uint32_t GetUint16ValueFromDecimal(const std::string &source, uint16_t &result);
71 
72     PlatformAdp &platformAdp_;
73     ImplClassMgr &implClassMgr_;
74     // dynDataLock_:
75     // for data that only changes in the register, we don't call it dynamic data.
76     // non-dynamic data are protected by other means, that is: mutual exclusion between
77     // the register and createObject processes.
78     // current dynamic data includes:
79     // state_, handle_, refNum_, startFunc_, stopFunc_, createFunc_, blocked_.
80     std::recursive_mutex dynDataLock_;
81     PluginState state_ = PluginState::PLUGIN_STATE_UNREGISTER;
82     std::weak_ptr<Plugin> plugin_;
83     void *handle_ = nullptr;
84     uint32_t refNum_ = 0;
85     std::string libraryPath_;
86     std::string packageName_;
87     std::string version_;
88     PluginStartFunc startFunc_ = nullptr;
89     PluginStopFunc stopFunc_ = nullptr;
90     PluginCreateFunc createFunc_ = nullptr;
91     bool blocked_ = false;
92 };
93 } // namespace MultimediaPlugin
94 } // namespace OHOS
95 
96 #endif // PLUGIN_H
97