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