llpluginmessage.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**
  2. * @file llpluginmessage.h
  3. * @brief LLPluginMessage encapsulates the serialization/deserialization of messages passed to and from plugins.
  4. *
  5. * $LicenseInfo:firstyear=2008&license=viewergpl$
  6. *
  7. * Copyright (c) 2008-2009, Linden Research, Inc.
  8. * Copyright (c) 2009-2024, Henri Beauchamp.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLPLUGINMESSAGE_H
  34. #define LL_LLPLUGINMESSAGE_H
  35. #include "llsd.h"
  36. // LLPluginMessage encapsulates the serialization/deserialization of
  37. // messages passed to and from plugins.
  38. class LLPluginMessage final
  39. {
  40. protected:
  41. LOG_CLASS(LLPluginMessage);
  42. public:
  43. LLPluginMessage() = default;
  44. LLPluginMessage(const LLPluginMessage& p);
  45. LLPluginMessage(const std::string& message_class,
  46. const std::string& message_name);
  47. // Resets the internal state
  48. void clear();
  49. // Sets the message class and name
  50. // Also has the side-effect of clearing any key/value pairs in the message.
  51. void setMessage(const std::string& message_class,
  52. const std::string& message_name);
  53. // Sets a key/value pair in the message
  54. void setValue(const std::string& key, const std::string& value);
  55. void setValueLLSD(const std::string& key, const LLSD& value);
  56. void setValueLLUUID(const std::string& key, const LLUUID& value);
  57. void setValueS32(const std::string& key, S32 value);
  58. void setValueU32(const std::string& key, U32 value);
  59. void setValueBoolean(const std::string& key, bool value);
  60. void setValueReal(const std::string& key, F64 value);
  61. void setValuePointer(const std::string& key, void* value);
  62. std::string getClass() const;
  63. std::string getName() const;
  64. // Returns true if the specified key exists in this message (useful for
  65. // optional parameters)
  66. bool hasValue(const std::string& key) const;
  67. // Gets the value of a particular key as a string. If the key does not
  68. // exist in the message, an empty string will be returned.
  69. std::string getValue(const std::string& key) const;
  70. // Gets the value of a particular key as LLSD. If the key does not exist in
  71. // the message, a null LLSD will be returned.
  72. LLSD getValueLLSD(const std::string& key) const;
  73. // Gets the value of a particular key as LLUUID. If the key does not exist
  74. // in the message, a null LLUUID will be returned.
  75. LLUUID getValueLLUUID(const std::string& key) const;
  76. // Gets the value of a key as a S32. If the value wasn't set as a S32,
  77. // behavior is undefined.
  78. S32 getValueS32(const std::string& key) const;
  79. // Gets the value of a key as a U32. Since there isn't an LLSD type for
  80. // this, we use a hexadecimal string instead.
  81. U32 getValueU32(const std::string& key) const;
  82. // Gets the value of a key as a Boolean.
  83. bool getValueBoolean(const std::string& key) const;
  84. // Gets the value of a key as a float.
  85. F64 getValueReal(const std::string& key) const;
  86. // Gets the value of a key as a pointer.
  87. void* getValuePointer(const std::string& key) const;
  88. LL_INLINE const LLSD& getParams() const { return mMessage["params"]; }
  89. // Flattens the message into a string
  90. std::string generate() const;
  91. // Parses an incoming message into component parts (this clears out any
  92. // existing state before starting the parse). Returns -1 on failure,
  93. // otherwise returns the number of key/value pairs in the message.
  94. S32 parse(const std::string& message);
  95. private:
  96. LLSD mMessage;
  97. };
  98. // Listener for plugin messages.
  99. class LLPluginMessageListener
  100. {
  101. public:
  102. LLPluginMessageListener() = default;
  103. virtual ~LLPluginMessageListener() = default;
  104. // Plugin receives message from plugin loader shell.
  105. virtual void receivePluginMessage(const LLPluginMessage& message) = 0;
  106. };
  107. // Dispatcher for plugin messages. Manages the set of plugin message listeners
  108. // and distributes messages to plugin message listeners.
  109. class LLPluginMessageDispatcher final
  110. {
  111. public:
  112. void addPluginMessageListener(LLPluginMessageListener*);
  113. void removePluginMessageListener(LLPluginMessageListener*);
  114. protected:
  115. void dispatchPluginMessage(const LLPluginMessage& message);
  116. protected:
  117. // A set of message listeners.
  118. typedef std::set<LLPluginMessageListener*> listener_set_t;
  119. // The set of message listeners.
  120. listener_set_t mListeners;
  121. };
  122. #endif // LL_LLPLUGINMESSAGE_H