llexperiencecache.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * @file llexperiencecache.h
  3. * @brief Caches information relating to experience keys
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewergpl$
  6. *
  7. * Copyright (c) 2012, Linden Research, Inc.
  8. *
  9. * Second Life Viewer Source Code
  10. * The source code in this file ("Source Code") is provided by Linden Lab
  11. * to you under the terms of the GNU General Public License, version 2.0
  12. * ("GPL"), unless you have obtained a separate licensing agreement
  13. * ("Other License"), formally executed by you and Linden Lab. Terms of
  14. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  15. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  16. *
  17. * There are special exceptions to the terms and conditions of the GPL as
  18. * it is applied to this Source Code. View the full text of the exception
  19. * in the file doc/FLOSS-exception.txt in this software distribution, or
  20. * online at
  21. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  22. *
  23. * By copying, modifying or distributing this software, you acknowledge
  24. * that you have read and understood your obligations described above,
  25. * and agree to abide by those obligations.
  26. *
  27. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  28. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  29. * COMPLETENESS OR PERFORMANCE.
  30. * $/LicenseInfo$
  31. */
  32. #ifndef LL_LLEXPERIENCECACHE_H
  33. #define LL_LLEXPERIENCECACHE_H
  34. #include "boost/function.hpp"
  35. #include "boost/signals2.hpp"
  36. #include "llcorehttputil.h"
  37. #include "hbfastmap.h"
  38. #include "llframetimer.h"
  39. #include "llsingleton.h"
  40. #include "llsd.h"
  41. #include "lluuid.h"
  42. class LLUUID;
  43. class LLExperienceCache final : public LLSingleton<LLExperienceCache>
  44. {
  45. friend class LLSingleton<LLExperienceCache>;
  46. protected:
  47. LOG_CLASS(LLExperienceCache);
  48. public:
  49. typedef boost::function<const std::string&(const char*)> cap_query_fn_t;
  50. LL_INLINE void setCapabilityQuery(cap_query_fn_t queryfn)
  51. {
  52. mCapability = queryfn;
  53. }
  54. void importFile(std::istream& istr);
  55. void exportFile(std::ostream& ostr) const;
  56. void cleanup();
  57. void erase(const LLUUID& key);
  58. bool fetch(const LLUUID& key, bool refresh = false);
  59. void insert(const LLSD& experience_data);
  60. const LLSD& get(const LLUUID& key);
  61. // If name information is in cache, callback will be called immediately.
  62. typedef boost::function<void(const LLSD&)> experience_get_fn_t;
  63. void get(const LLUUID& key, experience_get_fn_t slot);
  64. bool isRequestPending(const LLUUID& public_key);
  65. void fetchAssociatedExperience(const LLUUID& object_id,
  66. const LLUUID& item_id,
  67. const std::string& cap_url,
  68. experience_get_fn_t fn);
  69. void findExperienceByName(std::string text, int page,
  70. experience_get_fn_t fn);
  71. void getGroupExperiences(const LLUUID& group_id, experience_get_fn_t fn);
  72. // The get/set Region Experiences take a CapabilityQuery to get the
  73. // capability since the region being queried may not be the region that
  74. // the agent is standing on.
  75. void getRegionExperiences(cap_query_fn_t regioncaps,
  76. experience_get_fn_t fn);
  77. void setRegionExperiences(cap_query_fn_t regioncaps, const LLSD& exp,
  78. experience_get_fn_t fn);
  79. void getExperiencePermission(const LLUUID& exp_id, experience_get_fn_t fn);
  80. void setExperiencePermission(const LLUUID& exp_id, const std::string& perm,
  81. experience_get_fn_t fn);
  82. void forgetExperiencePermission(const LLUUID& exp_id,
  83. experience_get_fn_t fn);
  84. void getExperienceAdmin(const LLUUID& exp_id, experience_get_fn_t fn);
  85. void updateExperience(LLSD update_data, experience_get_fn_t fn);
  86. static void setLookupURL(const std::string& lookup_url);
  87. static bool FilterWithProperty(const LLSD& experience, S32 prop);
  88. static bool FilterWithoutProperty(const LLSD& experience, S32 prop);
  89. static bool FilterWithoutProperties(const LLSD& experience, S32 prop);
  90. static bool FilterMatching(const LLSD& experience, const LLUUID& id);
  91. private:
  92. LLExperienceCache();
  93. ~LLExperienceCache() override;
  94. void initSingleton() override;
  95. typedef boost::function<LLSD(LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t& adapter,
  96. std::string)> permissionInvoker_fn;
  97. void processExperience(const LLUUID& public_key, const LLSD& experience);
  98. void idleCoro();
  99. void eraseExpired();
  100. typedef LLCoreHttpUtil::HttpCoroutineAdapter::ptr_t adapter_ptr_t;
  101. void requestExperiencesCoro(adapter_ptr_t& adapter, std::string url,
  102. uuid_list_t requests);
  103. void requestExperiences();
  104. void fetchAssociatedExperienceCoro(adapter_ptr_t& adapter,
  105. LLUUID object_id, LLUUID item_id,
  106. std::string url,
  107. experience_get_fn_t fn);
  108. void findExperienceByNameCoro(adapter_ptr_t& adapter, std::string text,
  109. int page, experience_get_fn_t fn);
  110. void getGroupExperiencesCoro(adapter_ptr_t& adapter, LLUUID group_id,
  111. experience_get_fn_t fn);
  112. void regionExperiencesCoro(adapter_ptr_t& adapter,
  113. cap_query_fn_t regioncaps, bool update,
  114. LLSD experiences, experience_get_fn_t fn);
  115. void experiencePermissionCoro(adapter_ptr_t& adapter,
  116. permissionInvoker_fn invokerfn,
  117. std::string url, experience_get_fn_t fn);
  118. void getExperienceAdminCoro(adapter_ptr_t& adapter, LLUUID exp_id,
  119. experience_get_fn_t fn);
  120. void updateExperienceCoro(adapter_ptr_t& adapter, LLSD updateData,
  121. experience_get_fn_t fn);
  122. #if 0 // Not used
  123. // maps an experience private key to the experience id
  124. LLUUID getExperienceId(const LLUUID& private_key,
  125. bool null_if_not_found = false);
  126. void bootstrap(const LLSD& legacy_keys, S32 initial_expiration);
  127. static void mapKeys(const LLSD& legacy_keys);
  128. #endif
  129. typedef fast_hmap<LLUUID, LLSD> cache_t;
  130. LL_INLINE const cache_t& getCached()
  131. {
  132. return mCache;
  133. }
  134. LL_INLINE friend std::ostream& operator<<(std::ostream& os,
  135. const LLExperienceCache& cache)
  136. {
  137. cache.exportFile(os);
  138. return os;
  139. }
  140. LL_INLINE friend std::istream& operator>>(std::istream& is,
  141. LLExperienceCache& cache)
  142. {
  143. cache.importFile(is);
  144. return is;
  145. }
  146. static F64 getErrorRetryDeltaTime(S32 status, LLSD headers);
  147. static bool maxAgeFromCacheControl(const std::string& cache_control,
  148. S32* max_age);
  149. public:
  150. static const std::string MISSING;
  151. static const std::string AGENT_ID;
  152. static const std::string GROUP_ID;
  153. static const std::string PRIVATE_KEY;
  154. static const std::string EXPERIENCE_ID;
  155. static const std::string NAME;
  156. static const std::string PROPERTIES;
  157. static const std::string EXPIRES;
  158. static const std::string DESCRIPTION;
  159. static const std::string QUOTA;
  160. static const std::string MATURITY;
  161. static const std::string METADATA;
  162. static const std::string SLURL;
  163. static constexpr S32 PROPERTY_INVALID = 1 << 0;
  164. static constexpr S32 PROPERTY_PRIVILEGED = 1 << 3;
  165. static constexpr S32 PROPERTY_GRID = 1 << 4;
  166. static constexpr S32 PROPERTY_PRIVATE = 1 << 5;
  167. static constexpr S32 PROPERTY_DISABLED = 1 << 6;
  168. static constexpr S32 PROPERTY_SUSPENDED = 1 << 7;
  169. private:
  170. typedef boost::signals2::signal<void(const LLSD& exp)> callback_signal_t;
  171. typedef std::shared_ptr<callback_signal_t> signal_ptr;
  172. // May have multiple callbacks for a single ID, which are represented as
  173. // multiple slots bound to the signal. Avoid copying signals via pointers.
  174. typedef fast_hmap<LLUUID, signal_ptr> signal_map_t;
  175. signal_map_t mSignalMap;
  176. cache_t mCache;
  177. uuid_list_t mRequestQueue;
  178. typedef fast_hmap<LLUUID, F64> pending_map_t;
  179. pending_map_t mPendingQueue;
  180. // To periodically clean out expired entries from the cache
  181. LLFrameTimer mEraseExpiredTimer;
  182. cap_query_fn_t mCapability;
  183. LLCore::HttpOptions::ptr_t mHttpOptions;
  184. LLCore::HttpHeaders::ptr_t mHttpHeaders;
  185. #if 0 // Not used
  186. typedef fast_hmap<LLUUID, LLUUID> key_map_t;
  187. static key_map_t sPrivateToPublicKeyMap;
  188. #endif
  189. static std::string sLookupURL;
  190. static constexpr F64 DEFAULT_EXPIRATION = 600.0;
  191. static constexpr S32 DEFAULT_QUOTA = 128; // In megabytes
  192. static constexpr S32 SEARCH_PAGE_SIZE = 30;
  193. static bool sShutdown;
  194. };
  195. #endif // LL_LLEXPERIENCECACHE_H