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{
81
82 auto& moduleManager = ModuleManager::Get();
83 moduleManager.Initialize();
84 moduleManager.DiscoverProviders();
85
86 mClient = std::make_unique<IPCClient>(connectPort, *this);
87}
void InitPreferences(std::unique_ptr< audacity::BasicSettings > uPrefs)
Definition: Prefs.cpp:231
static result_type Call(Arguments &&...arguments)
Null check of the installed function is done for you.
static ModuleManager & Get()
std::unique_ptr< IPCClient > mClient
Definition: PluginHost.h:39
FILES_API void InitializePathList()

References GlobalHook< ApplicationSettings, std::unique_ptr< BasicSettings >()>::Call(), 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 202 of file PluginHost.cpp.

203{
204 return CommandLineArgs::argc >= 3 &&
205 wxStrcmp(CommandLineArgs::argv[1], HostArgument) == 0;
206}
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 89 of file PluginHost.cpp.

90{
91 std::lock_guard lck(mSync);
92 mChannel = &channel;
93}
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 100 of file PluginHost.cpp.

101{
102 Stop();
103}
void Stop() noexcept
Definition: PluginHost.cpp:164

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 105 of file PluginHost.cpp.

106{
107 try
108 {
111 {
112 {
113 std::lock_guard lck(mSync);
114 assert(!mRequest);
116 }
117 mRequestCondition.notify_one();
118 }
119 }
120 catch(...)
121 {
122 Stop();
123 }
124}
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 95 of file PluginHost.cpp.

96{
97 Stop();
98}

References Stop().

Here is the call graph for this function:

◆ Serve()

bool PluginHost::Serve ( )

Definition at line 126 of file PluginHost.cpp.

127{
128 std::unique_lock lck(mSync);
129 mRequestCondition.wait(lck, [this]{ return !mRunning || mRequest.has_value(); });
130
131 if(!mRunning)
132 return false;
133
134 if(mRequest)
135 {
136 if(mChannel)
137 detail::PutMessage(*mChannel, wxEmptyString);
138
139 std::optional<wxString> request;
140 mRequest.swap(request);
141
142 lck.unlock();
143
144 wxString providerId;
145 wxString pluginPath;
147 if(detail::ParseRequestString(*request, providerId, pluginPath))
148 Discover(result, providerId, pluginPath);
149 else
150 result.SetError("malformed request string");
151
152 XMLStringWriter xmlWriter;
153 result.WriteXML(xmlWriter);
154
155 lck.lock();
156 if(mChannel)
157 detail::PutMessage(*mChannel, xmlWriter);
158 }
159
160 return true;
161}
bool mRunning
Definition: PluginHost.h:46
Wrapper to output XML data to strings.
Definition: XMLWriter.h:139
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 184 of file PluginHost.cpp.

185{
186 const auto cmd = wxString::Format("\"%s\" %s %d",
189 connectPort);
190
191 auto process = std::make_unique<wxProcess>();
192 process->Detach();
193 if(wxExecute(cmd, wxEXEC_ASYNC, process.get()) != 0)
194 {
195 //process will delete itself upon termination
196 process.release();
197 return true;
198 }
199 return false;
200}
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 164 of file PluginHost.cpp.

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

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: