JniUtil.java revision 80ff5d82bdbad51378a45d3a378213346ea22df0
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;
20import android.net.Uri;
21import android.util.Log;
22
23import java.io.InputStream;
24
25class JniUtil {
26    private static final String LOGTAG = "webkit";
27    private JniUtil() {} // Utility class, do not instantiate.
28
29    // Used by the Chromium HTTP stack.
30    private static String sDatabaseDirectory;
31    private static String sCacheDirectory;
32    private static Boolean sUseChromiumHttpStack;
33    private static Context sContext;
34
35    private static boolean initialized = false;
36
37    private static void checkIntialized() {
38        if (!initialized) {
39            throw new IllegalStateException("Call CookieSyncManager::createInstance() or create a webview before using this class");
40        }
41    }
42
43    protected static synchronized void setContext(Context context) {
44        if (initialized)
45            return;
46
47        sContext = context.getApplicationContext();
48        initialized = true;
49    }
50
51    /**
52     * Called by JNI. Gets the application's database directory, excluding the trailing slash.
53     * @return String The application's database directory
54     */
55    private static synchronized String getDatabaseDirectory() {
56        checkIntialized();
57
58        if (sDatabaseDirectory == null)
59            sDatabaseDirectory = sContext.getDatabasePath("dummy").getParent();
60
61        return sDatabaseDirectory;
62    }
63
64    /**
65     * Called by JNI. Gets the application's cache directory, excluding the trailing slash.
66     * @return String The application's cache directory
67     */
68    private static synchronized String getCacheDirectory() {
69        checkIntialized();
70
71        if (sCacheDirectory == null)
72            sCacheDirectory = sContext.getCacheDir().getAbsolutePath();
73
74        return sCacheDirectory;
75    }
76
77    /**
78     * Called by JNI. Calculates the size of an input stream by reading it.
79     * @return long The size of the stream
80     */
81    private static synchronized long contentUrlSize(String url) {
82        final String ANDROID_CONTENT = "content:";
83
84        // content://
85        if (url.startsWith(ANDROID_CONTENT)) {
86            try {
87                // Strip off mimetype, for compatibility with ContentLoader.java
88                // If we don't do this, we can fail to load Gmail attachments,
89                // because the URL being loaded doesn't exactly match the URL we
90                // have permission to read.
91                int mimeIndex = url.lastIndexOf('?');
92                if (mimeIndex != -1) {
93                    url = url.substring(0, mimeIndex);
94                }
95                Uri uri = Uri.parse(url);
96                InputStream is = sContext.getContentResolver().openInputStream(uri);
97                byte[] buffer = new byte[1024];
98                int n;
99                long size = 0;
100                try {
101                    while ((n = is.read(buffer)) != -1) {
102                        size += n;
103                    }
104                } finally {
105                    is.close();
106                }
107                return size;
108            } catch (Exception e) {
109                Log.e(LOGTAG, "Exception: " + url);
110                return 0;
111            }
112        } else {
113            return 0;
114        }
115    }
116
117    /**
118     * Returns true if we're using the Chromium HTTP stack.
119     *
120     * TODO: Remove this if/when we permanently switch to the Chromium HTTP stack
121     * http:/b/3118772
122     */
123    static boolean useChromiumHttpStack() {
124        if (sUseChromiumHttpStack == null) {
125            sUseChromiumHttpStack = nativeUseChromiumHttpStack();
126        }
127        return sUseChromiumHttpStack;
128    }
129
130    private static native boolean nativeUseChromiumHttpStack();
131}
132