blob: b5d4933688444ed56d2a7ab68e0381e533e33d59 [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
Kristian Monsencbb59db2011-05-09 16:04:34 +010051 protected static synchronized Context getContext() {
52 if (!initialized)
53 return null;
54 return sContext;
55 }
56
Kristian Monsend89a30a2010-11-16 18:11:59 +000057 /**
58 * Called by JNI. Gets the application's database directory, excluding the trailing slash.
59 * @return String The application's database directory
60 */
61 private static synchronized String getDatabaseDirectory() {
62 checkIntialized();
Kristian Monsend24ce592010-12-16 15:39:59 +000063
64 if (sDatabaseDirectory == null)
65 sDatabaseDirectory = sContext.getDatabasePath("dummy").getParent();
66
Kristian Monsend89a30a2010-11-16 18:11:59 +000067 return sDatabaseDirectory;
68 }
69
70 /**
71 * Called by JNI. Gets the application's cache directory, excluding the trailing slash.
72 * @return String The application's cache directory
73 */
74 private static synchronized String getCacheDirectory() {
75 checkIntialized();
Kristian Monsend24ce592010-12-16 15:39:59 +000076
77 if (sCacheDirectory == null)
78 sCacheDirectory = sContext.getCacheDir().getAbsolutePath();
79
Kristian Monsend89a30a2010-11-16 18:11:59 +000080 return sCacheDirectory;
81 }
Iain Merricka1069932010-12-10 15:51:36 +000082
Kristian Monsen3ede3152011-01-10 12:05:37 +000083 private static final String ANDROID_CONTENT = "content:";
84
Iain Merricka1069932010-12-10 15:51:36 +000085 /**
Kristian Monsen80ff5d82010-12-24 11:53:50 +000086 * Called by JNI. Calculates the size of an input stream by reading it.
87 * @return long The size of the stream
88 */
89 private static synchronized long contentUrlSize(String url) {
Kristian Monsen80ff5d82010-12-24 11:53:50 +000090 // content://
91 if (url.startsWith(ANDROID_CONTENT)) {
92 try {
93 // Strip off mimetype, for compatibility with ContentLoader.java
94 // If we don't do this, we can fail to load Gmail attachments,
95 // because the URL being loaded doesn't exactly match the URL we
96 // have permission to read.
97 int mimeIndex = url.lastIndexOf('?');
98 if (mimeIndex != -1) {
99 url = url.substring(0, mimeIndex);
100 }
101 Uri uri = Uri.parse(url);
102 InputStream is = sContext.getContentResolver().openInputStream(uri);
103 byte[] buffer = new byte[1024];
104 int n;
105 long size = 0;
106 try {
107 while ((n = is.read(buffer)) != -1) {
108 size += n;
109 }
110 } finally {
111 is.close();
112 }
113 return size;
114 } catch (Exception e) {
115 Log.e(LOGTAG, "Exception: " + url);
116 return 0;
117 }
118 } else {
119 return 0;
120 }
121 }
122
123 /**
Kristian Monsen3ede3152011-01-10 12:05:37 +0000124 * Called by JNI.
125 *
126 * @return Opened input stream to content
127 * TODO: Make all content loading use this instead of BrowserFrame.java
128 */
129 private static synchronized InputStream contentUrlStream(String url) {
130 // content://
131 if (url.startsWith(ANDROID_CONTENT)) {
132 try {
133 // Strip off mimetype, for compatibility with ContentLoader.java
134 // If we don't do this, we can fail to load Gmail attachments,
135 // because the URL being loaded doesn't exactly match the URL we
136 // have permission to read.
137 int mimeIndex = url.lastIndexOf('?');
138 if (mimeIndex != -1) {
139 url = url.substring(0, mimeIndex);
140 }
141 Uri uri = Uri.parse(url);
142 return sContext.getContentResolver().openInputStream(uri);
143 } catch (Exception e) {
144 Log.e(LOGTAG, "Exception: " + url);
145 return null;
146 }
147 } else {
148 return null;
149 }
150 }
151
152 /**
Iain Merricka1069932010-12-10 15:51:36 +0000153 * Returns true if we're using the Chromium HTTP stack.
154 *
155 * TODO: Remove this if/when we permanently switch to the Chromium HTTP stack
156 * http:/b/3118772
157 */
158 static boolean useChromiumHttpStack() {
159 if (sUseChromiumHttpStack == null) {
160 sUseChromiumHttpStack = nativeUseChromiumHttpStack();
161 }
162 return sUseChromiumHttpStack;
163 }
164
165 private static native boolean nativeUseChromiumHttpStack();
Kristian Monsend89a30a2010-11-16 18:11:59 +0000166}