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/. */
19 // 1 MB should be enough for everyone.
20 static const uint32_t MAX_PREF_LENGTH
= 1 * 1024 * 1024;
21 // Actually, 4kb should be enough for everyone.
22 static const uint32_t MAX_ADVISABLE_PREF_LENGTH
= 4 * 1024;
31 struct PrefHashEntry
: PLDHashEntryHdr
34 PrefValue defaultPref
;
41 // The Init function initializes the preference context and creates
42 // the preference hashtable.
48 // Cleanup should be called at program exit to free the
49 // list of registered callbacks.
52 void PREF_CleanupPrefs();
56 // Preference flags, including the native type of the preference
60 typedef enum { PREF_INVALID
= 0,
61 PREF_LOCKED
= 1, PREF_USERSET
= 2, PREF_CONFIG
= 4, PREF_REMOTE
= 8,
62 PREF_LILOCAL
= 16, PREF_STRING
= 32, PREF_INT
= 64, PREF_BOOL
= 128,
63 PREF_HAS_DEFAULT
= 256,
64 PREF_VALUETYPE_MASK
= (PREF_STRING
| PREF_INT
| PREF_BOOL
)
69 // Set the various types of preferences. These functions take a dotted
70 // notation of the preference name (e.g. "browser.startup.homepage").
71 // Note that this will cause the preference to be saved to the file if
72 // it is different from the default. In other words, these are used
73 // to set the _user_ preferences.
75 // If set_default is set to true however, it sets the default value.
76 // This will only affect the program behavior if the user does not have a value
77 // saved over it for the particular preference. In addition, these will never
78 // be saved out to disk.
80 // Each set returns PREF_VALUECHANGED if the user value changed
81 // (triggering a callback), or PREF_NOERROR if the value was unchanged.
84 nsresult
PREF_SetCharPref(const char *pref
,const char* value
, bool set_default
= false);
85 nsresult
PREF_SetIntPref(const char *pref
,int32_t value
, bool set_default
= false);
86 nsresult
PREF_SetBoolPref(const char *pref
,bool value
, bool set_default
= false);
88 bool PREF_HasUserPref(const char* pref_name
);
92 // Get the various types of preferences. These functions take a dotted
93 // notation of the preference name (e.g. "browser.startup.homepage")
95 // They also take a pointer to fill in with the return value and return an
96 // error value. At the moment, this is simply an int but it may
97 // be converted to an enum once the global error strategy is worked out.
99 // They will perform conversion if the type doesn't match what was requested.
100 // (if it is reasonably possible)
103 nsresult
PREF_GetIntPref(const char *pref
,
104 int32_t * return_int
, bool get_default
);
105 nsresult
PREF_GetBoolPref(const char *pref
, bool * return_val
, bool get_default
);
108 // These functions are similar to the above "Get" version with the significant
109 // difference that the preference module will alloc the memory (e.g. XP_STRDUP) and
110 // the caller will need to be responsible for freeing it...
113 nsresult
PREF_CopyCharPref(const char *pref
, char ** return_buf
, bool get_default
);
116 // bool function that returns whether or not the preference is locked and therefore
117 // cannot be changed.
120 bool PREF_PrefIsLocked(const char *pref_name
);
124 // Function that sets whether or not the preference is locked and therefore
125 // cannot be changed.
128 nsresult
PREF_LockPref(const char *key
, bool lockIt
);
130 PrefType
PREF_GetPrefType(const char *pref_name
);
133 * Delete a branch of the tree
135 nsresult
PREF_DeleteBranch(const char *branch_name
);
138 * Clears the given pref (reverts it to its default value)
140 nsresult
PREF_ClearUserPref(const char *pref_name
);
143 * Clears all user prefs
145 nsresult
PREF_ClearAllUserPrefs();
150 // The callback function will get passed the pref_node which triggered the call
151 // and the void * instance_data which was passed to the register callback function.
152 // Return a non-zero result (nsresult) to pass an error up to the caller.
155 /* Temporarily conditionally compile PrefChangedFunc typedef.
156 ** During migration from old libpref to nsIPref we need it in
157 ** both header files. Eventually prefapi.h will become a private
158 ** file. The two types need to be in sync for now. Certain
159 ** compilers were having problems with multiple definitions.
161 #ifndef have_PrefChangedFunc_typedef
162 typedef void (*PrefChangedFunc
) (const char *, void *);
163 #define have_PrefChangedFunc_typedef
168 // Register a callback. This takes a node in the preference tree and will
169 // call the callback function if anything below that node is modified.
170 // Unregister returns PREF_NOERROR if a callback was found that
171 // matched all the parameters; otherwise it returns PREF_ERROR.
174 void PREF_RegisterCallback( const char* domain
,
175 PrefChangedFunc callback
, void* instance_data
);
176 nsresult
PREF_UnregisterCallback( const char* domain
,
177 PrefChangedFunc callback
, void* instance_data
);
180 * Used by nsPrefService as the callback function of the 'pref' parser
182 void PREF_ReaderCallback( void *closure
,