1 /** 2 * Copyright (C) 2016 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef PLUGIN_LOADER_H_ 18 #define PLUGIN_LOADER_H_ 19 20 #include "SharedLibrary.h" 21 22 #include <android-base/strings.h> 23 #include <utils/Log.h> 24 #include <utils/String8.h> 25 #include <utils/Vector.h> 26 27 namespace android { 28 namespace hardware { 29 namespace drm { 30 namespace V1_0 { 31 namespace helper { 32 33 template <class T> 34 class PluginLoader { 35 36 public: PluginLoader(const char * dir,const char * entry)37 PluginLoader(const char *dir, const char *entry) { 38 /** 39 * scan all plugins in the plugin directory and add them to the 40 * factories list. 41 */ 42 String8 pluginDir(dir); 43 44 DIR* pDir = opendir(pluginDir.c_str()); 45 if (pDir == NULL) { 46 ALOGE("Failed to find plugin directory %s", pluginDir.c_str()); 47 } else { 48 struct dirent* pEntry; 49 while ((pEntry = readdir(pDir))) { 50 String8 file(pEntry->d_name); 51 if (base::EndsWith(file.c_str(), ".so")) { 52 String8 path = pluginDir + "/" + pEntry->d_name; 53 T* plugin = loadOne(path.c_str(), entry); 54 if (plugin) { 55 factories.push(plugin); 56 } 57 } 58 } 59 closedir(pDir); 60 } 61 } 62 ~PluginLoader()63 ~PluginLoader() { 64 for (size_t i = 0; i < factories.size(); i++) { 65 delete factories[i]; 66 } 67 } 68 getFactory(size_t i)69 T *getFactory(size_t i) const { 70 return factories[i]; 71 } 72 factoryCount()73 size_t factoryCount() const {return factories.size();} 74 75 private: loadOne(const char * path,const char * entry)76 T* loadOne(const char *path, const char *entry) { 77 sp<SharedLibrary> library = new SharedLibrary(String8(path)); 78 if (!library.get()) { 79 ALOGE("Failed to open plugin library %s: %s", path, 80 library->lastError()); 81 } else { 82 typedef T *(*CreateFactoryFunc)(); 83 CreateFactoryFunc createFactoryFunc = 84 (CreateFactoryFunc)library->lookup(entry); 85 if (createFactoryFunc) { 86 ALOGV("Found plugin factory entry %s in %s", entry, path); 87 libraries.push(library); 88 T* result = createFactoryFunc(); 89 return result; 90 } else { 91 ALOGE("Failed to lookup symbol %s in library %s: %s", 92 entry, path, library->lastError()); 93 } 94 } 95 return NULL; 96 } 97 98 Vector<T *> factories; 99 Vector<sp<SharedLibrary> > libraries; 100 101 PluginLoader(const PluginLoader &) = delete; 102 void operator=(const PluginLoader &) = delete; 103 }; 104 105 } 106 } 107 } 108 } 109 } // namespace android 110 111 #endif // PLUGIN_LOADER_H_ 112 113