Audacity 3.2.0
PluginIPCUtils.cpp
Go to the documentation of this file.
1/**********************************************************************
2
3 Audacity: A Digital Audio Editor
4
5 @file PluginIPCUtils.cpp
6
7 @author Vitaly Sverchinsky
8
9 Part of lib-module-manager library.
10
11**********************************************************************/
12
13#include "PluginIPCUtils.h"
14
15#include <cstring>
16
17#include "PluginDescriptor.h"
18
19#include "IPCChannel.h"
20#include "XMLWriter.h"
21
22using namespace detail;
23
24namespace {
25 using HeaderBlock = size_t;
26 constexpr auto HeaderBlockSize = sizeof(HeaderBlock);
27
28 constexpr auto NodePlugin = "Plugin";
29 constexpr auto NodeError = "Error";
30 constexpr auto AttrErrorMessage = "msg";
31}
32
33bool detail::ParseRequestString(const wxString& req, wxString& providerId, wxString& pluginPath)
34{
35 auto strings = wxSplit(req, ';');
36 if(strings.size() == 2)
37 {
38 providerId = strings[0];
39 pluginPath = strings[1];
40 return true;
41 }
42 return false;
43}
44
45wxString detail::MakeRequestString(const wxString& providerId, const wxString& pluginPath)
46{
47 return wxJoin(wxArrayStringEx {providerId, pluginPath}, ';');
48}
49
50void detail::PutMessage(IPCChannel& channel, const wxString& value)
51{
52 auto utf8 = value.ToUTF8();
53 const HeaderBlock length = utf8.length();
54 channel.Send(&length, HeaderBlockSize);
55 if(length > 0)
56 channel.Send(utf8.data(), length);
57}
58
59void InputMessageReader::ConsumeBytes(const void* bytes, size_t length)
60{
61 auto from = mBuffer.size();
62 mBuffer.resize(from + length);
63 std::memcpy(&mBuffer[from], bytes, length);
64}
65
66bool InputMessageReader::CanPop() const noexcept
67{
68 if(mBuffer.size() >= HeaderBlockSize)
69 {
70 auto expectedSize = *reinterpret_cast<const HeaderBlock*>(mBuffer.data());
71 return mBuffer.size() >= expectedSize + HeaderBlockSize;
72 }
73 return false;
74}
75
77{
78 wxString message;
79 const auto length = *reinterpret_cast<const HeaderBlock*>(mBuffer.data());
80 assert(mBuffer.size() >= length + HeaderBlockSize);
81 if(length > 0)
82 message = wxString::FromUTF8(mBuffer.data() + HeaderBlockSize, length);
83 mBuffer.erase(mBuffer.begin(), mBuffer.begin() + HeaderBlockSize + length);
84 return message;
85}
86
88{
89 return !mHasError && !mDescriptors.empty();
90}
91
93{
94 return mHasError;
95}
96
97const wxString& PluginValidationResult::GetErrorMessage() const noexcept
98{
99 return mErrorMessage;
100}
101
103{
104 mDescriptors.push_back(std::move(desc));
105}
106
107void PluginValidationResult::SetError(const wxString& msg)
108{
109 mHasError = true;
110 mErrorMessage = msg;
111}
112
113const std::vector<PluginDescriptor>& PluginValidationResult::GetDescriptors() const noexcept
114{
115 return mDescriptors;
116}
117
118bool PluginValidationResult::HandleXMLTag(const std::string_view& tag, const AttributesList& attrs)
119{
120 if(tag == NodeError)
121 {
122 mHasError = true;
123 for(auto& p : attrs)
124 {
125 auto key = wxString(p.first.data(), p.first.length());
126 auto& attr = p.second;
127 if(key == AttrErrorMessage)
128 mErrorMessage = attr.ToWString();
129 }
130 }
131 return true;
132}
133void PluginValidationResult::HandleXMLEndTag(const std::string_view&) { }
134
136{
138 {
139 mDescriptors.resize(mDescriptors.size() + 1);
140 return &mDescriptors.back();
141 }
142 return nullptr;
143}
144
146{
147 if(mHasError)
148 {
149 writer.StartTag(NodeError);
151 writer.EndTag(NodeError);
152 }
153 if(!mDescriptors.empty())
154 {
155 writer.StartTag(NodePlugin);
156 for(auto& desc : mDescriptors)
157 desc.WriteXML(writer);
158 writer.EndTag(NodePlugin);
159 }
160}
static const AudacityProject::AttachedObjects::RegisteredFactory key
const TranslatableString desc
Definition: ExportPCM.cpp:58
std::vector< Attribute > AttributesList
Definition: XMLTagHandler.h:40
Interface for sending data from client to server or vice versa, complemented by IPCChannelStatusCallb...
Definition: IPCChannel.h:22
virtual void Send(const void *bytes, size_t length)=0
Write data to the channel.
static constexpr auto XMLNodeName
This class is an interface which should be implemented by classes which wish to be able to load and s...
Definition: XMLTagHandler.h:42
Base class for XMLFileWriter and XMLStringWriter that provides the general functionality for creating...
Definition: XMLWriter.h:26
virtual void StartTag(const wxString &name)
Definition: XMLWriter.cpp:80
void WriteAttr(const wxString &name, const Identifier &value)
Definition: XMLWriter.h:37
virtual void EndTag(const wxString &name)
Definition: XMLWriter.cpp:103
bool CanPop() const noexcept
std::vector< char > mBuffer
const wxString & GetErrorMessage() const noexcept
void WriteXML(XMLWriter &writer) const
const std::vector< PluginDescriptor > & GetDescriptors() const noexcept
bool IsValid() const noexcept
void SetError(const wxString &msg)
void HandleXMLEndTag(const std::string_view &) override
bool HasError() const noexcept
std::vector< PluginDescriptor > mDescriptors
void Add(PluginDescriptor &&desc)
XMLTagHandler * HandleXMLChild(const std::string_view &tag) override
bool HandleXMLTag(const std::string_view &tag, const AttributesList &attrs) override
Extend wxArrayString with move operations and construction and insertion fromstd::initializer_list.
bool ParseRequestString(const wxString &req, wxString &providerId, wxString &pluginPath)
void PutMessage(IPCChannel &channel, const wxString &value)
wxString MakeRequestString(const wxString &providerId, const wxString &pluginPath)