1/*
2 * Copyright (c) 2023-2023 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/cached_plugin_package.h"
17
18#include "common/log.h"
19
20namespace {
21constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "CachedPluginPackage" };
22}
23
24namespace OHOS {
25namespace Media {
26namespace Plugins {
27std::shared_ptr<PluginBase> CachedPluginPackage::CreatePlugin(PluginDescription pluginDescription)
28{
29    MEDIA_LOG_I("CreatePlugin " PUBLIC_LOG_S, pluginDescription.pluginName.c_str());
30    std::shared_ptr<PluginDefBase> pluginDef = GetPluginDef(pluginDescription);
31    if (pluginDef != nullptr) {
32        auto plugin = pluginDef->GetCreator()(pluginDef->name);
33        if (!plugin) {
34            return {};
35        }
36        return std::shared_ptr<PluginBase>(plugin);
37    } else {
38        MEDIA_LOG_I("pluginDef is nullptr");
39        return nullptr;
40    }
41}
42
43std::shared_ptr<PluginDefBase> CachedPluginPackage::GetPluginDef(PluginDescription pluginDescription)
44{
45    AutoLock lock(pluginMutex_);
46    std::vector<std::shared_ptr<PluginPackage>>::iterator itPluginPackage;
47    for (itPluginPackage = pluginPackageList_.begin();
48        itPluginPackage != pluginPackageList_.end(); itPluginPackage++) {
49        if (*itPluginPackage == nullptr) {
50            return nullptr;
51        }
52        std::vector<std::shared_ptr<PluginDefBase>> pluginDefList = (*itPluginPackage)->GetAllPlugins();
53        std::vector<std::shared_ptr<PluginDefBase>>::iterator itPluginDef;
54        for (itPluginDef = pluginDefList.begin(); itPluginDef != pluginDefList.end(); itPluginDef++) {
55            if (*itPluginDef == nullptr) {
56                return nullptr;
57            }
58            if (strcmp((*itPluginDef)->name.c_str(), pluginDescription.pluginName.c_str()) == 0) {
59                return (*itPluginDef);
60            }
61        }
62    }
63    std::shared_ptr<PluginPackage> pluginPackage = std::make_shared<PluginPackage>();
64    bool ret = pluginPackage->LoadPluginPackage(pluginDescription.packageName);
65    if (!ret) {
66        return nullptr;
67    }
68    pluginPackageList_.push_back(pluginPackage);
69    std::vector<std::shared_ptr<PluginDefBase>> pluginDefList = pluginPackage->GetAllPlugins();
70    std::vector<std::shared_ptr<PluginDefBase>>::iterator itPluginDef;
71    for (itPluginDef = pluginDefList.begin(); itPluginDef != pluginDefList.end(); itPluginDef++) {
72        if (*itPluginDef == nullptr) {
73            return nullptr;
74        }
75        if (strcmp((*itPluginDef)->name.c_str(), pluginDescription.pluginName.c_str()) == 0) {
76            return (*itPluginDef);
77        }
78    }
79    return nullptr;
80}
81
82} // namespace Plugins
83} // namespace Media
84} // namespace OHOS