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/plugin_manager_v2.h"
17 #include "common/log.h"
18 
19 namespace {
20 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "PluginManagerV2" };
21 }
22 
23 namespace OHOS {
24 namespace Media {
25 namespace Plugins {
PluginManagerV2()26 PluginManagerV2::PluginManagerV2()
27 {
28     MEDIA_LOG_I("PluginManagerV2");
29     cachedPluginPackage_ = std::make_shared<CachedPluginPackage>();
30 }
31 
CreatePluginByMime(PluginType pluginType,std::string mime)32 std::shared_ptr<PluginBase> PluginManagerV2::CreatePluginByMime(PluginType pluginType, std::string mime)
33 {
34     MEDIA_LOG_I("CreatePluginByMime pluginType: " PUBLIC_LOG_D32 " mime: " PUBLIC_LOG_S, pluginType, mime.c_str());
35     std::vector<PluginDescription> pluginDescriptions = PluginList::GetInstance().GetPluginsByCap(pluginType, mime);
36     for (auto desc: pluginDescriptions) {
37         std::shared_ptr<PluginBase> plugin = cachedPluginPackage_->CreatePlugin(desc);
38         if (plugin != nullptr) {
39             return plugin;
40         }
41     }
42     return nullptr;
43 }
44 
CreatePluginByName(std::string name)45 std::shared_ptr<PluginBase> PluginManagerV2::CreatePluginByName(std::string name)
46 {
47     MEDIA_LOG_I("CreatePluginByName pluginName: " PUBLIC_LOG_S, name.c_str());
48     PluginDescription pluginDescription = PluginList::GetInstance().GetPluginByName(name);
49     return cachedPluginPackage_->CreatePlugin(pluginDescription);
50 }
51 
SnifferPlugin(PluginType pluginType,std::shared_ptr<DataSource> dataSource)52 std::string PluginManagerV2::SnifferPlugin(PluginType pluginType, std::shared_ptr<DataSource> dataSource)
53 {
54     MEDIA_LOG_I("SnifferPlugin pluginType: " PUBLIC_LOG_D32, pluginType);
55     std::vector<PluginDescription> matchedPluginsDescriptions =
56         PluginList::GetInstance().GetPluginsByType(pluginType);
57     int maxProb = 0;
58     std::vector<PluginDescription>::iterator it;
59     PluginDescription bestMatchedPlugin;
60     for (it = matchedPluginsDescriptions.begin(); it != matchedPluginsDescriptions.end(); it++) {
61         std::shared_ptr<PluginDefBase> pluginDef = cachedPluginPackage_->GetPluginDef(*it);
62         if (pluginDef != nullptr) {
63             auto prob = pluginDef->GetSniffer()(pluginDef->name, dataSource);
64             if (prob > maxProb) {
65                 maxProb = prob;
66                 bestMatchedPlugin = (*it);
67             }
68         }
69     }
70     return bestMatchedPlugin.pluginName;
71 }
72 } // namespace Plugins
73 } // namespace Media
74 } // namespace OHOS