WebViewFactory.java revision 6090995951c6e2e4dcf38102f01793f8a94166e1
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
17package android.webkit;
18
19import android.os.StrictMode;
20import android.util.AndroidRuntimeException;
21import android.util.Log;
22
23/**
24 * Top level factory, used creating all the main WebView implementation classes.
25 *
26 * @hide
27 */
28public final class WebViewFactory {
29
30    private static final String CHROMIUM_WEBVIEW_FACTORY =
31            "com.android.webview.chromium.WebViewChromiumFactoryProvider";
32
33    private static final String LOGTAG = "WebViewFactory";
34
35    private static final boolean DEBUG = false;
36
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
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;
51    private static final Object sProviderLock = new Object();
52
53    public static boolean isExperimentalWebViewAvailable() {
54        // TODO: Remove callers of this method then remove it.
55        return false;  // Hide the toggle in Developer Settings.
56    }
57
58    /** @hide */
59    public static void setUseExperimentalWebView(boolean enable) {
60        // TODO: Remove callers of this method then remove it.
61    }
62
63    /** @hide */
64    public static boolean useExperimentalWebView() {
65        // TODO: Remove callers of this method then remove it.
66        return true;
67    }
68
69    /** @hide */
70    public static boolean isUseExperimentalWebViewSet() {
71        // TODO: Remove callers of this method then remove it.
72        return false;  // User has not modifed Developer Settings
73    }
74
75    static WebViewFactoryProvider getProvider() {
76        synchronized (sProviderLock) {
77            // For now the main purpose of this function (and the factory abstraction) is to keep
78            // us honest and minimize usage of WebView internals when binding the proxy.
79            if (sProviderInstance != null) return sProviderInstance;
80
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);
87            }
88
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;
95            }
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            }
109        }
110    }
111
112    private static Class<WebViewFactoryProvider> getFactoryClass() throws ClassNotFoundException {
113        return (Class<WebViewFactoryProvider>) Class.forName(CHROMIUM_WEBVIEW_FACTORY);
114    }
115}
116