WebViewChromiumFactoryProvider.java revision 72db47d0508d6733072c38ee44883ecfaf612ca6
146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)/*
246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * Copyright (C) 2012 The Android Open Source Project
346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) *
446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * Licensed under the Apache License, Version 2.0 (the "License");
546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * you may not use this file except in compliance with the License.
646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * You may obtain a copy of the License at
746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) *
846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) *      http://www.apache.org/licenses/LICENSE-2.0
946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) *
1046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * Unless required by applicable law or agreed to in writing, software
1146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * distributed under the License is distributed on an "AS IS" BASIS,
1246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * See the License for the specific language governing permissions and
1446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) * limitations under the License.
1546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles) */
1646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
1746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)package com.android.webview.chromium;
1846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
1946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.app.ActivityThread;
2046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.content.Context;
21009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsenimport android.content.SharedPreferences;
22970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixonimport android.os.Build;
2346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.os.Looper;
2446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.CookieManager;
2546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.GeolocationPermissions;
2646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebIconDatabase;
2746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebStorage;
2846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebView;
2946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebViewDatabase;
3046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebViewFactoryProvider;
3146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebViewProvider;
3246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
33aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsenimport org.chromium.android_webview.AwBrowserContext;
34f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixonimport org.chromium.android_webview.AwBrowserProcess;
35a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixonimport org.chromium.android_webview.AwContents;
36d5f17bbd8703a56ac084a64b1ce2f673f67bd72dKristian Monsenimport org.chromium.android_webview.AwCookieManager;
3709e718bac5900c84b8402b774d306dca7f7fdf09Selim Gurunimport org.chromium.android_webview.AwFormDatabase;
38009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsenimport org.chromium.android_webview.AwGeolocationPermissions;
395d20093562806fbce9cc0d3eb4a411ed6c5ce1c0Bo Liuimport org.chromium.android_webview.AwQuotaManagerBridge;
40d6b56ff89338d0b3a6ba0cb413b1fe559d6284e0Mikhail Naganovimport org.chromium.android_webview.AwSettings;
4146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import org.chromium.base.PathService;
427b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixonimport org.chromium.base.ThreadUtils;
437b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixonimport org.chromium.content.app.LibraryLoader;
4446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import org.chromium.content.browser.ContentViewStatics;
4546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import org.chromium.content.browser.ResourceExtractor;
46970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixonimport org.chromium.content.common.CommandLine;
47970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixonimport org.chromium.content.common.ProcessInitException;
4846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
4946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider {
5046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
51009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen    private static final String CHROMIUM_PREFS_NAME = "WebViewChromiumPrefs";
527962d3fa1b66ed25aba03a2d48f6c47528d7d95cPrimiano Tucci    private static final String COMMAND_LINE_FILE = "/data/local/tmp/webview-command-line";
53009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen
5472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    // Guards accees to the other members, and is notifyAll() signalled on the UI thread
5572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    // when the chromium process has been started.
5672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    private final Object mLock = new Object();
5772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
5846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    // Initialization guarded by mLock.
59aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen    private AwBrowserContext mBrowserContext;
60aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private Statics mStaticMethods;
61aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private GeolocationPermissionsAdapter mGeolocationPermissions;
62aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private CookieManagerAdapter mCookieManager;
63aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private WebIconDatabaseAdapter mWebIconDatabase;
64aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private WebStorageAdapter mWebStorage;
65aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private WebViewDatabaseAdapter mWebViewDatabase;
6646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
679aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon    // Read/write protected by mLock.
689aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon    private boolean mStarted;
69f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon
7011f7c1b02e49a6a8eaec26111f08edec45feb01aTorne (Richard Coles)    public WebViewChromiumFactoryProvider() {
71f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon        // Load chromium library.
72f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon        AwBrowserProcess.loadLibrary();
73a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon        // Load glue-layer support library.
74a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon        System.loadLibrary("webviewchromium_plat_support");
75f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon    }
76f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon
77f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon    private void initPlatSupportLibrary() {
789aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        DrawGLFunctor.setChromiumAwDrawGLFunction(AwContents.getAwDrawGLFunction());
799aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        AwContents.setAwDrawSWFunctionTable(GraphicsUtils.getDrawSWFunctionTable());
80c5cde8c18df853e11d1ec528508fd0fe2e742d5cKaan Baloglu        AwContents.setAwDrawGLFunctionTable(GraphicsUtils.getDrawGLFunctionTable());
81a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon    }
82a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon
83f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon    private void ensureChromiumStartedLocked() {
8446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        assert Thread.holdsLock(mLock);
8546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
8672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (mStarted) {  // Early-out for the common case.
8772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            return;
8872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
8972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
9072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (ThreadUtils.runningOnUiThread()) {
9172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            startChromiumLocked();
929aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon            return;
939aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        }
943053d8bc3be48b0cbc6f33e1975c03a10ac303a5Jonathan Dixon
9572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // We must post to the UI thread to cover the case that the user has invoked Chromium
9672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // startup by using the (thread-safe) CookieManager rather than creating a WebView.
9772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        ThreadUtils.postOnUiThread(new Runnable() {
987b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixon            @Override
997b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixon            public void run() {
10072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                synchronized (mLock) {
10172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                    startChromiumLocked();
102970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixon                }
10372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            }
10472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        });
10572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        while (!mStarted) {
10672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            try {
10772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                // Important: wait() releases |mLock| so the UI thread can take it :-)
10872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                mLock.wait();
10972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            } catch (InterruptedException e) {
11072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                // Keep trying... eventually the UI thread will process the task we sent it.
11172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            }
11272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
11372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    }
114970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixon
11572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    private void startChromiumLocked() {
11672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        assert Thread.holdsLock(mLock) && ThreadUtils.runningOnUiThread();
1171e5325bde6fb074ce556a4e2833114d890ed21f3Jonathan Dixon
11872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // The post-condition of this method is everything is ready, so notify now to cover all
11972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // return paths. (Other threads will not wake-up until we release |mLock|, whatever).
12072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        mLock.notifyAll();
121970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixon
12272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (mStarted) {
12372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            return;
12472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
1259aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon
12672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (Build.IS_DEBUGGABLE) {
12772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            CommandLine.initFromFile(COMMAND_LINE_FILE);
12872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        } else {
12972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            CommandLine.init(null);
13072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
1317b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixon
13272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        CommandLine cl = CommandLine.getInstance();
13372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // TODO: currently in a relase build the DCHECKs only log. We either need to insall
13472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // a report handler with SetLogReportHandler to make them assert, or else compile
13572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // them out of the build altogether (b/8284203). Either way, so long they're
13672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // compiled in, we may as unconditionally enable them here.
13772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        cl.appendSwitch("enable-dcheck");
13872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
13972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // TODO: Remove when GL is supported by default in the upstream code.
14072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (!cl.hasSwitch("disable-webview-gl-mode")) {
14172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            cl.appendSwitch("testing-webview-gl-mode");
14272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
14372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
14472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // We don't need to extract any paks because for WebView, they are
14572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // in the system image.
14672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        ResourceExtractor.setMandatoryPaksToExtract("");
14772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
14872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        try {
14972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            LibraryLoader.ensureInitialized();
15072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        } catch(ProcessInitException e) {
15172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            throw new RuntimeException("Error initializing WebView library", e);
15272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
15372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
15472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        PathService.override(PathService.DIR_MODULE, "/system/lib/");
15572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // TODO: DIR_RESOURCE_PAKS_ANDROID needs to live somewhere sensible,
15672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // inlined here for simplicity setting up the HTMLViewer demo. Unfortunately
15772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // it can't go into base.PathService, as the native constant it refers to
15872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // lives in the ui/ layer. See ui/base/ui_base_paths.h
15972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        final int DIR_RESOURCE_PAKS_ANDROID = 3003;
16072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        PathService.override(DIR_RESOURCE_PAKS_ANDROID,
16172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                "/system/framework/webview/paks");
16272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
16372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        AwBrowserProcess.start(ActivityThread.currentApplication());
16472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        initPlatSupportLibrary();
1659aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        mStarted = true;
16646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
16746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
16846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
16946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public Statics getStatics() {
17046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
17146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            if (mStaticMethods == null) {
1729aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon                // TODO: Optimization potential: most these methods only need the native library
173f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon                // loaded and initialized, not the entire browser process started.
174f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon                // See also http://b/7009882
1759aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon                ensureChromiumStartedLocked();
17646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                mStaticMethods = new WebViewFactoryProvider.Statics() {
17746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    @Override
17846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    public String findAddress(String addr) {
17946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                        return ContentViewStatics.findAddress(addr);
18046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    }
18146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
18246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    @Override
18346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    public void setPlatformNotificationsEnabled(boolean enable) {
18446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                        // noop
18546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    }
18646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
187f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                    @Override
18846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    public String getDefaultUserAgent(Context context) {
189d6b56ff89338d0b3a6ba0cb413b1fe559d6284e0Mikhail Naganov                        return AwSettings.getDefaultUserAgent();
19046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    }
19146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                };
19246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            }
19346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
19446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        return mStaticMethods;
19546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
19646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
19746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
19846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebViewProvider createWebView(WebView webView, WebView.PrivateAccess privateAccess) {
19946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        assert Looper.myLooper() == Looper.getMainLooper();
200aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        AwBrowserContext browserContext;
20146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
202f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon            ensureChromiumStartedLocked();
20346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            ResourceProvider.registerResources(webView.getContext());
204aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen            browserContext = getBrowserContextLocked();
20546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
2068166110add693878f19ab0551c630a22caa2e30cKristian Monsen        // Make sure GeolocationPermissions is created before creating a webview
2078166110add693878f19ab0551c630a22caa2e30cKristian Monsen        getGeolocationPermissions();
208aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        return new WebViewChromium(webView, privateAccess, browserContext);
20946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
21046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
21146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
21246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public GeolocationPermissions getGeolocationPermissions() {
213009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen        synchronized (mLock) {
214aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mGeolocationPermissions == null) {
215f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
21650a184c1e058bc62f659fb80e09e7507ef13d6c9Jonathan Dixon                mGeolocationPermissions = new GeolocationPermissionsAdapter(
217aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen                        getBrowserContextLocked().getGeolocationPermissions());
218009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen            }
219009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen        }
220ff2704e614eb490baea0acb5c8ab22e061033694Kristian Monsen        return mGeolocationPermissions;
22146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
22246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
223aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen    private AwBrowserContext getBrowserContextLocked() {
224aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        assert Thread.holdsLock(mLock);
225aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        assert mStarted;
226aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        if (mBrowserContext == null) {
227aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen            mBrowserContext = new AwBrowserContext(
228aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen                    ActivityThread.currentApplication().getSharedPreferences(
229aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen                            CHROMIUM_PREFS_NAME, Context.MODE_PRIVATE));
230aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        }
231aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        return mBrowserContext;
232aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen    }
233aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen
23446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
23546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public CookieManager getCookieManager() {
23646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
237aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mCookieManager == null) {
238f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
239d5f17bbd8703a56ac084a64b1ce2f673f67bd72dKristian Monsen                mCookieManager = new CookieManagerAdapter(new AwCookieManager());
24046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            }
24146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
242aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mCookieManager;
24346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
24446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
24546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
24646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebIconDatabase getWebIconDatabase() {
247aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        synchronized (mLock) {
248aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mWebIconDatabase == null) {
249f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
250aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon                mWebIconDatabase = new WebIconDatabaseAdapter();
251aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            }
252aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        }
253aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mWebIconDatabase;
25446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
25546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
25646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
25746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebStorage getWebStorage() {
258aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        synchronized (mLock) {
259aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mWebStorage == null) {
260f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
2615d20093562806fbce9cc0d3eb4a411ed6c5ce1c0Bo Liu                mWebStorage = new WebStorageAdapter(AwQuotaManagerBridge.getInstance());
262aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            }
263aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        }
264aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mWebStorage;
26546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
26646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
26746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
26846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebViewDatabase getWebViewDatabase(Context context) {
26946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
270aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mWebViewDatabase == null) {
271f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
27272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                AwBrowserContext browserContext = getBrowserContextLocked();
273a6f2939f98aeaf67533a7fd3f5c999f207451f1cSelim Gurun                mWebViewDatabase = new WebViewDatabaseAdapter(
274a6f2939f98aeaf67533a7fd3f5c999f207451f1cSelim Gurun                        browserContext.getFormDatabase(),
275a6f2939f98aeaf67533a7fd3f5c999f207451f1cSelim Gurun                        browserContext.getHttpAuthDatabase(context));
276aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            }
27746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
278aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mWebViewDatabase;
27946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
28046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)}
281