Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 19 | import android.os.Build; |
| 20 | import android.os.StrictMode; |
| 21 | import android.os.SystemProperties; |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 22 | import android.util.AndroidRuntimeException; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 23 | import android.util.Log; |
| 24 | |
| 25 | /** |
| 26 | * Top level factory, used creating all the main WebView implementation classes. |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 27 | * |
| 28 | * @hide |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 29 | */ |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 30 | public final class WebViewFactory { |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 31 | |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 32 | private static final String CHROMIUM_WEBVIEW_FACTORY = |
Torne (Richard Coles) | a9bbd94 | 2012-10-24 11:59:22 +0100 | [diff] [blame] | 33 | "com.android.webview.chromium.WebViewChromiumFactoryProvider"; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 34 | |
| 35 | private static final String LOGTAG = "WebViewFactory"; |
| 36 | |
| 37 | private static final boolean DEBUG = false; |
| 38 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 39 | private static class Preloader { |
| 40 | static WebViewFactoryProvider sPreloadedProvider; |
| 41 | static { |
| 42 | try { |
| 43 | sPreloadedProvider = getFactoryClass().newInstance(); |
| 44 | } catch (Exception e) { |
| 45 | Log.w(LOGTAG, "error preloading provider", e); |
| 46 | } |
| 47 | } |
| 48 | } |
| 49 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 50 | // Cache the factory both for efficiency, and ensure any one process gets all webviews from the |
| 51 | // same provider. |
| 52 | private static WebViewFactoryProvider sProviderInstance; |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 53 | private static final Object sProviderLock = new Object(); |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 54 | |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 55 | public static boolean isExperimentalWebViewAvailable() { |
Jonathan Dixon | be706eb | 2013-09-11 17:55:15 -0700 | [diff] [blame] | 56 | // TODO: Remove callers of this method then remove it. |
| 57 | return false; // Hide the toggle in Developer Settings. |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 58 | } |
| 59 | |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 60 | /** @hide */ |
| 61 | public static void setUseExperimentalWebView(boolean enable) { |
Jonathan Dixon | be706eb | 2013-09-11 17:55:15 -0700 | [diff] [blame] | 62 | // TODO: Remove callers of this method then remove it. |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | /** @hide */ |
| 66 | public static boolean useExperimentalWebView() { |
Jonathan Dixon | be706eb | 2013-09-11 17:55:15 -0700 | [diff] [blame] | 67 | // TODO: Remove callers of this method then remove it. |
Torne (Richard Coles) | d892afc | 2013-10-14 17:14:04 +0100 | [diff] [blame] | 68 | return true; |
Jonathan Dixon | a7eaa8e | 2013-07-25 19:52:47 -0700 | [diff] [blame] | 69 | } |
| 70 | |
Jonathan Dixon | b7d5cbc | 2013-08-21 14:00:08 -0700 | [diff] [blame] | 71 | /** @hide */ |
| 72 | public static boolean isUseExperimentalWebViewSet() { |
Jonathan Dixon | be706eb | 2013-09-11 17:55:15 -0700 | [diff] [blame] | 73 | // TODO: Remove callers of this method then remove it. |
| 74 | return false; // User has not modifed Developer Settings |
Jonathan Dixon | b7d5cbc | 2013-08-21 14:00:08 -0700 | [diff] [blame] | 75 | } |
| 76 | |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 77 | static WebViewFactoryProvider getProvider() { |
| 78 | synchronized (sProviderLock) { |
| 79 | // For now the main purpose of this function (and the factory abstraction) is to keep |
Torne (Richard Coles) | d892afc | 2013-10-14 17:14:04 +0100 | [diff] [blame] | 80 | // us honest and minimize usage of WebView internals when binding the proxy. |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 81 | if (sProviderInstance != null) return sProviderInstance; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 82 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 83 | Class<WebViewFactoryProvider> providerClass; |
| 84 | try { |
| 85 | providerClass = getFactoryClass(); |
| 86 | } catch (ClassNotFoundException e) { |
| 87 | Log.e(LOGTAG, "error loading provider", e); |
| 88 | throw new AndroidRuntimeException(e); |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 89 | } |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 90 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 91 | // This implicitly loads Preloader even if it wasn't preloaded at boot. |
| 92 | if (Preloader.sPreloadedProvider != null && |
| 93 | Preloader.sPreloadedProvider.getClass() == providerClass) { |
| 94 | sProviderInstance = Preloader.sPreloadedProvider; |
| 95 | if (DEBUG) Log.v(LOGTAG, "Using preloaded provider: " + sProviderInstance); |
| 96 | return sProviderInstance; |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 97 | } |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 98 | |
| 99 | // The preloaded provider isn't the one we wanted; construct our own. |
| 100 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 101 | try { |
| 102 | sProviderInstance = providerClass.newInstance(); |
| 103 | if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance); |
| 104 | return sProviderInstance; |
| 105 | } catch (Exception e) { |
| 106 | Log.e(LOGTAG, "error instantiating provider", e); |
| 107 | throw new AndroidRuntimeException(e); |
| 108 | } finally { |
| 109 | StrictMode.setThreadPolicy(oldPolicy); |
| 110 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 111 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 112 | } |
| 113 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 114 | private static Class<WebViewFactoryProvider> getFactoryClass() throws ClassNotFoundException { |
Torne (Richard Coles) | d892afc | 2013-10-14 17:14:04 +0100 | [diff] [blame] | 115 | return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY); |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 116 | } |
| 117 | } |