blob: 25bcd44a744c34468220d3fa86a46b94c94636e4 [file] [log] [blame]
Jonathan Dixond3101b12012-04-12 20:51:51 +01001/*
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
17package android.webkit;
18
Ben Murdoche09e9762012-07-19 14:48:13 +010019import android.os.StrictMode;
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010020import android.util.AndroidRuntimeException;
Jonathan Dixond3101b12012-04-12 20:51:51 +010021import android.util.Log;
22
23/**
24 * Top level factory, used creating all the main WebView implementation classes.
Jared Dukeb0e35842013-03-19 16:25:39 -070025 *
26 * @hide
Jonathan Dixond3101b12012-04-12 20:51:51 +010027 */
Jared Dukeb0e35842013-03-19 16:25:39 -070028public final class WebViewFactory {
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070029
Ben Murdoche09e9762012-07-19 14:48:13 +010030 private static final String CHROMIUM_WEBVIEW_FACTORY =
Torne (Richard Coles)a9bbd942012-10-24 11:59:22 +010031 "com.android.webview.chromium.WebViewChromiumFactoryProvider";
Jonathan Dixond3101b12012-04-12 20:51:51 +010032
33 private static final String LOGTAG = "WebViewFactory";
34
35 private static final boolean DEBUG = false;
36
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010037 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 Dixond3101b12012-04-12 20:51:51 +010048 // 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 Reck9f9d3452012-09-20 13:18:59 -070051 private static final Object sProviderLock = new Object();
Jonathan Dixond3101b12012-04-12 20:51:51 +010052
Jared Dukeb0e35842013-03-19 16:25:39 -070053 public static boolean isExperimentalWebViewAvailable() {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070054 // TODO: Remove callers of this method then remove it.
55 return false; // Hide the toggle in Developer Settings.
Jared Dukeb0e35842013-03-19 16:25:39 -070056 }
57
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070058 /** @hide */
59 public static void setUseExperimentalWebView(boolean enable) {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070060 // TODO: Remove callers of this method then remove it.
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070061 }
62
63 /** @hide */
64 public static boolean useExperimentalWebView() {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070065 // TODO: Remove callers of this method then remove it.
Torne (Richard Coles)d892afc2013-10-14 17:14:04 +010066 return true;
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070067 }
68
Jonathan Dixonb7d5cbc2013-08-21 14:00:08 -070069 /** @hide */
70 public static boolean isUseExperimentalWebViewSet() {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070071 // TODO: Remove callers of this method then remove it.
72 return false; // User has not modifed Developer Settings
Jonathan Dixonb7d5cbc2013-08-21 14:00:08 -070073 }
74
John Reck9f9d3452012-09-20 13:18:59 -070075 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)d892afc2013-10-14 17:14:04 +010078 // us honest and minimize usage of WebView internals when binding the proxy.
John Reck9f9d3452012-09-20 13:18:59 -070079 if (sProviderInstance != null) return sProviderInstance;
Jonathan Dixond3101b12012-04-12 20:51:51 +010080
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010081 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 Murdoche09e9762012-07-19 14:48:13 +010087 }
Ben Murdoche09e9762012-07-19 14:48:13 +010088
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010089 // 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 Murdoche09e9762012-07-19 14:48:13 +010095 }
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010096
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 Dixond3101b12012-04-12 20:51:51 +0100109 }
Jonathan Dixond3101b12012-04-12 20:51:51 +0100110 }
111
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100112 private static Class<WebViewFactoryProvider> getFactoryClass() throws ClassNotFoundException {
Torne (Richard Coles)d892afc2013-10-14 17:14:04 +0100113 return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY);
Jonathan Dixond3101b12012-04-12 20:51:51 +0100114 }
115}