Index: content/public/android/java/src/org/chromium/content/browser/AppWebMessagePortService.java |
diff --git a/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java b/content/public/android/java/src/org/chromium/content/browser/AppWebMessagePortService.java |
similarity index 65% |
rename from android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java |
rename to content/public/android/java/src/org/chromium/content/browser/AppWebMessagePortService.java |
index eec71e1e59ad6d5300647d29aa0e53d6a36c2ef2..d4f2665e857fde7b1954d2176f3cde8b6c0742a1 100644 |
--- a/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java |
+++ b/content/public/android/java/src/org/chromium/content/browser/AppWebMessagePortService.java |
@@ -2,7 +2,7 @@ |
// Use of this source code is governed by a BSD-style license that can be |
// found in the LICENSE file. |
-package org.chromium.android_webview; |
+package org.chromium.content.browser; |
import android.util.SparseArray; |
@@ -12,40 +12,38 @@ import org.chromium.base.annotations.CalledByNative; |
import org.chromium.base.annotations.JNINamespace; |
/** |
- * Provides the Message Channel functionality for Android Webview. Specifically |
+ * Provides the Message Channel functionality for Android Apps |
+ * (including WebView embedders and Chrome Custom Tabs clients). Specifically |
* manages the message ports that are associated with a message channel and |
* handles posting/receiving messages to/from them. |
* See https://html.spec.whatwg.org/multipage/comms.html#messagechannel for |
* further information on message channels. |
* |
- * The message ports have unique IDs. In Android webview implementation, |
+ * The message ports have unique IDs. In Android implementation, |
* the message ports are only known by their IDs at the native side. |
* At the java side, the embedder deals with MessagePort objects. The mapping |
- * from an ID to an object is in AwMessagePortService. AwMessagePortService |
+ * from an ID to an object is in AppWebMessagePortService. AppWebMessagePortService |
* keeps a strong ref to MessagePort objects until they are closed. |
* |
- * Ownership: The Java AwMessagePortService is owned by Java AwBrowserContext. |
- * The native AwMessagePortService is owned by native AwBrowserContext. The |
+ * Ownership: The Java AppWebMessagePortService has to be owned by Java side global. |
+ * The native AppWebMessagePortService is a singleton. The |
* native peer maintains a weak ref to the java object and deregisters itself |
* before being deleted. |
* |
* All methods are called on UI thread except as noted. |
*/ |
-@JNINamespace("android_webview") |
-public class AwMessagePortService { |
- |
- private static final String TAG = "AwMessagePortService"; |
+@JNINamespace("content") |
+public class AppWebMessagePortService { |
+ private static final String TAG = "AppWebMessagePortService"; |
/** |
* Observer for MessageChannel events. |
*/ |
- public static interface MessageChannelObserver { |
- void onMessageChannelCreated(); |
- } |
+ public static interface MessageChannelObserver { void onMessageChannelCreated(); } |
// A thread safe storage for Message Ports. |
private static class MessagePortStorage { |
- private SparseArray<AwMessagePort> mMessagePorts = new SparseArray<AwMessagePort>(); |
+ private SparseArray<AppWebMessagePort> mMessagePorts = new SparseArray<AppWebMessagePort>(); |
private final Object mLock = new Object(); |
public void remove(int portId) { |
@@ -54,12 +52,12 @@ public class AwMessagePortService { |
} |
} |
- public void put(int portId, AwMessagePort m) { |
+ public void put(int portId, AppWebMessagePort m) { |
synchronized (mLock) { |
mMessagePorts.put(portId, m); |
} |
} |
- public AwMessagePort get(int portId) { |
+ public AppWebMessagePort get(int portId) { |
synchronized (mLock) { |
return mMessagePorts.get(portId); |
} |
@@ -71,8 +69,8 @@ public class AwMessagePortService { |
private ObserverList<MessageChannelObserver> mObserverList = |
new ObserverList<MessageChannelObserver>(); |
- AwMessagePortService() { |
- mNativeMessagePortService = nativeInitAwMessagePortService(); |
+ public AppWebMessagePortService() { |
+ mNativeMessagePortService = nativeInitAppWebMessagePortService(); |
} |
public void addObserver(MessageChannelObserver observer) { |
@@ -90,7 +88,7 @@ public class AwMessagePortService { |
} |
public void postMessage(int senderId, String message, int[] sentPorts) { |
- // verify that webview still owns the port (not transferred) |
+ // verify that the port is owned by service still (not transferred). |
if (mPortStorage.get(senderId) == null) { |
throw new IllegalStateException("Cannot post to unknown port " + senderId); |
} |
@@ -99,10 +97,10 @@ public class AwMessagePortService { |
} |
public void removeSentPorts(int[] sentPorts) { |
- // verify that webview owns all the ports that are transferred |
+ // verify that this service still owns all the ports that are transferred |
if (sentPorts != null) { |
for (int port : sentPorts) { |
- AwMessagePort p = mPortStorage.get(port); |
+ AppWebMessagePort p = mPortStorage.get(port); |
if (p == null) { |
throw new IllegalStateException("Cannot transfer unknown port " + port); |
} |
@@ -111,8 +109,8 @@ public class AwMessagePortService { |
} |
} |
- public AwMessagePort[] createMessageChannel() { |
- return new AwMessagePort[]{new AwMessagePort(this), new AwMessagePort(this)}; |
+ public AppWebMessagePort[] createMessageChannel() { |
+ return new AppWebMessagePort[] {new AppWebMessagePort(this), new AppWebMessagePort(this)}; |
} |
// Called on UI thread. |
@@ -121,7 +119,7 @@ public class AwMessagePortService { |
nativeReleaseMessages(mNativeMessagePortService, portId); |
} |
- private AwMessagePort addPort(AwMessagePort m, int portId) { |
+ private AppWebMessagePort addPort(AppWebMessagePort m, int portId) { |
if (mPortStorage.get(portId) != null) { |
throw new IllegalStateException("Port already exists"); |
} |
@@ -131,8 +129,7 @@ public class AwMessagePortService { |
} |
@CalledByNative |
- private void onMessageChannelCreated(int portId1, int portId2, |
- AwMessagePort[] ports) { |
+ private void onMessageChannelCreated(int portId1, int portId2, AppWebMessagePort[] ports) { |
ThreadUtils.assertOnUiThread(); |
addPort(ports[0], portId1); |
addPort(ports[1], portId2); |
@@ -144,26 +141,25 @@ public class AwMessagePortService { |
// Called on IO thread. |
@CalledByNative |
private void onReceivedMessage(int portId, String message, int[] ports) { |
- AwMessagePort[] messagePorts = null; |
+ AppWebMessagePort[] messagePorts = null; |
for (int i = 0; i < ports.length; i++) { |
if (messagePorts == null) { |
- messagePorts = new AwMessagePort[ports.length]; |
+ messagePorts = new AppWebMessagePort[ports.length]; |
} |
- messagePorts[i] = addPort(new AwMessagePort(this), ports[i]); |
+ messagePorts[i] = addPort(new AppWebMessagePort(this), ports[i]); |
} |
mPortStorage.get(portId).onReceivedMessage(message, messagePorts); |
} |
@CalledByNative |
- private void unregisterNativeAwMessagePortService() { |
+ private void unregisterNativeAppWebMessagePortService() { |
mNativeMessagePortService = 0; |
} |
- private native long nativeInitAwMessagePortService(); |
- private native void nativePostAppToWebMessage(long nativeAwMessagePortServiceImpl, |
- int senderId, String message, int[] portIds); |
- private native void nativeClosePort(long nativeAwMessagePortServiceImpl, |
- int messagePortId); |
- private native void nativeReleaseMessages(long nativeAwMessagePortServiceImpl, |
- int messagePortId); |
+ private native long nativeInitAppWebMessagePortService(); |
+ private native void nativePostAppToWebMessage( |
+ long nativeAppWebMessagePortServiceImpl, int senderId, String message, int[] portIds); |
+ private native void nativeClosePort(long nativeAppWebMessagePortServiceImpl, int messagePortId); |
+ private native void nativeReleaseMessages( |
+ long nativeAppWebMessagePortServiceImpl, int messagePortId); |
} |