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 java.util.ArrayDeque; | 7 import java.util.ArrayDeque; |
| 8 | 8 |
| 9 /** | 9 /** |
| 10 * Sanity checks and sends post messages to web. Queues messages if necessary. | 10 * Sanity checks and sends post messages to web. Queues messages if necessary. |
| 11 */ | 11 */ |
| 12 public class PostMessageSender implements AwMessagePortService.MessageChannelObs erver { | 12 public class PostMessageSender implements AwMessagePortService.MessageChannelObs erver { |
| 13 | 13 |
| 14 /** | 14 /** |
| 15 * The interface for message handler. | 15 * The interface for message handler. |
| 16 */ | 16 */ |
| 17 public static interface PostMessageSenderDelegate { | 17 public static interface PostMessageSenderDelegate { |
| 18 | 18 |
| 19 /* | 19 /* |
| 20 * Posts a message to the destination frame for real. The unique message port | 20 * Posts a message to the destination frame for real. The unique message port |
| 21 * id of any transferred port should be known at this time. | 21 * id of any transferred port should be known at this time. |
| 22 */ | 22 */ |
| 23 void postMessageToWeb(String frameName, String message, String targetOri gin, | 23 void postMessageToWeb(String frameName, String message, String targetOri gin, |
| 24 int[] sentPortIds); | 24 int[] sentPortIds); |
| 25 | |
| 26 /* | |
| 27 * Whether the post message sender is ready to post messages. | |
| 28 */ | |
| 29 boolean isPostMessageSenderReady(); | |
| 30 | |
| 31 /* | |
| 32 * Informs that all messages are posted and message queue is empty. | |
| 33 */ | |
| 34 void onPostMessageQueueEmpty(); | |
| 25 }; | 35 }; |
| 26 | 36 |
| 27 // A struct to store Message parameters that are sent from App to Web. | 37 // A struct to store Message parameters that are sent from App to Web. |
| 28 private static class PostMessageParams { | 38 private static class PostMessageParams { |
| 29 public String frameName; | 39 public String frameName; |
| 30 public String message; | 40 public String message; |
| 31 public String targetOrigin; | 41 public String targetOrigin; |
| 32 public MessagePort[] sentPorts; | 42 public MessagePort[] sentPorts; |
| 33 | 43 |
| 34 public PostMessageParams(String frameName, String message, String target Origin, | 44 public PostMessageParams(String frameName, String message, String target Origin, |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 47 // is not internally created yet), this message, and any following messages will be | 57 // is not internally created yet), this message, and any following messages will be |
| 48 // queued until the transferred port becomes ready. This is necessary to pre vent any | 58 // queued until the transferred port becomes ready. This is necessary to pre vent any |
| 49 // reordering. | 59 // reordering. |
| 50 private ArrayDeque<PostMessageParams> mMessageQueue = new ArrayDeque<PostMes sageParams>(); | 60 private ArrayDeque<PostMessageParams> mMessageQueue = new ArrayDeque<PostMes sageParams>(); |
| 51 | 61 |
| 52 public PostMessageSender(PostMessageSenderDelegate delegate, AwMessagePortSe rvice service) { | 62 public PostMessageSender(PostMessageSenderDelegate delegate, AwMessagePortSe rvice service) { |
| 53 mDelegate = delegate; | 63 mDelegate = delegate; |
| 54 mService = service; | 64 mService = service; |
| 55 } | 65 } |
| 56 | 66 |
| 67 public boolean isMessageQueueEmpty() { | |
| 68 return mMessageQueue.size() > 0; | |
|
hush (inactive)
2015/02/25 19:44:27
shouldn't this be == 0? I will be surprised if the
sgurun-gerrit only
2015/02/25 21:38:53
Done.
| |
| 69 } | |
| 70 | |
| 57 // Return true if any sent port is pending. | 71 // Return true if any sent port is pending. |
| 58 private boolean anySentPortIsPending(MessagePort[] sentPorts) { | 72 private boolean anySentPortIsPending(MessagePort[] sentPorts) { |
| 59 if (sentPorts != null) { | 73 if (sentPorts != null) { |
| 60 for (MessagePort port : sentPorts) { | 74 for (MessagePort port : sentPorts) { |
| 61 if (!port.isReady()) { | 75 if (!port.isReady()) { |
| 62 return true; | 76 return true; |
| 63 } | 77 } |
| 64 } | 78 } |
| 65 } | 79 } |
| 66 return false; | 80 return false; |
| 67 } | 81 } |
| 68 | 82 |
| 69 // By default the sender is always ready. | |
| 70 protected boolean senderIsReady() { | |
| 71 return true; | |
| 72 } | |
| 73 | |
| 74 // A message to a frame is queued if: | 83 // A message to a frame is queued if: |
| 75 // 1. Sender is not ready to post. When posting messages to frames, sender i s always | 84 // 1. Sender is not ready to post. When posting messages to frames, sender i s always |
| 76 // ready. However, when posting messages using message channels, sender may be in | 85 // ready. However, when posting messages using message channels, sender may be in |
| 77 // a pending state. | 86 // a pending state. |
| 78 // 2. There are already queued messages | 87 // 2. There are already queued messages |
| 79 // 3. The message includes a port that is not ready yet. | 88 // 3. The message includes a port that is not ready yet. |
| 80 private boolean shouldQueueMessage(MessagePort[] sentPorts) { | 89 private boolean shouldQueueMessage(MessagePort[] sentPorts) { |
| 81 // if messages to frames are already in queue mode, simply queue it, no need to | 90 // if messages to frames are already in queue mode, simply queue it, no need to |
| 82 // check ports. | 91 // check ports. |
| 83 if (mMessageQueue.size() > 0 || !senderIsReady()) { | 92 if (mMessageQueue.size() > 0 || !mDelegate.isPostMessageSenderReady()) { |
|
hush (inactive)
2015/02/25 19:44:27
use isMessageQueueEmpty() here since you have crea
sgurun-gerrit only
2015/02/25 21:38:53
I think I can read this a lot faster :) the method
| |
| 84 return true; | 93 return true; |
| 85 } | 94 } |
| 86 if (anySentPortIsPending(sentPorts)) { | 95 if (anySentPortIsPending(sentPorts)) { |
| 87 return true; | 96 return true; |
| 88 } | 97 } |
| 89 return false; | 98 return false; |
| 90 } | 99 } |
| 91 | 100 |
| 92 private void postMessageToWeb(String frameName, String message, String targe tOrigin, | 101 private void postMessageToWeb(String frameName, String message, String targe tOrigin, |
| 93 MessagePort[] sentPorts) { | 102 MessagePort[] sentPorts) { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 124 postMessageToWeb(frameName, message, targetOrigin, sentPorts); | 133 postMessageToWeb(frameName, message, targetOrigin, sentPorts); |
| 125 } | 134 } |
| 126 } | 135 } |
| 127 | 136 |
| 128 /* | 137 /* |
| 129 * Starts posting any messages that are queued. | 138 * Starts posting any messages that are queued. |
| 130 */ | 139 */ |
| 131 public void onMessageChannelCreated() { | 140 public void onMessageChannelCreated() { |
| 132 PostMessageParams msg; | 141 PostMessageParams msg; |
| 133 | 142 |
| 134 if (!senderIsReady()) { | 143 if (!mDelegate.isPostMessageSenderReady()) { |
| 135 return; | 144 return; |
| 136 } | 145 } |
| 137 | 146 |
| 138 while ((msg = mMessageQueue.peek()) != null) { | 147 while ((msg = mMessageQueue.peek()) != null) { |
| 139 // If there are still pending ports, message cannot be posted. | 148 // If there are still pending ports, message cannot be posted. |
| 140 if (anySentPortIsPending(msg.sentPorts)) { | 149 if (anySentPortIsPending(msg.sentPorts)) { |
| 141 return; | 150 return; |
| 142 } | 151 } |
| 143 mMessageQueue.remove(); | 152 mMessageQueue.remove(); |
| 144 postMessageToWeb(msg.frameName, msg.message, msg.targetOrigin, msg.s entPorts); | 153 postMessageToWeb(msg.frameName, msg.message, msg.targetOrigin, msg.s entPorts); |
| 145 } | 154 } |
| 155 mDelegate.onPostMessageQueueEmpty(); | |
| 146 } | 156 } |
| 147 } | 157 } |
| OLD | NEW |