summaryrefslogtreecommitdiffstats
path: root/mobile/android/base/java/org/mozilla/gecko/restrictions/Restrictions.java
blob: 0cf68081033b20b3a2393fd4dfd18d5ed172e97a (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
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; 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/. */

package org.mozilla.gecko.restrictions;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.Log;

import org.mozilla.gecko.AppConstants.Versions;
import org.mozilla.gecko.GeckoAppShell;
import org.mozilla.gecko.GeckoProfile;
import org.mozilla.gecko.annotation.RobocopTarget;
import org.mozilla.gecko.annotation.WrapForJNI;

@RobocopTarget
public class Restrictions {
    private static final String LOGTAG = "GeckoRestrictedProfiles";

    private static RestrictionConfiguration configuration;

    private static RestrictionConfiguration getConfiguration(Context context) {
        if (configuration == null) {
            configuration = createConfiguration(context);
        }

        return configuration;
    }

    public static synchronized RestrictionConfiguration createConfiguration(Context context) {
        if (configuration != null) {
            // This method is synchronized and another thread might already have created the configuration.
            return configuration;
        }

        if (isGuestProfile(context)) {
            return new GuestProfileConfiguration();
        } else if (isRestrictedProfile(context)) {
            return new RestrictedProfileConfiguration(context);
        } else {
            return new DefaultConfiguration();
        }
    }

    private static boolean isGuestProfile(Context context) {
        if (configuration != null) {
            return configuration instanceof GuestProfileConfiguration;
        }

        GeckoAppShell.GeckoInterface geckoInterface = GeckoAppShell.getGeckoInterface();
        if (geckoInterface != null) {
            return geckoInterface.getProfile().inGuestMode();
        }

        return GeckoProfile.get(context).inGuestMode();
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
    public static boolean isRestrictedProfile(Context context) {
        if (configuration != null) {
            return configuration instanceof RestrictedProfileConfiguration;
        }

        if (Versions.preJBMR2) {
            // Early versions don't support restrictions at all
            return false;
        }

        // The user is on a restricted profile if, and only if, we injected application restrictions during account setup.
        return RestrictionCache.hasApplicationRestrictions(context);
    }

    public static void update(Context context) {
        getConfiguration(context).update();
    }

    private static Restrictable geckoActionToRestriction(int action) {
        for (Restrictable rest : Restrictable.values()) {
            if (rest.id == action) {
                return rest;
            }
        }

        throw new IllegalArgumentException("Unknown action " + action);
    }

    private static boolean canLoadUrl(final Context context, final String url) {
        return getConfiguration(context).canLoadUrl(url);
    }

    @WrapForJNI(calledFrom = "gecko")
    public static boolean isUserRestricted() {
        return isUserRestricted(GeckoAppShell.getApplicationContext());
    }

    public static boolean isUserRestricted(final Context context) {
        return getConfiguration(context).isRestricted();
    }

    public static boolean isAllowed(final Context context, final Restrictable restrictable) {
        return getConfiguration(context).isAllowed(restrictable);
    }

    @WrapForJNI(calledFrom = "gecko")
    public static boolean isAllowed(int action, String url) {
        final Restrictable restrictable;
        try {
            restrictable = geckoActionToRestriction(action);
        } catch (IllegalArgumentException ex) {
            // Unknown actions represent a coding error, so we
            // refuse the action and log.
            Log.e(LOGTAG, "Unknown action " + action + "; check calling code.");
            return false;
        }

        final Context context = GeckoAppShell.getApplicationContext();

        if (Restrictable.BROWSE == restrictable) {
            return canLoadUrl(context, url);
        } else {
            return isAllowed(context, restrictable);
        }
    }
}