WebViewFactory.java revision e09e976dad1d974ca28381451b0bbbeafbb872d5
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.Build;
20import android.os.StrictMode;
21import android.os.SystemProperties;
22import android.util.Log;
23
24import dalvik.system.PathClassLoader;
25
26/**
27 * Top level factory, used creating all the main WebView implementation classes.
28 */
29class WebViewFactory {
30    // Default Provider factory class name.
31    // TODO: When the Chromium powered WebView is ready, it should be the default factory class.
32    private static final String DEFAULT_WEBVIEW_FACTORY = "android.webkit.WebViewClassic$Factory";
33    private static final String CHROMIUM_WEBVIEW_FACTORY =
34            "com.android.webviewchromium.WebViewChromiumFactoryProvider";
35    private static final String CHROMIUM_WEBVIEW_JAR = "/system/framework/webviewchromium.jar";
36
37    private static final String LOGTAG = "WebViewFactory";
38
39    private static final boolean DEBUG = false;
40
41    // Cache the factory both for efficiency, and ensure any one process gets all webviews from the
42    // same provider.
43    private static WebViewFactoryProvider sProviderInstance;
44
45    static synchronized WebViewFactoryProvider getProvider() {
46        // For now the main purpose of this function (and the factory abstraction) is to keep
47        // us honest and minimize usage of WebViewClassic internals when binding the proxy.
48        if (sProviderInstance != null) return sProviderInstance;
49
50        // For debug builds, we allow a system property to specify that we should use the
51        // Chromium powered WebView. This enables us to switch between implementations
52        // at runtime. For user (release) builds, don't allow this.
53        if (Build.IS_DEBUGGABLE && SystemProperties.getBoolean("webview.use_chromium", false)) {
54            StrictMode.ThreadPolicy oldPolicy = StrictMode.allowThreadDiskReads();
55            try {
56                sProviderInstance = loadChromiumProvider();
57                if (DEBUG) Log.v(LOGTAG, "Loaded Chromium provider: " + sProviderInstance);
58            } finally {
59                StrictMode.setThreadPolicy(oldPolicy);
60            }
61        }
62
63        if (sProviderInstance == null) {
64            if (DEBUG) Log.v(LOGTAG, "Falling back to default provider: "
65                    + DEFAULT_WEBVIEW_FACTORY);
66            sProviderInstance = getFactoryByName(DEFAULT_WEBVIEW_FACTORY,
67                    WebViewFactory.class.getClassLoader());
68            if (sProviderInstance == null) {
69                if (DEBUG) Log.v(LOGTAG, "Falling back to explicit linkage");
70                sProviderInstance = new WebViewClassic.Factory();
71            }
72        }
73        return sProviderInstance;
74    }
75
76    // TODO: This allows us to have the legacy and Chromium WebView coexist for development
77    // and side-by-side testing. After transition, remove this when no longer required.
78    private static WebViewFactoryProvider loadChromiumProvider() {
79        ClassLoader clazzLoader = new PathClassLoader(CHROMIUM_WEBVIEW_JAR, null,
80                WebViewFactory.class.getClassLoader());
81        return getFactoryByName(CHROMIUM_WEBVIEW_FACTORY, clazzLoader);
82    }
83
84    private static WebViewFactoryProvider getFactoryByName(String providerName,
85            ClassLoader loader) {
86        try {
87            if (DEBUG) Log.v(LOGTAG, "attempt to load class " + providerName);
88            Class<?> c = Class.forName(providerName, true, loader);
89            if (DEBUG) Log.v(LOGTAG, "instantiating factory");
90            return (WebViewFactoryProvider) c.newInstance();
91        } catch (ClassNotFoundException e) {
92            Log.e(LOGTAG, "error loading " + providerName, e);
93        } catch (IllegalAccessException e) {
94            Log.e(LOGTAG, "error loading " + providerName, e);
95        } catch (InstantiationException e) {
96            Log.e(LOGTAG, "error loading " + providerName, e);
97        }
98        return null;
99    }
100}
101