summaryrefslogtreecommitdiff
path: root/src/player/PluginManager.cpp
blob: 57d33f0d74f57ca5edd5851bbb0f158bd374839c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
//  libavg - Media Playback Engine. 
//  Copyright (C) 2003-2014 Ulrich von Zadow
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
//  Current versions can be found at www.libavg.de
//
//  Original author of this file is Jan Boelsche (regular.gonzales@googlemail.com).
//

#include "PluginManager.h"

#include "../base/DlfcnWrapper.h"
#include "../base/FileHelper.h"
#include "../base/Logger.h"
#include "../base/OSHelper.h"

#include <iostream>
#include <string>

using namespace std;
using namespace avg;

#ifdef _WIN32
#define PATH_DELIMITER ";"
#define PLUGIN_EXTENSION ".dll"
#else
#define PATH_DELIMITER ":"
#define PLUGIN_EXTENSION ".so"
#endif

PluginManager::PluginNotFound::PluginNotFound(const string& sMessage) :
    Exception(AVG_ERR_FILEIO, sMessage) {}

PluginManager::PluginCorrupted::PluginCorrupted(const string& sMessage) :
    Exception(AVG_ERR_CORRUPT_PLUGIN, sMessage) {}
    
PluginManager& PluginManager::get()
{
    static PluginManager s_Instance;
    return s_Instance;
}

PluginManager::PluginManager()
{
    setSearchPath(string("."PATH_DELIMITER) +
            "./plugin"PATH_DELIMITER + 
            "./plugin/.libs"PATH_DELIMITER +
            getPath(getAvgLibPath()) + "plugin");
}

void PluginManager::setSearchPath(const string& sNewPath)
{
    m_sCurrentSearchPath = sNewPath;
    parsePath(m_sCurrentSearchPath);
}

string PluginManager::getSearchPath() const
{
    return m_sCurrentSearchPath;
}
    
py::object PluginManager::loadPlugin(const std::string& sPluginName)
{
    // is it loaded aready?
    PluginMap::iterator i = m_LoadedPlugins.find(sPluginName);
    if (i == m_LoadedPlugins.end()) {
        // no, let's try to load it!
        string sFullpath = locateSharedObject(sPluginName+PLUGIN_EXTENSION);
        void *handle = internalLoadPlugin(sFullpath);
        // add to map of loaded plugins
        m_LoadedPlugins[sPluginName] = make_pair(handle, 1);
    } else {
        // yes, just increase the reference count
        int referenceCount = i->second.second;
        ++referenceCount;
        m_LoadedPlugins[sPluginName] = make_pair(i->second.first, referenceCount);
    }
    py::object sysModule(py::handle<>(PyImport_ImportModule("sys")));
    return sysModule.attr("modules")[sPluginName];
}

string PluginManager::locateSharedObject(const string& sFilename)
{
    vector<string>::iterator i = m_PathComponents.begin();
    string sFullpath;
    while (i != m_PathComponents.end()) {
        sFullpath = *i + sFilename;
        if (fileExists(sFullpath)) {
            return sFullpath;
        }
        ++i;
    }
    string sMessage = "Unable to locate plugin file '" + sFilename 
            + "'. Was looking in " + m_sCurrentSearchPath;
    AVG_TRACE(Logger::category::PLUGIN, Logger::severity::INFO, sMessage);
    throw PluginNotFound(sMessage);
}

string PluginManager::checkDirectory(const string& sDirectory)
{
    string sFixedDirectory;
    char lastChar = *sDirectory.rbegin();
    if (lastChar != '/' && lastChar != '\\') {
        sFixedDirectory = sDirectory + "/";
    } else {
        sFixedDirectory = sDirectory;
    }
    return sFixedDirectory;
}

void PluginManager::parsePath(const string& sPath)
{
    // break the string into colon separated components
    // and make sure each component has a trailing slash
    // warn about non-existing directories
    
    m_PathComponents.clear();
    string sRemaining = sPath;
    string::size_type i;
    do {
        i = sRemaining.find(PATH_DELIMITER);
        string sDirectory;
        if (i == string::npos) {
            sDirectory = sRemaining;
            sRemaining = "";
        } else {
            sDirectory = sRemaining.substr(0, i);
            sRemaining = sRemaining.substr(i+1);
        }
        sDirectory = checkDirectory(sDirectory);
        
        m_PathComponents.push_back(sDirectory);
    } while (!sRemaining.empty());
    AVG_TRACE(Logger::category::PLUGIN, Logger::severity::INFO,
            "Plugin search path set to '" << sPath << "'"); 
}
    
void* PluginManager::internalLoadPlugin(const string& sFullpath)
{   
    void *handle = dlopen(sFullpath.c_str(), RTLD_LOCAL | RTLD_NOW);
    if (!handle) {
        string sMessage(dlerror());
        AVG_TRACE(Logger::category::PLUGIN, Logger::severity::ERROR,
                "Could not load plugin. dlopen failed with message '"
                << sMessage << "'");
        throw PluginCorrupted(sMessage);
    }
    try {
        registerPlugin(handle);
    } catch(PluginCorrupted& e) {
        dlclose(handle);
        throw e;
    }    
    AVG_TRACE(Logger::category::PLUGIN,Logger::severity::INFO,
            "Loaded plugin '" << sFullpath << "'");
    return handle;
}

void PluginManager::registerPlugin(void* handle)
{
   typedef void (*RegisterPluginPtr)();
   RegisterPluginPtr registerPlugin = 
       reinterpret_cast<RegisterPluginPtr>(dlsym(handle, "registerPlugin"));

    if (registerPlugin) {
        registerPlugin();
    } else {
        AVG_TRACE(Logger::category::PLUGIN, Logger::severity::ERROR,
                "No plugin registration function detected");
        throw PluginCorrupted("No plugin registration function detected");
    }
}