summaryrefslogtreecommitdiffstats
path: root/widget/android/PrefsHelper.h
blob: b053c979a4683c27f4040559d50845204cca4436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/* -*- Mode: c++; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef PrefsHelper_h
#define PrefsHelper_h

#include "GeneratedJNINatives.h"
#include "MainThreadUtils.h"
#include "nsAppShell.h"
#include "nsCOMPtr.h"
#include "nsVariant.h"

#include "mozilla/Preferences.h"
#include "mozilla/Services.h"

namespace mozilla {

class PrefsHelper
    : public java::PrefsHelper::Natives<PrefsHelper>
{
    PrefsHelper() = delete;

    static bool GetVariantPref(nsIObserverService* aObsServ,
                               nsIWritableVariant* aVariant,
                               jni::Object::Param aPrefHandler,
                               const jni::String::LocalRef& aPrefName)
    {
        if (NS_FAILED(aObsServ->NotifyObservers(aVariant, "android-get-pref",
                                                aPrefName->ToString().get()))) {
            return false;
        }

        uint16_t varType = nsIDataType::VTYPE_EMPTY;
        if (NS_FAILED(aVariant->GetDataType(&varType))) {
            return false;
        }

        int32_t type = java::PrefsHelper::PREF_INVALID;
        bool boolVal = false;
        int32_t intVal = 0;
        nsAutoString strVal;

        switch (varType) {
            case nsIDataType::VTYPE_BOOL:
                type = java::PrefsHelper::PREF_BOOL;
                if (NS_FAILED(aVariant->GetAsBool(&boolVal))) {
                    return false;
                }
                break;
            case nsIDataType::VTYPE_INT32:
                type = java::PrefsHelper::PREF_INT;
                if (NS_FAILED(aVariant->GetAsInt32(&intVal))) {
                    return false;
                }
                break;
            case nsIDataType::VTYPE_ASTRING:
                type = java::PrefsHelper::PREF_STRING;
                if (NS_FAILED(aVariant->GetAsAString(strVal))) {
                    return false;
                }
                break;
            default:
                return false;
        }

        jni::StringParam jstrVal(type == java::PrefsHelper::PREF_STRING ?
                jni::StringParam(strVal, aPrefName.Env()) :
                jni::StringParam(nullptr));

        if (aPrefHandler) {
            java::PrefsHelper::CallPrefHandler(
                    aPrefHandler, type, aPrefName,
                    boolVal, intVal, jstrVal);
        } else {
            java::PrefsHelper::OnPrefChange(
                    aPrefName, type, boolVal, intVal, jstrVal);
        }
        return true;
    }

    static bool SetVariantPref(nsIObserverService* aObsServ,
                               nsIWritableVariant* aVariant,
                               jni::String::Param aPrefName,
                               bool aFlush,
                               int32_t aType,
                               bool aBoolVal,
                               int32_t aIntVal,
                               jni::String::Param aStrVal)
    {
        nsresult rv = NS_ERROR_FAILURE;

        switch (aType) {
            case java::PrefsHelper::PREF_BOOL:
                rv = aVariant->SetAsBool(aBoolVal);
                break;
            case java::PrefsHelper::PREF_INT:
                rv = aVariant->SetAsInt32(aIntVal);
                break;
            case java::PrefsHelper::PREF_STRING:
                rv = aVariant->SetAsAString(aStrVal->ToString());
                break;
        }

        if (NS_SUCCEEDED(rv)) {
            rv = aObsServ->NotifyObservers(aVariant, "android-set-pref",
                                           aPrefName->ToString().get());
        }

        uint16_t varType = nsIDataType::VTYPE_EMPTY;
        if (NS_SUCCEEDED(rv)) {
            rv = aVariant->GetDataType(&varType);
        }

        // We use set-to-empty to signal the pref was handled.
        const bool handled = varType == nsIDataType::VTYPE_EMPTY;

        if (NS_SUCCEEDED(rv) && handled && aFlush) {
            rv = Preferences::GetService()->SavePrefFile(nullptr);
        }

        if (NS_SUCCEEDED(rv)) {
            return handled;
        }

        NS_WARNING(nsPrintfCString("Failed to set pref %s",
                                   aPrefName->ToCString().get()).get());
        // Pretend we handled the pref.
        return true;
    }

public:
    static void GetPrefs(const jni::Class::LocalRef& aCls,
                         jni::ObjectArray::Param aPrefNames,
                         jni::Object::Param aPrefHandler)
    {
        nsTArray<jni::Object::LocalRef> nameRefArray(aPrefNames->GetElements());
        nsCOMPtr<nsIObserverService> obsServ;
        nsCOMPtr<nsIWritableVariant> value;
        nsAdoptingString strVal;

        for (jni::Object::LocalRef& nameRef : nameRefArray) {
            jni::String::LocalRef nameStr(mozilla::Move(nameRef));
            const nsCString& name = nameStr->ToCString();

            int32_t type = java::PrefsHelper::PREF_INVALID;
            bool boolVal = false;
            int32_t intVal = 0;

            switch (Preferences::GetType(name.get())) {
                case nsIPrefBranch::PREF_BOOL:
                    type = java::PrefsHelper::PREF_BOOL;
                    boolVal = Preferences::GetBool(name.get());
                    break;

                case nsIPrefBranch::PREF_INT:
                    type = java::PrefsHelper::PREF_INT;
                    intVal = Preferences::GetInt(name.get());
                    break;

                case nsIPrefBranch::PREF_STRING:
                    type = java::PrefsHelper::PREF_STRING;
                    strVal = Preferences::GetLocalizedString(name.get());
                    if (!strVal) {
                        strVal = Preferences::GetString(name.get());
                    }
                    break;

                default:
                    // Pref not found; try to find it.
                    if (!obsServ) {
                        obsServ = services::GetObserverService();
                        if (!obsServ) {
                            continue;
                        }
                    }
                    if (value) {
                        value->SetAsEmpty();
                    } else {
                        value = new nsVariant();
                    }
                    if (!GetVariantPref(obsServ, value,
                                        aPrefHandler, nameStr)) {
                        NS_WARNING(nsPrintfCString("Failed to get pref %s",
                                                   name.get()).get());
                    }
                    continue;
            }

            java::PrefsHelper::CallPrefHandler(
                    aPrefHandler, type, nameStr, boolVal, intVal,
                    jni::StringParam(type == java::PrefsHelper::PREF_STRING ?
                        jni::StringParam(strVal, aCls.Env()) :
                        jni::StringParam(nullptr)));
        }

        java::PrefsHelper::CallPrefHandler(
                aPrefHandler, java::PrefsHelper::PREF_FINISH,
                nullptr, false, 0, nullptr);
    }

    static void SetPref(jni::String::Param aPrefName,
                        bool aFlush,
                        int32_t aType,
                        bool aBoolVal,
                        int32_t aIntVal,
                        jni::String::Param aStrVal)
    {
        const nsCString& name = aPrefName->ToCString();

        if (Preferences::GetType(name.get()) == nsIPrefBranch::PREF_INVALID) {
            // No pref; try asking first.
            nsCOMPtr<nsIObserverService> obsServ =
                    services::GetObserverService();
            nsCOMPtr<nsIWritableVariant> value = new nsVariant();
            if (obsServ && SetVariantPref(obsServ, value, aPrefName, aFlush,
                                          aType, aBoolVal, aIntVal, aStrVal)) {
                // The "pref" has changed; send a notification.
                GetVariantPref(obsServ, value, nullptr,
                               jni::String::LocalRef(aPrefName));
                return;
            }
        }

        switch (aType) {
            case java::PrefsHelper::PREF_BOOL:
                Preferences::SetBool(name.get(), aBoolVal);
                break;
            case java::PrefsHelper::PREF_INT:
                Preferences::SetInt(name.get(), aIntVal);
                break;
            case java::PrefsHelper::PREF_STRING:
                Preferences::SetString(name.get(), aStrVal->ToString());
                break;
            default:
                MOZ_ASSERT(false, "Invalid pref type");
        }

        if (aFlush) {
            Preferences::GetService()->SavePrefFile(nullptr);
        }
    }

    static void AddObserver(const jni::Class::LocalRef& aCls,
                            jni::ObjectArray::Param aPrefNames,
                            jni::Object::Param aPrefHandler,
                            jni::ObjectArray::Param aPrefsToObserve)
    {
        // Call observer immediately with existing pref values.
        GetPrefs(aCls, aPrefNames, aPrefHandler);

        if (!aPrefsToObserve) {
            return;
        }

        nsTArray<jni::Object::LocalRef> nameRefArray(
                aPrefsToObserve->GetElements());
        nsAppShell* const appShell = nsAppShell::Get();
        MOZ_ASSERT(appShell);

        for (jni::Object::LocalRef& nameRef : nameRefArray) {
            jni::String::LocalRef nameStr(mozilla::Move(nameRef));
            MOZ_ALWAYS_SUCCEEDS(Preferences::AddStrongObserver(
                    appShell, nameStr->ToCString().get()));
        }
    }

    static void RemoveObserver(const jni::Class::LocalRef& aCls,
                               jni::ObjectArray::Param aPrefsToUnobserve)
    {
        nsTArray<jni::Object::LocalRef> nameRefArray(
                aPrefsToUnobserve->GetElements());
        nsAppShell* const appShell = nsAppShell::Get();
        MOZ_ASSERT(appShell);

        for (jni::Object::LocalRef& nameRef : nameRefArray) {
            jni::String::LocalRef nameStr(mozilla::Move(nameRef));
            MOZ_ALWAYS_SUCCEEDS(Preferences::RemoveObserver(
                    appShell, nameStr->ToCString().get()));
        }
    }

    static void OnPrefChange(const char16_t* aData)
    {
        const nsCString& name = NS_LossyConvertUTF16toASCII(aData);

        int32_t type = -1;
        bool boolVal = false;
        int32_t intVal = false;
        nsAdoptingString strVal;

        switch (Preferences::GetType(name.get())) {
            case nsIPrefBranch::PREF_BOOL:
                type = java::PrefsHelper::PREF_BOOL;
                boolVal = Preferences::GetBool(name.get());
                break;
            case nsIPrefBranch::PREF_INT:
                type = java::PrefsHelper::PREF_INT;
                intVal = Preferences::GetInt(name.get());
                break;
            case nsIPrefBranch::PREF_STRING:
                type = java::PrefsHelper::PREF_STRING;
                strVal = Preferences::GetLocalizedString(name.get());
                if (!strVal) {
                    strVal = Preferences::GetString(name.get());
                }
                break;
            default:
                NS_WARNING(nsPrintfCString("Invalid pref %s",
                                           name.get()).get());
                return;
        }

        java::PrefsHelper::OnPrefChange(
                name, type, boolVal, intVal,
                jni::StringParam(type == java::PrefsHelper::PREF_STRING ?
                    jni::StringParam(strVal) : jni::StringParam(nullptr)));
    }
};

} // namespace

#endif // PrefsHelper_h