• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "plugin_mgr.h"
17 #include <fstream>
18 #include <sstream>
19 #include "directory_ex.h"
20 #include "image_log.h"
21 #include "json.hpp"
22 #include "json_helper.h"
23 #include "platform_adp.h"
24 #include "plugin.h"
25 #include "plugin_metadata.h"
26 
27 #undef LOG_DOMAIN
28 #define LOG_DOMAIN LOG_TAG_DOMAIN_ID_PLUGIN
29 
30 #undef LOG_TAG
31 #define LOG_TAG "PluginMgr"
32 
33 namespace OHOS {
34 namespace MultimediaPlugin {
35 using nlohmann::json;
36 using std::ifstream;
37 using std::istringstream;
38 using std::size_t;
39 using std::string;
40 using std::vector;
41 using std::weak_ptr;
42 PlatformAdp &PluginMgr::platformAdp_ = DelayedRefSingleton<PlatformAdp>::GetInstance();
43 
Register(const vector<string> & canonicalPaths)44 uint32_t PluginMgr::Register(const vector<string> &canonicalPaths)
45 {
46     if (canonicalPaths.empty()) {
47         const vector<string> &metadata = OHOS::MultimediaPlugin::META_DATA;
48         for (size_t i = 0; i < metadata.size(); i++) {
49             uint32_t errorCode = RegisterPlugin(metadata[i]);
50             if (errorCode != SUCCESS) {
51                 return errorCode;
52             }
53         }
54         return SUCCESS;
55     }
56 
57     bool pathTraversed = false;
58     uint32_t errorCode = SUCCESS;
59     for (const string &path : canonicalPaths) {
60         uint32_t result = TraverseFiles(path);
61         if (result == SUCCESS) {
62             pathTraversed = true;
63         } else {
64             // no target is not a critical error type, giving priority to more serious errors.
65             if ((errorCode == SUCCESS) || (errorCode == ERR_NO_TARGET)) {
66                 errorCode = result;
67             }
68         }
69     }
70 
71     if (!pathTraversed) {
72         return errorCode;
73     }
74 
75     return SUCCESS;
76 }
77 
78 // ------------------------------- private method -------------------------------
PluginMgr()79 PluginMgr::PluginMgr()
80 {}
81 
~PluginMgr()82 PluginMgr::~PluginMgr()
83 {}
84 
TraverseFiles(const string & canonicalPath)85 uint32_t PluginMgr::TraverseFiles(const string &canonicalPath)
86 {
87     bool noTarget = true;
88     vector<string> strFiles;
89     GetDirFiles(canonicalPath, strFiles);
90     if (strFiles.empty()) {
91         IMAGE_LOGE("failed to get dir files.");
92         return ERR_GENERAL;
93     }
94 
95     string libraryPath;
96     for (const auto &file : strFiles) {
97         if (!CheckPluginMetaFile(file, libraryPath)) {
98             continue;
99         }
100         noTarget = false;
101         RegisterPlugin(file, std::move(libraryPath));
102     }
103 
104     if (noTarget) {
105         IMAGE_LOGW("there is no plugin meta file in path.");
106         return ERR_NO_TARGET;
107     }
108 
109     return SUCCESS;
110 }
111 
CheckPluginMetaFile(const string & candidateFile,string & libraryPath)112 bool PluginMgr::CheckPluginMetaFile(const string &candidateFile, string &libraryPath)
113 {
114     const string meatedataFileSuffix = "pluginmeta";
115 
116 #ifdef _WIN32
117     const string libraryFileSuffix = "dll";
118 #elif defined _APPLE
119     const string libraryFileSuffix = "dylib";
120 #else
121     const string libraryFileSuffix = "so";
122 #endif
123 
124     string fileExt = ExtractFileExt(candidateFile);
125     if (fileExt != meatedataFileSuffix) {
126         // not a plugin metadata file, quietly skip this item.
127         return false;
128     }
129 
130     ifstream metadata(candidateFile);
131     if (!metadata) {
132         IMAGE_LOGE("failed to open metadata file.");
133         return false;
134     }
135 
136     json root;
137     metadata >> root;
138     metadata.close();
139     if (JsonHelper::GetStringValue(root, "libraryPath", libraryPath) != SUCCESS) {
140         IMAGE_LOGE("read libraryPath failed.");
141         return false;
142     }
143 
144 #if defined(_WIN32) || defined(_APPLE)
145     libraryPath = TransformFileName(libraryPath);
146 #endif
147 
148     fileExt = ExtractFileExt(libraryPath);
149     if (fileExt != libraryFileSuffix) {
150         IMAGE_LOGE("invalid library suffix.");
151         return false;
152     }
153 
154 #if !defined(_WIN32) && !defined(_APPLE)
155     const string dirSeparator = "/";
156     if (libraryPath.substr(0, 1) != dirSeparator) {
157         // relative path to absolute path.
158         // just keep original library name
159         return true;
160     }
161 #endif
162 
163     string realPath;
164     if (!PathToRealPath(libraryPath, realPath)) {
165         IMAGE_LOGE("library path to real path error.");
166         return false;
167     }
168 
169     libraryPath = std::move(realPath);
170     return true;
171 }
172 
RegisterPlugin(const string & metadataPath,string && libraryPath)173 uint32_t PluginMgr::RegisterPlugin(const string &metadataPath, string &&libraryPath)
174 {
175     auto iter = plugins_.find(&libraryPath);
176     if (iter != plugins_.end()) {
177         // already registered before, just skip it.
178         IMAGE_LOGD("the libraryPath has already been registered before.");
179         return ERR_GENERAL;
180     }
181 
182     ifstream metadata(metadataPath);
183     if (!metadata) {
184         IMAGE_LOGE("failed to open metadata file.");
185         return ERR_GENERAL;
186     }
187 
188     auto plugin = std::make_shared<Plugin>();
189     if (plugin == nullptr) {
190         IMAGE_LOGE("failed to create Plugin.");
191         return ERR_INTERNAL;
192     }
193 
194     weak_ptr<Plugin> weakPtr = plugin;
195     auto regRet = plugin->Register(metadata, std::move(libraryPath), weakPtr);
196     if (regRet != SUCCESS) {
197         IMAGE_LOGE("failed to register plugin,ERRNO: %{public}u.", regRet);
198         return regRet;
199     }
200 
201     const std::string &key = plugin->GetLibraryPath();
202     if (key.empty()) {
203         IMAGE_LOGE("get empty libraryPath.");
204         return ERR_INTERNAL;
205     }
206 
207     auto insertRet = plugins_.insert(PluginMap::value_type(&key, std::move(plugin)));
208     if (!insertRet.second) {
209         IMAGE_LOGE("failed to insert Plugin");
210         return ERR_INTERNAL;
211     }
212 
213     return SUCCESS;
214 }
215 
RegisterPlugin(const string & metadataJson)216 uint32_t PluginMgr::RegisterPlugin(const string &metadataJson)
217 {
218     string libraryPath;
219     json root = nlohmann::json::parse(metadataJson);
220     if (JsonHelper::GetStringValue(root, "libraryPath", libraryPath) != SUCCESS) {
221         IMAGE_LOGE("read libraryPath failed.");
222         return false;
223     }
224 
225     auto iter = plugins_.find(&libraryPath);
226     if (iter != plugins_.end()) {
227         // already registered before, just skip it.
228         IMAGE_LOGD("the libraryPath has already been registered before.");
229         return ERR_GENERAL;
230     }
231 
232     istringstream metadata(metadataJson);
233     if (!metadata) {
234         IMAGE_LOGE("failed to read metadata.");
235         return ERR_GENERAL;
236     }
237 
238     auto crossPlugin = std::make_shared<Plugin>();
239     weak_ptr<Plugin> weakPtr = crossPlugin;
240     auto regRet = crossPlugin->Register(metadata, std::move(libraryPath), weakPtr);
241     if (regRet != SUCCESS) {
242         IMAGE_LOGE("failed to register plugin,ERRNO: %{public}u.", regRet);
243         return regRet;
244     }
245 
246     const std::string &key = crossPlugin->GetLibraryPath();
247     if (key.empty()) {
248         IMAGE_LOGE("get empty libraryPath.");
249         return ERR_INTERNAL;
250     }
251 
252     auto insertRet = plugins_.insert(PluginMap::value_type(&key, std::move(crossPlugin)));
253     if (!insertRet.second) {
254         IMAGE_LOGE("failed to insert Plugin");
255         return ERR_INTERNAL;
256     }
257 
258     return SUCCESS;
259 }
260 } // namespace MultimediaPlugin
261 } // namespace OHOS
262