Commit 1f3669c3 authored by Gustav Sennton's avatar Gustav Sennton Committed by Commit Bot

Move MessagePort related functionality to SharedWebViewChromium.

Move MessagePort related functionality from WebViewChromium to
SharedWebViewChromium so the support library glue layer can avoid
depending on WebViewChromium.

Bug: 812657
Change-Id: Iacd6c0e2dd0ffd1e8307ffb06a70cb00a2a71202
Reviewed-on: https://chromium-review.googlesource.com/924207
Commit-Queue: Gustav Sennton <gsennton@chromium.org>
Reviewed-by: default avatarBo <boliu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#537422}
parent a2ea89e5
...@@ -7,6 +7,9 @@ package com.android.webview.chromium; ...@@ -7,6 +7,9 @@ package com.android.webview.chromium;
import org.chromium.android_webview.AwContents; import org.chromium.android_webview.AwContents;
import org.chromium.android_webview.WebViewChromiumRunQueue; import org.chromium.android_webview.WebViewChromiumRunQueue;
import org.chromium.base.ThreadUtils; import org.chromium.base.ThreadUtils;
import org.chromium.content_public.browser.MessagePort;
import java.util.concurrent.Callable;
/** /**
* This class contains the parts of WebViewChromium that should be shared between the webkit-glue * This class contains the parts of WebViewChromium that should be shared between the webkit-glue
...@@ -14,11 +17,13 @@ import org.chromium.base.ThreadUtils; ...@@ -14,11 +17,13 @@ import org.chromium.base.ThreadUtils;
*/ */
public class SharedWebViewChromium { public class SharedWebViewChromium {
private final WebViewChromiumRunQueue mRunQueue; private final WebViewChromiumRunQueue mRunQueue;
private final WebViewChromiumAwInit mAwInit;
// The WebView wrapper for ContentViewCore and required browser compontents. // The WebView wrapper for ContentViewCore and required browser compontents.
private AwContents mAwContents; private AwContents mAwContents;
public SharedWebViewChromium(WebViewChromiumRunQueue runQueue) { public SharedWebViewChromium(WebViewChromiumRunQueue runQueue, WebViewChromiumAwInit awInit) {
mRunQueue = runQueue; mRunQueue = runQueue;
mAwInit = awInit;
} }
public void setAwContentsOnUiThread(AwContents awContents) { public void setAwContentsOnUiThread(AwContents awContents) {
...@@ -44,6 +49,34 @@ public class SharedWebViewChromium { ...@@ -44,6 +49,34 @@ public class SharedWebViewChromium {
mAwContents.insertVisualStateCallback(requestId, callback); mAwContents.insertVisualStateCallback(requestId, callback);
} }
public MessagePort[] createWebMessageChannel() {
mAwInit.startYourEngines(true);
if (checkNeedsPost()) {
MessagePort[] ret = mRunQueue.runOnUiThreadBlocking(new Callable<MessagePort[]>() {
@Override
public MessagePort[] call() {
return createWebMessageChannel();
}
});
return ret;
}
return mAwContents.createMessageChannel();
}
public void postMessageToMainFrame(
final String message, final String targetOrigin, final MessagePort[] sentPorts) {
if (checkNeedsPost()) {
mRunQueue.addTask(new Runnable() {
@Override
public void run() {
postMessageToMainFrame(message, targetOrigin, sentPorts);
}
});
return;
}
mAwContents.postMessageToFrame(null, message, targetOrigin, sentPorts);
}
protected boolean checkNeedsPost() { protected boolean checkNeedsPost() {
boolean needsPost = !mRunQueue.chromiumHasStarted() || !ThreadUtils.runningOnUiThread(); boolean needsPost = !mRunQueue.chromiumHasStarted() || !ThreadUtils.runningOnUiThread();
if (!needsPost && mAwContents == null) { if (!needsPost && mAwContents == null) {
......
...@@ -128,7 +128,8 @@ class WebViewChromium implements WebViewProvider, WebViewProvider.ScrollDelegate ...@@ -128,7 +128,8 @@ class WebViewChromium implements WebViewProvider, WebViewProvider.ScrollDelegate
mFactory = factory; mFactory = factory;
mShouldDisableThreadChecking = shouldDisableThreadChecking; mShouldDisableThreadChecking = shouldDisableThreadChecking;
factory.getWebViewDelegate().addWebViewAssetPath(mWebView.getContext()); factory.getWebViewDelegate().addWebViewAssetPath(mWebView.getContext());
mSharedWebViewChromium = new SharedWebViewChromium(mFactory.getRunQueue()); mSharedWebViewChromium =
new SharedWebViewChromium(mFactory.getRunQueue(), mFactory.getAwInit());
} }
static void completeWindowCreation(WebView parent, WebView child) { static void completeWindowCreation(WebView parent, WebView child) {
...@@ -1353,32 +1354,14 @@ class WebViewChromium implements WebViewProvider, WebViewProvider.ScrollDelegate ...@@ -1353,32 +1354,14 @@ class WebViewChromium implements WebViewProvider, WebViewProvider.ScrollDelegate
@Override @Override
public WebMessagePort[] createWebMessageChannel() { public WebMessagePort[] createWebMessageChannel() {
mFactory.startYourEngines(true); return WebMessagePortAdapter.fromMessagePorts(
if (checkNeedsPost()) { mSharedWebViewChromium.createWebMessageChannel());
WebMessagePort[] ret = mFactory.runOnUiThreadBlocking(new Callable<WebMessagePort[]>() {
@Override
public WebMessagePort[] call() {
return createWebMessageChannel();
}
});
return ret;
}
return WebMessagePortAdapter.fromMessagePorts(mAwContents.createMessageChannel());
} }
@Override @Override
@TargetApi(Build.VERSION_CODES.M) @TargetApi(Build.VERSION_CODES.M)
public void postMessageToMainFrame(final WebMessage message, final Uri targetOrigin) { public void postMessageToMainFrame(final WebMessage message, final Uri targetOrigin) {
if (checkNeedsPost()) { mSharedWebViewChromium.postMessageToMainFrame(message.getData(), targetOrigin.toString(),
mFactory.addTask(new Runnable() {
@Override
public void run() {
postMessageToMainFrame(message, targetOrigin);
}
});
return;
}
mAwContents.postMessageToFrame(null, message.getData(), targetOrigin.toString(),
WebMessagePortAdapter.toMessagePorts(message.getPorts())); WebMessagePortAdapter.toMessagePorts(message.getPorts()));
} }
......
...@@ -470,4 +470,8 @@ public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider { ...@@ -470,4 +470,8 @@ public class WebViewChromiumFactoryProvider implements WebViewFactoryProvider {
AwBrowserContext getBrowserContextOnUiThread() { AwBrowserContext getBrowserContextOnUiThread() {
return mAwInit.getBrowserContextOnUiThread(); return mAwInit.getBrowserContextOnUiThread();
} }
WebViewChromiumAwInit getAwInit() {
return mAwInit;
}
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment