WebViewFactory.java revision d3101b1d300f5942fdb7dfa323dc8830c4edc007
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.util.Log;
20
21/**
22 * Top level factory, used creating all the main WebView implementation classes.
23 */
24class WebViewFactory {
25    // Default Provider factory class name.
26    private static final String DEFAULT_WEB_VIEW_FACTORY = "android.webkit.WebViewClassic$Factory";
27
28    private static final String LOGTAG = "WebViewFactory";
29
30    private static final boolean DEBUG = false;
31
32    // Cache the factory both for efficiency, and ensure any one process gets all webviews from the
33    // same provider.
34    private static WebViewFactoryProvider sProviderInstance;
35
36    static synchronized WebViewFactoryProvider getProvider() {
37        // For now the main purpose of this function (and the factory abstraction) is to keep
38        // us honest and minimize usage of WebViewClassic internals when binding the proxy.
39        if (sProviderInstance != null) return sProviderInstance;
40
41        sProviderInstance = getFactoryByName(DEFAULT_WEB_VIEW_FACTORY);
42        if (sProviderInstance == null) {
43            if (DEBUG) Log.v(LOGTAG, "Falling back to explicit linkage");
44            sProviderInstance = new WebViewClassic.Factory();
45        }
46        return sProviderInstance;
47    }
48
49    private static WebViewFactoryProvider getFactoryByName(String providerName) {
50        try {
51            if (DEBUG) Log.v(LOGTAG, "attempt to load class " + providerName);
52            Class<?> c = Class.forName(providerName);
53            if (DEBUG) Log.v(LOGTAG, "instantiating factory");
54            return (WebViewFactoryProvider) c.newInstance();
55        } catch (ClassNotFoundException e) {
56            Log.e(LOGTAG, "error loading " + providerName, e);
57        } catch (IllegalAccessException e) {
58            Log.e(LOGTAG, "error loading " + providerName, e);
59        } catch (InstantiationException e) {
60            Log.e(LOGTAG, "error loading " + providerName, e);
61        }
62        return null;
63    }
64}
65