prefapi.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /*
  6. // <pre>
  7. */
  8. #ifndef PREFAPI_H
  9. #define PREFAPI_H
  10. #include "nscore.h"
  11. #include "PLDHashTable.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. // 1 MB should be enough for everyone.
  16. static const uint32_t MAX_PREF_LENGTH = 1 * 1024 * 1024;
  17. // Actually, 4kb should be enough for everyone.
  18. static const uint32_t MAX_ADVISABLE_PREF_LENGTH = 4 * 1024;
  19. typedef union
  20. {
  21. char* stringVal;
  22. int32_t intVal;
  23. bool boolVal;
  24. } PrefValue;
  25. /*
  26. // <font color=blue>
  27. // The Init function initializes the preference context and creates
  28. // the preference hashtable.
  29. // </font>
  30. */
  31. void PREF_Init();
  32. /*
  33. // Cleanup should be called at program exit to free the
  34. // list of registered callbacks.
  35. */
  36. void PREF_Cleanup();
  37. void PREF_CleanupPrefs();
  38. /*
  39. // <font color=blue>
  40. // Preference flags, including the native type of the preference. Changing any of these
  41. // values will require modifying the code inside of PrefTypeFlags class.
  42. // </font>
  43. */
  44. enum class PrefType {
  45. Invalid = 0,
  46. String = 1,
  47. Int = 2,
  48. Bool = 3,
  49. };
  50. // Keep the type of the preference, as well as the flags guiding its behaviour.
  51. class PrefTypeFlags
  52. {
  53. public:
  54. PrefTypeFlags() : mValue(AsInt(PrefType::Invalid)) {}
  55. explicit PrefTypeFlags(PrefType aType) : mValue(AsInt(aType)) {}
  56. PrefTypeFlags& Reset() { mValue = AsInt(PrefType::Invalid); return *this; }
  57. bool IsTypeValid() const { return !IsPrefType(PrefType::Invalid); }
  58. bool IsTypeString() const { return IsPrefType(PrefType::String); }
  59. bool IsTypeInt() const { return IsPrefType(PrefType::Int); }
  60. bool IsTypeBool() const { return IsPrefType(PrefType::Bool); }
  61. bool IsPrefType(PrefType type) const { return GetPrefType() == type; }
  62. PrefTypeFlags& SetPrefType(PrefType aType) {
  63. mValue = mValue - AsInt(GetPrefType()) + AsInt(aType);
  64. return *this;
  65. }
  66. PrefType GetPrefType() const {
  67. return (PrefType)(mValue & (AsInt(PrefType::String) |
  68. AsInt(PrefType::Int) |
  69. AsInt(PrefType::Bool)));
  70. }
  71. bool HasDefault() const { return mValue & PREF_FLAG_HAS_DEFAULT; }
  72. PrefTypeFlags& SetHasDefault(bool aSetOrUnset) { return SetFlag(PREF_FLAG_HAS_DEFAULT, aSetOrUnset); }
  73. bool HasStickyDefault() const { return mValue & PREF_FLAG_STICKY_DEFAULT; }
  74. PrefTypeFlags& SetHasStickyDefault(bool aSetOrUnset) { return SetFlag(PREF_FLAG_STICKY_DEFAULT, aSetOrUnset); }
  75. bool IsLocked() const { return mValue & PREF_FLAG_LOCKED; }
  76. PrefTypeFlags& SetLocked(bool aSetOrUnset) { return SetFlag(PREF_FLAG_LOCKED, aSetOrUnset); }
  77. bool HasUserValue() const { return mValue & PREF_FLAG_USERSET; }
  78. PrefTypeFlags& SetHasUserValue(bool aSetOrUnset) { return SetFlag(PREF_FLAG_USERSET, aSetOrUnset); }
  79. private:
  80. static uint16_t AsInt(PrefType aType) { return (uint16_t)aType; }
  81. PrefTypeFlags& SetFlag(uint16_t aFlag, bool aSetOrUnset) {
  82. mValue = aSetOrUnset ? mValue | aFlag : mValue & ~aFlag;
  83. return *this;
  84. }
  85. // Pack both the value of type (PrefType) and flags into the same int. This is why
  86. // the flag enum starts at 4, as PrefType occupies the bottom two bits.
  87. enum {
  88. PREF_FLAG_LOCKED = 4,
  89. PREF_FLAG_USERSET = 8,
  90. PREF_FLAG_CONFIG = 16,
  91. PREF_FLAG_REMOTE = 32,
  92. PREF_FLAG_LILOCAL = 64,
  93. PREF_FLAG_HAS_DEFAULT = 128,
  94. PREF_FLAG_STICKY_DEFAULT = 256,
  95. };
  96. uint16_t mValue;
  97. };
  98. struct PrefHashEntry : PLDHashEntryHdr
  99. {
  100. PrefTypeFlags prefFlags; // This field goes first to minimize struct size on 64-bit.
  101. const char *key;
  102. PrefValue defaultPref;
  103. PrefValue userPref;
  104. };
  105. /*
  106. // <font color=blue>
  107. // Set the various types of preferences. These functions take a dotted
  108. // notation of the preference name (e.g. "browser.startup.homepage").
  109. // Note that this will cause the preference to be saved to the file if
  110. // it is different from the default. In other words, these are used
  111. // to set the _user_ preferences.
  112. //
  113. // If set_default is set to true however, it sets the default value.
  114. // This will only affect the program behavior if the user does not have a value
  115. // saved over it for the particular preference. In addition, these will never
  116. // be saved out to disk.
  117. //
  118. // Each set returns PREF_VALUECHANGED if the user value changed
  119. // (triggering a callback), or PREF_NOERROR if the value was unchanged.
  120. // </font>
  121. */
  122. nsresult PREF_SetCharPref(const char *pref,const char* value, bool set_default = false);
  123. nsresult PREF_SetIntPref(const char *pref,int32_t value, bool set_default = false);
  124. nsresult PREF_SetBoolPref(const char *pref,bool value, bool set_default = false);
  125. bool PREF_HasUserPref(const char* pref_name);
  126. /*
  127. // <font color=blue>
  128. // Get the various types of preferences. These functions take a dotted
  129. // notation of the preference name (e.g. "browser.startup.homepage")
  130. //
  131. // They also take a pointer to fill in with the return value and return an
  132. // error value. At the moment, this is simply an int but it may
  133. // be converted to an enum once the global error strategy is worked out.
  134. //
  135. // They will perform conversion if the type doesn't match what was requested.
  136. // (if it is reasonably possible)
  137. // </font>
  138. */
  139. nsresult PREF_GetIntPref(const char *pref,
  140. int32_t * return_int, bool get_default);
  141. nsresult PREF_GetBoolPref(const char *pref, bool * return_val, bool get_default);
  142. /*
  143. // <font color=blue>
  144. // These functions are similar to the above "Get" version with the significant
  145. // difference that the preference module will alloc the memory (e.g. XP_STRDUP) and
  146. // the caller will need to be responsible for freeing it...
  147. // </font>
  148. */
  149. nsresult PREF_CopyCharPref(const char *pref, char ** return_buf, bool get_default);
  150. /*
  151. // <font color=blue>
  152. // bool function that returns whether or not the preference is locked and therefore
  153. // cannot be changed.
  154. // </font>
  155. */
  156. bool PREF_PrefIsLocked(const char *pref_name);
  157. /*
  158. // <font color=blue>
  159. // Function that sets whether or not the preference is locked and therefore
  160. // cannot be changed.
  161. // </font>
  162. */
  163. nsresult PREF_LockPref(const char *key, bool lockIt);
  164. PrefType PREF_GetPrefType(const char *pref_name);
  165. /*
  166. * Delete a branch of the tree
  167. */
  168. nsresult PREF_DeleteBranch(const char *branch_name);
  169. /*
  170. * Clears the given pref (reverts it to its default value)
  171. */
  172. nsresult PREF_ClearUserPref(const char *pref_name);
  173. /*
  174. * Clears all user prefs
  175. */
  176. nsresult PREF_ClearAllUserPrefs();
  177. /*
  178. // <font color=blue>
  179. // The callback function will get passed the pref_node which triggered the call
  180. // and the void * instance_data which was passed to the register callback function.
  181. // Return a non-zero result (nsresult) to pass an error up to the caller.
  182. // </font>
  183. */
  184. /* Temporarily conditionally compile PrefChangedFunc typedef.
  185. ** During migration from old libpref to nsIPref we need it in
  186. ** both header files. Eventually prefapi.h will become a private
  187. ** file. The two types need to be in sync for now. Certain
  188. ** compilers were having problems with multiple definitions.
  189. */
  190. #ifndef have_PrefChangedFunc_typedef
  191. typedef void (*PrefChangedFunc) (const char *, void *);
  192. #define have_PrefChangedFunc_typedef
  193. #endif
  194. /*
  195. // <font color=blue>
  196. // Register a callback. This takes a node in the preference tree and will
  197. // call the callback function if anything below that node is modified.
  198. // Unregister returns PREF_NOERROR if a callback was found that
  199. // matched all the parameters; otherwise it returns PREF_ERROR.
  200. // </font>
  201. */
  202. void PREF_RegisterCallback(const char* domain,
  203. PrefChangedFunc callback, void* instance_data );
  204. nsresult PREF_UnregisterCallback(const char* domain,
  205. PrefChangedFunc callback, void* instance_data );
  206. /*
  207. * Used by nsPrefService as the callback function of the 'pref' parser
  208. */
  209. void PREF_ReaderCallback( void *closure,
  210. const char *pref,
  211. PrefValue value,
  212. PrefType type,
  213. bool isDefault,
  214. bool isStickyDefault);
  215. /*
  216. * Callback whenever we change a preference
  217. */
  218. typedef void (*PrefsDirtyFunc) ();
  219. void PREF_SetDirtyCallback(PrefsDirtyFunc);
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif