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 channel.Send(utf8.data(), length);
56}
57
58void InputMessageReader::ConsumeBytes(const void* bytes, size_t length)
59{
60 auto from = mBuffer.size();
61 mBuffer.resize(from + length);
62 std::memcpy(&mBuffer[from], bytes, length);
63}
64
65bool InputMessageReader::CanPop() const noexcept
66{
67 if(mBuffer.size() >= HeaderBlockSize)
68 {
69 auto expectedSize = *reinterpret_cast<const HeaderBlock*>(mBuffer.data());
70 return mBuffer.size() >= expectedSize + HeaderBlockSize;
71 }
72 return false;
73}
74
76{
77 wxString message;
78 const auto length = *reinterpret_cast<const HeaderBlock*>(mBuffer.data());
79 assert(mBuffer.size() >= length + HeaderBlockSize);
80 if(length > 0)
81 message = wxString::FromUTF8(mBuffer.data() + HeaderBlockSize, length);
82 mBuffer.erase(mBuffer.begin(), mBuffer.begin() + HeaderBlockSize + length);
83 return message;
84}
85
87{
88 return !mHasError && !mDescriptors.empty();
89}
90
92{
93 return mHasError;
94}
95
96const wxString& PluginValidationResult::GetErrorMessage() const noexcept
97{
98 return mErrorMessage;
99}
100
102{
103 mDescriptors.push_back(std::move(desc));
104}
105
106void PluginValidationResult::SetError(const wxString& msg)
107{
108 mHasError = true;
109 mErrorMessage = msg;
110}
111
112const std::vector<PluginDescriptor>& PluginValidationResult::GetDescriptors() const noexcept
113{
114 return mDescriptors;
115}
116
117bool PluginValidationResult::HandleXMLTag(const std::string_view& tag, const AttributesList& attrs)
118{
119 if(tag == NodeError)
120 {
121 mHasError = true;
122 for(auto& p : attrs)
123 {
124 auto key = wxString(p.first.data(), p.first.length());
125 auto& attr = p.second;
126 if(key == AttrErrorMessage)
127 mErrorMessage = attr.ToWString();
128 }
129 }
130 return true;
131}
132void PluginValidationResult::HandleXMLEndTag(const std::string_view&) { }
133
135{
137 {
138 mDescriptors.resize(mDescriptors.size() + 1);
139 return &mDescriptors.back();
140 }
141 return nullptr;
142}
143
145{
146 if(mHasError)
147 {
148 writer.StartTag(NodeError);
150 writer.EndTag(NodeError);
151 }
152 if(!mDescriptors.empty())
153 {
154 writer.StartTag(NodePlugin);
155 for(auto& desc : mDescriptors)
156 desc.WriteXML(writer);
157 writer.EndTag(NodePlugin);
158 }
159}
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)