Audacity 3.2.0
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Static Private Attributes | List of all members
PluginHost Class Referencefinal

Internal class, processes plugin validation requests from the main app. Request is a simple string formatted by detail::MakeRequestString. After connection is established host starts to wait for a request from server. Once request is successfully processed host sends a reply. Host is capable to handle only one request at a time, so it's not allowed to send another request until host hasn't finish processing previous request. More...

#include <PluginHost.h>

Inheritance diagram for PluginHost:
[legend]
Collaboration diagram for PluginHost:
[legend]

Public Member Functions

 PluginHost (int connectPort)
 
void OnConnect (IPCChannel &channel) noexcept override
 Called when connection established. More...
 
void OnDisconnect () noexcept override
 Invalidates IPCChannel passed as argument in OnConnect. More...
 
void OnConnectionError () noexcept override
 Called when connection attempt fails. More...
 
void OnDataAvailable (const void *data, size_t size) noexcept override
 Called when data chunk received as a result of a preceding call to IPCChannel::Send. Generally, data pointer should not be accessed outside this method, copied if necessary. More...
 
bool Serve ()
 
- Public Member Functions inherited from IPCChannelStatusCallback
virtual ~IPCChannelStatusCallback ()
 
virtual void OnConnectionError () noexcept=0
 Called when connection attempt fails. More...
 
virtual void OnConnect (IPCChannel &channel) noexcept=0
 Called when connection established. More...
 
virtual void OnDisconnect () noexcept=0
 Invalidates IPCChannel passed as argument in OnConnect. More...
 
virtual void OnDataAvailable (const void *data, size_t size) noexcept=0
 Called when data chunk received as a result of a preceding call to IPCChannel::Send. Generally, data pointer should not be accessed outside this method, copied if necessary. More...
 

Static Public Member Functions

static bool Start (int connectPort)
 Attempts to start a host application (should be called from the main application) More...
 
static bool IsHostProcess ()
 Returns true if current process is considered to be a plugin host process. More...
 

Private Member Functions

void Stop () noexcept
 

Private Attributes

std::unique_ptr< IPCClientmClient
 
IPCChannelmChannel {nullptr}
 
detail::InputMessageReader mInputMessageReader
 
std::mutex mSync
 
std::condition_variable mRequestCondition
 
std::optional< wxString > mRequest
 
bool mRunning {true}
 

Static Private Attributes

static constexpr auto HostArgument = "--host"
 

Detailed Description

Internal class, processes plugin validation requests from the main app. Request is a simple string formatted by detail::MakeRequestString. After connection is established host starts to wait for a request from server. Once request is successfully processed host sends a reply. Host is capable to handle only one request at a time, so it's not allowed to send another request until host hasn't finish processing previous request.

Definition at line 35 of file PluginHost.h.

Constructor & Destructor Documentation

◆ PluginHost()

PluginHost::PluginHost ( int  connectPort)
explicit

Definition at line 77 of file PluginHost.cpp.

78{
80
81 wxFileName configFileName{ FileNames::Configuration() };
82 auto pConfig = std::make_unique<FileConfig>(
83 AppName, wxEmptyString, configFileName.GetFullPath(),
84 wxEmptyString, wxCONFIG_USE_LOCAL_FILE);
85 pConfig->Init();
86 InitPreferences(std::move(pConfig));
87
88 auto& moduleManager = ModuleManager::Get();
89 moduleManager.Initialize();
90 moduleManager.DiscoverProviders();
91
92 mClient = std::make_unique<IPCClient>(connectPort, *this);
93}
const std::wstring AppName
This program's name.
void InitPreferences(std::unique_ptr< FileConfig > uPrefs)
Definition: Prefs.cpp:200
static ModuleManager & Get()
std::unique_ptr< IPCClient > mClient
Definition: PluginHost.h:39
FILES_API FilePath Configuration()
FILES_API void InitializePathList()

References AppName, FileNames::Configuration(), ModuleManager::Get(), FileNames::InitializePathList(), InitPreferences(), and mClient.

Here is the call graph for this function:

Member Function Documentation

◆ IsHostProcess()

bool PluginHost::IsHostProcess ( )
static

Returns true if current process is considered to be a plugin host process.

Definition at line 208 of file PluginHost.cpp.

209{
210 return CommandLineArgs::argc >= 3 &&
211 wxStrcmp(CommandLineArgs::argv[1], HostArgument) == 0;
212}
static constexpr auto HostArgument
Definition: PluginHost.h:37
UTILITY_API const char *const * argv
A copy of argv; responsibility of application startup to assign it.
UTILITY_API int argc
A copy of argc; responsibility of application startup to assign it.

References CommandLineArgs::argc, CommandLineArgs::argv, and HostArgument.

Referenced by AudacityApp::Initialize(), main(), and PluginHostModule::OnInit().

Here is the caller graph for this function:

◆ OnConnect()

void PluginHost::OnConnect ( IPCChannel channel)
overridevirtualnoexcept

Called when connection established.

Parameters
channelUsing this channel client or server can send data to the other side.

Implements IPCChannelStatusCallback.

Definition at line 95 of file PluginHost.cpp.

96{
97 std::lock_guard lck(mSync);
98 mChannel = &channel;
99}
std::mutex mSync
Definition: PluginHost.h:42
IPCChannel * mChannel
Definition: PluginHost.h:40

◆ OnConnectionError()

void PluginHost::OnConnectionError ( )
overridevirtualnoexcept

Called when connection attempt fails.

Implements IPCChannelStatusCallback.

Definition at line 106 of file PluginHost.cpp.

107{
108 Stop();
109}
void Stop() noexcept
Definition: PluginHost.cpp:170

References Stop().

Here is the call graph for this function:

◆ OnDataAvailable()

void PluginHost::OnDataAvailable ( const void *  data,
size_t  size 
)
overridevirtualnoexcept

Called when data chunk received as a result of a preceding call to IPCChannel::Send. Generally, data pointer should not be accessed outside this method, copied if necessary.

Parameters
dataPointer to the chunk
sizeSize of the chunk

Implements IPCChannelStatusCallback.

Definition at line 111 of file PluginHost.cpp.

112{
113 try
114 {
117 {
118 {
119 std::lock_guard lck(mSync);
120 assert(!mRequest);
122 }
123 mRequestCondition.notify_one();
124 }
125 }
126 catch(...)
127 {
128 Stop();
129 }
130}
detail::InputMessageReader mInputMessageReader
Definition: PluginHost.h:41
std::optional< wxString > mRequest
Definition: PluginHost.h:44
std::condition_variable mRequestCondition
Definition: PluginHost.h:43
void ConsumeBytes(const void *bytes, size_t length)
fills internal buffer
bool CanPop() const noexcept

References size.

◆ OnDisconnect()

void PluginHost::OnDisconnect ( )
overridevirtualnoexcept

Invalidates IPCChannel passed as argument in OnConnect.

Implements IPCChannelStatusCallback.

Definition at line 101 of file PluginHost.cpp.

102{
103 Stop();
104}

References Stop().

Here is the call graph for this function:

◆ Serve()

bool PluginHost::Serve ( )

Definition at line 132 of file PluginHost.cpp.

133{
134 std::unique_lock lck(mSync);
135 mRequestCondition.wait(lck, [this]{ return !mRunning || mRequest.has_value(); });
136
137 if(!mRunning)
138 return false;
139
140 if(mRequest)
141 {
142 if(mChannel)
143 detail::PutMessage(*mChannel, wxEmptyString);
144
145 std::optional<wxString> request;
146 mRequest.swap(request);
147
148 lck.unlock();
149
150 wxString providerId;
151 wxString pluginPath;
153 if(detail::ParseRequestString(*request, providerId, pluginPath))
154 Discover(result, providerId, pluginPath);
155 else
156 result.SetError("malformed request string");
157
158 XMLStringWriter xmlWriter;
159 result.WriteXML(xmlWriter);
160
161 lck.lock();
162 if(mChannel)
163 detail::PutMessage(*mChannel, xmlWriter);
164 }
165
166 return true;
167}
bool mRunning
Definition: PluginHost.h:46
Wrapper to output XML data to strings.
Definition: XMLWriter.h:140
void WriteXML(XMLWriter &writer) const
void SetError(const wxString &msg)
void Discover(detail::PluginValidationResult &result, const wxString &providerId, const wxString &pluginPath)
Definition: PluginHost.cpp:31
bool ParseRequestString(const wxString &req, wxString &providerId, wxString &pluginPath)
void PutMessage(IPCChannel &channel, const wxString &value)

References anonymous_namespace{PluginHost.cpp}::Discover(), mChannel, mRequest, mRequestCondition, mRunning, mSync, detail::ParseRequestString(), detail::PutMessage(), detail::PluginValidationResult::SetError(), and detail::PluginValidationResult::WriteXML().

Referenced by PluginHostModule::OnInit().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Start()

bool PluginHost::Start ( int  connectPort)
static

Attempts to start a host application (should be called from the main application)

Returns
true if host has started successfully

Definition at line 190 of file PluginHost.cpp.

191{
192 const auto cmd = wxString::Format("\"%s\" %s %d",
195 connectPort);
196
197 auto process = std::make_unique<wxProcess>();
198 process->Detach();
199 if(wxExecute(cmd, wxEXEC_ASYNC, process.get()) != 0)
200 {
201 //process will delete itself upon termination
202 process.release();
203 return true;
204 }
205 return false;
206}
static const FilePath & GetExecutablePath()

References PlatformCompatibility::GetExecutablePath(), and HostArgument.

Referenced by AsyncPluginValidator::Impl::StartHost().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Stop()

void PluginHost::Stop ( )
privatenoexcept

Definition at line 170 of file PluginHost.cpp.

171{
172 try
173 {
174 {
175 std::lock_guard lck(mSync);//may throw
176 mRunning = false;
177 mChannel = nullptr;
178 }
179 }
180 catch(...)
181 {
182 //If something went wrong with mutex locking we'll try to
183 //awake main thread if it's blocked on condition variable.
184 //Attempt to relock the mutex there should throw as well(?)...
185 //which, in turn, will result in std::terminate being called
186 }
187 mRequestCondition.notify_one();
188}

References mChannel, mRequestCondition, mRunning, and mSync.

Referenced by OnConnectionError(), and OnDisconnect().

Here is the caller graph for this function:

Member Data Documentation

◆ HostArgument

constexpr auto PluginHost::HostArgument = "--host"
staticconstexprprivate

Definition at line 37 of file PluginHost.h.

Referenced by IsHostProcess(), and Start().

◆ mChannel

IPCChannel* PluginHost::mChannel {nullptr}
private

Definition at line 40 of file PluginHost.h.

Referenced by Serve(), and Stop().

◆ mClient

std::unique_ptr<IPCClient> PluginHost::mClient
private

Definition at line 39 of file PluginHost.h.

Referenced by PluginHost().

◆ mInputMessageReader

detail::InputMessageReader PluginHost::mInputMessageReader
private

Definition at line 41 of file PluginHost.h.

◆ mRequest

std::optional<wxString> PluginHost::mRequest
private

Definition at line 44 of file PluginHost.h.

Referenced by Serve().

◆ mRequestCondition

std::condition_variable PluginHost::mRequestCondition
private

Definition at line 43 of file PluginHost.h.

Referenced by Serve(), and Stop().

◆ mRunning

bool PluginHost::mRunning {true}
private

Definition at line 46 of file PluginHost.h.

Referenced by Serve(), and Stop().

◆ mSync

std::mutex PluginHost::mSync
private

Definition at line 42 of file PluginHost.h.

Referenced by Serve(), and Stop().


The documentation for this class was generated from the following files: