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 | |
Kristian Monsen | cbb59db | 2011-05-09 16:04:34 +0100 | [diff] [blame] | 51 | protected static synchronized Context getContext() { |
| 52 | if (!initialized) |
| 53 | return null; |
| 54 | return sContext; |
| 55 | } |
| 56 | |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 57 | /** |
| 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 Monsen | d24ce59 | 2010-12-16 15:39:59 +0000 | [diff] [blame] | 63 | |
| 64 | if (sDatabaseDirectory == null) |
| 65 | sDatabaseDirectory = sContext.getDatabasePath("dummy").getParent(); |
| 66 | |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 67 | 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 Monsen | d24ce59 | 2010-12-16 15:39:59 +0000 | [diff] [blame] | 76 | |
| 77 | if (sCacheDirectory == null) |
| 78 | sCacheDirectory = sContext.getCacheDir().getAbsolutePath(); |
| 79 | |
Kristian Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 80 | return sCacheDirectory; |
| 81 | } |
Iain Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 82 | |
Kristian Monsen | 3ede315 | 2011-01-10 12:05:37 +0000 | [diff] [blame] | 83 | private static final String ANDROID_CONTENT = "content:"; |
| 84 | |
Iain Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 85 | /** |
Kristian Monsen | 80ff5d8 | 2010-12-24 11:53:50 +0000 | [diff] [blame] | 86 | * 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 Monsen | 80ff5d8 | 2010-12-24 11:53:50 +0000 | [diff] [blame] | 90 | // 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 Monsen | 3ede315 | 2011-01-10 12:05:37 +0000 | [diff] [blame] | 124 | * 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 Merrick | a106993 | 2010-12-10 15:51:36 +0000 | [diff] [blame] | 153 | * 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 Monsen | d89a30a | 2010-11-16 18:11:59 +0000 | [diff] [blame] | 166 | } |