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

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

Issue 831523004: Enable posting a message from JS to Android webview. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: minor fixes 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
new file mode 100644
index 0000000000000000000000000000000000000000..a1f9c4ba65e0682e2511b9dd8d7f5210d36aa610
--- /dev/null
+++ b/android_webview/java/src/org/chromium/android_webview/AwMessagePortService.java
@@ -0,0 +1,69 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// 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;
+
+import android.util.SparseArray;
+import android.webkit.ValueCallback;
+
+import org.chromium.base.CalledByNative;
+import org.chromium.base.JNINamespace;
+
+/**
+ * Provides the Message Channel functionality for Android Webview. 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 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
+ * 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
+ * native peer maintains a weak ref to the java object and deregisters itself
+ * before being deleted.
+ */
+@JNINamespace("android_webview")
+public class AwMessagePortService {
+
+ private static final String TAG = "AwMessagePortService";
+ private long mNativeMessagePortService;
+ private SparseArray<MessagePort> mMessagePorts = new SparseArray<MessagePort>();
mnaganov (inactive) 2015/01/07 11:43:54 This class is only used on UI thread, right? It's
sgurun-gerrit only 2015/01/10 02:36:29 yes, good point. clarified on class documentation
+
+ AwMessagePortService() {
+ mNativeMessagePortService = nativeInitAwMessagePortService();
+ }
+
+ private MessagePort addPort(int portId) {
+ if (mMessagePorts.get(portId) != null) {
+ throw new IllegalStateException("Port already exists");
+ }
+ MessagePort m = new MessagePort(portId);
+ mMessagePorts.put(portId, m);
+ return m;
+ }
+
+ @CalledByNative
+ private void onMessageChannelCreated(int portId1, int portId2,
+ ValueCallback<MessageChannel> callback) {
+ callback.onReceiveValue(new MessageChannel(addPort(portId1), addPort(portId2)));
+ }
+
+
+ @CalledByNative
+ private void onPostMessage(int portId, String message, int[] ports) {
+ mMessagePorts.get(portId).onMessage(message);
+ }
+
+ @CalledByNative
+ private void unregisterNativeAwMessagePortService() {
+ mNativeMessagePortService = 0;
+ }
+
+ private native long nativeInitAwMessagePortService();
+}

Powered by Google App Engine
This is Rietveld 408576698