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 if (RegisterPlugin(file, std::move(libraryPath)) != SUCCESS) {
101 continue;
102 }
103 noTarget = false;
104 }
105
106 if (noTarget) {
107 IMAGE_LOGW("there is no plugin meta file in path.");
108 return ERR_NO_TARGET;
109 }
110
111 return SUCCESS;
112 }
113
CheckPluginMetaFile(const string & candidateFile,string & libraryPath)114 bool PluginMgr::CheckPluginMetaFile(const string &candidateFile, string &libraryPath)
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 return CheckPluginMetaFile(candidateFile, libraryPath, libraryFileSuffix);
124 }
125
CheckPluginMetaFile(const string & candidateFile,string & libraryPath,const string & libraryFileSuffix)126 bool PluginMgr::CheckPluginMetaFile(const string &candidateFile, string &libraryPath, const string &libraryFileSuffix)
127 {
128 const string meatedataFileSuffix = "pluginmeta";
129 string fileExt = ExtractFileExt(candidateFile);
130 if (fileExt != meatedataFileSuffix) {
131 // not a plugin metadata file, quietly skip this item.
132 return false;
133 }
134
135 ifstream metadata(candidateFile);
136 if (!metadata.is_open()) {
137 IMAGE_LOGE("failed to open metadata file.");
138 return false;
139 }
140 json root = nlohmann::json::parse(metadata, nullptr, false); // no callback, no exceptions
141 metadata.close();
142 if (root.is_discarded()) {
143 IMAGE_LOGE("metadata json parsing failed.");
144 return false;
145 }
146 if (JsonHelper::GetStringValue(root, "libraryPath", libraryPath) != SUCCESS) {
147 IMAGE_LOGE("read libraryPath failed.");
148 return false;
149 }
150
151 #if defined(_WIN32) || defined(_APPLE)
152 libraryPath = TransformFileName(libraryPath);
153 #endif
154
155 fileExt = ExtractFileExt(libraryPath);
156 if (fileExt != libraryFileSuffix) {
157 IMAGE_LOGE("invalid library suffix.");
158 return false;
159 }
160
161 #if !defined(_WIN32) && !defined(_APPLE)
162 const string dirSeparator = "/";
163 if (libraryPath.substr(0, 1) != dirSeparator) {
164 // relative path to absolute path.
165 // just keep original library name
166 return true;
167 }
168 #endif
169
170 string realPath;
171 if (!PathToRealPath(libraryPath, realPath)) {
172 IMAGE_LOGE("library path to real path error.");
173 return false;
174 }
175
176 libraryPath = std::move(realPath);
177 return true;
178 }
179
RegisterPlugin(const string & metadataPath,string && libraryPath)180 uint32_t PluginMgr::RegisterPlugin(const string &metadataPath, string &&libraryPath)
181 {
182 auto iter = plugins_.find(&libraryPath);
183 if (iter != plugins_.end()) {
184 // already registered before, just skip it.
185 IMAGE_LOGD("the libraryPath has already been registered before.");
186 return ERR_GENERAL;
187 }
188
189 ifstream metadata(metadataPath);
190 if (!metadata.is_open()) {
191 IMAGE_LOGE("failed to open metadata file.");
192 return ERR_GENERAL;
193 }
194
195 auto plugin = std::make_shared<Plugin>();
196 if (plugin == nullptr) {
197 IMAGE_LOGE("failed to create Plugin.");
198 metadata.close();
199 return ERR_INTERNAL;
200 }
201
202 weak_ptr<Plugin> weakPtr = plugin;
203 auto regRet = plugin->Register(metadata, std::move(libraryPath), weakPtr);
204 if (regRet != SUCCESS) {
205 IMAGE_LOGE("failed to register plugin,ERRNO: %{public}u.", regRet);
206 metadata.close();
207 return regRet;
208 }
209 metadata.close();
210
211 const std::string &key = plugin->GetLibraryPath();
212 if (key.empty()) {
213 IMAGE_LOGE("get empty libraryPath.");
214 return ERR_INTERNAL;
215 }
216
217 auto insertRet = plugins_.insert(PluginMap::value_type(&key, std::move(plugin)));
218 if (!insertRet.second) {
219 IMAGE_LOGE("failed to insert Plugin");
220 return ERR_INTERNAL;
221 }
222
223 return SUCCESS;
224 }
225
RegisterPlugin(const string & metadataJson)226 uint32_t PluginMgr::RegisterPlugin(const string &metadataJson)
227 {
228 if (metadataJson.empty() || !nlohmann::json::accept(metadataJson)) {
229 IMAGE_LOGE("metadataJson not match, %{public}s", metadataJson.c_str());
230 return ERR_INVALID_PARAMETER;
231 }
232 string libraryPath;
233 json root = nlohmann::json::parse(metadataJson, nullptr, false); // no callback, no exceptions
234 if (root.is_discarded()) {
235 IMAGE_LOGE("RegisterPlugin parse json failed.");
236 return ERR_INVALID_PARAMETER;
237 }
238 if (JsonHelper::GetStringValue(root, "libraryPath", libraryPath) != SUCCESS) {
239 IMAGE_LOGE("read libraryPath failed.");
240 return ERR_INVALID_PARAMETER;
241 }
242
243 auto iter = plugins_.find(&libraryPath);
244 if (iter != plugins_.end()) {
245 // already registered before, just skip it.
246 IMAGE_LOGD("the libraryPath has already been registered before.");
247 return ERR_GENERAL;
248 }
249
250 istringstream metadata(metadataJson);
251 if (!metadata) {
252 IMAGE_LOGE("failed to read metadata.");
253 return ERR_GENERAL;
254 }
255
256 auto crossPlugin = std::make_shared<Plugin>();
257 weak_ptr<Plugin> weakPtr = crossPlugin;
258 auto regRet = crossPlugin->Register(metadata, std::move(libraryPath), weakPtr);
259 if (regRet != SUCCESS) {
260 IMAGE_LOGE("failed to register plugin,ERRNO: %{public}u.", regRet);
261 return regRet;
262 }
263
264 const std::string &key = crossPlugin->GetLibraryPath();
265 if (key.empty()) {
266 IMAGE_LOGE("get empty libraryPath.");
267 return ERR_INTERNAL;
268 }
269
270 auto insertRet = plugins_.insert(PluginMap::value_type(&key, std::move(crossPlugin)));
271 if (!insertRet.second) {
272 IMAGE_LOGE("failed to insert Plugin");
273 return ERR_INTERNAL;
274 }
275
276 return SUCCESS;
277 }
278 } // namespace MultimediaPlugin
279 } // namespace OHOS
280