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