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 { |
Primiano Tucci | eb665d6 | 2013-05-17 11:50:19 +0100 | [diff] [blame] | 31 | public static final boolean DEFAULT_TO_EXPERIMENTAL_WEBVIEW = false; |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 32 | public static final String WEBVIEW_EXPERIMENTAL_PROPERTY = "persist.sys.webview.exp"; |
Primiano Tucci | eb665d6 | 2013-05-17 11:50:19 +0100 | [diff] [blame] | 33 | private static final String FORCE_PROVIDER_PROPERTY = "webview.force_provider"; |
| 34 | private static final String FORCE_PROVIDER_PROPERTY_VALUE_CHROMIUM = "chromium"; |
| 35 | private static final String FORCE_PROVIDER_PROPERTY_VALUE_CLASSIC = "classic"; |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 36 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 37 | // Default Provider factory class name. |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 38 | // TODO: When the Chromium powered WebView is ready, it should be the default factory class. |
| 39 | private static final String DEFAULT_WEBVIEW_FACTORY = "android.webkit.WebViewClassic$Factory"; |
| 40 | private static final String CHROMIUM_WEBVIEW_FACTORY = |
Torne (Richard Coles) | a9bbd94 | 2012-10-24 11:59:22 +0100 | [diff] [blame] | 41 | "com.android.webview.chromium.WebViewChromiumFactoryProvider"; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 42 | |
| 43 | private static final String LOGTAG = "WebViewFactory"; |
| 44 | |
| 45 | private static final boolean DEBUG = false; |
| 46 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 47 | private static class Preloader { |
| 48 | static WebViewFactoryProvider sPreloadedProvider; |
| 49 | static { |
| 50 | try { |
| 51 | sPreloadedProvider = getFactoryClass().newInstance(); |
| 52 | } catch (Exception e) { |
| 53 | Log.w(LOGTAG, "error preloading provider", e); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 58 | // Cache the factory both for efficiency, and ensure any one process gets all webviews from the |
| 59 | // same provider. |
| 60 | private static WebViewFactoryProvider sProviderInstance; |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 61 | private static final Object sProviderLock = new Object(); |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 62 | |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 63 | public static boolean isExperimentalWebViewAvailable() { |
Torne (Richard Coles) | eb07f3c | 2013-06-04 15:50:13 +0100 | [diff] [blame] | 64 | try { |
| 65 | // Pass false so we don't initialize the class at this point, as this will be wasted if |
| 66 | // it's not enabled. |
| 67 | Class.forName(CHROMIUM_WEBVIEW_FACTORY, false, WebViewFactory.class.getClassLoader()); |
| 68 | return true; |
| 69 | } catch (ClassNotFoundException e) { |
| 70 | return false; |
| 71 | } |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 72 | } |
| 73 | |
John Reck | 9f9d345 | 2012-09-20 13:18:59 -0700 | [diff] [blame] | 74 | static WebViewFactoryProvider getProvider() { |
| 75 | synchronized (sProviderLock) { |
| 76 | // For now the main purpose of this function (and the factory abstraction) is to keep |
| 77 | // us honest and minimize usage of WebViewClassic internals when binding the proxy. |
| 78 | if (sProviderInstance != null) return sProviderInstance; |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 79 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 80 | Class<WebViewFactoryProvider> providerClass; |
| 81 | try { |
| 82 | providerClass = getFactoryClass(); |
| 83 | } catch (ClassNotFoundException e) { |
| 84 | Log.e(LOGTAG, "error loading provider", e); |
| 85 | throw new AndroidRuntimeException(e); |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 86 | } |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 87 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 88 | // This implicitly loads Preloader even if it wasn't preloaded at boot. |
| 89 | if (Preloader.sPreloadedProvider != null && |
| 90 | Preloader.sPreloadedProvider.getClass() == providerClass) { |
| 91 | sProviderInstance = Preloader.sPreloadedProvider; |
| 92 | if (DEBUG) Log.v(LOGTAG, "Using preloaded provider: " + sProviderInstance); |
| 93 | return sProviderInstance; |
Ben Murdoch | e09e976 | 2012-07-19 14:48:13 +0100 | [diff] [blame] | 94 | } |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 95 | |
| 96 | // The preloaded provider isn't the one we wanted; construct our own. |
| 97 | StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads(); |
| 98 | try { |
| 99 | sProviderInstance = providerClass.newInstance(); |
| 100 | if (DEBUG) Log.v(LOGTAG, "Loaded provider: " + sProviderInstance); |
| 101 | return sProviderInstance; |
| 102 | } catch (Exception e) { |
| 103 | Log.e(LOGTAG, "error instantiating provider", e); |
| 104 | throw new AndroidRuntimeException(e); |
| 105 | } finally { |
| 106 | StrictMode.setThreadPolicy(oldPolicy); |
| 107 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 108 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 109 | } |
| 110 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 111 | // We allow a system property to specify that we should use the experimental Chromium powered |
| 112 | // WebView. This enables us to switch between implementations at runtime. |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 113 | private static boolean isExperimentalWebViewEnabled() { |
Primiano Tucci | eb665d6 | 2013-05-17 11:50:19 +0100 | [diff] [blame] | 114 | if (!isExperimentalWebViewAvailable()) return false; |
| 115 | boolean use_experimental_webview = SystemProperties.getBoolean( |
| 116 | WEBVIEW_EXPERIMENTAL_PROPERTY, DEFAULT_TO_EXPERIMENTAL_WEBVIEW); |
| 117 | String forceProviderName = SystemProperties.get(FORCE_PROVIDER_PROPERTY); |
| 118 | if (forceProviderName.isEmpty()) return use_experimental_webview; |
| 119 | |
| 120 | Log.i(LOGTAG, String.format("Provider overridden by property: %s=%s", |
| 121 | FORCE_PROVIDER_PROPERTY, forceProviderName)); |
| 122 | if (forceProviderName.equals(FORCE_PROVIDER_PROPERTY_VALUE_CHROMIUM)) return true; |
| 123 | if (forceProviderName.equals(FORCE_PROVIDER_PROPERTY_VALUE_CLASSIC)) return false; |
| 124 | Log.e(LOGTAG, String.format("Unrecognized provider: %s", forceProviderName)); |
| 125 | return use_experimental_webview; |
Jared Duke | b0e3584 | 2013-03-19 16:25:39 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Torne (Richard Coles) | 03ce9b3 | 2013-06-12 16:02:03 +0100 | [diff] [blame] | 128 | private static Class<WebViewFactoryProvider> getFactoryClass() throws ClassNotFoundException { |
| 129 | if (isExperimentalWebViewEnabled()) { |
| 130 | return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY); |
| 131 | } else { |
| 132 | return (Class<WebViewFactoryProvider>) Class.forName(DEFAULT_WEBVIEW_FACTORY); |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 133 | } |
Jonathan Dixon | d3101b1 | 2012-04-12 20:51:51 +0100 | [diff] [blame] | 134 | } |
| 135 | } |