• 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 "platform_adp.h"
17 #ifndef _WIN32
18 #include <dlfcn.h>
19 #endif
20 #include "directory_ex.h"
21 #include "hilog/log_c.h"
22 #include "hilog/log_cpp.h"
23 #include "log_tags.h"
24 #include "string"
25 #include "plugin_errors.h"
26 #include "type_traits"
27 
28 namespace OHOS {
29 namespace MultimediaPlugin {
30 using std::string;
31 using namespace OHOS::HiviewDFX;
32 
33 static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_TAG_DOMAIN_ID_PLUGIN, "PlatformAdp" };
34 
35 const string PlatformAdp::DIR_SEPARATOR = "/";
36 const string PlatformAdp::LIBRARY_FILE_SUFFIX = "so";
37 
38 #ifdef _WIN32
AdpLoadLibrary(const string & packageName)39 HMODULE PlatformAdp::AdpLoadLibrary(const string &packageName)
40 {
41     return LoadLibrary(packageName.c_str());
42 }
43 
AdpFreeLibrary(HMODULE handle)44 void PlatformAdp::AdpFreeLibrary(HMODULE handle)
45 {
46     FreeLibrary(handle);
47 }
48 
AdpGetSymAddress(HMODULE handle,const string & symbol)49 FARPROC PlatformAdp::AdpGetSymAddress(HMODULE handle, const string &symbol)
50 {
51     return GetProcAddress(handle, symbol.c_str());
52 }
53 #else
LoadLibrary(const std::string & packageName)54 void *PlatformAdp::LoadLibrary(const std::string &packageName)
55 {
56     return dlopen(packageName.c_str(), RTLD_LAZY);
57 }
58 
FreeLibrary(void * handle)59 void PlatformAdp::FreeLibrary(void *handle)
60 {
61     dlclose(handle);
62 }
63 
GetSymAddress(void * handle,const std::string & symbol)64 void *PlatformAdp::GetSymAddress(void *handle, const std::string &symbol)
65 {
66     return dlsym(handle, symbol.c_str());
67 }
68 #endif
69 
CheckAndNormalizePath(string & path)70 uint32_t PlatformAdp::CheckAndNormalizePath(string &path)
71 {
72 #if !defined(_WIN32) && !defined(_APPLE)
73     if (path.empty()) {
74         HiLog::Error(LABEL, "check path empty.");
75         return ERR_GENERAL;
76     }
77 #endif
78 
79     string realPath;
80     if (!PathToRealPath(path, realPath)) {
81         HiLog::Error(LABEL, "path to real path error.");
82         return ERR_GENERAL;
83     }
84 
85     path = std::move(realPath);
86 
87     return SUCCESS;
88 }
89 
90 // ------------------------------- private method -------------------------------
PlatformAdp()91 PlatformAdp::PlatformAdp() {}
92 
~PlatformAdp()93 PlatformAdp::~PlatformAdp() {}
94 } // namespace MultimediaPlugin
95 } // namespace OHOS
96