nsIPrefBranch.idl 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /* -*- Mode: IDL; tab-width: 2; 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. #include "nsISupports.idl"
  6. interface nsIObserver;
  7. /**
  8. * The nsIPrefBranch interface is used to manipulate the preferences data. This
  9. * object may be obtained from the preferences service (nsIPrefService) and
  10. * used to get and set default and/or user preferences across the application.
  11. *
  12. * This object is created with a "root" value which describes the base point in
  13. * the preferences "tree" from which this "branch" stems. Preferences are
  14. * accessed off of this root by using just the final portion of the preference.
  15. * For example, if this object is created with the root "browser.startup.",
  16. * the preferences "browser.startup.page", "browser.startup.homepage",
  17. * and "browser.startup.homepage_override" can be accessed by simply passing
  18. * "page", "homepage", or "homepage_override" to the various Get/Set methods.
  19. *
  20. * @see nsIPrefService
  21. */
  22. [scriptable, uuid(55d25e49-793f-4727-a69f-de8b15f4b985)]
  23. interface nsIPrefBranch : nsISupports
  24. {
  25. /**
  26. * Values describing the basic preference types.
  27. *
  28. * @see getPrefType
  29. */
  30. const long PREF_INVALID = 0;
  31. const long PREF_STRING = 32;
  32. const long PREF_INT = 64;
  33. const long PREF_BOOL = 128;
  34. /**
  35. * Called to get the root on which this branch is based, such as
  36. * "browser.startup."
  37. */
  38. readonly attribute string root;
  39. /**
  40. * Called to determine the type of a specific preference.
  41. *
  42. * @param aPrefName The preference to get the type of.
  43. *
  44. * @return long A value representing the type of the preference. This
  45. * value will be PREF_STRING, PREF_INT, or PREF_BOOL.
  46. */
  47. long getPrefType(in string aPrefName);
  48. /**
  49. * Called to get the state of an individual boolean preference.
  50. *
  51. * @param aPrefName The boolean preference to get the state of.
  52. * @param aDefaultValue The value to return if the preference is not set.
  53. *
  54. * @return boolean The value of the requested boolean preference.
  55. *
  56. * @see setBoolPref
  57. */
  58. [optional_argc,binaryname(GetBoolPrefWithDefault)]
  59. boolean getBoolPref(in string aPrefName, [optional] in boolean aDefaultValue);
  60. [noscript,binaryname(GetBoolPref)]
  61. boolean getBoolPrefXPCOM(in string aPrefName);
  62. /**
  63. * Called to set the state of an individual boolean preference.
  64. *
  65. * @param aPrefName The boolean preference to set the state of.
  66. * @param aValue The boolean value to set the preference to.
  67. *
  68. * @throws Error if setting failed or the preference has a default
  69. value of a type other than boolean.
  70. *
  71. * @see getBoolPref
  72. */
  73. void setBoolPref(in string aPrefName, in boolean aValue);
  74. /**
  75. * Called to get the state of an individual floating-point preference.
  76. * "Floating point" preferences are really string preferences that
  77. * are converted to floating point numbers.
  78. *
  79. * @param aPrefName The floating point preference to get the state of.
  80. * @param aDefaultValue The value to return if the preference is not set.
  81. *
  82. * @return float The value of the requested floating point preference.
  83. *
  84. * @see setCharPref
  85. */
  86. [optional_argc,binaryname(GetFloatPrefWithDefault)]
  87. float getFloatPref(in string aPrefName, [optional] in float aDefaultValue);
  88. [noscript,binaryname(GetFloatPref)]
  89. float getFloatPrefXPCOM(in string aPrefName);
  90. /**
  91. * Called to get the state of an individual string preference.
  92. *
  93. * @param aPrefName The string preference to retrieve.
  94. * @param aDefaultValue The string to return if the preference is not set.
  95. *
  96. * @return string The value of the requested string preference.
  97. *
  98. * @see setCharPref
  99. */
  100. [optional_argc,binaryname(GetCharPrefWithDefault)]
  101. string getCharPref(in string aPrefName, [optional] in string aDefaultValue);
  102. [noscript,binaryname(GetCharPref)]
  103. string getCharPrefXPCOM(in string aPrefName);
  104. /**
  105. * Called to set the state of an individual string preference.
  106. *
  107. * @param aPrefName The string preference to set.
  108. * @param aValue The string value to set the preference to.
  109. *
  110. * @throws Error if setting failed or the preference has a default
  111. value of a type other than string.
  112. *
  113. * @see getCharPref
  114. */
  115. void setCharPref(in string aPrefName, in string aValue);
  116. /**
  117. * Called to get the state of an individual integer preference.
  118. *
  119. * @param aPrefName The integer preference to get the value of.
  120. * @param aDefaultValue The value to return if the preference is not set.
  121. *
  122. * @return long The value of the requested integer preference.
  123. *
  124. * @see setIntPref
  125. */
  126. [optional_argc,binaryname(GetIntPrefWithDefault)]
  127. long getIntPref(in string aPrefName, [optional] in long aDefaultValue);
  128. [noscript,binaryname(GetIntPref)]
  129. long getIntPrefXPCOM(in string aPrefName);
  130. /**
  131. * Called to set the state of an individual integer preference.
  132. *
  133. * @param aPrefName The integer preference to set the value of.
  134. * @param aValue The integer value to set the preference to.
  135. *
  136. * @throws Error if setting failed or the preference has a default
  137. value of a type other than integer.
  138. *
  139. * @see getIntPref
  140. */
  141. void setIntPref(in string aPrefName, in long aValue);
  142. /**
  143. * Called to get the state of an individual complex preference. A complex
  144. * preference is a preference which represents an XPCOM object that can not
  145. * be easily represented using a standard boolean, integer or string value.
  146. *
  147. * @param aPrefName The complex preference to get the value of.
  148. * @param aType The XPCOM interface that this complex preference
  149. * represents. Interfaces currently supported are:
  150. * - nsIFile
  151. * - nsISupportsString (UniChar)
  152. * - nsIPrefLocalizedString (Localized UniChar)
  153. * @param aValue The XPCOM object into which to the complex preference
  154. * value should be retrieved.
  155. *
  156. * @throws Error The value does not exist or is the wrong type.
  157. *
  158. * @see setComplexValue
  159. */
  160. void getComplexValue(in string aPrefName, in nsIIDRef aType,
  161. [iid_is(aType), retval] out nsQIResult aValue);
  162. /**
  163. * Called to set the state of an individual complex preference. A complex
  164. * preference is a preference which represents an XPCOM object that can not
  165. * be easily represented using a standard boolean, integer or string value.
  166. *
  167. * @param aPrefName The complex preference to set the value of.
  168. * @param aType The XPCOM interface that this complex preference
  169. * represents. Interfaces currently supported are:
  170. * - nsIFile
  171. * - nsISupportsString (UniChar)
  172. * - nsIPrefLocalizedString (Localized UniChar)
  173. * @param aValue The XPCOM object from which to set the complex preference
  174. * value.
  175. *
  176. * @throws Error if setting failed or the value is the wrong type.
  177. *
  178. * @see getComplexValue
  179. */
  180. void setComplexValue(in string aPrefName, in nsIIDRef aType, in nsISupports aValue);
  181. /**
  182. * Called to clear a user set value from a specific preference. This will, in
  183. * effect, reset the value to the default value. If no default value exists
  184. * the preference will cease to exist.
  185. *
  186. * @param aPrefName The preference to be cleared.
  187. *
  188. * @note
  189. * This method does nothing if this object is a default branch.
  190. */
  191. void clearUserPref(in string aPrefName);
  192. /**
  193. * Called to lock a specific preference. Locking a preference will cause the
  194. * preference service to always return the default value regardless of
  195. * whether there is a user set value or not.
  196. *
  197. * @param aPrefName The preference to be locked.
  198. *
  199. * @note
  200. * This method can be called on either a default or user branch but, in
  201. * effect, always operates on the default branch.
  202. *
  203. * @throws Error The preference does not exist or an error occurred.
  204. *
  205. * @see unlockPref
  206. */
  207. void lockPref(in string aPrefName);
  208. /**
  209. * Called to check if a specific preference has a user value associated to
  210. * it.
  211. *
  212. * @param aPrefName The preference to be tested.
  213. *
  214. * @note
  215. * This method can be called on either a default or user branch but, in
  216. * effect, always operates on the user branch.
  217. *
  218. * @note
  219. * If a preference was manually set to a value that equals the default value,
  220. * then the preference no longer has a user set value, i.e. it is
  221. * considered reset to its default value.
  222. * In particular, this method will return false for such a preference and
  223. * the preference will not be saved to a file by nsIPrefService.savePrefFile.
  224. *
  225. * @return boolean true The preference has a user set value.
  226. * false The preference only has a default value.
  227. */
  228. boolean prefHasUserValue(in string aPrefName);
  229. /**
  230. * Called to check if a specific preference is locked. If a preference is
  231. * locked calling its Get method will always return the default value.
  232. *
  233. * @param aPrefName The preference to be tested.
  234. *
  235. * @note
  236. * This method can be called on either a default or user branch but, in
  237. * effect, always operates on the default branch.
  238. *
  239. * @return boolean true The preference is locked.
  240. * false The preference is not locked.
  241. *
  242. * @see lockPref
  243. * @see unlockPref
  244. */
  245. boolean prefIsLocked(in string aPrefName);
  246. /**
  247. * Called to unlock a specific preference. Unlocking a previously locked
  248. * preference allows the preference service to once again return the user set
  249. * value of the preference.
  250. *
  251. * @param aPrefName The preference to be unlocked.
  252. *
  253. * @note
  254. * This method can be called on either a default or user branch but, in
  255. * effect, always operates on the default branch.
  256. *
  257. * @throws Error The preference does not exist or an error occurred.
  258. *
  259. * @see lockPref
  260. */
  261. void unlockPref(in string aPrefName);
  262. /**
  263. * Called to remove all of the preferences referenced by this branch.
  264. *
  265. * @param aStartingAt The point on the branch at which to start the deleting
  266. * preferences. Pass in "" to remove all preferences
  267. * referenced by this branch.
  268. *
  269. * @note
  270. * This method can be called on either a default or user branch but, in
  271. * effect, always operates on both.
  272. *
  273. * @throws Error The preference(s) do not exist or an error occurred.
  274. */
  275. void deleteBranch(in string aStartingAt);
  276. /**
  277. * Returns an array of strings representing the child preferences of the
  278. * root of this branch.
  279. *
  280. * @param aStartingAt The point on the branch at which to start enumerating
  281. * the child preferences. Pass in "" to enumerate all
  282. * preferences referenced by this branch.
  283. * @param aCount Receives the number of elements in the array.
  284. * @param aChildArray Receives the array of child preferences.
  285. *
  286. * @note
  287. * This method can be called on either a default or user branch but, in
  288. * effect, always operates on both.
  289. *
  290. * @throws Error The preference(s) do not exist or an error occurred.
  291. */
  292. void getChildList(in string aStartingAt,
  293. [optional] out unsigned long aCount,
  294. [array, size_is(aCount), retval] out string aChildArray);
  295. /**
  296. * Called to reset all of the preferences referenced by this branch to their
  297. * default values.
  298. *
  299. * @param aStartingAt The point on the branch at which to start the resetting
  300. * preferences to their default values. Pass in "" to
  301. * reset all preferences referenced by this branch.
  302. *
  303. * @note
  304. * This method can be called on either a default or user branch but, in
  305. * effect, always operates on the user branch.
  306. *
  307. * @throws Error The preference(s) do not exist or an error occurred.
  308. */
  309. void resetBranch(in string aStartingAt);
  310. /**
  311. * Add a preference change observer. On preference changes, the following
  312. * arguments will be passed to the nsIObserver.observe() method:
  313. * aSubject - The nsIPrefBranch object (this)
  314. * aTopic - The string defined by NS_PREFBRANCH_PREFCHANGE_TOPIC_ID
  315. * aData - The name of the preference which has changed, relative to
  316. * the |root| of the aSubject branch.
  317. *
  318. * aSubject.get*Pref(aData) will get the new value of the modified
  319. * preference. For example, if your observer is registered with
  320. * addObserver("bar.", ...) on a branch with root "foo.", modifying
  321. * the preference "foo.bar.baz" will trigger the observer, and aData
  322. * parameter will be "bar.baz".
  323. *
  324. * @param aDomain The preference on which to listen for changes. This can
  325. * be the name of an entire branch to observe.
  326. * e.g. Holding the "root" prefbranch and calling
  327. * addObserver("foo.bar.", ...) will observe changes to
  328. * foo.bar.baz and foo.bar.bzip
  329. * @param aObserver The object to be notified if the preference changes.
  330. * @param aHoldWeak true Hold a weak reference to |aObserver|. The object
  331. * must implement the nsISupportsWeakReference
  332. * interface or this will fail.
  333. * false Hold a strong reference to |aObserver|.
  334. *
  335. * @note
  336. * Registering as a preference observer can open an object to potential
  337. * cyclical references which will cause memory leaks. These cycles generally
  338. * occur because an object both registers itself as an observer (causing the
  339. * branch to hold a reference to the observer) and holds a reference to the
  340. * branch object for the purpose of getting/setting preference values. There
  341. * are 3 approaches which have been implemented in an attempt to avoid these
  342. * situations.
  343. * 1) The nsPrefBranch object supports nsISupportsWeakReference. Any consumer
  344. * may hold a weak reference to it instead of a strong one.
  345. * 2) The nsPrefBranch object listens for xpcom-shutdown and frees all of the
  346. * objects currently in its observer list. This ensures that long lived
  347. * objects (services for example) will be freed correctly.
  348. * 3) The observer can request to be held as a weak reference when it is
  349. * registered. This insures that shorter lived objects (say one tied to an
  350. * open window) will not fall into the cyclical reference trap.
  351. *
  352. * @note
  353. * The list of registered observers may be changed during the dispatch of
  354. * nsPref:changed notification. However, the observers are not guaranteed
  355. * to be notified in any particular order, so you can't be sure whether the
  356. * added/removed observer will be called during the notification when it
  357. * is added/removed.
  358. *
  359. * @note
  360. * It is possible to change preferences during the notification.
  361. *
  362. * @note
  363. * It is not safe to change observers during this callback in Gecko
  364. * releases before 1.9. If you want a safe way to remove a pref observer,
  365. * please use an nsITimer.
  366. *
  367. * @see nsIObserver
  368. * @see removeObserver
  369. */
  370. void addObserver(in string aDomain, in nsIObserver aObserver,
  371. in boolean aHoldWeak);
  372. /**
  373. * Remove a preference change observer.
  374. *
  375. * @param aDomain The preference which is being observed for changes.
  376. * @param aObserver An observer previously registered with addObserver().
  377. *
  378. * @note
  379. * Note that you must call removeObserver() on the same nsIPrefBranch
  380. * instance on which you called addObserver() in order to remove aObserver;
  381. * otherwise, the observer will not be removed.
  382. *
  383. * @see nsIObserver
  384. * @see addObserver
  385. */
  386. void removeObserver(in string aDomain, in nsIObserver aObserver);
  387. };
  388. %{C++
  389. #define NS_PREFBRANCH_CONTRACTID "@mozilla.org/preferencesbranch;1"
  390. /**
  391. * Notification sent when a preference changes.
  392. */
  393. #define NS_PREFBRANCH_PREFCHANGE_TOPIC_ID "nsPref:changed"
  394. %}