Chromium Code Reviews| 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 org.chromium.android_webview; | 5 package org.chromium.android_webview; |
| 6 | 6 |
| 7 import android.os.Handler; | 7 import android.os.Handler; |
| 8 import android.os.Looper; | 8 import android.os.Looper; |
| 9 import android.os.Message; | 9 import android.os.Message; |
| 10 import android.util.SparseArray; | 10 import android.util.SparseArray; |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 99 | 99 |
| 100 private long mNativeMessagePortService; | 100 private long mNativeMessagePortService; |
| 101 private MessagePortStorage mPortStorage = new MessagePortStorage(); | 101 private MessagePortStorage mPortStorage = new MessagePortStorage(); |
| 102 private MessageHandlerThread mMessageHandlerThread = new MessageHandlerThrea d(); | 102 private MessageHandlerThread mMessageHandlerThread = new MessageHandlerThrea d(); |
| 103 | 103 |
| 104 AwMessagePortService() { | 104 AwMessagePortService() { |
| 105 mNativeMessagePortService = nativeInitAwMessagePortService(); | 105 mNativeMessagePortService = nativeInitAwMessagePortService(); |
| 106 mMessageHandlerThread.start(); | 106 mMessageHandlerThread.start(); |
| 107 } | 107 } |
| 108 | 108 |
| 109 public void postMessage(int senderId, String message, int[] sentPorts) { | |
| 110 // verify that webview still owns the port (not transferred) | |
| 111 if (mPortStorage.get(senderId) == null) { | |
| 112 throw new IllegalStateException(" Cannot post to unknown port " + se nderId); | |
|
mnaganov (inactive)
2015/01/27 13:30:17
nit: extra leading space in the message
sgurun-gerrit only
2015/01/29 03:06:08
Done.
| |
| 113 } | |
| 114 removeSentPorts(sentPorts); | |
| 115 if (mNativeMessagePortService == 0) return; | |
| 116 nativePostOutMessage(mNativeMessagePortService, senderId, message, sentP orts); | |
| 117 } | |
| 118 | |
| 119 public void removeSentPorts(int[] sentPorts) { | |
| 120 // verify that webview owns all the ports that are transferred | |
| 121 if (sentPorts != null) { | |
| 122 for (int port : sentPorts) { | |
| 123 MessagePort p = mPortStorage.get(port); | |
| 124 if (p == null) { | |
| 125 throw new IllegalStateException("Cannot transfer unknown por t " + port); | |
| 126 } | |
| 127 p.close(); | |
| 128 // close the port so users can get feedback if they use in futur e. | |
| 129 mPortStorage.put(port, null); | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 109 private MessagePort addPort(int portId) { | 134 private MessagePort addPort(int portId) { |
| 110 if (mPortStorage.get(portId) != null) { | 135 if (mPortStorage.get(portId) != null) { |
| 111 throw new IllegalStateException("Port already exists"); | 136 throw new IllegalStateException("Port already exists"); |
| 112 } | 137 } |
| 113 MessagePort m = new MessagePort(portId); | 138 MessagePort m = new MessagePort(portId, this); |
| 114 mPortStorage.put(portId, m); | 139 mPortStorage.put(portId, m); |
| 115 return m; | 140 return m; |
| 116 } | 141 } |
| 117 | 142 |
| 118 @CalledByNative | 143 @CalledByNative |
| 119 private void onMessageChannelCreated(int portId1, int portId2, | 144 private void onMessageChannelCreated(int portId1, int portId2, |
| 120 ValueCallback<MessageChannel> callback) { | 145 ValueCallback<MessageChannel> callback) { |
| 121 callback.onReceiveValue(new MessageChannel(addPort(portId1), addPort(por tId2))); | 146 callback.onReceiveValue(new MessageChannel(addPort(portId1), addPort(por tId2))); |
| 122 } | 147 } |
| 123 | 148 |
| 124 // Called on IO thread. | 149 // Called on IO thread. |
| 125 @CalledByNative | 150 @CalledByNative |
| 126 private void onPostMessage(int portId, String message, int[] ports) { | 151 private void onPostMessage(int portId, String message, int[] ports) { |
| 127 PostMessage m = new PostMessage(mPortStorage.get(portId), message); | 152 PostMessage m = new PostMessage(mPortStorage.get(portId), message); |
| 128 Handler handler = mMessageHandlerThread.getHandler(); | 153 Handler handler = mMessageHandlerThread.getHandler(); |
| 129 Message msg = handler.obtainMessage(POST_MESSAGE, m); | 154 Message msg = handler.obtainMessage(POST_MESSAGE, m); |
| 130 handler.sendMessage(msg); | 155 handler.sendMessage(msg); |
| 131 } | 156 } |
| 132 | 157 |
| 133 @CalledByNative | 158 @CalledByNative |
| 134 private void unregisterNativeAwMessagePortService() { | 159 private void unregisterNativeAwMessagePortService() { |
| 135 mNativeMessagePortService = 0; | 160 mNativeMessagePortService = 0; |
| 136 } | 161 } |
| 137 | 162 |
| 138 private native long nativeInitAwMessagePortService(); | 163 private native long nativeInitAwMessagePortService(); |
| 164 private native void nativePostOutMessage(long nativeAwMessagePortServiceImpl , | |
| 165 int senderId, String message, int[] portIds); | |
| 139 } | 166 } |
| OLD | NEW |