Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 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 | |
| 17 | package android.webkit; |
| 18 | |
| 19 | import android.content.Context; |
Kristian Monsen | 80ff5d8 | 2010-12-24 11:53:50 +0000 | [diff] [blame] | 20 | import android.net.Uri; |
| 21 | import android.util.Log; |
| 22 | |
| 23 | import java.io.InputStream; |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 24 | |
| 25 | class JniUtil { |
Kristian Monsen | 80ff5d8 | 2010-12-24 11:53:50 +0000 | [diff] [blame] | 26 | private static final String LOGTAG = "webkit"; |
Iain Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 27 | private JniUtil() {} // Utility class, do not instantiate. |
| 28 | |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 29 | // Used by the Chromium HTTP stack. |
| 30 | private static String sDatabaseDirectory; |
| 31 | private static String sCacheDirectory; |
Iain Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 32 | private static Boolean sUseChromiumHttpStack; |
Kristian Monsen | d24ce59 | 2010-12-16 15:39:59 +0000 | [diff] [blame] | 33 | private static Context sContext; |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 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 | |
Kristian Monsen | 6b91b45 | 2010-12-23 12:47:39 +0000 | [diff] [blame] | 47 | sContext = context.getApplicationContext(); |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 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(); |
Kristian Monsen | d24ce59 | 2010-12-16 15:39:59 +0000 | [diff] [blame] | 57 | |
| 58 | if (sDatabaseDirectory == null) |
| 59 | sDatabaseDirectory = sContext.getDatabasePath("dummy").getParent(); |
| 60 | |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 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(); |
Kristian Monsen | d24ce59 | 2010-12-16 15:39:59 +0000 | [diff] [blame] | 70 | |
| 71 | if (sCacheDirectory == null) |
| 72 | sCacheDirectory = sContext.getCacheDir().getAbsolutePath(); |
| 73 | |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 74 | return sCacheDirectory; |
| 75 | } |
Iain Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 76 | |
Kristian Monsen | 3ede315 | 2011-01-10 12:05:37 +0000 | [diff] [blame] | 77 | private static final String ANDROID_CONTENT = "content:"; |
| 78 | |
Iain Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 79 | /** |
Kristian Monsen | 80ff5d8 | 2010-12-24 11:53:50 +0000 | [diff] [blame] | 80 | * 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 Monsen | 80ff5d8 | 2010-12-24 11:53:50 +0000 | [diff] [blame] | 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 | /** |
Kristian Monsen | 3ede315 | 2011-01-10 12:05:37 +0000 | [diff] [blame] | 118 | * 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 Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 147 | * 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 Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 160 | } |