WebViewChromiumFactoryProvider.java revision 9d0315cd966ad486abfcb51afa6692249461d14c
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)
19b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucciimport android.app.ActivityManager;
2046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.app.ActivityThread;
21b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucciimport android.content.ComponentCallbacks2;
2246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.content.Context;
23009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsenimport android.content.SharedPreferences;
24970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixonimport android.os.Build;
2546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.os.Looper;
2646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.CookieManager;
2746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.GeolocationPermissions;
2846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebIconDatabase;
2946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebStorage;
3046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebView;
3146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebViewDatabase;
3246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebViewFactoryProvider;
3346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import android.webkit.WebViewProvider;
3446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
35aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsenimport org.chromium.android_webview.AwBrowserContext;
36f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixonimport org.chromium.android_webview.AwBrowserProcess;
37a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixonimport org.chromium.android_webview.AwContents;
38d5f17bbd8703a56ac084a64b1ce2f673f67bd72dKristian Monsenimport org.chromium.android_webview.AwCookieManager;
399d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganovimport org.chromium.android_webview.AwDevToolsServer;
4009e718bac5900c84b8402b774d306dca7f7fdf09Selim Gurunimport org.chromium.android_webview.AwFormDatabase;
41009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsenimport org.chromium.android_webview.AwGeolocationPermissions;
425d20093562806fbce9cc0d3eb4a411ed6c5ce1c0Bo Liuimport org.chromium.android_webview.AwQuotaManagerBridge;
43d6b56ff89338d0b3a6ba0cb413b1fe559d6284e0Mikhail Naganovimport org.chromium.android_webview.AwSettings;
44b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucciimport org.chromium.base.MemoryPressureListener;
4546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import org.chromium.base.PathService;
467b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixonimport org.chromium.base.ThreadUtils;
477b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixonimport org.chromium.content.app.LibraryLoader;
4846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import org.chromium.content.browser.ContentViewStatics;
4946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)import org.chromium.content.browser.ResourceExtractor;
50970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixonimport org.chromium.content.common.CommandLine;
51970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixonimport org.chromium.content.common.ProcessInitException;
5246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
5346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider {
5446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
55009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen    private static final String CHROMIUM_PREFS_NAME = "WebViewChromiumPrefs";
567962d3fa1b66ed25aba03a2d48f6c47528d7d95cPrimiano Tucci    private static final String COMMAND_LINE_FILE = "/data/local/tmp/webview-command-line";
57009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen
5872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    // Guards accees to the other members, and is notifyAll() signalled on the UI thread
5972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    // when the chromium process has been started.
6072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    private final Object mLock = new Object();
6172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
6246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    // Initialization guarded by mLock.
63aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen    private AwBrowserContext mBrowserContext;
64aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private Statics mStaticMethods;
65aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private GeolocationPermissionsAdapter mGeolocationPermissions;
66aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private CookieManagerAdapter mCookieManager;
67aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private WebIconDatabaseAdapter mWebIconDatabase;
68aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private WebStorageAdapter mWebStorage;
69aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon    private WebViewDatabaseAdapter mWebViewDatabase;
709d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov    private AwDevToolsServer mDevToolsServer;
7146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
729aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon    // Read/write protected by mLock.
739aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon    private boolean mStarted;
74f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon
7511f7c1b02e49a6a8eaec26111f08edec45feb01aTorne (Richard Coles)    public WebViewChromiumFactoryProvider() {
76f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon        // Load chromium library.
77f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon        AwBrowserProcess.loadLibrary();
78a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon        // Load glue-layer support library.
79a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon        System.loadLibrary("webviewchromium_plat_support");
80f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon    }
81f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon
82f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon    private void initPlatSupportLibrary() {
839aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        DrawGLFunctor.setChromiumAwDrawGLFunction(AwContents.getAwDrawGLFunction());
849aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        AwContents.setAwDrawSWFunctionTable(GraphicsUtils.getDrawSWFunctionTable());
85c5cde8c18df853e11d1ec528508fd0fe2e742d5cKaan Baloglu        AwContents.setAwDrawGLFunctionTable(GraphicsUtils.getDrawGLFunctionTable());
86a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon    }
87a2b0d0b4d6d1ff22fbb1329e358e1cc0a112aab3Jonathan Dixon
88f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon    private void ensureChromiumStartedLocked() {
8946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        assert Thread.holdsLock(mLock);
9046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
9172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (mStarted) {  // Early-out for the common case.
9272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            return;
9372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
9472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
9572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (ThreadUtils.runningOnUiThread()) {
9672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            startChromiumLocked();
979aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon            return;
989aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        }
993053d8bc3be48b0cbc6f33e1975c03a10ac303a5Jonathan Dixon
10072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // We must post to the UI thread to cover the case that the user has invoked Chromium
10172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // startup by using the (thread-safe) CookieManager rather than creating a WebView.
10272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        ThreadUtils.postOnUiThread(new Runnable() {
1037b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixon            @Override
1047b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixon            public void run() {
10572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                synchronized (mLock) {
10672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                    startChromiumLocked();
107970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixon                }
10872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            }
10972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        });
11072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        while (!mStarted) {
11172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            try {
11272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                // Important: wait() releases |mLock| so the UI thread can take it :-)
11372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                mLock.wait();
11472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            } catch (InterruptedException e) {
11572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                // Keep trying... eventually the UI thread will process the task we sent it.
11672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            }
11772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
11872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    }
119970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixon
12072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon    private void startChromiumLocked() {
12172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        assert Thread.holdsLock(mLock) && ThreadUtils.runningOnUiThread();
1221e5325bde6fb074ce556a4e2833114d890ed21f3Jonathan Dixon
12372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // The post-condition of this method is everything is ready, so notify now to cover all
12472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // return paths. (Other threads will not wake-up until we release |mLock|, whatever).
12572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        mLock.notifyAll();
126970a2065df4f96ceaa9bc38d82caea9221dfa4c9Jonathan Dixon
12772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (mStarted) {
12872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            return;
12972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
1309aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon
13172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (Build.IS_DEBUGGABLE) {
13272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            CommandLine.initFromFile(COMMAND_LINE_FILE);
13372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        } else {
13472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            CommandLine.init(null);
13572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
1367b77b2fb0f7148bc31e23cabadb234231699ff93Jonathan Dixon
13772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        CommandLine cl = CommandLine.getInstance();
13872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // TODO: currently in a relase build the DCHECKs only log. We either need to insall
13972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // a report handler with SetLogReportHandler to make them assert, or else compile
14072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // them out of the build altogether (b/8284203). Either way, so long they're
14172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // compiled in, we may as unconditionally enable them here.
14272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        cl.appendSwitch("enable-dcheck");
14372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
14472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // TODO: Remove when GL is supported by default in the upstream code.
14572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        if (!cl.hasSwitch("disable-webview-gl-mode")) {
14672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            cl.appendSwitch("testing-webview-gl-mode");
14772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
14872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
14972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // We don't need to extract any paks because for WebView, they are
15072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // in the system image.
15172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        ResourceExtractor.setMandatoryPaksToExtract("");
15272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
15372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        try {
15472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            LibraryLoader.ensureInitialized();
15572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        } catch(ProcessInitException e) {
15672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon            throw new RuntimeException("Error initializing WebView library", e);
15772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        }
15872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
15972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        PathService.override(PathService.DIR_MODULE, "/system/lib/");
16072db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // TODO: DIR_RESOURCE_PAKS_ANDROID needs to live somewhere sensible,
16172db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // inlined here for simplicity setting up the HTMLViewer demo. Unfortunately
16272db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // it can't go into base.PathService, as the native constant it refers to
16372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        // lives in the ui/ layer. See ui/base/ui_base_paths.h
16472db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        final int DIR_RESOURCE_PAKS_ANDROID = 3003;
16572db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        PathService.override(DIR_RESOURCE_PAKS_ANDROID,
16672db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                "/system/framework/webview/paks");
16772db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon
16872db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        AwBrowserProcess.start(ActivityThread.currentApplication());
16972db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon        initPlatSupportLibrary();
1709d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov
1719d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        if (Build.IS_DEBUGGABLE) {
1729d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov            setWebContentsDebuggingEnabled(true);
1739d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        }
1749d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov
1759aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon        mStarted = true;
17646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
17746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
17846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
17946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public Statics getStatics() {
18046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
18146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            if (mStaticMethods == null) {
1829aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon                // TODO: Optimization potential: most these methods only need the native library
183f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon                // loaded and initialized, not the entire browser process started.
184f3e3e4e6b6c5798f435c1d14379edeb479e3b25dJonathan Dixon                // See also http://b/7009882
1859aa44b4e29be9f4b271edb3f4d438d806d4cc176Jonathan Dixon                ensureChromiumStartedLocked();
18646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                mStaticMethods = new WebViewFactoryProvider.Statics() {
18746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    @Override
18846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    public String findAddress(String addr) {
18946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                        return ContentViewStatics.findAddress(addr);
19046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    }
19146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
19246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    @Override
19346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    public void setPlatformNotificationsEnabled(boolean enable) {
19446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                        // noop
19546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    }
19646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
197f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                    @Override
19846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    public String getDefaultUserAgent(Context context) {
199d6b56ff89338d0b3a6ba0cb413b1fe559d6284e0Mikhail Naganov                        return AwSettings.getDefaultUserAgent();
20046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                    }
201b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci
202b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                    @Override
2039d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                    public void setWebContentsDebuggingEnabled(boolean enable) {
2049d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                        // Web Contents debugging is always enabled on debug builds.
2059d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                        if (!Build.IS_DEBUGGABLE) {
2069d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                            WebViewChromiumFactoryProvider.this.
2079d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                                    setWebContentsDebuggingEnabled(enable);
2089d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                        }
2099d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                    }
2109d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov
2119d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                    @Override
212b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                    public void freeMemoryForTests() {
213b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                        if (ActivityManager.isRunningInTestHarness()) {
214b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                            MemoryPressureListener.simulateMemoryPressureSignal(
215b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                                    ComponentCallbacks2.TRIM_MEMORY_COMPLETE);
216b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                        }
217b7f8bdb39f5ddfba3394d5335e728d9bf416f0caPrimiano Tucci                    }
21808b38428998be747f4c1c4abc43b96648ecb53ddJonathan Dixon
21908b38428998be747f4c1c4abc43b96648ecb53ddJonathan Dixon                    // TODO: replace with real implementation on next master-chromium merge.
22008b38428998be747f4c1c4abc43b96648ecb53ddJonathan Dixon                    public void setWebContentsDebuggingEnabled(boolean enable) {
22108b38428998be747f4c1c4abc43b96648ecb53ddJonathan Dixon                    }
22246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)                };
22346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            }
22446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
22546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        return mStaticMethods;
22646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
22746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
22846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
22946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebViewProvider createWebView(WebView webView, WebView.PrivateAccess privateAccess) {
23046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        assert Looper.myLooper() == Looper.getMainLooper();
231aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        AwBrowserContext browserContext;
23246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
233f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon            ensureChromiumStartedLocked();
23446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            ResourceProvider.registerResources(webView.getContext());
235aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen            browserContext = getBrowserContextLocked();
23646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
2378166110add693878f19ab0551c630a22caa2e30cKristian Monsen        // Make sure GeolocationPermissions is created before creating a webview
2388166110add693878f19ab0551c630a22caa2e30cKristian Monsen        getGeolocationPermissions();
239aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        return new WebViewChromium(webView, privateAccess, browserContext);
24046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
24146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
24246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
24346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public GeolocationPermissions getGeolocationPermissions() {
244009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen        synchronized (mLock) {
245aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mGeolocationPermissions == null) {
246f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
24750a184c1e058bc62f659fb80e09e7507ef13d6c9Jonathan Dixon                mGeolocationPermissions = new GeolocationPermissionsAdapter(
248aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen                        getBrowserContextLocked().getGeolocationPermissions());
249009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen            }
250009ed27511c4606dcd16b352489b2d3f70b4b513Kristian Monsen        }
251ff2704e614eb490baea0acb5c8ab22e061033694Kristian Monsen        return mGeolocationPermissions;
25246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
25346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
254aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen    private AwBrowserContext getBrowserContextLocked() {
255aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        assert Thread.holdsLock(mLock);
256aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        assert mStarted;
257aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        if (mBrowserContext == null) {
258aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen            mBrowserContext = new AwBrowserContext(
259aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen                    ActivityThread.currentApplication().getSharedPreferences(
260aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen                            CHROMIUM_PREFS_NAME, Context.MODE_PRIVATE));
261aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        }
262aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen        return mBrowserContext;
263aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen    }
264aa8b39f3e3f208586f3750d7278f1239a09ebd32Kristian Monsen
26546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
26646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public CookieManager getCookieManager() {
26746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
268aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mCookieManager == null) {
269f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
270d5f17bbd8703a56ac084a64b1ce2f673f67bd72dKristian Monsen                mCookieManager = new CookieManagerAdapter(new AwCookieManager());
27146f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)            }
27246f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
273aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mCookieManager;
27446f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
27546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
27646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
27746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebIconDatabase getWebIconDatabase() {
278aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        synchronized (mLock) {
279aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mWebIconDatabase == null) {
280f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
281aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon                mWebIconDatabase = new WebIconDatabaseAdapter();
282aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            }
283aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        }
284aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mWebIconDatabase;
28546f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
28646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
28746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
28846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebStorage getWebStorage() {
289aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        synchronized (mLock) {
290aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mWebStorage == null) {
291f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
2925d20093562806fbce9cc0d3eb4a411ed6c5ce1c0Bo Liu                mWebStorage = new WebStorageAdapter(AwQuotaManagerBridge.getInstance());
293aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            }
294aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        }
295aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mWebStorage;
29646f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
29746f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)
29846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    @Override
29946f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    public WebViewDatabase getWebViewDatabase(Context context) {
30046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        synchronized (mLock) {
301aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            if (mWebViewDatabase == null) {
302f0f355514e4420755c15491fe3b21a2eeaa42654Jonathan Dixon                ensureChromiumStartedLocked();
30372db47d0508d6733072c38ee44883ecfaf612ca6Jonathan Dixon                AwBrowserContext browserContext = getBrowserContextLocked();
304a6f2939f98aeaf67533a7fd3f5c999f207451f1cSelim Gurun                mWebViewDatabase = new WebViewDatabaseAdapter(
305a6f2939f98aeaf67533a7fd3f5c999f207451f1cSelim Gurun                        browserContext.getFormDatabase(),
306a6f2939f98aeaf67533a7fd3f5c999f207451f1cSelim Gurun                        browserContext.getHttpAuthDatabase(context));
307aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon            }
30846f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)        }
309aadb04e4198e1f0dce390e9434cb189603b8e713Jonathan Dixon        return mWebViewDatabase;
31046f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)    }
3119d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov
3129d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov    private void setWebContentsDebuggingEnabled(boolean enable) {
3139d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        if (Looper.myLooper() != Looper.getMainLooper()) {
3149d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov            throw new RuntimeException(
3159d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov                    "Toggling of Web Contents Debugging must be done on the main thread");
3169d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        }
3179d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        if (mDevToolsServer == null) {
3189d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov            if (!enable) return;
3199d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov            mDevToolsServer = new AwDevToolsServer();
3209d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        }
3219d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov        mDevToolsServer.setRemoteDebuggingEnabled(enable);
3229d0315cd966ad486abfcb51afa6692249461d14cMikhail Naganov    }
32346f104289826e85ba76a1a2139d7ecc7daca07b1Torne (Richard Coles)}
324