blob: b9131bf3d8da1f49a25c61ea39bf0ede62d87e81 [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.Build;
20import android.os.StrictMode;
21import android.os.SystemProperties;
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010022import android.util.AndroidRuntimeException;
Jonathan Dixond3101b12012-04-12 20:51:51 +010023import android.util.Log;
24
25/**
26 * Top level factory, used creating all the main WebView implementation classes.
Jared Dukeb0e35842013-03-19 16:25:39 -070027 *
28 * @hide
Jonathan Dixond3101b12012-04-12 20:51:51 +010029 */
Jared Dukeb0e35842013-03-19 16:25:39 -070030public final class WebViewFactory {
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070031
Ben Murdoche09e9762012-07-19 14:48:13 +010032 private static final String CHROMIUM_WEBVIEW_FACTORY =
Torne (Richard Coles)a9bbd942012-10-24 11:59:22 +010033 "com.android.webview.chromium.WebViewChromiumFactoryProvider";
Jonathan Dixond3101b12012-04-12 20:51:51 +010034
35 private static final String LOGTAG = "WebViewFactory";
36
37 private static final boolean DEBUG = false;
38
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010039 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 Dixond3101b12012-04-12 20:51:51 +010050 // 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 Reck9f9d3452012-09-20 13:18:59 -070053 private static final Object sProviderLock = new Object();
Jonathan Dixond3101b12012-04-12 20:51:51 +010054
Jared Dukeb0e35842013-03-19 16:25:39 -070055 public static boolean isExperimentalWebViewAvailable() {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070056 // TODO: Remove callers of this method then remove it.
57 return false; // Hide the toggle in Developer Settings.
Jared Dukeb0e35842013-03-19 16:25:39 -070058 }
59
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070060 /** @hide */
61 public static void setUseExperimentalWebView(boolean enable) {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070062 // TODO: Remove callers of this method then remove it.
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070063 }
64
65 /** @hide */
66 public static boolean useExperimentalWebView() {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070067 // TODO: Remove callers of this method then remove it.
Torne (Richard Coles)d892afc2013-10-14 17:14:04 +010068 return true;
Jonathan Dixona7eaa8e2013-07-25 19:52:47 -070069 }
70
Jonathan Dixonb7d5cbc2013-08-21 14:00:08 -070071 /** @hide */
72 public static boolean isUseExperimentalWebViewSet() {
Jonathan Dixonbe706eb2013-09-11 17:55:15 -070073 // TODO: Remove callers of this method then remove it.
74 return false; // User has not modifed Developer Settings
Jonathan Dixonb7d5cbc2013-08-21 14:00:08 -070075 }
76
John Reck9f9d3452012-09-20 13:18:59 -070077 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)d892afc2013-10-14 17:14:04 +010080 // us honest and minimize usage of WebView internals when binding the proxy.
John Reck9f9d3452012-09-20 13:18:59 -070081 if (sProviderInstance != null) return sProviderInstance;
Jonathan Dixond3101b12012-04-12 20:51:51 +010082
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010083 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 Murdoche09e9762012-07-19 14:48:13 +010089 }
Ben Murdoche09e9762012-07-19 14:48:13 +010090
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010091 // 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 Murdoche09e9762012-07-19 14:48:13 +010097 }
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +010098
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 Dixond3101b12012-04-12 20:51:51 +0100111 }
Jonathan Dixond3101b12012-04-12 20:51:51 +0100112 }
113
Torne (Richard Coles)03ce9b32013-06-12 16:02:03 +0100114 private static Class<WebViewFactoryProvider> getFactoryClass() throws ClassNotFoundException {
Torne (Richard Coles)d892afc2013-10-14 17:14:04 +0100115 return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY);
Jonathan Dixond3101b12012-04-12 20:51:51 +0100116 }
117}