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 #include <utils/Log.h> 22 #include <utils/String8.h> 23 #include <utils/Vector.h> 24 25 namespace android { 26 namespace hardware { 27 namespace drm { 28 namespace V1_0 { 29 namespace helper { 30 31 template <class T> 32 class PluginLoader { 33 34 public: PluginLoader(const char * dir,const char * entry)35 PluginLoader(const char *dir, const char *entry) { 36 /** 37 * scan all plugins in the plugin directory and add them to the 38 * factories list. 39 */ 40 String8 pluginDir(dir); 41 42 DIR* pDir = opendir(pluginDir.string()); 43 if (pDir == NULL) { 44 ALOGE("Failed to find plugin directory %s", pluginDir.string()); 45 } else { 46 struct dirent* pEntry; 47 while ((pEntry = readdir(pDir))) { 48 String8 file(pEntry->d_name); 49 if (file.getPathExtension() == ".so") { 50 String8 path = pluginDir + "/" + pEntry->d_name; 51 T *plugin = loadOne(path, entry); 52 if (plugin) { 53 factories.push(plugin); 54 } 55 } 56 } 57 closedir(pDir); 58 } 59 } 60 ~PluginLoader()61 ~PluginLoader() { 62 for (size_t i = 0; i < factories.size(); i++) { 63 delete factories[i]; 64 } 65 } 66 getFactory(size_t i)67 T *getFactory(size_t i) const { 68 return factories[i]; 69 } 70 factoryCount()71 size_t factoryCount() const {return factories.size();} 72 73 private: loadOne(const char * path,const char * entry)74 T* loadOne(const char *path, const char *entry) { 75 sp<SharedLibrary> library = new SharedLibrary(String8(path)); 76 if (!library.get()) { 77 ALOGE("Failed to open plugin library %s: %s", path, 78 library->lastError()); 79 } else { 80 typedef T *(*CreateFactoryFunc)(); 81 CreateFactoryFunc createFactoryFunc = 82 (CreateFactoryFunc)library->lookup(entry); 83 if (createFactoryFunc) { 84 ALOGV("Found plugin factory entry %s in %s", entry, path); 85 libraries.push(library); 86 T* result = createFactoryFunc(); 87 return result; 88 } 89 } 90 return NULL; 91 } 92 93 Vector<T *> factories; 94 Vector<sp<SharedLibrary> > libraries; 95 96 PluginLoader(const PluginLoader &) = delete; 97 void operator=(const PluginLoader &) = delete; 98 }; 99 100 } 101 } 102 } 103 } 104 } // namespace android 105 106 #endif // PLUGIN_LOADER_H_ 107 108