1fa7767c5Sopenharmony_ci/*
2fa7767c5Sopenharmony_ci * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3fa7767c5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4fa7767c5Sopenharmony_ci * you may not use this file except in compliance with the License.
5fa7767c5Sopenharmony_ci * You may obtain a copy of the License at
6fa7767c5Sopenharmony_ci *
7fa7767c5Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8fa7767c5Sopenharmony_ci *
9fa7767c5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10fa7767c5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11fa7767c5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fa7767c5Sopenharmony_ci * See the License for the specific language governing permissions and
13fa7767c5Sopenharmony_ci * limitations under the License.
14fa7767c5Sopenharmony_ci */
15fa7767c5Sopenharmony_ci
16fa7767c5Sopenharmony_ci#include "plugin/plugin_manager_v2.h"
17fa7767c5Sopenharmony_ci#include "common/log.h"
18fa7767c5Sopenharmony_ci
19fa7767c5Sopenharmony_cinamespace {
20fa7767c5Sopenharmony_ciconstexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "PluginManagerV2" };
21fa7767c5Sopenharmony_ci}
22fa7767c5Sopenharmony_ci
23fa7767c5Sopenharmony_cinamespace OHOS {
24fa7767c5Sopenharmony_cinamespace Media {
25fa7767c5Sopenharmony_cinamespace Plugins {
26fa7767c5Sopenharmony_ciPluginManagerV2::PluginManagerV2()
27fa7767c5Sopenharmony_ci{
28fa7767c5Sopenharmony_ci    MEDIA_LOG_D("PluginManagerV2");
29fa7767c5Sopenharmony_ci    cachedPluginPackage_ = std::make_shared<CachedPluginPackage>();
30fa7767c5Sopenharmony_ci}
31fa7767c5Sopenharmony_ci
32fa7767c5Sopenharmony_cistd::shared_ptr<PluginBase> PluginManagerV2::CreatePluginByMime(PluginType pluginType, std::string mime)
33fa7767c5Sopenharmony_ci{
34fa7767c5Sopenharmony_ci    MEDIA_LOG_D("CreatePluginByMime pluginType: " PUBLIC_LOG_D32 " mime: " PUBLIC_LOG_S, pluginType, mime.c_str());
35fa7767c5Sopenharmony_ci    PluginDescription pluginDescription = PluginList::GetInstance().GetPluginByCap(pluginType, mime);
36fa7767c5Sopenharmony_ci    return cachedPluginPackage_->CreatePlugin(pluginDescription);
37fa7767c5Sopenharmony_ci}
38fa7767c5Sopenharmony_ci
39fa7767c5Sopenharmony_cistd::shared_ptr<PluginBase> PluginManagerV2::CreatePluginByName(std::string name)
40fa7767c5Sopenharmony_ci{
41fa7767c5Sopenharmony_ci    MEDIA_LOG_D("CreatePluginByName pluginName: " PUBLIC_LOG_S, name.c_str());
42fa7767c5Sopenharmony_ci    PluginDescription pluginDescription = PluginList::GetInstance().GetPluginByName(name);
43fa7767c5Sopenharmony_ci    return cachedPluginPackage_->CreatePlugin(pluginDescription);
44fa7767c5Sopenharmony_ci}
45fa7767c5Sopenharmony_ci
46fa7767c5Sopenharmony_cistd::string PluginManagerV2::SnifferPlugin(PluginType pluginType, std::shared_ptr<DataSource> dataSource)
47fa7767c5Sopenharmony_ci{
48fa7767c5Sopenharmony_ci    MEDIA_LOG_D("SnifferPlugin pluginType: " PUBLIC_LOG_D32, pluginType);
49fa7767c5Sopenharmony_ci    std::vector<PluginDescription> matchedPluginsDescriptions =
50fa7767c5Sopenharmony_ci        PluginList::GetInstance().GetPluginsByType(pluginType);
51fa7767c5Sopenharmony_ci    int maxProb = 0;
52fa7767c5Sopenharmony_ci    std::vector<PluginDescription>::iterator it;
53fa7767c5Sopenharmony_ci    PluginDescription bestMatchedPlugin;
54fa7767c5Sopenharmony_ci    for (it = matchedPluginsDescriptions.begin(); it != matchedPluginsDescriptions.end(); it++) {
55fa7767c5Sopenharmony_ci        std::shared_ptr<PluginDefBase> pluginDef = cachedPluginPackage_->GetPluginDef(*it);
56fa7767c5Sopenharmony_ci        if (pluginDef != nullptr) {
57fa7767c5Sopenharmony_ci            auto prob = pluginDef->GetSniffer()(pluginDef->name, dataSource);
58fa7767c5Sopenharmony_ci            if (prob > maxProb) {
59fa7767c5Sopenharmony_ci                maxProb = prob;
60fa7767c5Sopenharmony_ci                bestMatchedPlugin = (*it);
61fa7767c5Sopenharmony_ci            }
62fa7767c5Sopenharmony_ci        }
63fa7767c5Sopenharmony_ci    }
64fa7767c5Sopenharmony_ci    return bestMatchedPlugin.pluginName;
65fa7767c5Sopenharmony_ci}
66fa7767c5Sopenharmony_ci} // namespace Plugins
67fa7767c5Sopenharmony_ci} // namespace Media
68fa7767c5Sopenharmony_ci} // namespace OHOS