blob: 62b415c78407cd8245052ab3b1819318b5ee01f7 [file] [log] [blame]
Kristian Monsend89a30a2010-11-16 18:11:59 +00001/*
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;
Kristian Monsen80ff5d82010-12-24 11:53:50 +000020import android.net.Uri;
21import android.util.Log;
22
23import java.io.InputStream;
Kristian Monsend89a30a2010-11-16 18:11:59 +000024
25class JniUtil {
Kristian Monsen80ff5d82010-12-24 11:53:50 +000026 private static final String LOGTAG = "webkit";
Iain Merricka1069932010-12-10 15:51:36 +000027 private JniUtil() {} // Utility class, do not instantiate.
28
Kristian Monsend89a30a2010-11-16 18:11:59 +000029 // Used by the Chromium HTTP stack.
30 private static String sDatabaseDirectory;
31 private static String sCacheDirectory;
Iain Merricka1069932010-12-10 15:51:36 +000032 private static Boolean sUseChromiumHttpStack;
Kristian Monsend24ce592010-12-16 15:39:59 +000033 private static Context sContext;
Kristian Monsend89a30a2010-11-16 18:11:59 +000034
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
Kristian Monsen6b91b452010-12-23 12:47:39 +000047 sContext = context.getApplicationContext();
Kristian Monsend89a30a2010-11-16 18:11:59 +000048 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();
Kristian Monsend24ce592010-12-16 15:39:59 +000057
58 if (sDatabaseDirectory == null)
59 sDatabaseDirectory = sContext.getDatabasePath("dummy").getParent();
60
Kristian Monsend89a30a2010-11-16 18:11:59 +000061 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();
Kristian Monsend24ce592010-12-16 15:39:59 +000070
71 if (sCacheDirectory == null)
72 sCacheDirectory = sContext.getCacheDir().getAbsolutePath();
73
Kristian Monsend89a30a2010-11-16 18:11:59 +000074 return sCacheDirectory;
75 }
Iain Merricka1069932010-12-10 15:51:36 +000076
Kristian Monsen3ede3152011-01-10 12:05:37 +000077 private static final String ANDROID_CONTENT = "content:";
78
Iain Merricka1069932010-12-10 15:51:36 +000079 /**
Kristian Monsen80ff5d82010-12-24 11:53:50 +000080 * Called by JNI. Calculates the size of an input stream by reading it.
81 * @return long The size of the stream
82 */
83 private static synchronized long contentUrlSize(String url) {
Kristian Monsen80ff5d82010-12-24 11:53:50 +000084 // 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 /**
Kristian Monsen3ede3152011-01-10 12:05:37 +0000118 * Called by JNI.
119 *
120 * @return Opened input stream to content
121 * TODO: Make all content loading use this instead of BrowserFrame.java
122 */
123 private static synchronized InputStream contentUrlStream(String url) {
124 // content://
125 if (url.startsWith(ANDROID_CONTENT)) {
126 try {
127 // Strip off mimetype, for compatibility with ContentLoader.java
128 // If we don't do this, we can fail to load Gmail attachments,
129 // because the URL being loaded doesn't exactly match the URL we
130 // have permission to read.
131 int mimeIndex = url.lastIndexOf('?');
132 if (mimeIndex != -1) {
133 url = url.substring(0, mimeIndex);
134 }
135 Uri uri = Uri.parse(url);
136 return sContext.getContentResolver().openInputStream(uri);
137 } catch (Exception e) {
138 Log.e(LOGTAG, "Exception: " + url);
139 return null;
140 }
141 } else {
142 return null;
143 }
144 }
145
146 /**
Iain Merricka1069932010-12-10 15:51:36 +0000147 * Returns true if we're using the Chromium HTTP stack.
148 *
149 * TODO: Remove this if/when we permanently switch to the Chromium HTTP stack
150 * http:/b/3118772
151 */
152 static boolean useChromiumHttpStack() {
153 if (sUseChromiumHttpStack == null) {
154 sUseChromiumHttpStack = nativeUseChromiumHttpStack();
155 }
156 return sUseChromiumHttpStack;
157 }
158
159 private static native boolean nativeUseChromiumHttpStack();
Kristian Monsend89a30a2010-11-16 18:11:59 +0000160}