summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoSharedPrefs.java
blob: ec928dd86a089f55f9b44d6b6151a2da24b1c625 (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
/* 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/. */

package org.mozilla.gecko;

import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

import org.mozilla.gecko.annotation.RobocopTarget;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;

/**
 * {@code GeckoSharedPrefs} provides scoped SharedPreferences instances.
 * You should use this API instead of using Context.getSharedPreferences()
 * directly. There are four methods to get scoped SharedPreferences instances:
 *
 * forApp()
 *     Use it for app-wide, cross-profile pref keys.
 * forCrashReporter()
 *     For the crash reporter, which runs in its own process.
 * forProfile()
 *     Use it to fetch and store keys for the current profile.
 * forProfileName()
 *     Use it to fetch and store keys from/for a specific profile.
 *
 * {@code GeckoSharedPrefs} has a notion of migrations. Migrations can used to
 * migrate keys from one scope to another. You can trigger a new migration by
 * incrementing PREFS_VERSION and updating migrateIfNecessary() accordingly.
 *
 * Migration history:
 *     1: Move all PreferenceManager keys to app/profile scopes
 *     2: Move the crash reporter's private preferences into their own scope
 */
@RobocopTarget
public final class GeckoSharedPrefs {
    private static final String LOGTAG = "GeckoSharedPrefs";

    // Increment it to trigger a new migration
    public static final int PREFS_VERSION = 2;

    // Name for app-scoped prefs
    public static final String APP_PREFS_NAME = "GeckoApp";

    // Name for crash reporter prefs
    public static final String CRASH_PREFS_NAME = "CrashReporter";

    // Used when fetching profile-scoped prefs.
    public static final String PROFILE_PREFS_NAME_PREFIX = "GeckoProfile-";

    // The prefs key that holds the current migration
    private static final String PREFS_VERSION_KEY = "gecko_shared_prefs_migration";

    // For disabling migration when getting a SharedPreferences instance
    private static final EnumSet<Flags> disableMigrations = EnumSet.of(Flags.DISABLE_MIGRATIONS);

    // The keys that have to be moved from ProfileManager's default
    // shared prefs to the profile from version 0 to 1.
    private static final String[] PROFILE_MIGRATIONS_0_TO_1 = {
        "home_panels",
        "home_locale"
    };

    // The keys that have to be moved from the app prefs
    // into the crash reporter's own prefs.
    private static final String[] PROFILE_MIGRATIONS_1_TO_2 = {
        "sendReport",
        "includeUrl",
        "allowContact",
        "contactEmail"
    };

    // For optimizing the migration check in subsequent get() calls
    private static volatile boolean migrationDone;

    public enum Flags {
        DISABLE_MIGRATIONS
    }

    public static SharedPreferences forApp(Context context) {
        return forApp(context, EnumSet.noneOf(Flags.class));
    }

    /**
     * Returns an app-scoped SharedPreferences instance. You can disable
     * migrations by using the DISABLE_MIGRATIONS flag.
     */
    public static SharedPreferences forApp(Context context, EnumSet<Flags> flags) {
        if (flags != null && !flags.contains(Flags.DISABLE_MIGRATIONS)) {
            migrateIfNecessary(context);
        }

        return context.getSharedPreferences(APP_PREFS_NAME, 0);
    }

    public static SharedPreferences forCrashReporter(Context context) {
        return forCrashReporter(context, EnumSet.noneOf(Flags.class));
    }

    /**
     * Returns a crash-reporter-scoped SharedPreferences instance. You can disable
     * migrations by using the DISABLE_MIGRATIONS flag.
     */
    public static SharedPreferences forCrashReporter(Context context, EnumSet<Flags> flags) {
        if (flags != null && !flags.contains(Flags.DISABLE_MIGRATIONS)) {
            migrateIfNecessary(context);
        }

        return context.getSharedPreferences(CRASH_PREFS_NAME, 0);
    }

    public static SharedPreferences forProfile(Context context) {
        return forProfile(context, EnumSet.noneOf(Flags.class));
    }

    /**
     * Returns a SharedPreferences instance scoped to the current profile
     * in the app. You can disable migrations by using the DISABLE_MIGRATIONS
     * flag.
     */
    public static SharedPreferences forProfile(Context context, EnumSet<Flags> flags) {
        String profileName = GeckoProfile.get(context).getName();
        if (profileName == null) {
            throw new IllegalStateException("Could not get current profile name");
        }

        return forProfileName(context, profileName, flags);
    }

    public static SharedPreferences forProfileName(Context context, String profileName) {
        return forProfileName(context, profileName, EnumSet.noneOf(Flags.class));
    }

    /**
     * Returns an SharedPreferences instance scoped to the given profile name.
     * You can disable migrations by using the DISABLE_MIGRATION flag.
     */
    public static SharedPreferences forProfileName(Context context, String profileName,
            EnumSet<Flags> flags) {
        if (flags != null && !flags.contains(Flags.DISABLE_MIGRATIONS)) {
            migrateIfNecessary(context);
        }

        final String prefsName = PROFILE_PREFS_NAME_PREFIX + profileName;
        return context.getSharedPreferences(prefsName, 0);
    }

    /**
     * Returns the current version of the prefs.
     */
    public static int getVersion(Context context) {
        return forApp(context, disableMigrations).getInt(PREFS_VERSION_KEY, 0);
    }

    /**
     * Resets migration flag. Should only be used in tests.
     */
    public static synchronized void reset() {
        migrationDone = false;
    }

    /**
     * Performs all prefs migrations in the background thread to avoid StrictMode
     * exceptions from reading/writing in the UI thread. This method will block
     * the current thread until the migration is finished.
     */
    private static synchronized void migrateIfNecessary(final Context context) {
        if (migrationDone) {
            return;
        }

        // We deliberately perform the migration in the current thread (which
        // is likely the UI thread) as this is actually cheaper than enforcing a
        // context switch to another thread (see bug 940575).
        // Avoid strict mode warnings when doing so.
        final StrictMode.ThreadPolicy savedPolicy = StrictMode.allowThreadDiskReads();
        StrictMode.allowThreadDiskWrites();
        try {
            performMigration(context);
        } finally {
            StrictMode.setThreadPolicy(savedPolicy);
        }

        migrationDone = true;
    }

    private static void performMigration(Context context) {
        final SharedPreferences appPrefs = forApp(context, disableMigrations);

        final int currentVersion = appPrefs.getInt(PREFS_VERSION_KEY, 0);
        Log.d(LOGTAG, "Current version = " + currentVersion + ", prefs version = " + PREFS_VERSION);

        if (currentVersion == PREFS_VERSION) {
            return;
        }

        Log.d(LOGTAG, "Performing migration");

        final Editor appEditor = appPrefs.edit();

        // The migration always moves prefs to the default profile, not
        // the current one. We might have to revisit this if we ever support
        // multiple profiles.
        final String defaultProfileName;
        try {
            defaultProfileName = GeckoProfile.getDefaultProfileName(context);
        } catch (Exception e) {
            throw new IllegalStateException("Failed to get default profile name for migration");
        }

        final Editor profileEditor = forProfileName(context, defaultProfileName, disableMigrations).edit();
        final Editor crashEditor = forCrashReporter(context, disableMigrations).edit();

        List<String> profileKeys;
        Editor pmEditor = null;

        for (int v = currentVersion + 1; v <= PREFS_VERSION; v++) {
            Log.d(LOGTAG, "Migrating to version = " + v);

            switch (v) {
                case 1:
                    profileKeys = Arrays.asList(PROFILE_MIGRATIONS_0_TO_1);
                    pmEditor = migrateFromPreferenceManager(context, appEditor, profileEditor, profileKeys);
                    break;
                case 2:
                    profileKeys = Arrays.asList(PROFILE_MIGRATIONS_1_TO_2);
                    migrateCrashReporterSettings(appPrefs, appEditor, crashEditor, profileKeys);
                    break;
            }
        }

        // Update prefs version accordingly.
        appEditor.putInt(PREFS_VERSION_KEY, PREFS_VERSION);

        appEditor.apply();
        profileEditor.apply();
        crashEditor.apply();
        if (pmEditor != null) {
            pmEditor.apply();
        }

        Log.d(LOGTAG, "All keys have been migrated");
    }

    /**
     * Moves all preferences stored in PreferenceManager's default prefs
     * to either app or profile scopes. The profile-scoped keys are defined
     * in given profileKeys list, all other keys are moved to the app scope.
     */
    public static Editor migrateFromPreferenceManager(Context context, Editor appEditor,
            Editor profileEditor, List<String> profileKeys) {
        Log.d(LOGTAG, "Migrating from PreferenceManager");

        final SharedPreferences pmPrefs =
                PreferenceManager.getDefaultSharedPreferences(context);

        for (Map.Entry<String, ?> entry : pmPrefs.getAll().entrySet()) {
            final String key = entry.getKey();

            final Editor to;
            if (profileKeys.contains(key)) {
                to = profileEditor;
            } else {
                to = appEditor;
            }

            putEntry(to, key, entry.getValue());
        }

        // Clear PreferenceManager's prefs once we're done
        // and return the Editor to be committed.
        return pmPrefs.edit().clear();
    }

    /**
     * Moves the crash reporter's preferences from the app-wide prefs
     * into its own shared prefs to avoid cross-process pref accesses.
     */
    public static void migrateCrashReporterSettings(SharedPreferences appPrefs, Editor appEditor,
                                                    Editor crashEditor, List<String> profileKeys) {
        Log.d(LOGTAG, "Migrating crash reporter settings");

        for (Map.Entry<String, ?> entry : appPrefs.getAll().entrySet()) {
            final String key = entry.getKey();

            if (profileKeys.contains(key)) {
                putEntry(crashEditor, key, entry.getValue());
                appEditor.remove(key);
            }
        }
    }

    private static void putEntry(Editor to, String key, Object value) {
        Log.d(LOGTAG, "Migrating key = " + key + " with value = " + value);

        if (value instanceof String) {
            to.putString(key, (String) value);
        } else if (value instanceof Boolean) {
            to.putBoolean(key, (Boolean) value);
        } else if (value instanceof Long) {
            to.putLong(key, (Long) value);
        } else if (value instanceof Float) {
            to.putFloat(key, (Float) value);
        } else if (value instanceof Integer) {
            to.putInt(key, (Integer) value);
        } else {
            throw new IllegalStateException("Unrecognized value type for key: " + key);
        }
    }
}