Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(2005)

Unified Diff: android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java

Issue 869133005: Post a Message from Java to JS (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: spelling fix Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java
diff --git a/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java b/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java
index 2f34e250fe624539a0930f4ec8eb0d3979ca318f..c21546acead86c3adb56e75ff77fed290205b8aa 100644
--- a/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java
+++ b/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java
@@ -106,11 +106,36 @@ public class AwMessagePortService {
mMessageHandlerThread.start();
}
+ public void postMessage(int senderId, String message, int[] sentPorts) {
+ // verify that webview still owns the port (not transferred)
+ if (mPortStorage.get(senderId) == null) {
+ throw new IllegalStateException("Cannot post to unknown port " + senderId);
+ }
+ removeSentPorts(sentPorts);
+ if (mNativeMessagePortService == 0) return;
+ nativePostAppToWebMessage(mNativeMessagePortService, senderId, message, sentPorts);
+ }
+
+ public void removeSentPorts(int[] sentPorts) {
+ // verify that webview owns all the ports that are transferred
+ if (sentPorts != null) {
+ for (int port : sentPorts) {
+ MessagePort p = mPortStorage.get(port);
+ if (p == null) {
+ throw new IllegalStateException("Cannot transfer unknown port " + port);
+ }
+ p.close();
+ // close the port so users can get feedback if they use in future.
+ mPortStorage.put(port, null);
+ }
+ }
+ }
+
private MessagePort addPort(int portId) {
if (mPortStorage.get(portId) != null) {
throw new IllegalStateException("Port already exists");
}
- MessagePort m = new MessagePort(portId);
+ MessagePort m = new MessagePort(portId, this);
mPortStorage.put(portId, m);
return m;
}
@@ -136,4 +161,6 @@ public class AwMessagePortService {
}
private native long nativeInitAwMessagePortService();
+ private native void nativePostAppToWebMessage(long nativeAwMessagePortServiceImpl,
+ int senderId, String message, int[] portIds);
}

Powered by Google App Engine
This is Rietveld 408576698