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 | 21 |
22 private AwMessagePort mPort; | 22 private AppWebMessagePort mPort; |
23 | 23 |
24 public WebMessagePortAdapter(AwMessagePort port) { | 24 public WebMessagePortAdapter(AppWebMessagePort port) { |
25 mPort = port; | 25 mPort = port; |
26 } | 26 } |
27 | 27 |
28 public void postMessage(WebMessage message) { | 28 public void postMessage(WebMessage message) { |
29 mPort.postMessage(message.getData(), toAwMessagePorts(message.getPorts()
)); | 29 mPort.postMessage(message.getData(), toAppWebMessagePorts(message.getPor
ts())); |
30 } | 30 } |
31 | 31 |
32 public void close() { | 32 public void close() { |
33 mPort.close(); | 33 mPort.close(); |
34 } | 34 } |
35 | 35 |
36 public void setWebMessageCallback(WebMessageCallback callback) { | 36 public void setWebMessageCallback(WebMessageCallback callback) { |
37 setWebMessageCallback(callback, null); | 37 setWebMessageCallback(callback, null); |
38 } | 38 } |
39 | 39 |
40 public void setWebMessageCallback(final WebMessageCallback callback, final H
andler handler) { | 40 public void setWebMessageCallback(final WebMessageCallback callback, final H
andler handler) { |
41 mPort.setMessageCallback(new AwMessagePort.MessageCallback() { | 41 mPort.setMessageCallback(new AppWebMessagePort.MessageCallback() { |
42 @Override | 42 @Override |
43 public void onMessage(String message, AwMessagePort[] ports) { | 43 public void onMessage(String message, AppWebMessagePort[] ports) { |
44 callback.onMessage(WebMessagePortAdapter.this, | 44 callback.onMessage(WebMessagePortAdapter.this, |
45 new WebMessage(message, fromAwMessagePorts(ports))); | 45 new WebMessage(message, fromAppWebMessagePorts(ports))); |
46 } | 46 } |
47 }, handler); | 47 }, handler); |
48 } | 48 } |
49 | 49 |
50 public AwMessagePort getPort() { | 50 public AppWebMessagePort getPort() { |
51 return mPort; | 51 return mPort; |
52 } | 52 } |
53 | 53 |
54 public static WebMessagePort[] fromAwMessagePorts(AwMessagePort[] messagePor
ts) { | 54 public static WebMessagePort[] fromAppWebMessagePorts(AppWebMessagePort[] me
ssagePorts) { |
55 if (messagePorts == null) return null; | 55 if (messagePorts == null) return null; |
56 WebMessagePort[] ports = new WebMessagePort[messagePorts.length]; | 56 WebMessagePort[] ports = new WebMessagePort[messagePorts.length]; |
57 for (int i = 0; i < messagePorts.length; i++) { | 57 for (int i = 0; i < messagePorts.length; i++) { |
58 ports[i] = new WebMessagePortAdapter(messagePorts[i]); | 58 ports[i] = new WebMessagePortAdapter(messagePorts[i]); |
59 } | 59 } |
60 return ports; | 60 return ports; |
61 } | 61 } |
62 | 62 |
63 public static AwMessagePort[] toAwMessagePorts(WebMessagePort[] webMessagePo
rts) { | 63 public static AppWebMessagePort[] toAppWebMessagePorts(WebMessagePort[] webM
essagePorts) { |
64 if (webMessagePorts == null) return null; | 64 if (webMessagePorts == null) return null; |
65 AwMessagePort[] ports = new AwMessagePort[webMessagePorts.length]; | 65 AppWebMessagePort[] ports = new AppWebMessagePort[webMessagePorts.length
]; |
66 for (int i = 0; i < webMessagePorts.length; i++) { | 66 for (int i = 0; i < webMessagePorts.length; i++) { |
67 ports[i] = ((WebMessagePortAdapter) webMessagePorts[i]).getPort(); | 67 ports[i] = ((WebMessagePortAdapter) webMessagePorts[i]).getPort(); |
68 } | 68 } |
69 return ports; | 69 return ports; |
70 } | 70 } |
71 } | 71 } |
OLD | NEW |