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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
|
/**********************************************************************
Audacity: A Digital Audio Editor
@file PluginHost.cpp
@author Vitaly Sverchinsky
Part of lib-module-manager library
**********************************************************************/
#include "PluginHost.h"
#include <wx/app.h>
#include <wx/log.h>
#include <wx/module.h>
#include <wx/process.h>
#include "BasicUI.h"
#include "PathList.h"
#include "FileNames.h"
#include "ModuleManager.h"
#include "IPCClient.h"
#include "PlatformCompatibility.h"
#include "PluginManager.h"
namespace
{
//Attempts to instantiate plugin module and put plugin descriptors into result
void Discover(detail::PluginValidationResult& result, const wxString& providerId, const wxString& pluginPath)
{
try
{
if(auto provider = ModuleManager::Get().CreateProviderInstance(providerId, wxEmptyString))
{
TranslatableString errorMessage{};
auto validator = provider->MakeValidator();
auto numPlugins = provider->DiscoverPluginsAtPath(
pluginPath, errorMessage, [&](PluginProvider *provider, ComponentInterface *ident)
{
//Workaround: use DefaultRegistrationCallback to create all descriptors for us
//and then put a copy into result
auto id = PluginManager::DefaultRegistrationCallback(provider, ident);
if(const auto ptr = PluginManager::Get().GetPlugin(id))
{
auto desc = *ptr;
try
{
if(validator)
validator->Validate(*ident);
}
catch(...)
{
desc.SetEnabled(false);
desc.SetValid(false);
}
result.Add(std::move(desc));
}
return id;
});
if(!errorMessage.empty())
result.SetError(errorMessage.Debug());
else if(numPlugins == 0)
result.SetError("no plugins found");
}
else
result.SetError("provider not found");
}
catch(...)
{
result.SetError("unknown error");
}
}
}
PluginHost::PluginHost(int connectPort)
{
FileNames::InitializePathList();
wxFileName configFileName{ FileNames::Configuration() };
auto pConfig = std::make_unique<FileConfig>(
AppName, wxEmptyString, configFileName.GetFullPath(),
wxEmptyString, wxCONFIG_USE_LOCAL_FILE);
pConfig->Init();
InitPreferences(std::move(pConfig));
auto& moduleManager = ModuleManager::Get();
moduleManager.Initialize();
moduleManager.DiscoverProviders();
mClient = std::make_unique<IPCClient>(connectPort, *this);
}
void PluginHost::OnConnect(IPCChannel& channel) noexcept
{
std::lock_guard lck(mSync);
mChannel = &channel;
}
void PluginHost::OnDisconnect() noexcept
{
Stop();
}
void PluginHost::OnConnectionError() noexcept
{
Stop();
}
void PluginHost::OnDataAvailable(const void* data, size_t size) noexcept
{
try
{
mInputMessageReader.ConsumeBytes(data, size);
if(mInputMessageReader.CanPop())
{
{
std::lock_guard lck(mSync);
assert(!mRequest);
mRequest = mInputMessageReader.Pop();
}
mRequestCondition.notify_one();
}
}
catch(...)
{
Stop();
}
}
bool PluginHost::Serve()
{
std::unique_lock lck(mSync);
mRequestCondition.wait(lck, [this]{ return !mRunning || mRequest.has_value(); });
if(!mRunning)
return false;
if(mRequest)
{
if(mChannel)
detail::PutMessage(*mChannel, wxEmptyString);
std::optional<wxString> request;
mRequest.swap(request);
lck.unlock();
wxString providerId;
wxString pluginPath;
detail::PluginValidationResult result;
if(detail::ParseRequestString(*request, providerId, pluginPath))
Discover(result, providerId, pluginPath);
else
result.SetError("malformed request string");
XMLStringWriter xmlWriter;
result.WriteXML(xmlWriter);
lck.lock();
if(mChannel)
detail::PutMessage(*mChannel, xmlWriter);
}
return true;
}
void PluginHost::Stop() noexcept
{
try
{
{
std::lock_guard lck(mSync);//may throw
mRunning = false;
mChannel = nullptr;
}
}
catch(...)
{
//If something went wrong with mutex locking we'll try to
//awake main thread if it's blocked on condition variable.
//Attempt to relock the mutex there should throw as well(?)...
//which, in turn, will result in std::terminate being called
}
mRequestCondition.notify_one();
}
bool PluginHost::Start(int connectPort)
{
const auto cmd = wxString::Format("\"%s\" %s %d",
PlatformCompatibility::GetExecutablePath(),
PluginHost::HostArgument,
connectPort);
auto process = std::make_unique<wxProcess>();
process->Detach();
if(wxExecute(cmd, wxEXEC_ASYNC, process.get()) != 0)
{
//process will delete itself upon termination
process.release();
return true;
}
return false;
}
bool PluginHost::IsHostProcess(int argc, wxChar** argv)
{
return argc >= 3 && wxStrcmp(argv[1], HostArgument) == 0;
}
class PluginHostModule final :
public wxModule
{
public:
DECLARE_DYNAMIC_CLASS(PluginHostModule)
bool OnInit() override
{
if(PluginHost::IsHostProcess(wxTheApp->argc, wxTheApp->argv))
{
long connectPort;
if(!wxTheApp->argv[2].ToLong(&connectPort))
return false;
//log messages will appear in a separate window
//redirect to log file later
wxLog::EnableLogging(false);
//Handle requests...
PluginHost host(connectPort);
while(host.Serve()) { }
//...and terminate app
return false;
}
//do noting if current process isn't a host process
return true;
}
void OnExit() override
{
}
};
IMPLEMENT_DYNAMIC_CLASS(PluginHostModule, wxModule);
|