JniUtil.java revision d24ce5949dcc6e0c577ee9b9278ba1401b9e9c54
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.webkit;
18
19import android.content.Context;
20
21class JniUtil {
22    private JniUtil() {} // Utility class, do not instantiate.
23
24    // Used by the Chromium HTTP stack.
25    private static String sDatabaseDirectory;
26    private static String sCacheDirectory;
27    private static Boolean sUseChromiumHttpStack;
28    private static Context sContext;
29
30    private static boolean initialized = false;
31
32    private static void checkIntialized() {
33        if (!initialized) {
34            throw new IllegalStateException("Call CookieSyncManager::createInstance() or create a webview before using this class");
35        }
36    }
37
38    protected static synchronized void setContext(Context context) {
39        if (initialized)
40            return;
41
42        sContext = context;
43        initialized = true;
44    }
45
46    /**
47     * Called by JNI. Gets the application's database directory, excluding the trailing slash.
48     * @return String The application's database directory
49     */
50    private static synchronized String getDatabaseDirectory() {
51        checkIntialized();
52
53        if (sDatabaseDirectory == null)
54            sDatabaseDirectory = sContext.getDatabasePath("dummy").getParent();
55
56        return sDatabaseDirectory;
57    }
58
59    /**
60     * Called by JNI. Gets the application's cache directory, excluding the trailing slash.
61     * @return String The application's cache directory
62     */
63    private static synchronized String getCacheDirectory() {
64        checkIntialized();
65
66        if (sCacheDirectory == null)
67            sCacheDirectory = sContext.getCacheDir().getAbsolutePath();
68
69        return sCacheDirectory;
70    }
71
72    /**
73     * Returns true if we're using the Chromium HTTP stack.
74     *
75     * TODO: Remove this if/when we permanently switch to the Chromium HTTP stack
76     * http:/b/3118772
77     */
78    static boolean useChromiumHttpStack() {
79        if (sUseChromiumHttpStack == null) {
80            sUseChromiumHttpStack = nativeUseChromiumHttpStack();
81        }
82        return sUseChromiumHttpStack;
83    }
84
85    private static native boolean nativeUseChromiumHttpStack();
86}
87