| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package com.android.webview.chromium; | 5 package com.android.webview.chromium; |
| 6 | 6 |
| 7 import android.annotation.TargetApi; | 7 import android.annotation.TargetApi; |
| 8 import android.os.Build; | 8 import android.os.Build; |
| 9 import android.os.Handler; | 9 import android.os.Handler; |
| 10 import android.webkit.WebMessage; | 10 import android.webkit.WebMessage; |
| 11 import android.webkit.WebMessagePort; | 11 import android.webkit.WebMessagePort; |
| 12 | 12 |
| 13 import org.chromium.android_webview.AwMessagePort; | 13 import org.chromium.content.browser.AppWebMessagePort; |
| 14 | 14 |
| 15 /** | 15 /** |
| 16 * This class is used to convert a WebMessagePort to a MessagePort in chromium | 16 * This class is used to convert a WebMessagePort to a MessagePort in chromium |
| 17 * world. | 17 * world. |
| 18 */ | 18 */ |
| 19 @TargetApi(Build.VERSION_CODES.M) | 19 @TargetApi(Build.VERSION_CODES.M) |
| 20 public class WebMessagePortAdapter extends WebMessagePort { | 20 public class WebMessagePortAdapter extends WebMessagePort { |
| 21 private AppWebMessagePort mPort; |
| 21 | 22 |
| 22 private AwMessagePort mPort; | 23 public WebMessagePortAdapter(AppWebMessagePort port) { |
| 23 | |
| 24 public WebMessagePortAdapter(AwMessagePort port) { | |
| 25 mPort = port; | 24 mPort = port; |
| 26 } | 25 } |
| 27 | 26 |
| 28 public void postMessage(WebMessage message) { | 27 public void postMessage(WebMessage message) { |
| 29 mPort.postMessage(message.getData(), toAwMessagePorts(message.getPorts()
)); | 28 mPort.postMessage(message.getData(), toAppWebMessagePorts(message.getPor
ts())); |
| 30 } | 29 } |
| 31 | 30 |
| 32 public void close() { | 31 public void close() { |
| 33 mPort.close(); | 32 mPort.close(); |
| 34 } | 33 } |
| 35 | 34 |
| 36 public void setWebMessageCallback(WebMessageCallback callback) { | 35 public void setWebMessageCallback(WebMessageCallback callback) { |
| 37 setWebMessageCallback(callback, null); | 36 setWebMessageCallback(callback, null); |
| 38 } | 37 } |
| 39 | 38 |
| 40 public void setWebMessageCallback(final WebMessageCallback callback, final H
andler handler) { | 39 public void setWebMessageCallback(final WebMessageCallback callback, final H
andler handler) { |
| 41 mPort.setMessageCallback(new AwMessagePort.MessageCallback() { | 40 mPort.setMessageCallback(new AppWebMessagePort.MessageCallback() { |
| 42 @Override | 41 @Override |
| 43 public void onMessage(String message, AwMessagePort[] ports) { | 42 public void onMessage(String message, AppWebMessagePort[] ports) { |
| 44 callback.onMessage(WebMessagePortAdapter.this, | 43 callback.onMessage(WebMessagePortAdapter.this, |
| 45 new WebMessage(message, fromAwMessagePorts(ports))); | 44 new WebMessage(message, fromAppWebMessagePorts(ports))); |
| 46 } | 45 } |
| 47 }, handler); | 46 }, handler); |
| 48 } | 47 } |
| 49 | 48 |
| 50 public AwMessagePort getPort() { | 49 public AppWebMessagePort getPort() { |
| 51 return mPort; | 50 return mPort; |
| 52 } | 51 } |
| 53 | 52 |
| 54 public static WebMessagePort[] fromAwMessagePorts(AwMessagePort[] messagePor
ts) { | 53 public static WebMessagePort[] fromAppWebMessagePorts(AppWebMessagePort[] me
ssagePorts) { |
| 55 if (messagePorts == null) return null; | 54 if (messagePorts == null) return null; |
| 56 WebMessagePort[] ports = new WebMessagePort[messagePorts.length]; | 55 WebMessagePort[] ports = new WebMessagePort[messagePorts.length]; |
| 57 for (int i = 0; i < messagePorts.length; i++) { | 56 for (int i = 0; i < messagePorts.length; i++) { |
| 58 ports[i] = new WebMessagePortAdapter(messagePorts[i]); | 57 ports[i] = new WebMessagePortAdapter(messagePorts[i]); |
| 59 } | 58 } |
| 60 return ports; | 59 return ports; |
| 61 } | 60 } |
| 62 | 61 |
| 63 public static AwMessagePort[] toAwMessagePorts(WebMessagePort[] webMessagePo
rts) { | 62 public static AppWebMessagePort[] toAppWebMessagePorts(WebMessagePort[] webM
essagePorts) { |
| 64 if (webMessagePorts == null) return null; | 63 if (webMessagePorts == null) return null; |
| 65 AwMessagePort[] ports = new AwMessagePort[webMessagePorts.length]; | 64 AppWebMessagePort[] ports = new AppWebMessagePort[webMessagePorts.length
]; |
| 66 for (int i = 0; i < webMessagePorts.length; i++) { | 65 for (int i = 0; i < webMessagePorts.length; i++) { |
| 67 ports[i] = ((WebMessagePortAdapter) webMessagePorts[i]).getPort(); | 66 ports[i] = ((WebMessagePortAdapter) webMessagePorts[i]).getPort(); |
| 68 } | 67 } |
| 69 return ports; | 68 return ports; |
| 70 } | 69 } |
| 71 } | 70 } |
| OLD | NEW |